| 1 |
import { |
| 2 |
isExportDeclaration, |
| 3 |
isIdentifier, |
| 4 |
isDeclaration, |
| 5 |
isFunctionDeclaration, |
| 6 |
isFunctionExpression, |
| 7 |
isExportAllDeclaration, |
| 8 |
} from "../validators/generated"; |
| 9 |
import type * as t from ".."; |
| 10 |
|
| 11 |
export { getBindingIdentifiers as default }; |
| 12 |
|
| 13 |
function getBindingIdentifiers( |
| 14 |
node: t.Node, |
| 15 |
duplicates: true, |
| 16 |
outerOnly?: boolean, |
| 17 |
): Record<string, Array<t.Identifier>>; |
| 18 |
|
| 19 |
function getBindingIdentifiers( |
| 20 |
node: t.Node, |
| 21 |
duplicates?: false, |
| 22 |
outerOnly?: boolean, |
| 23 |
): Record<string, t.Identifier>; |
| 24 |
|
| 25 |
function getBindingIdentifiers( |
| 26 |
node: t.Node, |
| 27 |
duplicates?: boolean, |
| 28 |
outerOnly?: boolean, |
| 29 |
): Record<string, t.Identifier> | Record<string, Array<t.Identifier>>; |
| 30 |
|
| 31 |
/** |
| 32 |
* Return a list of binding identifiers associated with the input `node`. |
| 33 |
*/ |
| 34 |
function getBindingIdentifiers( |
| 35 |
node: t.Node, |
| 36 |
duplicates?: boolean, |
| 37 |
outerOnly?: boolean, |
| 38 |
): Record<string, t.Identifier> | Record<string, Array<t.Identifier>> { |
| 39 |
const search: t.Node[] = [].concat(node); |
| 40 |
const ids = Object.create(null); |
| 41 |
|
| 42 |
while (search.length) { |
| 43 |
const id = search.shift(); |
| 44 |
if (!id) continue; |
| 45 |
|
| 46 |
const keys = |
| 47 |
// @ts-expect-error getBindingIdentifiers.keys do not cover all AST types |
| 48 |
getBindingIdentifiers.keys[id.type]; |
| 49 |
|
| 50 |
if (isIdentifier(id)) { |
| 51 |
if (duplicates) { |
| 52 |
const _ids = (ids[id.name] = ids[id.name] || []); |
| 53 |
_ids.push(id); |
| 54 |
} else { |
| 55 |
ids[id.name] = id; |
| 56 |
} |
| 57 |
continue; |
| 58 |
} |
| 59 |
|
| 60 |
if (isExportDeclaration(id) && !isExportAllDeclaration(id)) { |
| 61 |
if (isDeclaration(id.declaration)) { |
| 62 |
search.push(id.declaration); |
| 63 |
} |
| 64 |
continue; |
| 65 |
} |
| 66 |
|
| 67 |
if (outerOnly) { |
| 68 |
if (isFunctionDeclaration(id)) { |
| 69 |
search.push(id.id); |
| 70 |
continue; |
| 71 |
} |
| 72 |
|
| 73 |
if (isFunctionExpression(id)) { |
| 74 |
continue; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
if (keys) { |
| 79 |
for (let i = 0; i < keys.length; i++) { |
| 80 |
const key = keys[i]; |
| 81 |
const nodes = |
| 82 |
// @ts-expect-error key must present in id |
| 83 |
id[key] as t.Node[] | t.Node | undefined | null; |
| 84 |
if (nodes) { |
| 85 |
Array.isArray(nodes) ? search.push(...nodes) : search.push(nodes); |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
// $FlowIssue Object.create() seems broken |
| 92 |
return ids; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Mapping of types to their identifier keys. |
| 97 |
*/ |
| 98 |
getBindingIdentifiers.keys = { |
| 99 |
DeclareClass: ["id"], |
| 100 |
DeclareFunction: ["id"], |
| 101 |
DeclareModule: ["id"], |
| 102 |
DeclareVariable: ["id"], |
| 103 |
DeclareInterface: ["id"], |
| 104 |
DeclareTypeAlias: ["id"], |
| 105 |
DeclareOpaqueType: ["id"], |
| 106 |
InterfaceDeclaration: ["id"], |
| 107 |
TypeAlias: ["id"], |
| 108 |
OpaqueType: ["id"], |
| 109 |
|
| 110 |
CatchClause: ["param"], |
| 111 |
LabeledStatement: ["label"], |
| 112 |
UnaryExpression: ["argument"], |
| 113 |
AssignmentExpression: ["left"], |
| 114 |
|
| 115 |
ImportSpecifier: ["local"], |
| 116 |
ImportNamespaceSpecifier: ["local"], |
| 117 |
ImportDefaultSpecifier: ["local"], |
| 118 |
ImportDeclaration: ["specifiers"], |
| 119 |
|
| 120 |
ExportSpecifier: ["exported"], |
| 121 |
ExportNamespaceSpecifier: ["exported"], |
| 122 |
ExportDefaultSpecifier: ["exported"], |
| 123 |
|
| 124 |
FunctionDeclaration: ["id", "params"], |
| 125 |
FunctionExpression: ["id", "params"], |
| 126 |
ArrowFunctionExpression: ["params"], |
| 127 |
ObjectMethod: ["params"], |
| 128 |
ClassMethod: ["params"], |
| 129 |
ClassPrivateMethod: ["params"], |
| 130 |
|
| 131 |
ForInStatement: ["left"], |
| 132 |
ForOfStatement: ["left"], |
| 133 |
|
| 134 |
ClassDeclaration: ["id"], |
| 135 |
ClassExpression: ["id"], |
| 136 |
|
| 137 |
RestElement: ["argument"], |
| 138 |
UpdateExpression: ["argument"], |
| 139 |
|
| 140 |
ObjectProperty: ["value"], |
| 141 |
|
| 142 |
AssignmentPattern: ["left"], |
| 143 |
ArrayPattern: ["elements"], |
| 144 |
ObjectPattern: ["properties"], |
| 145 |
|
| 146 |
VariableDeclaration: ["declarations"], |
| 147 |
VariableDeclarator: ["id"], |
| 148 |
}; |