| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
module.exports = function getReferenceOrigin( |
| 26 |
node, |
| 27 |
scope, |
| 28 |
) { |
| 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 |
|
| 90 |
|
| 91 |
|
| 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 |
|
| 117 |
function getPatternPath(node) { |
| 118 |
let current = node; |
| 119 |
const path = []; |
| 120 |
|
| 121 |
|
| 122 |
do { |
| 123 |
const property = current.parent; |
| 124 |
if ( |
| 125 |
property.type === "ArrayPattern" || |
| 126 |
property.type === "AssignmentPattern" || |
| 127 |
property.computed |
| 128 |
) { |
| 129 |
|
| 130 |
return null; |
| 131 |
} |
| 132 |
if (property.type === "Property") { |
| 133 |
path.unshift(property.key.name); |
| 134 |
} else { |
| 135 |
|
| 136 |
break; |
| 137 |
} |
| 138 |
} while ((current = current.parent.parent)); |
| 139 |
|
| 140 |
return path; |
| 141 |
} |