| 1 |
import { parse } from "@babel/parser"; |
| 2 |
import * as t from "@babel/types"; |
| 3 |
|
| 4 |
import _traverse, { NodePath } from "../lib/index.js"; |
| 5 |
const traverse = _traverse.default || _traverse; |
| 6 |
|
| 7 |
function getPath(code, options) { |
| 8 |
const ast = |
| 9 |
typeof code === "string" ? parse(code, options) : createNode(code); |
| 10 |
let path; |
| 11 |
traverse(ast, { |
| 12 |
Program: function (_path) { |
| 13 |
path = _path; |
| 14 |
_path.stop(); |
| 15 |
}, |
| 16 |
}); |
| 17 |
return path; |
| 18 |
} |
| 19 |
|
| 20 |
function getIdentifierPath(code) { |
| 21 |
const ast = parse(code); |
| 22 |
let nodePath; |
| 23 |
traverse(ast, { |
| 24 |
Identifier: function (path) { |
| 25 |
nodePath = path; |
| 26 |
path.stop(); |
| 27 |
}, |
| 28 |
}); |
| 29 |
|
| 30 |
return nodePath; |
| 31 |
} |
| 32 |
|
| 33 |
function createNode(node) { |
| 34 |
const ast = t.file(t.program(Array.isArray(node) ? node : [node])); |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
NodePath.get({ |
| 40 |
hub: { |
| 41 |
buildError: (_, msg) => new Error(msg), |
| 42 |
}, |
| 43 |
parentPath: null, |
| 44 |
parent: ast, |
| 45 |
container: ast, |
| 46 |
key: "program", |
| 47 |
}).setContext(); |
| 48 |
|
| 49 |
return ast; |
| 50 |
} |
| 51 |
|
| 52 |
describe("scope", () => { |
| 53 |
describe("binding paths", () => { |
| 54 |
it("function declaration id", function () { |
| 55 |
expect( |
| 56 |
getPath("function foo() {}").scope.getBinding("foo").path.type, |
| 57 |
).toBe("FunctionDeclaration"); |
| 58 |
}); |
| 59 |
|
| 60 |
it("function expression id", function () { |
| 61 |
expect( |
| 62 |
getPath("(function foo() {})") |
| 63 |
.get("body")[0] |
| 64 |
.get("expression") |
| 65 |
.scope.getBinding("foo").path.type, |
| 66 |
).toBe("FunctionExpression"); |
| 67 |
}); |
| 68 |
|
| 69 |
it("function param", function () { |
| 70 |
expect( |
| 71 |
getPath("(function (foo) {})") |
| 72 |
.get("body")[0] |
| 73 |
.get("expression") |
| 74 |
.scope.getBinding("foo").path.type, |
| 75 |
).toBe("Identifier"); |
| 76 |
}); |
| 77 |
|
| 78 |
describe("function parameter expression", function () { |
| 79 |
it("should not have visibility of declarations inside function body", () => { |
| 80 |
expect( |
| 81 |
getPath( |
| 82 |
`var a = "outside"; (function foo(b = a) { let a = "inside" })`, |
| 83 |
) |
| 84 |
.get("body.1.expression.params.0") |
| 85 |
.scope.getBinding("a").path.node.init.value, |
| 86 |
).toBe("outside"); |
| 87 |
}); |
| 88 |
it("should have visibility on parameter bindings", () => { |
| 89 |
expect( |
| 90 |
getPath(`var a = "outside"; (function foo(b = a, a = "inside") {})`) |
| 91 |
.get("body.1.expression.params.0") |
| 92 |
.scope.getBinding("a").path.node.right.value, |
| 93 |
).toBe("inside"); |
| 94 |
}); |
| 95 |
}); |
| 96 |
|
| 97 |
describe("import declaration", () => { |
| 98 |
it.each([ |
| 99 |
[ |
| 100 |
"import default", |
| 101 |
"import foo from 'foo';(foo)=>{}", |
| 102 |
"foo", |
| 103 |
"ImportDefaultSpecifier", |
| 104 |
], |
| 105 |
[ |
| 106 |
"import named default", |
| 107 |
"import { default as foo } from 'foo';(foo)=>{}", |
| 108 |
"foo", |
| 109 |
"ImportSpecifier", |
| 110 |
], |
| 111 |
[ |
| 112 |
"import named", |
| 113 |
"import { foo } from 'foo';(foo)=>{}", |
| 114 |
"foo", |
| 115 |
"ImportSpecifier", |
| 116 |
], |
| 117 |
[ |
| 118 |
"import named aliased", |
| 119 |
"import { _foo as foo } from 'foo';(foo)=>{}", |
| 120 |
"foo", |
| 121 |
"ImportSpecifier", |
| 122 |
], |
| 123 |
[ |
| 124 |
"import namespace", |
| 125 |
"import * as foo from 'foo';(foo)=>{}", |
| 126 |
"foo", |
| 127 |
"ImportNamespaceSpecifier", |
| 128 |
], |
| 129 |
])("%s", (testTitle, source, bindingName, bindingNodeType) => { |
| 130 |
expect( |
| 131 |
getPath(source, { sourceType: "module" }).scope.getBinding( |
| 132 |
bindingName, |
| 133 |
).path.type, |
| 134 |
).toBe(bindingNodeType); |
| 135 |
}); |
| 136 |
}); |
| 137 |
|
| 138 |
describe("export declaration", () => { |
| 139 |
it.each([ |
| 140 |
[ |
| 141 |
"export default function", |
| 142 |
"export default function foo(foo) {}", |
| 143 |
"foo", |
| 144 |
"FunctionDeclaration", |
| 145 |
], |
| 146 |
[ |
| 147 |
"export default class", |
| 148 |
"export default class foo extends function foo () {} {}", |
| 149 |
"foo", |
| 150 |
"ClassDeclaration", |
| 151 |
], |
| 152 |
[ |
| 153 |
"export named default", |
| 154 |
"export const foo = function foo(foo) {};", |
| 155 |
"foo", |
| 156 |
"VariableDeclarator", |
| 157 |
], |
| 158 |
[ |
| 159 |
"export named default", |
| 160 |
"export const [ { foo } ] = function foo(foo) {};", |
| 161 |
"foo", |
| 162 |
"VariableDeclarator", |
| 163 |
], |
| 164 |
])("%s", (testTitle, source, bindingName, bindingNodeType) => { |
| 165 |
expect( |
| 166 |
getPath(source, { sourceType: "module" }).scope.getBinding( |
| 167 |
bindingName, |
| 168 |
).path.type, |
| 169 |
).toBe(bindingNodeType); |
| 170 |
}); |
| 171 |
}); |
| 172 |
|
| 173 |
describe("computed method key", () => { |
| 174 |
describe("should not have visibility of declarations inside method body", () => { |
| 175 |
it("when path is computed key", () => { |
| 176 |
expect( |
| 177 |
getPath(`var a = "outside"; ({ [a]() { let a = "inside" } })`) |
| 178 |
.get("body.1.expression.properties.0.key") |
| 179 |
.scope.getBinding("a").path.node.init.value, |
| 180 |
).toBe("outside"); |
| 181 |
|
| 182 |
expect( |
| 183 |
getPath( |
| 184 |
`var a = "outside"; class foo { [a]() { let a = "inside" } }`, |
| 185 |
) |
| 186 |
.get("body.1.body.body.0.key") |
| 187 |
.scope.getBinding("a").path.node.init.value, |
| 188 |
).toBe("outside"); |
| 189 |
}); |
| 190 |
|
| 191 |
it("when path is in nested scope which is computed key", () => { |
| 192 |
expect( |
| 193 |
getPath(`var a = "outside"; ({ [() => a]() { let a = "inside" } })`) |
| 194 |
.get("body.1.expression.properties.0.key.body") |
| 195 |
.scope.getBinding("a").path.node.init.value, |
| 196 |
).toBe("outside"); |
| 197 |
|
| 198 |
expect( |
| 199 |
getPath( |
| 200 |
`var a = "outside"; class foo { [() => a]() { let a = "inside" } }`, |
| 201 |
) |
| 202 |
.get("body.1.body.body.0.key.body") |
| 203 |
.scope.getBinding("a").path.node.init.value, |
| 204 |
).toBe("outside"); |
| 205 |
}); |
| 206 |
|
| 207 |
it("when path is in nested scope within computed key", () => { |
| 208 |
expect( |
| 209 |
getPath( |
| 210 |
`var a = "outside"; ({ [(() => a)() + ""]() { let a = "inside" } })`, |
| 211 |
) |
| 212 |
.get("body.1.expression.properties.0.key.left.callee.body") |
| 213 |
.scope.getBinding("a").path.node.init.value, |
| 214 |
).toBe("outside"); |
| 215 |
|
| 216 |
expect( |
| 217 |
getPath( |
| 218 |
`var a = "outside"; class foo { [(() => a)() + ""]() { let a = "inside" } }`, |
| 219 |
) |
| 220 |
.get("body.1.body.body.0.key.left.callee.body") |
| 221 |
.scope.getBinding("a").path.node.init.value, |
| 222 |
).toBe("outside"); |
| 223 |
}); |
| 224 |
|
| 225 |
it("when path is in nested within another computed key", () => { |
| 226 |
expect( |
| 227 |
getPath( |
| 228 |
`var a = "outside"; ({ get [ { get [a]() { let a = "inside"; return a; } }.outside ]() { let a = "middle"; return a; } })`, |
| 229 |
) |
| 230 |
.get("body.1.expression.properties.0.key.object.properties.0.key") |
| 231 |
.scope.getBinding("a").path.node.init.value, |
| 232 |
).toBe("outside"); |
| 233 |
|
| 234 |
expect( |
| 235 |
getPath( |
| 236 |
`var a = "outside"; class foo { static get [ class { static get [a]() { let a = "inside"; return a; } }.outside ]() { let a = "middle"; return a; } }`, |
| 237 |
) |
| 238 |
.get("body.1.body.body.0.key.object.body.body.0.key") |
| 239 |
.scope.getBinding("a").path.node.init.value, |
| 240 |
).toBe("outside"); |
| 241 |
}); |
| 242 |
}); |
| 243 |
|
| 244 |
it("should not have visibility on parameter bindings", () => { |
| 245 |
expect( |
| 246 |
getPath(`var a = "outside"; ({ [a](a = "inside") {} })`) |
| 247 |
.get("body.1.expression.properties.0.key") |
| 248 |
.scope.getBinding("a").path.node.init.value, |
| 249 |
).toBe("outside"); |
| 250 |
|
| 251 |
expect( |
| 252 |
getPath(`var a = "outside"; class foo { [a](a = "inside") {} }`) |
| 253 |
.get("body.1.body.body.0.key") |
| 254 |
.scope.getBinding("a").path.node.init.value, |
| 255 |
).toBe("outside"); |
| 256 |
}); |
| 257 |
}); |
| 258 |
|
| 259 |
describe("decorator", () => { |
| 260 |
const parserOptions = { |
| 261 |
plugins: [["decorators", { decoratorsBeforeExport: true }]], |
| 262 |
}; |
| 263 |
it("should not have visibility of declarations inside method body", () => { |
| 264 |
expect( |
| 265 |
getPath( |
| 266 |
`var a = "outside"; class foo { @(() => () => a) m() { let a = "inside"; } }`, |
| 267 |
parserOptions, |
| 268 |
) |
| 269 |
.get("body.1.body.body.0.decorators.0.expression.body.body") |
| 270 |
.scope.getBinding("a").path.node.init.value, |
| 271 |
).toBe("outside"); |
| 272 |
}); |
| 273 |
it("should not have visibility on parameter bindings", () => { |
| 274 |
expect( |
| 275 |
getPath( |
| 276 |
`var a = "outside"; class foo { @(() => () => a) m(a = "inside") {} }`, |
| 277 |
parserOptions, |
| 278 |
) |
| 279 |
.get("body.1.body.body.0.decorators.0.expression.body.body") |
| 280 |
.scope.getBinding("a").path.node.init.value, |
| 281 |
).toBe("outside"); |
| 282 |
}); |
| 283 |
}); |
| 284 |
|
| 285 |
it("switch discriminant scope", () => { |
| 286 |
expect( |
| 287 |
getPath(`let a = "outside"; switch (a) { default: let a = "inside" }`) |
| 288 |
.get("body.1.discriminant") |
| 289 |
.scope.getBinding("a").path.node.init.value, |
| 290 |
).toBe("outside"); |
| 291 |
}); |
| 292 |
|
| 293 |
it("variable declaration", function () { |
| 294 |
expect(getPath("var foo = null;").scope.getBinding("foo").path.type).toBe( |
| 295 |
"VariableDeclarator", |
| 296 |
); |
| 297 |
expect( |
| 298 |
getPath("var { foo } = null;").scope.getBinding("foo").path.type, |
| 299 |
).toBe("VariableDeclarator"); |
| 300 |
expect( |
| 301 |
getPath("var [ foo ] = null;").scope.getBinding("foo").path.type, |
| 302 |
).toBe("VariableDeclarator"); |
| 303 |
expect( |
| 304 |
getPath("var { bar: [ foo ] } = null;").scope.getBinding("foo").path |
| 305 |
.type, |
| 306 |
).toBe("VariableDeclarator"); |
| 307 |
}); |
| 308 |
|
| 309 |
it("declare var", function () { |
| 310 |
expect( |
| 311 |
getPath("declare var foo;", { plugins: ["flow"] }).scope.getBinding( |
| 312 |
"foo", |
| 313 |
).path.type, |
| 314 |
).toBe("DeclareVariable"); |
| 315 |
}); |
| 316 |
|
| 317 |
it("declare function", function () { |
| 318 |
expect( |
| 319 |
getPath("declare function foo(): void;", { |
| 320 |
plugins: ["flow"], |
| 321 |
}).scope.getBinding("foo").path.type, |
| 322 |
).toBe("DeclareFunction"); |
| 323 |
}); |
| 324 |
|
| 325 |
it("declare module", function () { |
| 326 |
expect( |
| 327 |
getPath("declare module foo {};", { |
| 328 |
plugins: ["flow"], |
| 329 |
}).scope.getBinding("foo").path.type, |
| 330 |
).toBe("DeclareModule"); |
| 331 |
}); |
| 332 |
|
| 333 |
it("declare type alias", function () { |
| 334 |
expect( |
| 335 |
getPath("declare type foo = string;", { |
| 336 |
plugins: ["flow"], |
| 337 |
}).scope.getBinding("foo").path.type, |
| 338 |
).toBe("DeclareTypeAlias"); |
| 339 |
}); |
| 340 |
|
| 341 |
it("declare opaque type", function () { |
| 342 |
expect( |
| 343 |
getPath("declare opaque type foo;", { |
| 344 |
plugins: ["flow"], |
| 345 |
}).scope.getBinding("foo").path.type, |
| 346 |
).toBe("DeclareOpaqueType"); |
| 347 |
}); |
| 348 |
|
| 349 |
it("declare interface", function () { |
| 350 |
expect( |
| 351 |
getPath("declare interface Foo {};", { |
| 352 |
plugins: ["flow"], |
| 353 |
}).scope.getBinding("Foo").path.type, |
| 354 |
).toBe("DeclareInterface"); |
| 355 |
}); |
| 356 |
|
| 357 |
it("type alias", function () { |
| 358 |
expect( |
| 359 |
getPath("type foo = string;", { |
| 360 |
plugins: ["flow"], |
| 361 |
}).scope.getBinding("foo").path.type, |
| 362 |
).toBe("TypeAlias"); |
| 363 |
}); |
| 364 |
|
| 365 |
it("opaque type alias", function () { |
| 366 |
expect( |
| 367 |
getPath("opaque type foo = string;", { |
| 368 |
plugins: ["flow"], |
| 369 |
}).scope.getBinding("foo").path.type, |
| 370 |
).toBe("OpaqueType"); |
| 371 |
}); |
| 372 |
|
| 373 |
it("interface", function () { |
| 374 |
expect( |
| 375 |
getPath("interface Foo {};", { |
| 376 |
plugins: ["flow"], |
| 377 |
}).scope.getBinding("Foo").path.type, |
| 378 |
).toBe("InterfaceDeclaration"); |
| 379 |
}); |
| 380 |
|
| 381 |
it("import type", function () { |
| 382 |
expect( |
| 383 |
getPath("import type {Foo} from 'foo';", { |
| 384 |
plugins: ["flow"], |
| 385 |
}).scope.getBinding("Foo").path.type, |
| 386 |
).toBe("ImportSpecifier"); |
| 387 |
}); |
| 388 |
|
| 389 |
it("import type and func with duplicate name", function () { |
| 390 |
expect(() => { |
| 391 |
getPath( |
| 392 |
` |
| 393 |
import type {Foo} from 'foo'; |
| 394 |
import {type Foo2} from 'foo'; |
| 395 |
function Foo(){} |
| 396 |
function Foo2(){} |
| 397 |
`, |
| 398 |
{ |
| 399 |
plugins: ["typescript"], |
| 400 |
sourceType: "module", |
| 401 |
}, |
| 402 |
); |
| 403 |
}).not.toThrow(); |
| 404 |
}); |
| 405 |
|
| 406 |
it("variable constantness", function () { |
| 407 |
expect(getPath("var a = 1;").scope.getBinding("a").constant).toBe(true); |
| 408 |
expect(getPath("var a = 1; a = 2;").scope.getBinding("a").constant).toBe( |
| 409 |
false, |
| 410 |
); |
| 411 |
expect(getPath("var a = 1, a = 2;").scope.getBinding("a").constant).toBe( |
| 412 |
false, |
| 413 |
); |
| 414 |
expect( |
| 415 |
getPath("var a = 1; var a = 2;").scope.getBinding("a").constant, |
| 416 |
).toBe(false); |
| 417 |
}); |
| 418 |
|
| 419 |
it("variable constantness in loops", () => { |
| 420 |
let scopePath = null; |
| 421 |
const isAConstant = code => { |
| 422 |
let path = getPath(code); |
| 423 |
if (scopePath) path = path.get(scopePath); |
| 424 |
return path.scope.getBinding("a").constant; |
| 425 |
}; |
| 426 |
|
| 427 |
expect(isAConstant("for (_ of ns) { var a = 1; }")).toBe(false); |
| 428 |
expect(isAConstant("for (_ in ns) { var a = 1; }")).toBe(false); |
| 429 |
expect(isAConstant("for (;;) { var a = 1; }")).toBe(false); |
| 430 |
expect(isAConstant("while (1) { var a = 1; }")).toBe(false); |
| 431 |
expect(isAConstant("do { var a = 1; } while (1)")).toBe(false); |
| 432 |
|
| 433 |
expect(isAConstant("for (var a of ns) {}")).toBe(false); |
| 434 |
expect(isAConstant("for (var a in ns) {}")).toBe(false); |
| 435 |
expect(isAConstant("for (var a;;) {}")).toBe(true); |
| 436 |
|
| 437 |
scopePath = "body.0.body.expression"; |
| 438 |
expect(isAConstant("for (_ of ns) () => { var a = 1; }")).toBe(true); |
| 439 |
expect(isAConstant("for (_ in ns) () => { var a = 1; }")).toBe(true); |
| 440 |
expect(isAConstant("for (;;) () => { var a = 1; }")).toBe(true); |
| 441 |
expect(isAConstant("while (1) () => { var a = 1; }")).toBe(true); |
| 442 |
expect(isAConstant("do () => { var a = 1; }; while (1)")).toBe(true); |
| 443 |
|
| 444 |
scopePath = "body.0.body"; |
| 445 |
expect(isAConstant("for (_ of ns) { let a = 1; }")).toBe(true); |
| 446 |
expect(isAConstant("for (_ in ns) { let a = 1; }")).toBe(true); |
| 447 |
expect(isAConstant("for (;;) { let a = 1; }")).toBe(true); |
| 448 |
expect(isAConstant("while (1) { let a = 1; }")).toBe(true); |
| 449 |
expect(isAConstant("do { let a = 1; } while (1)")).toBe(true); |
| 450 |
|
| 451 |
scopePath = "body.0"; |
| 452 |
expect(isAConstant("for (let a of ns) {}")).toBe(true); |
| 453 |
expect(isAConstant("for (let a in ns) {}")).toBe(true); |
| 454 |
expect(isAConstant("for (let a;;) {}")).toBe(true); |
| 455 |
}); |
| 456 |
|
| 457 |
test("label", function () { |
| 458 |
expect(getPath("foo: { }").scope.getBinding("foo")).toBeUndefined(); |
| 459 |
expect(getPath("foo: { }").scope.getLabel("foo").type).toBe( |
| 460 |
"LabeledStatement", |
| 461 |
); |
| 462 |
expect(getPath("foo: { }").scope.getLabel("toString")).toBeUndefined(); |
| 463 |
|
| 464 |
expect( |
| 465 |
getPath( |
| 466 |
` |
| 467 |
foo: { } |
| 468 |
`, |
| 469 |
).scope.generateUid("foo"), |
| 470 |
).toBe("_foo"); |
| 471 |
}); |
| 472 |
|
| 473 |
test("generateUid collision check with labels", function () { |
| 474 |
expect( |
| 475 |
getPath( |
| 476 |
` |
| 477 |
_foo: { } |
| 478 |
`, |
| 479 |
).scope.generateUid("foo"), |
| 480 |
).toBe("_foo2"); |
| 481 |
|
| 482 |
expect( |
| 483 |
getPath( |
| 484 |
` |
| 485 |
_foo: { } |
| 486 |
_foo1: { } |
| 487 |
_foo2: { } |
| 488 |
`, |
| 489 |
).scope.generateUid("foo"), |
| 490 |
).toBe("_foo3"); |
| 491 |
}); |
| 492 |
|
| 493 |
describe("reference paths", () => { |
| 494 |
it("param referenced in function body", function () { |
| 495 |
const path = getIdentifierPath("function square(n) { return n * n}"); |
| 496 |
const referencePaths = path.context.scope.bindings.n.referencePaths; |
| 497 |
expect(referencePaths).toHaveLength(2); |
| 498 |
expect(referencePaths[0].node.loc.start).toEqual({ |
| 499 |
line: 1, |
| 500 |
column: 28, |
| 501 |
index: 28, |
| 502 |
}); |
| 503 |
expect(referencePaths[1].node.loc.start).toEqual({ |
| 504 |
line: 1, |
| 505 |
column: 32, |
| 506 |
index: 32, |
| 507 |
}); |
| 508 |
}); |
| 509 |
it("id referenced in function body", () => { |
| 510 |
const path = getIdentifierPath("(function n(m) { return n })"); |
| 511 |
const { referencePaths, identifier } = path.scope.getOwnBinding("n"); |
| 512 |
expect(identifier.start).toMatchInlineSnapshot(`10`); |
| 513 |
expect(referencePaths).toHaveLength(1); |
| 514 |
expect(referencePaths[0].node.start).toMatchInlineSnapshot(`24`); |
| 515 |
}); |
| 516 |
it("id referenced in param initializer - function expression", () => { |
| 517 |
const path = getIdentifierPath("(function n(m = n) {})"); |
| 518 |
const { referencePaths, identifier } = path.scope.getOwnBinding("n"); |
| 519 |
expect(identifier.start).toMatchInlineSnapshot(`10`); |
| 520 |
expect(referencePaths).toHaveLength(1); |
| 521 |
expect(referencePaths[0].node.start).toMatchInlineSnapshot(`16`); |
| 522 |
}); |
| 523 |
it("id referenced in param initializer - function declaration", () => { |
| 524 |
const path = getIdentifierPath("function n(m = n) {}"); |
| 525 |
const { referencePaths, identifier } = path.scope.getBinding("n"); |
| 526 |
expect(identifier.start).toMatchInlineSnapshot(`9`); |
| 527 |
expect(referencePaths).toHaveLength(1); |
| 528 |
expect(referencePaths[0].node.start).toMatchInlineSnapshot(`15`); |
| 529 |
}); |
| 530 |
it("param referenced in function body with id collision", () => { |
| 531 |
const path = getIdentifierPath("(function n(n) { return n })"); |
| 532 |
const { referencePaths, identifier } = path.scope.getOwnBinding("n"); |
| 533 |
expect(identifier.start).toMatchInlineSnapshot(`12`); |
| 534 |
expect(referencePaths).toHaveLength(1); |
| 535 |
expect(referencePaths[0].node.start).toMatchInlineSnapshot(`24`); |
| 536 |
}); |
| 537 |
it("param referenced in param initializer with id collision", () => { |
| 538 |
const path = getIdentifierPath("(function n(n, m = n) {})"); |
| 539 |
const { referencePaths, identifier } = path.scope.getOwnBinding("n"); |
| 540 |
expect(identifier.start).toMatchInlineSnapshot(`12`); |
| 541 |
expect(referencePaths).toHaveLength(1); |
| 542 |
expect(referencePaths[0].node.start).toMatchInlineSnapshot(`19`); |
| 543 |
}); |
| 544 |
}); |
| 545 |
|
| 546 |
describe("after crawl", () => { |
| 547 |
it("modified function identifier available in function scope", () => { |
| 548 |
const path = getPath("(function f(f) {})") |
| 549 |
.get("body")[0] |
| 550 |
.get("expression"); |
| 551 |
path.get("id").replaceWith(t.identifier("g")); |
| 552 |
path.scope.crawl(); |
| 553 |
const binding = path.scope.getBinding("g"); |
| 554 |
expect(binding.kind).toBe("local"); |
| 555 |
}); |
| 556 |
it("modified function param available in function scope", () => { |
| 557 |
const path = getPath("(function f(f) {})") |
| 558 |
.get("body")[0] |
| 559 |
.get("expression"); |
| 560 |
path.get("params")[0].replaceWith(t.identifier("g")); |
| 561 |
path.scope.crawl(); |
| 562 |
const binding = path.scope.getBinding("g"); |
| 563 |
expect(binding.kind).toBe("param"); |
| 564 |
}); |
| 565 |
it("modified class identifier available in class expression scope", () => { |
| 566 |
const path = getPath("(class c {})").get("body")[0].get("expression"); |
| 567 |
path.get("id").replaceWith(t.identifier("g")); |
| 568 |
path.scope.crawl(); |
| 569 |
const binding = path.scope.getBinding("g"); |
| 570 |
expect(binding.kind).toBe("local"); |
| 571 |
}); |
| 572 |
it("modified class identifier available in class declaration scope", () => { |
| 573 |
const path = getPath("class c {}").get("body")[0]; |
| 574 |
path.get("id").replaceWith(t.identifier("g")); |
| 575 |
path.scope.crawl(); |
| 576 |
const binding = path.scope.getBinding("g"); |
| 577 |
expect(binding.kind).toBe("let"); |
| 578 |
}); |
| 579 |
}); |
| 580 |
|
| 581 |
it("class identifier available in class scope after crawl", function () { |
| 582 |
const path = getPath("class a { build() { return new a(); } }"); |
| 583 |
|
| 584 |
path.scope.crawl(); |
| 585 |
|
| 586 |
let referencePaths = path.scope.bindings.a.referencePaths; |
| 587 |
expect(referencePaths).toHaveLength(1); |
| 588 |
|
| 589 |
referencePaths = path.get("body[0]").scope.bindings.a.referencePaths; |
| 590 |
expect(referencePaths).toHaveLength(1); |
| 591 |
|
| 592 |
expect(path.scope.bindings.a).toBe(path.get("body[0]").scope.bindings.a); |
| 593 |
}); |
| 594 |
|
| 595 |
it("references after re-crawling", function () { |
| 596 |
const path = getPath("function Foo() { var _jsx; }"); |
| 597 |
|
| 598 |
path.scope.crawl(); |
| 599 |
path.scope.crawl(); |
| 600 |
|
| 601 |
expect(path.scope.references._jsx).toBe(true); |
| 602 |
}); |
| 603 |
|
| 604 |
test("generateUid collision check after re-crawling", function () { |
| 605 |
const path = getPath("function Foo() { var _jsx; }"); |
| 606 |
|
| 607 |
path.scope.crawl(); |
| 608 |
path.scope.crawl(); |
| 609 |
|
| 610 |
expect(path.scope.generateUid("jsx")).toBe("_jsx2"); |
| 611 |
}); |
| 612 |
|
| 613 |
test("generateUid collision check after re-crawling (function expression local id)", function () { |
| 614 |
const path = getPath("var fn = function _name(){}"); |
| 615 |
|
| 616 |
path.scope.crawl(); |
| 617 |
path.scope.crawl(); |
| 618 |
|
| 619 |
expect(path.scope.generateUid("name")).toBe("_name2"); |
| 620 |
}); |
| 621 |
|
| 622 |
test("generateUid collision check after re-crawling (function params)", function () { |
| 623 |
const path = getPath("[].map(_unicorn => [_unicorn])"); |
| 624 |
|
| 625 |
path.scope.crawl(); |
| 626 |
path.scope.crawl(); |
| 627 |
|
| 628 |
expect(path.scope.generateUid("unicorn")).toBe("_unicorn2"); |
| 629 |
}); |
| 630 |
|
| 631 |
test("generateUid collision check after re-crawling (catch clause)", function () { |
| 632 |
const path = getPath("try {} catch (_err) {}"); |
| 633 |
|
| 634 |
path.scope.crawl(); |
| 635 |
path.scope.crawl(); |
| 636 |
|
| 637 |
expect(path.scope.generateUid("err")).toBe("_err2"); |
| 638 |
}); |
| 639 |
|
| 640 |
test("generateUid collision check after re-crawling (class expression local id)", function () { |
| 641 |
const path = getPath("var C = class _Cls{}"); |
| 642 |
|
| 643 |
path.scope.crawl(); |
| 644 |
path.scope.crawl(); |
| 645 |
|
| 646 |
expect(path.scope.generateUid("Cls")).toBe("_Cls2"); |
| 647 |
}); |
| 648 |
|
| 649 |
it("re-exports are not references", () => { |
| 650 |
const path = getPath("export { x } from 'y'", { |
| 651 |
sourceType: "module", |
| 652 |
}); |
| 653 |
expect(path.scope.hasGlobal("x")).toBe(false); |
| 654 |
}); |
| 655 |
}); |
| 656 |
|
| 657 |
describe("duplicate bindings", () => { |
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
describe("catch", () => { |
| 664 |
|
| 665 |
const createTryCatch = function (kind) { |
| 666 |
return t.tryStatement( |
| 667 |
t.blockStatement([]), |
| 668 |
t.catchClause( |
| 669 |
t.identifier("e"), |
| 670 |
t.blockStatement([ |
| 671 |
t.variableDeclaration(kind, [ |
| 672 |
t.variableDeclarator(t.identifier("e"), t.stringLiteral("1")), |
| 673 |
]), |
| 674 |
]), |
| 675 |
), |
| 676 |
); |
| 677 |
}; |
| 678 |
it("let", () => { |
| 679 |
const ast = createTryCatch("let"); |
| 680 |
|
| 681 |
expect(() => getPath(ast)).toThrowErrorMatchingSnapshot(); |
| 682 |
}); |
| 683 |
|
| 684 |
it("const", () => { |
| 685 |
const ast = createTryCatch("const"); |
| 686 |
|
| 687 |
expect(() => getPath(ast)).toThrowErrorMatchingSnapshot(); |
| 688 |
}); |
| 689 |
|
| 690 |
it("using", () => { |
| 691 |
const ast = createTryCatch("using"); |
| 692 |
|
| 693 |
expect(() => getPath(ast)).toThrowErrorMatchingSnapshot(); |
| 694 |
}); |
| 695 |
|
| 696 |
it("var", () => { |
| 697 |
const ast = createTryCatch("var"); |
| 698 |
|
| 699 |
expect(() => getPath(ast)).not.toThrow(); |
| 700 |
}); |
| 701 |
}); |
| 702 |
|
| 703 |
["let", "const"].forEach(name => { |
| 704 |
it(`${name} and function in sub scope`, () => { |
| 705 |
const ast = [ |
| 706 |
t.variableDeclaration(name, [ |
| 707 |
t.variableDeclarator(t.identifier("foo")), |
| 708 |
]), |
| 709 |
t.blockStatement([ |
| 710 |
t.functionDeclaration( |
| 711 |
t.identifier("foo"), |
| 712 |
[], |
| 713 |
t.blockStatement([]), |
| 714 |
), |
| 715 |
]), |
| 716 |
]; |
| 717 |
|
| 718 |
expect(() => getPath(ast)).not.toThrow(); |
| 719 |
}); |
| 720 |
}); |
| 721 |
|
| 722 |
describe("duplicate declaration", () => { |
| 723 |
it("should not throw error on duplicate class and function declaration", () => { |
| 724 |
const ast = [ |
| 725 |
t.classDeclaration(t.identifier("A"), null, t.classBody([]), []), |
| 726 |
t.functionDeclaration(t.identifier("A"), [], t.blockStatement([])), |
| 727 |
]; |
| 728 |
|
| 729 |
ast[0].declare = true; |
| 730 |
expect(() => getPath(ast)).not.toThrow(); |
| 731 |
}); |
| 732 |
}); |
| 733 |
|
| 734 |
describe("global", () => { |
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
const cases = [ |
| 739 |
["const", "let", false], |
| 740 |
["const", "const", false], |
| 741 |
["const", "function", false], |
| 742 |
["const", "class", false], |
| 743 |
["const", "var", false], |
| 744 |
|
| 745 |
["let", "let", false], |
| 746 |
["let", "class", false], |
| 747 |
["let", "function", false], |
| 748 |
["let", "var", false], |
| 749 |
|
| 750 |
["var", "class", false], |
| 751 |
["var", "function", true], |
| 752 |
["var", "var", true], |
| 753 |
|
| 754 |
["class", "function", false], |
| 755 |
["class", "class", false], |
| 756 |
|
| 757 |
["function", "function", true], |
| 758 |
]; |
| 759 |
|
| 760 |
const createNode = function (kind) { |
| 761 |
switch (kind) { |
| 762 |
case "let": |
| 763 |
case "const": |
| 764 |
case "var": |
| 765 |
return t.variableDeclaration(kind, [ |
| 766 |
t.variableDeclarator(t.identifier("foo")), |
| 767 |
]); |
| 768 |
case "class": |
| 769 |
return t.classDeclaration( |
| 770 |
t.identifier("foo"), |
| 771 |
null, |
| 772 |
t.classBody([]), |
| 773 |
); |
| 774 |
case "function": |
| 775 |
return t.functionDeclaration( |
| 776 |
t.identifier("foo"), |
| 777 |
[], |
| 778 |
t.blockStatement([]), |
| 779 |
); |
| 780 |
} |
| 781 |
}; |
| 782 |
|
| 783 |
const createAST = function (kind1, kind2) { |
| 784 |
return [createNode(kind1), createNode(kind2)]; |
| 785 |
}; |
| 786 |
|
| 787 |
for (const [kind1, kind2, success] of cases) { |
| 788 |
if (success) { |
| 789 |
it(`${kind1}/${kind2} should succeed`, () => { |
| 790 |
const ast = createAST(kind1, kind2); |
| 791 |
expect(() => getPath(ast)).not.toThrow(); |
| 792 |
}); |
| 793 |
} else { |
| 794 |
it(`${kind1}/${kind2} should fail`, () => { |
| 795 |
const ast = createAST(kind1, kind2); |
| 796 |
expect(() => getPath(ast)).toThrowErrorMatchingSnapshot(); |
| 797 |
}); |
| 798 |
} |
| 799 |
|
| 800 |
if (kind1 !== kind2) { |
| 801 |
|
| 802 |
if (kind1 === "const" && (kind2 === "function" || kind2 === "var")) { |
| 803 |
continue; |
| 804 |
} |
| 805 |
if (success) { |
| 806 |
it(`${kind2}/${kind1} should succeed`, () => { |
| 807 |
const ast = createAST(kind2, kind1); |
| 808 |
expect(() => getPath(ast)).not.toThrow(); |
| 809 |
}); |
| 810 |
} else { |
| 811 |
it(`${kind2}/${kind1} should fail`, () => { |
| 812 |
const ast = createAST(kind2, kind1); |
| 813 |
expect(() => getPath(ast)).toThrowErrorMatchingSnapshot(); |
| 814 |
}); |
| 815 |
} |
| 816 |
} |
| 817 |
} |
| 818 |
}); |
| 819 |
}); |
| 820 |
|
| 821 |
describe("own bindings", () => { |
| 822 |
|
| 823 |
describe("var declarations should be registered", () => { |
| 824 |
it("in program", () => { |
| 825 |
const program = getPath("var foo;"); |
| 826 |
expect(program.scope.hasOwnBinding("foo")).toBe(true); |
| 827 |
}); |
| 828 |
it("in function declaration", () => { |
| 829 |
const functionDeclaration = getPath("function f() { var foo; }").get( |
| 830 |
"body.0", |
| 831 |
); |
| 832 |
expect(functionDeclaration.scope.hasOwnBinding("foo")).toBe(true); |
| 833 |
}); |
| 834 |
it("in function expression", () => { |
| 835 |
const functionExpression = getPath("(function () { var foo; })").get( |
| 836 |
"body.0.expression", |
| 837 |
); |
| 838 |
expect(functionExpression.scope.hasOwnBinding("foo")).toBe(true); |
| 839 |
}); |
| 840 |
it("in arrow expression", () => { |
| 841 |
const arrowExpression = |
| 842 |
getPath("() => { var foo; }").get("body.0.expression"); |
| 843 |
expect(arrowExpression.scope.hasOwnBinding("foo")).toBe(true); |
| 844 |
}); |
| 845 |
it("in object method", () => { |
| 846 |
const objectMethod = getPath("({ method() { var foo; } })").get( |
| 847 |
"body.0.expression.properties.0", |
| 848 |
); |
| 849 |
expect(objectMethod.scope.hasOwnBinding("foo")).toBe(true); |
| 850 |
}); |
| 851 |
it("in class method", () => { |
| 852 |
const classMethod = getPath("(class { method() { var foo; } })").get( |
| 853 |
"body.0.expression.body.body.0", |
| 854 |
); |
| 855 |
expect(classMethod.scope.hasOwnBinding("foo")).toBe(true); |
| 856 |
}); |
| 857 |
it("in class private method", () => { |
| 858 |
const classMethod = getPath("(class { #method() { var foo; } })").get( |
| 859 |
"body.0.expression.body.body.0", |
| 860 |
); |
| 861 |
expect(classMethod.scope.hasOwnBinding("foo")).toBe(true); |
| 862 |
}); |
| 863 |
it("in static block", () => { |
| 864 |
const staticBlock = getPath("(class { static { var foo; } })", { |
| 865 |
plugins: ["classStaticBlock"], |
| 866 |
}).get("body.0.expression.body.body.0"); |
| 867 |
expect(staticBlock.scope.hasOwnBinding("foo")).toBe(true); |
| 868 |
}); |
| 869 |
}); |
| 870 |
describe("var declarations should not be registered", () => { |
| 871 |
it("in block statement", () => { |
| 872 |
const blockStatement = getPath("{ var foo; }").get("body.0"); |
| 873 |
expect(blockStatement.scope.hasOwnBinding("foo")).toBe(false); |
| 874 |
}); |
| 875 |
it("in catch clause", () => { |
| 876 |
const catchClause = getPath("try {} catch { var foo; }").get( |
| 877 |
"body.0.handler", |
| 878 |
); |
| 879 |
expect(catchClause.scope.hasOwnBinding("foo")).toBe(false); |
| 880 |
}); |
| 881 |
it("in for-init statement", () => { |
| 882 |
const forStatement = getPath("for (var foo;;);").get("body.0"); |
| 883 |
expect(forStatement.scope.hasOwnBinding("foo")).toBe(false); |
| 884 |
}); |
| 885 |
it("in for-in statement", () => { |
| 886 |
const forStatement = getPath("for (var foo in x);").get("body.0"); |
| 887 |
expect(forStatement.scope.hasOwnBinding("foo")).toBe(false); |
| 888 |
}); |
| 889 |
it("in for-of statement", () => { |
| 890 |
const forStatement = getPath("for (var foo of x);").get("body.0"); |
| 891 |
expect(forStatement.scope.hasOwnBinding("foo")).toBe(false); |
| 892 |
}); |
| 893 |
it("in switch statement", () => { |
| 894 |
const switchStatement = getPath("switch (0) { case 0: var foo; }").get( |
| 895 |
"body.0", |
| 896 |
); |
| 897 |
expect(switchStatement.scope.hasOwnBinding("foo")).toBe(false); |
| 898 |
}); |
| 899 |
it("in while statement", () => { |
| 900 |
const whileStatement = getPath("while (0) \n var foo;").get("body.0"); |
| 901 |
expect(whileStatement.scope.hasOwnBinding("foo")).toBe(false); |
| 902 |
}); |
| 903 |
it("in do-while statement", () => { |
| 904 |
const doWhileStatement = getPath("do \n var foo \n while(0);").get( |
| 905 |
"body.0", |
| 906 |
); |
| 907 |
expect(doWhileStatement.scope.hasOwnBinding("foo")).toBe(false); |
| 908 |
}); |
| 909 |
}); |
| 910 |
|
| 911 |
describe("let declarations should be registered", () => { |
| 912 |
it("in program", () => { |
| 913 |
const program = getPath("let foo;"); |
| 914 |
expect(program.scope.hasOwnBinding("foo")).toBe(true); |
| 915 |
}); |
| 916 |
it("in function declaration", () => { |
| 917 |
const functionDeclaration = getPath("function f() { let foo; }").get( |
| 918 |
"body.0", |
| 919 |
); |
| 920 |
expect(functionDeclaration.scope.hasOwnBinding("foo")).toBe(true); |
| 921 |
}); |
| 922 |
it("in function expression", () => { |
| 923 |
const functionExpression = getPath("(function () { let foo; })").get( |
| 924 |
"body.0.expression", |
| 925 |
); |
| 926 |
expect(functionExpression.scope.hasOwnBinding("foo")).toBe(true); |
| 927 |
}); |
| 928 |
it("in arrow expression", () => { |
| 929 |
const arrowExpression = |
| 930 |
getPath("() => { let foo; }").get("body.0.expression"); |
| 931 |
expect(arrowExpression.scope.hasOwnBinding("foo")).toBe(true); |
| 932 |
}); |
| 933 |
it("in object method", () => { |
| 934 |
const objectMethod = getPath("({ method() { let foo; } })").get( |
| 935 |
"body.0.expression.properties.0", |
| 936 |
); |
| 937 |
expect(objectMethod.scope.hasOwnBinding("foo")).toBe(true); |
| 938 |
}); |
| 939 |
it("in class method", () => { |
| 940 |
const classMethod = getPath("(class { method() { let foo; } })").get( |
| 941 |
"body.0.expression.body.body.0", |
| 942 |
); |
| 943 |
expect(classMethod.scope.hasOwnBinding("foo")).toBe(true); |
| 944 |
}); |
| 945 |
it("in class private method", () => { |
| 946 |
const classMethod = getPath("(class { #method() { let foo; } })").get( |
| 947 |
"body.0.expression.body.body.0", |
| 948 |
); |
| 949 |
expect(classMethod.scope.hasOwnBinding("foo")).toBe(true); |
| 950 |
}); |
| 951 |
it("in static block", () => { |
| 952 |
const staticBlock = getPath("(class { static { let foo; } })", { |
| 953 |
plugins: ["classStaticBlock"], |
| 954 |
}).get("body.0.expression.body.body.0"); |
| 955 |
expect(staticBlock.scope.hasOwnBinding("foo")).toBe(true); |
| 956 |
}); |
| 957 |
it("in block statement", () => { |
| 958 |
const blockStatement = getPath("{ let foo; }").get("body.0"); |
| 959 |
expect(blockStatement.scope.hasOwnBinding("foo")).toBe(true); |
| 960 |
}); |
| 961 |
it("in catch clause", () => { |
| 962 |
const catchClause = getPath("try {} catch { let foo; }").get( |
| 963 |
"body.0.handler", |
| 964 |
); |
| 965 |
expect(catchClause.scope.hasOwnBinding("foo")).toBe(true); |
| 966 |
}); |
| 967 |
it("in for-init statement", () => { |
| 968 |
const forStatement = getPath("for (let foo;;);").get("body.0"); |
| 969 |
expect(forStatement.scope.hasOwnBinding("foo")).toBe(true); |
| 970 |
}); |
| 971 |
it("in for-in statement", () => { |
| 972 |
const forStatement = getPath("for (let foo in x);").get("body.0"); |
| 973 |
expect(forStatement.scope.hasOwnBinding("foo")).toBe(true); |
| 974 |
}); |
| 975 |
it("in for-of statement", () => { |
| 976 |
const forStatement = getPath("for (let foo of x);").get("body.0"); |
| 977 |
expect(forStatement.scope.hasOwnBinding("foo")).toBe(true); |
| 978 |
}); |
| 979 |
it("in switch statement", () => { |
| 980 |
const switchStatement = getPath("switch (0) { case 0: let foo; }").get( |
| 981 |
"body.0", |
| 982 |
); |
| 983 |
expect(switchStatement.scope.hasOwnBinding("foo")).toBe(true); |
| 984 |
}); |
| 985 |
}); |
| 986 |
}); |
| 987 |
|
| 988 |
describe(".push", () => { |
| 989 |
it("registers the new binding in the correct scope", () => { |
| 990 |
const program = getPath("class A {}"); |
| 991 |
const classDeclaration = program.get("body.0"); |
| 992 |
classDeclaration.scope.push({ id: t.identifier("class") }); |
| 993 |
expect(program.toString()).toMatchInlineSnapshot(` |
| 994 |
"var class; |
| 995 |
class A {}" |
| 996 |
`); |
| 997 |
expect(program.scope.hasOwnBinding("class")).toBe(true); |
| 998 |
}); |
| 999 |
it("registers the new binding outside function when the path is a param initializer", () => { |
| 1000 |
const program = getPath("(a = f()) => {}"); |
| 1001 |
const assignmentPattern = program.get("body.0.expression.params.0"); |
| 1002 |
assignmentPattern.scope.push({ id: t.identifier("ref") }); |
| 1003 |
expect(program.toString()).toMatchInlineSnapshot(` |
| 1004 |
"var ref; |
| 1005 |
(a = f()) => {};" |
| 1006 |
`); |
| 1007 |
expect(program.scope.hasOwnBinding("ref")).toBe(true); |
| 1008 |
}); |
| 1009 |
it("registers the new binding outside class method when the path is a param initializer", () => { |
| 1010 |
const program = getPath("class C { m(a = f()) {} }"); |
| 1011 |
const assignmentPattern = program.get("body.0.body.body.0.params.0"); |
| 1012 |
assignmentPattern.scope.push({ id: t.identifier("ref") }); |
| 1013 |
expect(program.toString()).toMatchInlineSnapshot(` |
| 1014 |
"var ref; |
| 1015 |
class C { |
| 1016 |
m(a = f()) {} |
| 1017 |
}" |
| 1018 |
`); |
| 1019 |
expect(program.scope.hasOwnBinding("ref")).toBe(true); |
| 1020 |
}); |
| 1021 |
}); |
| 1022 |
|
| 1023 |
describe("rename", () => { |
| 1024 |
it(".parentPath after renaming variable in switch", () => { |
| 1025 |
const program = getPath(` |
| 1026 |
switch (x) { |
| 1027 |
case y: |
| 1028 |
let a; |
| 1029 |
} |
| 1030 |
`); |
| 1031 |
program.traverse({ |
| 1032 |
VariableDeclaration(path) { |
| 1033 |
if (path.node.declarations[0].id.name !== "a") return; |
| 1034 |
|
| 1035 |
expect(path.parentPath.type).toBe("SwitchCase"); |
| 1036 |
path.scope.rename("a"); |
| 1037 |
expect(path.parentPath.type).toBe("SwitchCase"); |
| 1038 |
}, |
| 1039 |
}); |
| 1040 |
}); |
| 1041 |
}); |
| 1042 |
}); |