Debian Codegraph @

packages/babel-helper-define-polyfill-provider/test/descriptors.js

raw symbols SCIP
1 import * as babel from "@babel/core";
2 import definePolyfillProvider from "../lib";
3
4 function transform(code, method, obj) {
5 return babel.transformSync(code, {
6 configFile: false,
7 plugins: [[definePolyfillProvider(() => obj), { method }]],
8 });
9 }
10
11 function getDescriptor(code, filter, useEntry) {
12 const fn = jest.fn();
13
14 transform(code, useEntry ? "entry-global" : "usage-global", {
15 entryGlobal: fn,
16 usageGlobal: fn,
17 });
18
19 let args;
20 if (filter) {
21 const test =
22 typeof filter === "string"
23 ? ([d]) => d.kind === filter
24 : ([d]) => filter(d);
25 args = fn.mock.calls.find(test);
26 } else {
27 expect(fn).toHaveBeenCalledTimes(1);
28 args = fn.mock.calls[0];
29 }
30
31 return [args[0], args[2], fn];
32 }
33
34 describe("descriptors", () => {
35 it("import", () => {
36 const [desc, path] = getDescriptor(`import "foo";`, null, true);
37
38 expect(desc).toEqual({ kind: "import", source: "foo" });
39 expect(path.type).toBe("ImportDeclaration");
40 expect(path.toString()).toBe(`import "foo";`);
41 });
42
43 it("require", () => {
44 const [desc, path] = getDescriptor(`require("foo");`, null, true);
45
46 expect(desc).toEqual({ kind: "import", source: "foo" });
47 expect(path.type).toBe("ExpressionStatement");
48 expect(path.toString()).toBe(`require("foo");`);
49 });
50
51 it("global", () => {
52 const [desc, path] = getDescriptor("Promise;");
53
54 expect(desc).toEqual({ kind: "global", name: "Promise" });
55 expect(path.type).toBe("Identifier");
56 expect(path.toString()).toBe("Promise");
57 });
58
59 it("static property", () => {
60 const [desc, path] = getDescriptor("Promise.try;", "property");
61
62 expect(desc).toEqual({
63 kind: "property",
64 object: "Promise",
65 key: "try",
66 placement: "static",
67 });
68 expect(path.type).toBe("MemberExpression");
69 expect(path.toString()).toBe("Promise.try");
70 });
71
72 it("prototype property", () => {
73 const [desc, path] = getDescriptor(
74 "Element.prototype.appendChild;",
75 "property",
76 );
77
78 expect(desc).toEqual({
79 kind: "property",
80 object: "Element",
81 key: "appendChild",
82 placement: "prototype",
83 });
84 expect(path.type).toBe("MemberExpression");
85 expect(path.toString()).toBe("Element.prototype.appendChild");
86 });
87
88 it("instance property - known type", () => {
89 const [desc, path] = getDescriptor("[].includes;");
90
91 expect(desc).toEqual({
92 kind: "property",
93 object: "Array",
94 key: "includes",
95 placement: "prototype",
96 });
97 expect(path.type).toBe("MemberExpression");
98 expect(path.toString()).toBe("[].includes");
99 });
100
101 it("instance property - unknown type", () => {
102 const [desc, path] = getDescriptor("foo().includes;", "property");
103
104 expect(desc).toEqual({
105 kind: "property",
106 object: null,
107 key: "includes",
108 placement: null,
109 });
110 expect(path.type).toBe("MemberExpression");
111 expect(path.toString()).toBe("foo().includes");
112 });
113
114 it("symbol property", () => {
115 const [desc, path] = getDescriptor(
116 "NodeList.prototype[Symbol.iterator];",
117 d => d.object === "NodeList",
118 );
119
120 expect(desc).toEqual({
121 kind: "property",
122 object: "NodeList",
123 key: "Symbol.iterator",
124 placement: "prototype",
125 });
126 expect(path.type).toBe("MemberExpression");
127 expect(path.toString()).toBe("NodeList.prototype[Symbol.iterator]");
128 });
129
130 it("destruturing", () => {
131 const [desc, path] = getDescriptor("var { all } = Promise;", "property");
132
133 expect(desc).toEqual({
134 kind: "property",
135 object: "Promise",
136 key: "all",
137 placement: "static",
138 });
139 expect(path.type).toBe("ObjectProperty");
140 expect(path.toString()).toBe("all");
141 });
142
143 it("in expression", () => {
144 const [desc, path] = getDescriptor("'values' in Object;", "in");
145
146 expect(desc).toEqual({
147 kind: "in",
148 object: "Object",
149 key: "values",
150 placement: "static",
151 });
152 expect(path.type).toBe("BinaryExpression");
153 expect(path.toString()).toBe("'values' in Object");
154 });
155 });