| 1 |
import * as t from "../lib/index.js"; |
| 2 |
import { parse } from "@babel/parser"; |
| 3 |
|
| 4 |
function getBody(program) { |
| 5 |
return parse(program, { sourceType: "module" }).program.body; |
| 6 |
} |
| 7 |
|
| 8 |
describe("retrievers", function () { |
| 9 |
describe("getBindingIdentifiers", function () { |
| 10 |
it.each([ |
| 11 |
[ |
| 12 |
"variable declarations", |
| 13 |
getBody("var a = 1; let b = 2; const c = 3;"), |
| 14 |
["a", "b", "c"], |
| 15 |
], |
| 16 |
[ |
| 17 |
"function declarations", |
| 18 |
getBody("var foo = 1; function bar() { var baz = 2; }"), |
| 19 |
["bar", "foo"], |
| 20 |
], |
| 21 |
[ |
| 22 |
"object methods", |
| 23 |
getBody("({ a(b) { let c } })")[0].expression.properties[0], |
| 24 |
["b"], |
| 25 |
], |
| 26 |
[ |
| 27 |
"class methods", |
| 28 |
getBody("(class { a(b) { let c } })")[0].expression.body.body, |
| 29 |
["b"], |
| 30 |
], |
| 31 |
[ |
| 32 |
"class private methods", |
| 33 |
getBody("(class { #a(b) { let c } })")[0].expression.body.body, |
| 34 |
["b"], |
| 35 |
], |
| 36 |
[ |
| 37 |
"export named declarations", |
| 38 |
getBody("export const foo = 'foo';"), |
| 39 |
["foo"], |
| 40 |
], |
| 41 |
[ |
| 42 |
"export default class declarations", |
| 43 |
getBody("export default class foo {}"), |
| 44 |
["foo"], |
| 45 |
], |
| 46 |
[ |
| 47 |
"export default referenced identifiers", |
| 48 |
getBody("export default foo"), |
| 49 |
[], |
| 50 |
], |
| 51 |
["export all declarations", getBody("export * from 'x'"), []], |
| 52 |
[ |
| 53 |
"export all as namespace declarations", |
| 54 |
getBody("export * as ns from 'x'"), |
| 55 |
[], // exported bindings are not associated with declarations |
| 56 |
], |
| 57 |
[ |
| 58 |
"export namespace specifiers", |
| 59 |
getBody("export * as ns from 'x'")[0].specifiers, |
| 60 |
["ns"], |
| 61 |
], |
| 62 |
[ |
| 63 |
"object patterns", |
| 64 |
getBody("const { a, b: { ...c } = { d } } = {}"), |
| 65 |
["a", "c"], |
| 66 |
], |
| 67 |
[ |
| 68 |
"array patterns", |
| 69 |
getBody("var [ a, ...{ b, ...c } ] = {}"), |
| 70 |
["a", "b", "c"], |
| 71 |
], |
| 72 |
["update expression", getBody("++x")[0].expression, ["x"]], |
| 73 |
["assignment expression", getBody("x ??= 1")[0].expression, ["x"]], |
| 74 |
])("%s", (_, program, bindingNames) => { |
| 75 |
const ids = t.getBindingIdentifiers(program); |
| 76 |
expect(Object.keys(ids)).toEqual(bindingNames); |
| 77 |
}); |
| 78 |
}); |
| 79 |
}); |