This repository has been archived by the owner on Aug 18, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 208
/
babel-eslint.js
505 lines (425 loc) · 11.5 KB
/
babel-eslint.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
"use strict";
const assert = require("assert");
const babelEslint = require("../..");
const espree = require("espree");
const escope = require("eslint-scope");
const unpad = require("dedent");
const assertImplementsAST = require("../helpers/assert-implements-ast");
function parseAndAssertSame(code) {
code = unpad(code);
const esAST = espree.parse(code, {
ecmaFeatures: {
// enable JSX parsing
jsx: true,
// enable return in global scope
globalReturn: true,
// enable implied strict mode (if ecmaVersion >= 5)
impliedStrict: true,
// allow experimental object rest/spread
experimentalObjectRestSpread: true,
},
tokens: true,
loc: true,
range: true,
comment: true,
ecmaVersion: 2018,
sourceType: "module",
});
const babylonAST = babelEslint.parseForESLint(code, {
eslintVisitorKeys: true,
eslintScopeManager: true,
}).ast;
assertImplementsAST(esAST, babylonAST);
}
describe("babylon-to-espree", () => {
describe("compatibility", () => {
it("should allow ast.analyze to be called without options", function() {
const esAST = babelEslint.parseForESLint("`test`", {
eslintScopeManager: true,
eslintVisitorKeys: true,
}).ast;
assert.doesNotThrow(
() => {
escope.analyze(esAST);
},
TypeError,
"Should allow no options argument."
);
});
});
describe("templates", () => {
it("empty template string", () => {
parseAndAssertSame("``");
});
it("template string", () => {
parseAndAssertSame("`test`");
});
it("template string using $", () => {
parseAndAssertSame("`$`");
});
it("template string with expression", () => {
parseAndAssertSame("`${a}`");
});
it("template string with multiple expressions", () => {
parseAndAssertSame("`${a}${b}${c}`");
});
it("template string with expression and strings", () => {
parseAndAssertSame("`a${a}a`");
});
it("template string with binary expression", () => {
parseAndAssertSame("`a${a + b}a`");
});
it("tagged template", () => {
parseAndAssertSame("jsx`<Button>Click</Button>`");
});
it("tagged template with expression", () => {
parseAndAssertSame("jsx`<Button>Hi ${name}</Button>`");
});
it("tagged template with new operator", () => {
parseAndAssertSame("new raw`42`");
});
it("template with nested function/object", () => {
parseAndAssertSame(
"`outer${{x: {y: 10}}}bar${`nested${function(){return 1;}}endnest`}end`"
);
});
it("template with braces inside and outside of template string #96", () => {
parseAndAssertSame(
"if (a) { var target = `{}a:${webpackPort}{}}}}`; } else { app.use(); }"
);
});
it("template also with braces #96", () => {
parseAndAssertSame(`
export default function f1() {
function f2(foo) {
const bar = 3;
return \`\${foo} \${bar}\`;
}
return f2;
}
`);
});
it("template with destructuring #31", () => {
parseAndAssertSame(`
module.exports = {
render() {
var {name} = this.props;
return Math.max(null, \`Name: \${name}, Name: \${name}\`);
}
};
`);
});
it("template with arrow returning template #603", () => {
parseAndAssertSame(`
var a = \`\${() => {
\`\${''}\`
}}\`;
`);
});
it("template string with object with template string inside", () => {
parseAndAssertSame("`${ { a:`${2}` } }`");
});
});
it("simple expression", () => {
parseAndAssertSame("a = 1");
});
it("logical NOT", () => {
parseAndAssertSame("!0");
});
it("bitwise NOT", () => {
parseAndAssertSame("~0");
});
it("class declaration", () => {
parseAndAssertSame("class Foo {}");
});
it("class expression", () => {
parseAndAssertSame("var a = class Foo {}");
});
it("jsx expression", () => {
parseAndAssertSame("<App />");
});
it("jsx expression with 'this' as identifier", () => {
parseAndAssertSame("<this />");
});
it("jsx expression with a dynamic attribute", () => {
parseAndAssertSame("<App foo={bar} />");
});
it("jsx expression with a member expression as identifier", () => {
parseAndAssertSame("<foo.bar />");
});
it("jsx expression with spread", () => {
parseAndAssertSame("var myDivElement = <div {...this.props} />;");
});
it("empty jsx text", () => {
parseAndAssertSame("<a></a>");
});
it("jsx text with content", () => {
parseAndAssertSame("<a>Hello, world!</a>");
});
it("nested jsx", () => {
parseAndAssertSame("<div>\n<h1>Wat</h1>\n</div>");
});
it("default import", () => {
parseAndAssertSame('import foo from "foo";');
});
it("import specifier", () => {
parseAndAssertSame('import { foo } from "foo";');
});
it("import specifier with name", () => {
parseAndAssertSame('import { foo as bar } from "foo";');
});
it("import bare", () => {
parseAndAssertSame('import "foo";');
});
it("export default class declaration", () => {
parseAndAssertSame("export default class Foo {}");
});
it("export default class expression", () => {
parseAndAssertSame("export default class {}");
});
it("export default function declaration", () => {
parseAndAssertSame("export default function Foo() {}");
});
it("export default function expression", () => {
parseAndAssertSame("export default function () {}");
});
it("export all", () => {
parseAndAssertSame('export * from "foo";');
});
it("export named", () => {
parseAndAssertSame("export { foo };");
});
it("export named alias", () => {
parseAndAssertSame("export { foo as bar };");
});
// Espree doesn't support the optional chaining operator yet
it("optional chaining operator (token)", () => {
const code = "foo?.bar";
const babylonAST = babelEslint.parseForESLint(code, {
eslintVisitorKeys: true,
eslintScopeManager: true,
}).ast;
assert.strictEqual(babylonAST.tokens[1].type, "Punctuator");
});
// Espree doesn't support the nullish coalescing operator yet
it("nullish coalescing operator (token)", () => {
const code = "foo ?? bar";
const babylonAST = babelEslint.parseForESLint(code, {
eslintVisitorKeys: true,
eslintScopeManager: true,
}).ast;
assert.strictEqual(babylonAST.tokens[1].type, "Punctuator");
});
// Espree doesn't support the pipeline operator yet
it("pipeline operator (token)", () => {
const code = "foo |> bar";
const babylonAST = babelEslint.parseForESLint(code, {
eslintVisitorKeys: true,
eslintScopeManager: true,
}).ast;
assert.strictEqual(babylonAST.tokens[1].type, "Punctuator");
});
it.skip("empty program with line comment", () => {
parseAndAssertSame("// single comment");
});
it.skip("empty program with block comment", () => {
parseAndAssertSame(" /* multiline\n * comment\n*/");
});
it("line comments", () => {
parseAndAssertSame(`
// single comment
var foo = 15; // comment next to statement
// second comment after statement
`);
});
it("block comments", () => {
parseAndAssertSame(`
/* single comment */
var foo = 15; /* comment next to statement */
/*
* multiline
* comment
*/
`);
});
it("block comments #124", () => {
parseAndAssertSame(`
React.createClass({
render() {
// return (
// <div />
// ); // <-- this is the line that is reported
}
});
`);
});
it("null", () => {
parseAndAssertSame("null");
});
it("boolean", () => {
parseAndAssertSame("if (true) {} else if (false) {}");
});
it("regexp", () => {
parseAndAssertSame("/affix-top|affix-bottom|affix|[a-z]/");
});
it("regexp", () => {
parseAndAssertSame("const foo = /foo/;");
});
it("regexp y flag", () => {
parseAndAssertSame("const foo = /foo/y;");
});
it("regexp u flag", () => {
parseAndAssertSame("const foo = /foo/u;");
});
it("regexp in a template string", () => {
parseAndAssertSame('`${/\\d/.exec("1")[0]}`');
});
it("first line is empty", () => {
parseAndAssertSame('\nimport Immutable from "immutable";');
});
it("empty", () => {
parseAndAssertSame("");
});
it("jsdoc", () => {
parseAndAssertSame(`
/**
* @param {object} options
* @return {number}
*/
const test = function({ a, b, c }) {
return a + b + c;
};
module.exports = test;
`);
});
it("empty block with comment", () => {
parseAndAssertSame(`
function a () {
try {
b();
} catch (e) {
// asdf
}
}
`);
});
describe("babel tests", () => {
it("MethodDefinition", () => {
parseAndAssertSame(`
export default class A {
a() {}
}
`);
});
it("MethodDefinition 2", () => {
parseAndAssertSame(
"export default class Bar { get bar() { return 42; }}"
);
});
it("ClassMethod", () => {
parseAndAssertSame(`
class A {
constructor() {
}
}
`);
});
it("ClassMethod multiple params", () => {
parseAndAssertSame(`
class A {
constructor(a, b, c) {
}
}
`);
});
it("ClassMethod multiline", () => {
parseAndAssertSame(`
class A {
constructor (
a,
b,
c
)
{
}
}
`);
});
it("ClassMethod oneline", () => {
parseAndAssertSame("class A { constructor(a, b, c) {} }");
});
it("ObjectMethod", () => {
parseAndAssertSame(`
var a = {
b(c) {
}
}
`);
});
it("do not allow import export everywhere", () => {
assert.throws(() => {
parseAndAssertSame('function F() { import a from "a"; }');
}, /SyntaxError: 'import' and 'export' may only appear at the top level/);
});
it("return outside function", () => {
parseAndAssertSame("return;");
});
it("super outside method", () => {
parseAndAssertSame("function F() { super(); }");
});
it("StringLiteral", () => {
parseAndAssertSame("");
parseAndAssertSame("");
parseAndAssertSame("a");
});
it("getters and setters", () => {
parseAndAssertSame("class A { get x ( ) { ; } }");
parseAndAssertSame(`
class A {
get x(
)
{
;
}
}
`);
parseAndAssertSame("class A { set x (a) { ; } }");
parseAndAssertSame(`
class A {
set x(a
)
{
;
}
}
`);
parseAndAssertSame(`
var B = {
get x () {
return this.ecks;
},
set x (ecks) {
this.ecks = ecks;
}
};
`);
});
it("RestOperator", () => {
parseAndAssertSame("var { a, ...b } = c");
parseAndAssertSame("var [ a, ...b ] = c");
parseAndAssertSame("var a = function (...b) {}");
});
it("SpreadOperator", () => {
parseAndAssertSame("var a = { b, ...c }");
parseAndAssertSame("var a = [ a, ...b ]");
parseAndAssertSame("var a = sum(...b)");
});
it("Async/Await", () => {
parseAndAssertSame(`
async function a() {
await 1;
}
`);
});
});
});