This repository has been archived by the owner on Apr 13, 2020. It is now read-only.
forked from babel/babel
/
validators.js
378 lines (315 loc) 路 13.3 KB
/
validators.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
import * as t from "../lib";
import { parse } from "@babel/parser";
describe("validators", function() {
describe("isNodesEquivalent", function() {
it("should handle simple cases", function() {
const mem = t.memberExpression(t.identifier("a"), t.identifier("b"));
expect(t.isNodesEquivalent(mem, mem)).toBe(true);
const mem2 = t.memberExpression(t.identifier("a"), t.identifier("c"));
expect(t.isNodesEquivalent(mem, mem2)).toBe(false);
});
it("should handle full programs", function() {
expect(t.isNodesEquivalent(parse("1 + 1"), parse("1+1"))).toBe(true);
expect(t.isNodesEquivalent(parse("1 + 1"), parse("1+2"))).toBe(false);
});
it("should handle complex programs", function() {
const program = "'use strict'; function lol() { wow();return 1; }";
expect(t.isNodesEquivalent(parse(program), parse(program))).toBe(true);
const program2 = "'use strict'; function lol() { wow();return -1; }";
expect(t.isNodesEquivalent(parse(program), parse(program2))).toBe(false);
});
it("should handle nodes with object properties", function() {
const original = t.templateElement({ raw: "\\'a", cooked: "'a" }, true);
const identical = t.templateElement({ raw: "\\'a", cooked: "'a" }, true);
const different = t.templateElement({ raw: "'a", cooked: "'a" }, true);
expect(t.isNodesEquivalent(original, identical)).toBe(true);
expect(t.isNodesEquivalent(original, different)).toBe(false);
});
it("rejects 'await' as an identifier", function() {
expect(t.isValidIdentifier("await")).toBe(false);
});
});
describe("isCompatTag", function() {
it("should handle lowercase tag names", function() {
expect(t.react.isCompatTag("div")).toBe(true);
expect(t.react.isCompatTag("a")).toBe(true); // one letter
expect(t.react.isCompatTag("h3")).toBe(true); // letters and numbers
});
it("should handle custom element tag names", function() {
expect(t.react.isCompatTag("plastic-button")).toBe(true); // ascii letters
expect(t.react.isCompatTag("math-伪")).toBe(true); // non-latin chars
expect(t.react.isCompatTag("img-viewer2")).toBe(true); // numbers
expect(t.react.isCompatTag("emotion-馃槏")).toBe(true); // emoji
});
it("accepts trailing dash '-' in custom element tag names", function() {
expect(t.react.isCompatTag("div-")).toBe(true);
expect(t.react.isCompatTag("a-")).toBe(true);
expect(t.react.isCompatTag("h3-")).toBe(true);
});
it("rejects empty or null tag names", function() {
expect(t.react.isCompatTag(null)).toBe(false);
expect(t.react.isCompatTag()).toBe(false);
expect(t.react.isCompatTag(undefined)).toBe(false);
expect(t.react.isCompatTag("")).toBe(false);
});
it("rejects tag names starting with an uppercase letter", function() {
expect(t.react.isCompatTag("Div")).toBe(false);
expect(t.react.isCompatTag("A")).toBe(false);
expect(t.react.isCompatTag("H3")).toBe(false);
});
it("rejects all uppercase tag names", function() {
expect(t.react.isCompatTag("DIV")).toBe(false);
expect(t.react.isCompatTag("A")).toBe(false);
expect(t.react.isCompatTag("H3")).toBe(false);
});
it("rejects leading dash '-'", function() {
expect(t.react.isCompatTag("-div")).toBe(false);
expect(t.react.isCompatTag("-a")).toBe(false);
expect(t.react.isCompatTag("-h3")).toBe(false);
});
});
describe("patterns", function() {
it("allows nested pattern structures", function() {
const pattern = t.objectPattern([
t.objectProperty(
t.identifier("a"),
t.objectPattern([
t.objectProperty(t.identifier("b"), t.identifier("foo")),
t.objectProperty(
t.identifier("c"),
t.arrayPattern([t.identifier("value")]),
),
]),
),
]);
expect(t.isNodesEquivalent(pattern, pattern)).toBe(true);
});
});
describe("isReferenced", function() {
it("returns false if node is a key of ObjectTypeProperty", function() {
const node = t.identifier("a");
const parent = t.objectTypeProperty(node, t.numberTypeAnnotation());
expect(t.isReferenced(node, parent)).toBe(false);
});
it("returns true if node is a value of ObjectTypeProperty", function() {
const node = t.identifier("a");
const parent = t.objectTypeProperty(
t.identifier("someKey"),
t.genericTypeAnnotation(node),
);
expect(t.isReferenced(node, parent)).toBe(true);
});
it("returns true if node is a value of ObjectProperty of an expression", function() {
const node = t.identifier("a");
const parent = t.objectProperty(t.identifier("key"), node);
const grandparent = t.objectExpression([parent]);
expect(t.isReferenced(node, parent, grandparent)).toBe(true);
});
it("returns false if node is a value of ObjectProperty of a pattern", function() {
const node = t.identifier("a");
const parent = t.objectProperty(t.identifier("key"), node);
const grandparent = t.objectPattern([parent]);
expect(t.isReferenced(node, parent, grandparent)).toBe(false);
});
describe("TSPropertySignature", function() {
it("returns false if node is a key", function() {
// { A: string }
const node = t.identifier("A");
const parent = t.tsPropertySignature(
node,
t.tsTypeAnnotation(t.tsStringKeyword()),
);
expect(t.isReferenced(node, parent)).toBe(false);
});
it("returns true if node is a value", function() {
// { someKey: A }
const node = t.identifier("A");
const parent = t.tsPropertySignature(
t.identifier("someKey"),
t.tsTypeAnnotation(t.tsTypeReference(node)),
);
expect(t.isReferenced(node, parent)).toBe(true);
});
});
describe("TSEnumMember", function() {
it("returns false if node is an id", function() {
// enum X = { A };
const node = t.identifier("A");
const parent = t.tsEnumMember(node);
expect(t.isReferenced(node, parent)).toBe(false);
});
it("returns true if node is a value", function() {
// enum X = { Foo = A }
const node = t.identifier("A");
const parent = t.tsEnumMember(t.identifier("Foo"), node);
expect(t.isReferenced(node, parent)).toBe(true);
});
});
describe("ObjectMethod", function() {
it("returns false if node is method key", function() {
const node = t.identifier("A");
const parent = t.objectMethod("method", node, [], t.blockStatement([]));
expect(t.isReferenced(node, parent)).toBe(false);
});
it("returns true if node is computed method key", function() {
const node = t.identifier("A");
const parent = t.objectMethod(
"method",
node,
[],
t.blockStatement([]),
true,
);
expect(t.isReferenced(node, parent)).toBe(true);
});
it("returns false if node is method param", function() {
const node = t.identifier("A");
const parent = t.objectMethod(
"method",
t.identifier("foo"),
[node],
t.blockStatement([]),
);
expect(t.isReferenced(node, parent)).toBe(false);
});
});
describe("ClassMethod", function() {
it("returns false if node is method key", function() {
const node = t.identifier("A");
const parent = t.classMethod("method", node, [], t.blockStatement([]));
expect(t.isReferenced(node, parent)).toBe(false);
});
it("returns true if node is computed method key", function() {
const node = t.identifier("A");
const parent = t.classMethod(
"method",
node,
[],
t.blockStatement([]),
true,
);
expect(t.isReferenced(node, parent)).toBe(true);
});
it("returns false if node is method param", function() {
const node = t.identifier("A");
const parent = t.classMethod(
"method",
t.identifier("foo"),
[node],
t.blockStatement([]),
);
expect(t.isReferenced(node, parent)).toBe(false);
});
});
});
describe("isBinding", function() {
it("returns false if node id a value of ObjectProperty of an expression", function() {
const node = t.identifier("a");
const parent = t.objectProperty(t.identifier("key"), node);
const grandparent = t.objectExpression([parent]);
expect(t.isBinding(node, parent, grandparent)).toBe(false);
});
it("returns true if node id a value of ObjectProperty of a pattern", function() {
const node = t.identifier("a");
const parent = t.objectProperty(t.identifier("key"), node);
const grandparent = t.objectPattern([parent]);
expect(t.isBinding(node, parent, grandparent)).toBe(true);
});
});
describe("isType", function() {
it("returns true if nodeType equals targetType", function() {
expect(t.isType("Identifier", "Identifier")).toBe(true);
});
it("returns false if targetType is a primary node type", function() {
expect(t.isType("Expression", "ArrayExpression")).toBe(false);
});
it("returns true if targetType is an alias of nodeType", function() {
expect(t.isType("ArrayExpression", "Expression")).toBe(true);
});
it("returns false if nodeType and targetType are unrelated", function() {
expect(t.isType("ArrayExpression", "ClassBody")).toBe(false);
});
it("returns false if nodeType is undefined", function() {
expect(t.isType(undefined, "Expression")).toBe(false);
});
});
describe("placeholders", function() {
describe("isPlaceholderType", function() {
describe("when placeholderType is a specific node type", function() {
const placeholder = "Identifier";
it("returns true if targetType is placeholderType", function() {
expect(t.isPlaceholderType(placeholder, "Identifier")).toBe(true);
});
it("returns true if targetType an alias for placeholderType", function() {
expect(t.isPlaceholderType(placeholder, "Expression")).toBe(true);
});
it("returns false for unrelated types", function() {
expect(t.isPlaceholderType(placeholder, "String")).toBe(false);
});
});
describe("when placeholderType is a generic alias type", function() {
const placeholder = "Pattern";
it("returns true if targetType is placeholderType", function() {
expect(t.isPlaceholderType(placeholder, "Pattern")).toBe(true);
});
it("returns true if targetType an alias for placeholderType", function() {
expect(t.isPlaceholderType(placeholder, "LVal")).toBe(true);
});
it("returns false for unrelated types", function() {
expect(t.isPlaceholderType(placeholder, "Expression")).toBe(false);
});
it("returns false if targetType is aliased by placeholderType", function() {
// i.e. a Pattern might not be an Identifier
expect(t.isPlaceholderType(placeholder, "Identifier")).toBe(false);
});
});
});
describe("is", function() {
describe("when the placeholder matches a specific node", function() {
const identifier = t.placeholder("Identifier", t.identifier("foo"));
it("returns false if targetType is expectedNode", function() {
expect(t.is("Identifier", identifier)).toBe(false);
});
it("returns true if targetType is an alias", function() {
expect(t.is("LVal", identifier)).toBe(true);
});
});
describe("when the placeholder matches a generic alias", function() {
const pattern = t.placeholder("Pattern", t.identifier("bar"));
it("returns false if targetType is aliased as expectedNode", function() {
// i.e. a Pattern might not be an Identifier
expect(t.is("Identifier", pattern)).toBe(false);
});
it("returns true if targetType is expectedNode", function() {
expect(t.is("Pattern", pattern)).toBe(true);
});
it("returns true if targetType is an alias for expectedNode", function() {
expect(t.is("LVal", pattern)).toBe(true);
});
});
});
describe("is[Type]", function() {
describe("when the placeholder matches a specific node", function() {
const identifier = t.placeholder("Identifier", t.identifier("foo"));
it("returns false if targetType is expectedNode", function() {
expect(t.isIdentifier(identifier)).toBe(false);
});
it("returns true if targetType is an alias", function() {
expect(t.isLVal(identifier)).toBe(true);
});
});
describe("when the placeholder matches a generic alias", function() {
const pattern = t.placeholder("Pattern", t.identifier("bar"));
it("returns false if targetType is aliased as expectedNode", function() {
expect(t.isIdentifier(pattern)).toBe(false);
});
it("returns true if targetType is expectedNode", function() {
expect(t.isPattern(pattern)).toBe(true);
});
it("returns true if targetType is an alias for expectedNode", function() {
expect(t.isLVal(pattern)).toBe(true);
});
});
});
});
});