Debian Codegraph @

eslint/babel-eslint-plugin-development/src/utils/get-reference-origin.cjs

raw symbols SCIP
1 /*::
2 type ReferenceOriginImport = { kind: "import", source: string, name: string };
3 type ReferenceOriginParam = {
4 kind: "param",
5 index: number,
6 functionNode: Node,
7 };
8
9 type ReferenceOrigin =
10 | ReferenceOriginImport
11 | ReferenceOriginParam
12 | { kind: "import *", source: string }
13 | {
14 kind: "property",
15 base: ReferenceOriginImport | ReferenceOriginParam,
16 path: string,
17 name: string,
18 };
19 */
20
21 // Given a node and a context, returns a description of where its value comes
22 // from.
23 // It resolves imports, parameters of exported functions and property accesses.
24 // See the ReferenceOrigin type for more information.
25 module.exports = function getReferenceOrigin(
26 node,
27 scope,
28 ) /*: ?ReferenceOrigin */ {
29 if (node.type === "Identifier") {
30 const variable = getVariableDefinition(node.name, scope);
31 if (!variable) return null;
32
33 const definition = variable.definition;
34 const defNode = definition.node;
35
36 if (definition.type === "ImportBinding") {
37 if (defNode.type === "ImportSpecifier") {
38 return {
39 kind: "import",
40 source: definition.parent.source.value,
41 name: defNode.imported.name,
42 };
43 }
44 if (defNode.type === "ImportNamespaceSpecifier") {
45 return {
46 kind: "import *",
47 source: definition.parent.source.value,
48 };
49 }
50 }
51
52 if (definition.type === "Variable" && defNode.init) {
53 const origin = getReferenceOrigin(defNode.init, variable.scope);
54 return origin && patternToProperty(definition.name, origin);
55 }
56
57 if (definition.type === "Parameter") {
58 return patternToProperty(definition.name, {
59 kind: "param",
60 index: definition.index,
61 functionNode: definition.node,
62 });
63 }
64 }
65
66 if (node.type === "MemberExpression" && !node.computed) {
67 const origin = getReferenceOrigin(node.object, scope);
68 return origin && addProperty(origin, node.property.name);
69 }
70
71 return null;
72 };
73
74 function getVariableDefinition(name, scope) {
75 let currentScope = scope;
76 do {
77 const variable = currentScope.set.get(name);
78 if (variable && variable.defs[0]) {
79 return { scope: currentScope, definition: variable.defs[0] };
80 }
81 } while ((currentScope = currentScope.upper));
82 }
83
84 function patternToProperty(id, base) {
85 const path = getPatternPath(id);
86 return path && path.reduce(addProperty, base);
87 }
88
89 // Adds a property to a given origin. If it was a namespace import it becomes
90 // a named import, so that `import * as x from "foo"; x.bar` and
91 // `import { bar } from "foo"` have the same origin.
92 function addProperty(origin, name) {
93 if (origin.kind === "import *") {
94 return {
95 kind: "import",
96 source: origin.source,
97 name,
98 };
99 }
100 if (origin.kind === "property") {
101 return {
102 kind: "property",
103 base: origin.base,
104 path: origin.path + "." + name,
105 name,
106 };
107 }
108 return {
109 kind: "property",
110 base: origin,
111 path: name,
112 name,
113 };
114 }
115
116 // if "node" is c of { a: { b: c } }, the result is ["a","b"]
117 function getPatternPath(node) {
118 let current = node;
119 const path = [];
120
121 // Unshift keys to path while going up
122 do {
123 const property = current.parent;
124 if (
125 property.type === "ArrayPattern" ||
126 property.type === "AssignmentPattern" ||
127 property.computed
128 ) {
129 // These nodes are not supported.
130 return null;
131 }
132 if (property.type === "Property") {
133 path.unshift(property.key.name);
134 } else {
135 // The destructuring pattern is finished
136 break;
137 }
138 } while ((current = current.parent.parent));
139
140 return path;
141 }