| 1 |
import path from "path"; |
| 2 |
import escope from "eslint-scope"; |
| 3 |
import unpad from "dedent"; |
| 4 |
import { fileURLToPath } from "url"; |
| 5 |
import { createRequire } from "module"; |
| 6 |
import { parseForESLint } from "../lib/index.cjs"; |
| 7 |
import { ESLint } from "eslint"; |
| 8 |
|
| 9 |
const ESLINT_VERSION = ESLint.version; |
| 10 |
const isESLint7 = ESLINT_VERSION.startsWith("7."); |
| 11 |
const dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 12 |
|
| 13 |
const itESLint7 = isESLint7 ? it : it.skip; |
| 14 |
const itNotESLint7 = isESLint7 ? it.skip : it; |
| 15 |
|
| 16 |
const BABEL_OPTIONS = { |
| 17 |
configFile: path.resolve( |
| 18 |
dirname, |
| 19 |
"../../babel-eslint-shared-fixtures/config/babel.config.js", |
| 20 |
), |
| 21 |
}; |
| 22 |
const PROPS_TO_REMOVE = [ |
| 23 |
"importKind", |
| 24 |
"exportKind", |
| 25 |
"variance", |
| 26 |
"typeArguments", |
| 27 |
"filename", |
| 28 |
"identifierName", |
| 29 |
]; |
| 30 |
|
| 31 |
function deeplyRemoveProperties(obj, props) { |
| 32 |
for (const [k, v] of Object.entries(obj)) { |
| 33 |
if (typeof v === "object") { |
| 34 |
if (Array.isArray(v)) { |
| 35 |
for (const el of v) { |
| 36 |
if (el != null) { |
| 37 |
deeplyRemoveProperties(el, props); |
| 38 |
} |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
if (props.includes(k)) { |
| 43 |
delete obj[k]; |
| 44 |
} else if (v != null) { |
| 45 |
deeplyRemoveProperties(v, props); |
| 46 |
} |
| 47 |
continue; |
| 48 |
} |
| 49 |
|
| 50 |
if (props.includes(k)) { |
| 51 |
delete obj[k]; |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
describe("Babel and Espree", () => { |
| 57 |
let espree; |
| 58 |
|
| 59 |
const espreeOptions = { |
| 60 |
ecmaFeatures: { |
| 61 |
|
| 62 |
jsx: true, |
| 63 |
|
| 64 |
globalReturn: true, |
| 65 |
|
| 66 |
impliedStrict: true, |
| 67 |
}, |
| 68 |
tokens: true, |
| 69 |
loc: true, |
| 70 |
range: true, |
| 71 |
comment: true, |
| 72 |
sourceType: "module", |
| 73 |
}; |
| 74 |
|
| 75 |
function parseAndAssertSame(code, babelEcmaFeatures = null) { |
| 76 |
code = unpad(code); |
| 77 |
|
| 78 |
if (isESLint7) { |
| 79 |
|
| 80 |
const espreeAST = espree.parse(code, { |
| 81 |
...espreeOptions, |
| 82 |
ecmaVersion: 2021, |
| 83 |
}); |
| 84 |
const babelAST = parseForESLint(code, { |
| 85 |
eslintVisitorKeys: true, |
| 86 |
eslintScopeManager: true, |
| 87 |
babelOptions: BABEL_OPTIONS, |
| 88 |
ecmaFeatures: babelEcmaFeatures, |
| 89 |
}).ast; |
| 90 |
|
| 91 |
deeplyRemoveProperties(babelAST, PROPS_TO_REMOVE); |
| 92 |
expect(babelAST).toEqual(espreeAST); |
| 93 |
} else { |
| 94 |
|
| 95 |
const espreeAST = espree.parse(code, { |
| 96 |
...espreeOptions, |
| 97 |
ecmaVersion: "latest", |
| 98 |
}); |
| 99 |
|
| 100 |
const babelAST = parseForESLint(code, { |
| 101 |
eslintVisitorKeys: true, |
| 102 |
eslintScopeManager: true, |
| 103 |
babelOptions: BABEL_OPTIONS, |
| 104 |
ecmaFeatures: babelEcmaFeatures, |
| 105 |
}).ast; |
| 106 |
|
| 107 |
deeplyRemoveProperties(babelAST, PROPS_TO_REMOVE); |
| 108 |
expect(babelAST).toEqual(espreeAST); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
beforeAll(() => { |
| 113 |
const require = createRequire(import.meta.url); |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
const espreePath = require.resolve("espree", { |
| 118 |
paths: [path.dirname(require.resolve("eslint"))], |
| 119 |
}); |
| 120 |
|
| 121 |
espree = require(espreePath); |
| 122 |
}); |
| 123 |
|
| 124 |
describe("compatibility", () => { |
| 125 |
it("should allow ast.analyze to be called without options", () => { |
| 126 |
const ast = parseForESLint("`test`", { |
| 127 |
eslintScopeManager: true, |
| 128 |
eslintVisitorKeys: true, |
| 129 |
babelOptions: BABEL_OPTIONS, |
| 130 |
}).ast; |
| 131 |
expect(() => { |
| 132 |
escope.analyze(ast); |
| 133 |
}).not.toThrow(new TypeError("Should allow no options argument.")); |
| 134 |
}); |
| 135 |
|
| 136 |
it("should not crash when `loadPartialConfigSync` returns `null`", () => { |
| 137 |
const thunk = () => |
| 138 |
parseForESLint("`test`", { |
| 139 |
eslintScopeManager: true, |
| 140 |
eslintVisitorKeys: true, |
| 141 |
babelOptions: { filename: "test.js", ignore: [/./] }, |
| 142 |
}); |
| 143 |
expect(thunk).not.toThrow(); |
| 144 |
}); |
| 145 |
}); |
| 146 |
|
| 147 |
describe("templates", () => { |
| 148 |
it("empty template string", () => { |
| 149 |
parseAndAssertSame("``"); |
| 150 |
}); |
| 151 |
|
| 152 |
it("template string", () => { |
| 153 |
parseAndAssertSame("`test`"); |
| 154 |
}); |
| 155 |
|
| 156 |
it("template string using $", () => { |
| 157 |
parseAndAssertSame("`$`"); |
| 158 |
}); |
| 159 |
|
| 160 |
it("template string with expression", () => { |
| 161 |
parseAndAssertSame("`${a}`"); |
| 162 |
}); |
| 163 |
|
| 164 |
it("template string with multiple expressions", () => { |
| 165 |
parseAndAssertSame("`${a}${b}${c}`"); |
| 166 |
}); |
| 167 |
|
| 168 |
it("template string with expression and strings", () => { |
| 169 |
parseAndAssertSame("`a${a}a`"); |
| 170 |
}); |
| 171 |
|
| 172 |
it("template string with binary expression", () => { |
| 173 |
parseAndAssertSame("`a${a + b}a`"); |
| 174 |
}); |
| 175 |
|
| 176 |
it("tagged template", () => { |
| 177 |
parseAndAssertSame("jsx`<Button>Click</Button>`"); |
| 178 |
}); |
| 179 |
|
| 180 |
it("tagged template with expression", () => { |
| 181 |
parseAndAssertSame("jsx`<Button>Hi ${name}</Button>`"); |
| 182 |
}); |
| 183 |
|
| 184 |
it("tagged template with new operator", () => { |
| 185 |
parseAndAssertSame("new raw`42`"); |
| 186 |
}); |
| 187 |
|
| 188 |
it("template with nested function/object", () => { |
| 189 |
parseAndAssertSame( |
| 190 |
"`outer${{x: {y: 10}}}bar${`nested${function(){return 1;}}endnest`}end`", |
| 191 |
); |
| 192 |
}); |
| 193 |
|
| 194 |
it("template with braces inside and outside of template string #96", () => { |
| 195 |
parseAndAssertSame( |
| 196 |
"if (a) { var target = `{}a:${webpackPort}{}}}}`; } else { app.use(); }", |
| 197 |
); |
| 198 |
}); |
| 199 |
|
| 200 |
it("template also with braces #96", () => { |
| 201 |
parseAndAssertSame(` |
| 202 |
export default function f1() { |
| 203 |
function f2(foo) { |
| 204 |
const bar = 3; |
| 205 |
return \`\${foo} \${bar}\`; |
| 206 |
} |
| 207 |
return f2; |
| 208 |
} |
| 209 |
`); |
| 210 |
}); |
| 211 |
|
| 212 |
it("template with destructuring #31", () => { |
| 213 |
parseAndAssertSame(` |
| 214 |
module.exports = { |
| 215 |
render() { |
| 216 |
var {name} = this.props; |
| 217 |
return Math.max(null, \`Name: \${name}, Name: \${name}\`); |
| 218 |
} |
| 219 |
}; |
| 220 |
`); |
| 221 |
}); |
| 222 |
|
| 223 |
it("template with arrow returning template #603", () => { |
| 224 |
parseAndAssertSame(` |
| 225 |
var a = \`\${() => { |
| 226 |
\`\${''}\` |
| 227 |
}}\`; |
| 228 |
`); |
| 229 |
}); |
| 230 |
|
| 231 |
it("template string with object with template string inside", () => { |
| 232 |
parseAndAssertSame("`${ { a:`${2}` } }`"); |
| 233 |
}); |
| 234 |
}); |
| 235 |
|
| 236 |
it("simple expression", () => { |
| 237 |
parseAndAssertSame("a = 1"); |
| 238 |
}); |
| 239 |
|
| 240 |
it("logical NOT", () => { |
| 241 |
parseAndAssertSame("!0"); |
| 242 |
}); |
| 243 |
|
| 244 |
it("bitwise NOT", () => { |
| 245 |
parseAndAssertSame("~0"); |
| 246 |
}); |
| 247 |
|
| 248 |
it("class declaration", () => { |
| 249 |
parseAndAssertSame("class Foo {}"); |
| 250 |
}); |
| 251 |
|
| 252 |
it("class expression", () => { |
| 253 |
parseAndAssertSame("var a = class Foo {}"); |
| 254 |
}); |
| 255 |
|
| 256 |
it("jsx expression", () => { |
| 257 |
parseAndAssertSame("<App />"); |
| 258 |
}); |
| 259 |
|
| 260 |
it("jsx expression with 'this' as identifier", () => { |
| 261 |
parseAndAssertSame("<this />"); |
| 262 |
}); |
| 263 |
|
| 264 |
it("jsx expression with a dynamic attribute", () => { |
| 265 |
parseAndAssertSame("<App foo={bar} />"); |
| 266 |
}); |
| 267 |
|
| 268 |
it("jsx expression with a member expression as identifier", () => { |
| 269 |
parseAndAssertSame("<foo.bar />"); |
| 270 |
}); |
| 271 |
|
| 272 |
it("jsx expression with spread", () => { |
| 273 |
parseAndAssertSame("var myDivElement = <div {...this.props} />;"); |
| 274 |
}); |
| 275 |
|
| 276 |
it("empty jsx text", () => { |
| 277 |
parseAndAssertSame("<a></a>"); |
| 278 |
}); |
| 279 |
|
| 280 |
it("jsx text with content", () => { |
| 281 |
parseAndAssertSame("<a>Hello, world!</a>"); |
| 282 |
}); |
| 283 |
|
| 284 |
it("nested jsx", () => { |
| 285 |
parseAndAssertSame("<div>\n<h1>Wat</h1>\n</div>"); |
| 286 |
}); |
| 287 |
|
| 288 |
it("default import", () => { |
| 289 |
parseAndAssertSame('import foo from "foo";'); |
| 290 |
}); |
| 291 |
|
| 292 |
it("import specifier", () => { |
| 293 |
parseAndAssertSame('import { foo } from "foo";'); |
| 294 |
}); |
| 295 |
|
| 296 |
it("import specifier with name", () => { |
| 297 |
parseAndAssertSame('import { foo as bar } from "foo";'); |
| 298 |
}); |
| 299 |
|
| 300 |
it("import bare", () => { |
| 301 |
parseAndAssertSame('import "foo";'); |
| 302 |
}); |
| 303 |
|
| 304 |
it("import meta", () => { |
| 305 |
parseAndAssertSame("const url = import.meta.url"); |
| 306 |
}); |
| 307 |
|
| 308 |
it("export default class declaration", () => { |
| 309 |
parseAndAssertSame("export default class Foo {}"); |
| 310 |
}); |
| 311 |
|
| 312 |
it("export default class expression", () => { |
| 313 |
parseAndAssertSame("export default class {}"); |
| 314 |
}); |
| 315 |
|
| 316 |
it("export default function declaration", () => { |
| 317 |
parseAndAssertSame("export default function Foo() {}"); |
| 318 |
}); |
| 319 |
|
| 320 |
it("export default function expression", () => { |
| 321 |
parseAndAssertSame("export default function () {}"); |
| 322 |
}); |
| 323 |
|
| 324 |
it("export all", () => { |
| 325 |
parseAndAssertSame('export * from "foo";'); |
| 326 |
}); |
| 327 |
|
| 328 |
it("export * as ns", () => { |
| 329 |
parseAndAssertSame('export * as Foo from "foo";'); |
| 330 |
}); |
| 331 |
|
| 332 |
it("export named", () => { |
| 333 |
parseAndAssertSame("var foo = 1;export { foo };"); |
| 334 |
}); |
| 335 |
|
| 336 |
it("export named alias", () => { |
| 337 |
parseAndAssertSame("var foo = 1;export { foo as bar };"); |
| 338 |
}); |
| 339 |
|
| 340 |
it("optional chaining operator", () => { |
| 341 |
parseAndAssertSame("foo?.bar?.().qux()"); |
| 342 |
}); |
| 343 |
|
| 344 |
it("nullish coalescing operator", () => { |
| 345 |
parseAndAssertSame("foo ?? bar"); |
| 346 |
}); |
| 347 |
|
| 348 |
it("logical assignment", () => { |
| 349 |
parseAndAssertSame("foo ??= bar &&= qux ||= quux"); |
| 350 |
}); |
| 351 |
|
| 352 |
it("numeric separator", () => { |
| 353 |
parseAndAssertSame("1_0.0_0e0_1"); |
| 354 |
}); |
| 355 |
|
| 356 |
|
| 357 |
it("pipeline operator (token)", () => { |
| 358 |
const code = "foo |> bar"; |
| 359 |
const babylonAST = parseForESLint(code, { |
| 360 |
eslintVisitorKeys: true, |
| 361 |
eslintScopeManager: true, |
| 362 |
babelOptions: BABEL_OPTIONS, |
| 363 |
}).ast; |
| 364 |
expect(babylonAST.tokens[1].type).toEqual("Punctuator"); |
| 365 |
}); |
| 366 |
|
| 367 |
it("brace and bracket hash operator (token)", () => { |
| 368 |
const code = "#[]; #{}"; |
| 369 |
const babylonAST = parseForESLint(code, { |
| 370 |
eslintVisitorKeys: true, |
| 371 |
eslintScopeManager: true, |
| 372 |
babelOptions: { |
| 373 |
filename: "test.js", |
| 374 |
parserOpts: { |
| 375 |
plugins: [["recordAndTuple", { syntaxType: "hash" }]], |
| 376 |
tokens: true, |
| 377 |
}, |
| 378 |
}, |
| 379 |
}).ast; |
| 380 |
expect(babylonAST.tokens[0]).toEqual( |
| 381 |
expect.objectContaining({ type: "Punctuator", value: "#[" }), |
| 382 |
); |
| 383 |
expect(babylonAST.tokens[3]).toEqual( |
| 384 |
expect.objectContaining({ type: "Punctuator", value: "#{" }), |
| 385 |
); |
| 386 |
}); |
| 387 |
|
| 388 |
it("brace and bracket bar operator (token)", () => { |
| 389 |
const code = "{||}; [||]"; |
| 390 |
const babylonAST = parseForESLint(code, { |
| 391 |
eslintVisitorKeys: true, |
| 392 |
eslintScopeManager: true, |
| 393 |
babelOptions: { |
| 394 |
filename: "test.js", |
| 395 |
parserOpts: { |
| 396 |
plugins: [["recordAndTuple", { syntaxType: "bar" }]], |
| 397 |
tokens: true, |
| 398 |
}, |
| 399 |
}, |
| 400 |
}).ast; |
| 401 |
expect(babylonAST.tokens[0]).toEqual( |
| 402 |
expect.objectContaining({ type: "Punctuator", value: "{|" }), |
| 403 |
); |
| 404 |
expect(babylonAST.tokens[1]).toEqual( |
| 405 |
expect.objectContaining({ type: "Punctuator", value: "|}" }), |
| 406 |
); |
| 407 |
expect(babylonAST.tokens[3]).toEqual( |
| 408 |
expect.objectContaining({ type: "Punctuator", value: "[|" }), |
| 409 |
); |
| 410 |
expect(babylonAST.tokens[4]).toEqual( |
| 411 |
expect.objectContaining({ type: "Punctuator", value: "|]" }), |
| 412 |
); |
| 413 |
}); |
| 414 |
|
| 415 |
if (process.env.BABEL_8_BREAKING) { |
| 416 |
itESLint7("private identifier (token) - ESLint 7", () => { |
| 417 |
const code = "class A { #x }"; |
| 418 |
const babylonAST = parseForESLint(code, { |
| 419 |
eslintVisitorKeys: true, |
| 420 |
eslintScopeManager: true, |
| 421 |
babelOptions: BABEL_OPTIONS, |
| 422 |
}).ast; |
| 423 |
expect(babylonAST.tokens[3].type).toEqual("PrivateIdentifier"); |
| 424 |
expect(babylonAST.tokens[3].value).toEqual("x"); |
| 425 |
}); |
| 426 |
} else { |
| 427 |
itESLint7("hash (token) - ESLint 7", () => { |
| 428 |
const code = "class A { #x }"; |
| 429 |
const babylonAST = parseForESLint(code, { |
| 430 |
eslintVisitorKeys: true, |
| 431 |
eslintScopeManager: true, |
| 432 |
babelOptions: BABEL_OPTIONS, |
| 433 |
}).ast; |
| 434 |
expect(babylonAST.tokens[3].type).toEqual("Punctuator"); |
| 435 |
expect(babylonAST.tokens[3].value).toEqual("#"); |
| 436 |
}); |
| 437 |
} |
| 438 |
|
| 439 |
itNotESLint7("private identifier (token) - ESLint 8", () => { |
| 440 |
const code = "class A { #x }"; |
| 441 |
const babylonAST = parseForESLint(code, { |
| 442 |
eslintVisitorKeys: true, |
| 443 |
eslintScopeManager: true, |
| 444 |
babelOptions: BABEL_OPTIONS, |
| 445 |
}).ast; |
| 446 |
|
| 447 |
expect(babylonAST.tokens[3].type).toEqual("PrivateIdentifier"); |
| 448 |
expect(babylonAST.tokens[3].value).toEqual("x"); |
| 449 |
}); |
| 450 |
|
| 451 |
it("static (token)", () => { |
| 452 |
const code = ` |
| 453 |
import { static as foo } from "foo"; |
| 454 |
|
| 455 |
class A { |
| 456 |
static m() {} |
| 457 |
static() {} |
| 458 |
} |
| 459 |
`; |
| 460 |
|
| 461 |
parseAndAssertSame(code); |
| 462 |
|
| 463 |
const babylonAST = parseForESLint(code, { |
| 464 |
eslintVisitorKeys: true, |
| 465 |
eslintScopeManager: true, |
| 466 |
babelOptions: BABEL_OPTIONS, |
| 467 |
}).ast; |
| 468 |
|
| 469 |
const staticKw = { type: "Keyword", value: "static" }; |
| 470 |
|
| 471 |
expect(babylonAST.tokens[2]).toMatchObject(staticKw); |
| 472 |
expect(babylonAST.tokens[12]).toMatchObject(staticKw); |
| 473 |
expect(babylonAST.tokens[18]).toMatchObject(staticKw); |
| 474 |
}); |
| 475 |
|
| 476 |
it("parse to PropertyDeclaration when `classFeatures: true`", () => { |
| 477 |
const code = "class A { #x }"; |
| 478 |
const babylonAST = parseForESLint(code, { |
| 479 |
eslintVisitorKeys: true, |
| 480 |
eslintScopeManager: true, |
| 481 |
babelOptions: { |
| 482 |
filename: "test.js", |
| 483 |
parserOpts: { |
| 484 |
plugins: [ |
| 485 |
["estree", { classFeatures: true }], |
| 486 |
"classPrivateProperties", |
| 487 |
"classProperties", |
| 488 |
], |
| 489 |
}, |
| 490 |
}, |
| 491 |
}).ast; |
| 492 |
const classDeclaration = babylonAST.body[0]; |
| 493 |
expect(classDeclaration.body.body[0].type).toEqual("PropertyDefinition"); |
| 494 |
}); |
| 495 |
|
| 496 |
itNotESLint7("class fields with ESLint 8", () => { |
| 497 |
parseAndAssertSame( |
| 498 |
` |
| 499 |
class A { |
| 500 |
x = 2; |
| 501 |
static #y = 3; |
| 502 |
asi |
| 503 |
#m() {} |
| 504 |
} |
| 505 |
`, |
| 506 |
); |
| 507 |
}); |
| 508 |
|
| 509 |
itESLint7("static (token) - ESLint 7", () => { |
| 510 |
const code = ` |
| 511 |
class A { |
| 512 |
static m() {} |
| 513 |
static() {} |
| 514 |
static x; |
| 515 |
static #y; |
| 516 |
static; |
| 517 |
static = 2; |
| 518 |
} |
| 519 |
`; |
| 520 |
const babylonAST = parseForESLint(code, { |
| 521 |
eslintVisitorKeys: true, |
| 522 |
eslintScopeManager: true, |
| 523 |
babelOptions: BABEL_OPTIONS, |
| 524 |
}).ast; |
| 525 |
|
| 526 |
const staticKw = { type: "Keyword", value: "static" }; |
| 527 |
|
| 528 |
expect(babylonAST.tokens[3]).toMatchObject(staticKw); |
| 529 |
expect(babylonAST.tokens[9]).toMatchObject(staticKw); |
| 530 |
expect(babylonAST.tokens[14]).toMatchObject(staticKw); |
| 531 |
expect(babylonAST.tokens[17]).toMatchObject(staticKw); |
| 532 |
expect( |
| 533 |
babylonAST.tokens[process.env.BABEL_8_BREAKING ? 20 : 21], |
| 534 |
).toMatchObject(staticKw); |
| 535 |
expect( |
| 536 |
babylonAST.tokens[process.env.BABEL_8_BREAKING ? 22 : 23], |
| 537 |
).toMatchObject(staticKw); |
| 538 |
}); |
| 539 |
|
| 540 |
itNotESLint7("static (token) - ESLint 8", () => { |
| 541 |
const code = ` |
| 542 |
class A { |
| 543 |
static m() {} |
| 544 |
static() {} |
| 545 |
static x; |
| 546 |
static #y; |
| 547 |
static; |
| 548 |
static = 2; |
| 549 |
} |
| 550 |
`; |
| 551 |
const babylonAST = parseForESLint(code, { |
| 552 |
eslintVisitorKeys: true, |
| 553 |
eslintScopeManager: true, |
| 554 |
babelOptions: BABEL_OPTIONS, |
| 555 |
}).ast; |
| 556 |
|
| 557 |
const staticKw = { type: "Keyword", value: "static" }; |
| 558 |
|
| 559 |
expect(babylonAST.tokens[3]).toMatchObject(staticKw); |
| 560 |
expect(babylonAST.tokens[9]).toMatchObject(staticKw); |
| 561 |
expect(babylonAST.tokens[14]).toMatchObject(staticKw); |
| 562 |
expect(babylonAST.tokens[17]).toMatchObject(staticKw); |
| 563 |
expect(babylonAST.tokens[20]).toMatchObject(staticKw); |
| 564 |
expect(babylonAST.tokens[22]).toMatchObject(staticKw); |
| 565 |
}); |
| 566 |
|
| 567 |
if (process.env.BABEL_8_BREAKING) { |
| 568 |
itESLint7("pipeline # topic token - ESLint 7", () => { |
| 569 |
const code = ` |
| 570 |
x |> # |
| 571 |
y |> #[0] |
| 572 |
class A { |
| 573 |
#x = y |> |
| 574 |
# |
| 575 |
z |
| 576 |
} |
| 577 |
`; |
| 578 |
const babylonAST = parseForESLint(code, { |
| 579 |
eslintVisitorKeys: true, |
| 580 |
eslintScopeManager: true, |
| 581 |
babelOptions: { |
| 582 |
filename: "test.js", |
| 583 |
parserOpts: { |
| 584 |
plugins: [ |
| 585 |
["pipelineOperator", { proposal: "hack", topicToken: "#" }], |
| 586 |
], |
| 587 |
tokens: true, |
| 588 |
}, |
| 589 |
}, |
| 590 |
}).ast; |
| 591 |
|
| 592 |
const topicToken = { type: "Punctuator", value: "#" }; |
| 593 |
expect(babylonAST.tokens[2]).toMatchObject(topicToken); |
| 594 |
expect(babylonAST.tokens[5]).toMatchObject(topicToken); |
| 595 |
expect(babylonAST.tokens[16]).toMatchObject(topicToken); |
| 596 |
}); |
| 597 |
} else { |
| 598 |
itESLint7("pipeline # topic token - ESLint 7", () => { |
| 599 |
const code = ` |
| 600 |
x |> # |
| 601 |
y |> #[0] |
| 602 |
class A { |
| 603 |
#x = y |> |
| 604 |
# |
| 605 |
z |
| 606 |
} |
| 607 |
`; |
| 608 |
const babylonAST = parseForESLint(code, { |
| 609 |
eslintVisitorKeys: true, |
| 610 |
eslintScopeManager: true, |
| 611 |
babelOptions: { |
| 612 |
filename: "test.js", |
| 613 |
parserOpts: { |
| 614 |
plugins: [ |
| 615 |
["pipelineOperator", { proposal: "hack", topicToken: "#" }], |
| 616 |
], |
| 617 |
tokens: true, |
| 618 |
}, |
| 619 |
}, |
| 620 |
}).ast; |
| 621 |
|
| 622 |
const topicToken = { type: "Punctuator", value: "#" }; |
| 623 |
expect(babylonAST.tokens[2]).toMatchObject(topicToken); |
| 624 |
expect(babylonAST.tokens[5]).toMatchObject(topicToken); |
| 625 |
expect(babylonAST.tokens[17]).toMatchObject(topicToken); |
| 626 |
}); |
| 627 |
} |
| 628 |
|
| 629 |
itNotESLint7("pipeline # topic token - ESLint 8", () => { |
| 630 |
const code = ` |
| 631 |
x |> # |
| 632 |
y |> #[0] |
| 633 |
class A { |
| 634 |
#x = y |> |
| 635 |
# |
| 636 |
z |
| 637 |
} |
| 638 |
`; |
| 639 |
const babylonAST = parseForESLint(code, { |
| 640 |
eslintVisitorKeys: true, |
| 641 |
eslintScopeManager: true, |
| 642 |
babelOptions: { |
| 643 |
filename: "test.js", |
| 644 |
parserOpts: { |
| 645 |
plugins: [ |
| 646 |
["pipelineOperator", { proposal: "hack", topicToken: "#" }], |
| 647 |
], |
| 648 |
tokens: true, |
| 649 |
}, |
| 650 |
}, |
| 651 |
}).ast; |
| 652 |
|
| 653 |
const topicToken = { type: "Punctuator", value: "#" }; |
| 654 |
expect(babylonAST.tokens[2]).toMatchObject(topicToken); |
| 655 |
expect(babylonAST.tokens[5]).toMatchObject(topicToken); |
| 656 |
expect(babylonAST.tokens[16]).toMatchObject(topicToken); |
| 657 |
}); |
| 658 |
|
| 659 |
it.each(["^", "%", "^^", "@@"])("pipeline %s topic token", tok => { |
| 660 |
const code = ` |
| 661 |
x |> ${tok} |
| 662 |
y |> ${tok}[0] |
| 663 |
`; |
| 664 |
|
| 665 |
const babylonAST = parseForESLint(code, { |
| 666 |
eslintVisitorKeys: true, |
| 667 |
eslintScopeManager: true, |
| 668 |
babelOptions: { |
| 669 |
filename: "test.js", |
| 670 |
parserOpts: { |
| 671 |
plugins: [ |
| 672 |
["pipelineOperator", { proposal: "hack", topicToken: tok }], |
| 673 |
], |
| 674 |
tokens: true, |
| 675 |
}, |
| 676 |
}, |
| 677 |
}).ast; |
| 678 |
|
| 679 |
const topicToken = { type: "Punctuator", value: tok }; |
| 680 |
expect(babylonAST.tokens[2]).toMatchObject(topicToken); |
| 681 |
expect(babylonAST.tokens[5]).toMatchObject(topicToken); |
| 682 |
}); |
| 683 |
|
| 684 |
it("empty program with line comment", () => { |
| 685 |
parseAndAssertSame("// single comment"); |
| 686 |
}); |
| 687 |
|
| 688 |
it("empty program with block comment", () => { |
| 689 |
parseAndAssertSame(" /* multiline\n * comment\n*/"); |
| 690 |
}); |
| 691 |
|
| 692 |
it("line comments", () => { |
| 693 |
parseAndAssertSame(` |
| 694 |
// single comment |
| 695 |
var foo = 15; // comment next to statement |
| 696 |
// second comment after statement |
| 697 |
`); |
| 698 |
}); |
| 699 |
|
| 700 |
it("block comments", () => { |
| 701 |
parseAndAssertSame(` |
| 702 |
/* single comment */ |
| 703 |
var foo = 15; /* comment next to statement */ |
| 704 |
/* |
| 705 |
* multiline |
| 706 |
* comment |
| 707 |
*/ |
| 708 |
`); |
| 709 |
}); |
| 710 |
|
| 711 |
it("block comments #124", () => { |
| 712 |
parseAndAssertSame(` |
| 713 |
React.createClass({ |
| 714 |
render() { |
| 715 |
// return ( |
| 716 |
// <div /> |
| 717 |
// ); // <-- this is the line that is reported |
| 718 |
} |
| 719 |
}); |
| 720 |
`); |
| 721 |
}); |
| 722 |
|
| 723 |
it("null", () => { |
| 724 |
parseAndAssertSame("null"); |
| 725 |
}); |
| 726 |
|
| 727 |
it("boolean", () => { |
| 728 |
parseAndAssertSame("if (true) {} else if (false) {}"); |
| 729 |
}); |
| 730 |
|
| 731 |
it("regexp", () => { |
| 732 |
parseAndAssertSame("/affix-top|affix-bottom|affix|[a-z]/"); |
| 733 |
}); |
| 734 |
|
| 735 |
it("regexp", () => { |
| 736 |
parseAndAssertSame("const foo = /foo/;"); |
| 737 |
}); |
| 738 |
|
| 739 |
it("regexp y flag", () => { |
| 740 |
parseAndAssertSame("const foo = /foo/y;"); |
| 741 |
}); |
| 742 |
|
| 743 |
it("regexp u flag", () => { |
| 744 |
parseAndAssertSame("const foo = /foo/u;"); |
| 745 |
}); |
| 746 |
|
| 747 |
it("regexp in a template string", () => { |
| 748 |
parseAndAssertSame('`${/\\d/.exec("1")[0]}`'); |
| 749 |
}); |
| 750 |
|
| 751 |
it("first line is empty", () => { |
| 752 |
parseAndAssertSame('\nimport Immutable from "immutable";'); |
| 753 |
}); |
| 754 |
|
| 755 |
it("empty", () => { |
| 756 |
parseAndAssertSame(""); |
| 757 |
}); |
| 758 |
|
| 759 |
it("jsdoc", () => { |
| 760 |
parseAndAssertSame(` |
| 761 |
/** |
| 762 |
* @param {object} options |
| 763 |
* @return {number} |
| 764 |
*/ |
| 765 |
const test = function({ a, b, c }) { |
| 766 |
return a + b + c; |
| 767 |
}; |
| 768 |
module.exports = test; |
| 769 |
`); |
| 770 |
}); |
| 771 |
|
| 772 |
it("empty block with comment", () => { |
| 773 |
parseAndAssertSame(` |
| 774 |
function a () { |
| 775 |
try { |
| 776 |
b(); |
| 777 |
} catch (e) { |
| 778 |
// asdf |
| 779 |
} |
| 780 |
} |
| 781 |
`); |
| 782 |
}); |
| 783 |
|
| 784 |
describe("babel tests", () => { |
| 785 |
it("MethodDefinition", () => { |
| 786 |
parseAndAssertSame(` |
| 787 |
export default class A { |
| 788 |
a() {} |
| 789 |
} |
| 790 |
`); |
| 791 |
}); |
| 792 |
|
| 793 |
it("MethodDefinition 2", () => { |
| 794 |
parseAndAssertSame( |
| 795 |
"export default class Bar { get bar() { return 42; }}", |
| 796 |
); |
| 797 |
}); |
| 798 |
|
| 799 |
it("ClassMethod", () => { |
| 800 |
parseAndAssertSame(` |
| 801 |
class A { |
| 802 |
constructor() { |
| 803 |
} |
| 804 |
} |
| 805 |
`); |
| 806 |
}); |
| 807 |
|
| 808 |
it("ClassMethod multiple params", () => { |
| 809 |
parseAndAssertSame(` |
| 810 |
class A { |
| 811 |
constructor(a, b, c) { |
| 812 |
} |
| 813 |
} |
| 814 |
`); |
| 815 |
}); |
| 816 |
|
| 817 |
it("ClassMethod multiline", () => { |
| 818 |
parseAndAssertSame(` |
| 819 |
class A { |
| 820 |
constructor ( |
| 821 |
a, |
| 822 |
b, |
| 823 |
c |
| 824 |
) |
| 825 |
|
| 826 |
{ |
| 827 |
|
| 828 |
} |
| 829 |
} |
| 830 |
`); |
| 831 |
}); |
| 832 |
|
| 833 |
it("ClassMethod oneline", () => { |
| 834 |
parseAndAssertSame("class A { constructor(a, b, c) {} }"); |
| 835 |
}); |
| 836 |
|
| 837 |
it("ObjectMethod", () => { |
| 838 |
parseAndAssertSame(` |
| 839 |
var a = { |
| 840 |
b(c) { |
| 841 |
} |
| 842 |
} |
| 843 |
`); |
| 844 |
}); |
| 845 |
|
| 846 |
it("do not allow import export everywhere", () => { |
| 847 |
expect(() => { |
| 848 |
parseForESLint('function F() { import a from "a"; }', { |
| 849 |
babelOptions: BABEL_OPTIONS, |
| 850 |
}); |
| 851 |
}).toThrow(/'import' and 'export' may only appear at the top level/); |
| 852 |
}); |
| 853 |
|
| 854 |
it("allowImportExportEverywhere", () => { |
| 855 |
expect(() => { |
| 856 |
parseForESLint('function F() { import a from "a"; }', { |
| 857 |
babelOptions: { |
| 858 |
...BABEL_OPTIONS, |
| 859 |
parserOpts: { |
| 860 |
allowImportExportEverywhere: true, |
| 861 |
}, |
| 862 |
}, |
| 863 |
}); |
| 864 |
}).not.toThrow(); |
| 865 |
}); |
| 866 |
|
| 867 |
if (!process.env.BABEL_8_BREAKING) { |
| 868 |
it("top-level allowImportExportEverywhere", () => { |
| 869 |
expect(() => { |
| 870 |
parseForESLint('function F() { import a from "a"; }', { |
| 871 |
babelOptions: BABEL_OPTIONS, |
| 872 |
allowImportExportEverywhere: true, |
| 873 |
}); |
| 874 |
}).not.toThrow(); |
| 875 |
}); |
| 876 |
} |
| 877 |
|
| 878 |
if (process.env.BABEL_8_BREAKING) { |
| 879 |
it("return outside function with ecmaFeatures.globalReturn: true", () => { |
| 880 |
parseAndAssertSame("return;", { |
| 881 |
globalReturn: true, |
| 882 |
}); |
| 883 |
}); |
| 884 |
|
| 885 |
it("return outside function with ecmaFeatures.globalReturn: false", () => { |
| 886 |
expect(() => |
| 887 |
parseForESLint("return;", { |
| 888 |
babelOptions: BABEL_OPTIONS, |
| 889 |
ecmaVersion: { globalReturn: false }, |
| 890 |
}), |
| 891 |
).toThrow(new SyntaxError("'return' outside of function. (1:0)")); |
| 892 |
}); |
| 893 |
|
| 894 |
it("return outside function without ecmaFeatures.globalReturn", () => { |
| 895 |
expect(() => |
| 896 |
parseForESLint("return;", { babelOptions: BABEL_OPTIONS }), |
| 897 |
).toThrow(new SyntaxError("'return' outside of function. (1:0)")); |
| 898 |
}); |
| 899 |
} else { |
| 900 |
it("return outside function", () => { |
| 901 |
parseAndAssertSame("return;"); |
| 902 |
}); |
| 903 |
} |
| 904 |
|
| 905 |
if (process.env.BABEL_8_BREAKING) { |
| 906 |
it("super outside method", () => { |
| 907 |
expect(() => { |
| 908 |
parseForESLint("function F() { super(); }", { |
| 909 |
babelOptions: BABEL_OPTIONS, |
| 910 |
}); |
| 911 |
}).toThrow( |
| 912 |
/`super\(\)` is only valid inside a class constructor of a subclass\./, |
| 913 |
); |
| 914 |
}); |
| 915 |
|
| 916 |
it("super outside method - enabled", () => { |
| 917 |
expect(() => { |
| 918 |
parseForESLint("function F() { super(); }", { |
| 919 |
babelOptions: { |
| 920 |
...BABEL_OPTIONS, |
| 921 |
parserOpts: { |
| 922 |
allowSuperOutsideMethod: true, |
| 923 |
}, |
| 924 |
}, |
| 925 |
}); |
| 926 |
}).not.toThrow(); |
| 927 |
}); |
| 928 |
} else { |
| 929 |
it("super outside method", () => { |
| 930 |
expect(() => { |
| 931 |
parseForESLint("function F() { super(); }", { |
| 932 |
babelOptions: BABEL_OPTIONS, |
| 933 |
}); |
| 934 |
}).not.toThrow(); |
| 935 |
}); |
| 936 |
|
| 937 |
it("super outside method - disabled", () => { |
| 938 |
expect(() => { |
| 939 |
parseForESLint("function F() { super(); }", { |
| 940 |
babelOptions: { |
| 941 |
...BABEL_OPTIONS, |
| 942 |
parserOpts: { |
| 943 |
allowSuperOutsideMethod: false, |
| 944 |
}, |
| 945 |
}, |
| 946 |
}); |
| 947 |
}).toThrow( |
| 948 |
/`super\(\)` is only valid inside a class constructor of a subclass\./, |
| 949 |
); |
| 950 |
}); |
| 951 |
} |
| 952 |
|
| 953 |
it("StringLiteral", () => { |
| 954 |
parseAndAssertSame(""); |
| 955 |
parseAndAssertSame(""); |
| 956 |
parseAndAssertSame("a"); |
| 957 |
}); |
| 958 |
|
| 959 |
it("getters and setters", () => { |
| 960 |
parseAndAssertSame("class A { get x ( ) { ; } }"); |
| 961 |
parseAndAssertSame(` |
| 962 |
class A { |
| 963 |
get x( |
| 964 |
) |
| 965 |
{ |
| 966 |
; |
| 967 |
} |
| 968 |
} |
| 969 |
`); |
| 970 |
parseAndAssertSame("class A { set x (a) { ; } }"); |
| 971 |
parseAndAssertSame(` |
| 972 |
class A { |
| 973 |
set x(a |
| 974 |
) |
| 975 |
{ |
| 976 |
; |
| 977 |
} |
| 978 |
} |
| 979 |
`); |
| 980 |
parseAndAssertSame(` |
| 981 |
var B = { |
| 982 |
get x () { |
| 983 |
return this.ecks; |
| 984 |
}, |
| 985 |
set x (ecks) { |
| 986 |
this.ecks = ecks; |
| 987 |
} |
| 988 |
}; |
| 989 |
`); |
| 990 |
}); |
| 991 |
|
| 992 |
it("RestOperator", () => { |
| 993 |
parseAndAssertSame("var { a, ...b } = c"); |
| 994 |
parseAndAssertSame("var [ a, ...b ] = c"); |
| 995 |
parseAndAssertSame("var a = function (...b) {}"); |
| 996 |
}); |
| 997 |
|
| 998 |
it("SpreadOperator", () => { |
| 999 |
parseAndAssertSame("var a = { b, ...c }"); |
| 1000 |
parseAndAssertSame("var a = [ a, ...b ]"); |
| 1001 |
parseAndAssertSame("var a = sum(...b)"); |
| 1002 |
}); |
| 1003 |
|
| 1004 |
it("Async/Await", () => { |
| 1005 |
parseAndAssertSame(` |
| 1006 |
async function a() { |
| 1007 |
await 1; |
| 1008 |
} |
| 1009 |
`); |
| 1010 |
}); |
| 1011 |
|
| 1012 |
it("BigInt", () => { |
| 1013 |
parseAndAssertSame(` |
| 1014 |
const a = 1n; |
| 1015 |
`); |
| 1016 |
}); |
| 1017 |
|
| 1018 |
it("Dynamic Import", () => { |
| 1019 |
parseAndAssertSame(` |
| 1020 |
const a = import(moduleName); |
| 1021 |
`); |
| 1022 |
}); |
| 1023 |
}); |
| 1024 |
}); |