| 1 |
const { |
| 2 |
Definition, |
| 3 |
PatternVisitor: OriginalPatternVisitor, |
| 4 |
Referencer: OriginalReferencer, |
| 5 |
Scope, |
| 6 |
ScopeManager, |
| 7 |
} = process.env.BABEL_8_BREAKING |
| 8 |
? require("eslint-scope") |
| 9 |
: require("@nicolo-ribaudo/eslint-scope-5-internals"); |
| 10 |
const { getKeys: fallback } = require("eslint-visitor-keys"); |
| 11 |
|
| 12 |
let visitorKeysMap; |
| 13 |
function getVisitorValues(nodeType, client) { |
| 14 |
if (visitorKeysMap) return visitorKeysMap[nodeType]; |
| 15 |
|
| 16 |
const { FLOW_FLIPPED_ALIAS_KEYS, VISITOR_KEYS } = client.getTypesInfo(); |
| 17 |
|
| 18 |
const flowFlippedAliasKeys = FLOW_FLIPPED_ALIAS_KEYS.concat([ |
| 19 |
"ArrayPattern", |
| 20 |
"ClassDeclaration", |
| 21 |
"ClassExpression", |
| 22 |
"FunctionDeclaration", |
| 23 |
"FunctionExpression", |
| 24 |
"Identifier", |
| 25 |
"ObjectPattern", |
| 26 |
"RestElement", |
| 27 |
]); |
| 28 |
|
| 29 |
visitorKeysMap = Object.entries(VISITOR_KEYS).reduce((acc, [key, value]) => { |
| 30 |
if (!flowFlippedAliasKeys.includes(value)) { |
| 31 |
acc[key] = value; |
| 32 |
} |
| 33 |
return acc; |
| 34 |
}, {}); |
| 35 |
|
| 36 |
return visitorKeysMap[nodeType]; |
| 37 |
} |
| 38 |
|
| 39 |
const propertyTypes = { |
| 40 |
|
| 41 |
callProperties: { type: "loop", values: ["value"] }, |
| 42 |
indexers: { type: "loop", values: ["key", "value"] }, |
| 43 |
properties: { type: "loop", values: ["argument", "value"] }, |
| 44 |
types: { type: "loop" }, |
| 45 |
params: { type: "loop" }, |
| 46 |
|
| 47 |
argument: { type: "single" }, |
| 48 |
elementType: { type: "single" }, |
| 49 |
qualification: { type: "single" }, |
| 50 |
rest: { type: "single" }, |
| 51 |
returnType: { type: "single" }, |
| 52 |
|
| 53 |
typeAnnotation: { type: "typeAnnotation" }, |
| 54 |
typeParameters: { type: "typeParameters" }, |
| 55 |
id: { type: "id" }, |
| 56 |
}; |
| 57 |
|
| 58 |
class PatternVisitor extends OriginalPatternVisitor { |
| 59 |
ArrayPattern(node) { |
| 60 |
node.elements.forEach(this.visit, this); |
| 61 |
} |
| 62 |
|
| 63 |
ObjectPattern(node) { |
| 64 |
node.properties.forEach(this.visit, this); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
class Referencer extends OriginalReferencer { |
| 69 |
#client; |
| 70 |
|
| 71 |
constructor(options, scopeManager, client) { |
| 72 |
super(options, scopeManager); |
| 73 |
this.#client = client; |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
visitPattern(node, options, callback) { |
| 78 |
if (!node) { |
| 79 |
return; |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
this._checkIdentifierOrVisit(node.typeAnnotation); |
| 84 |
if (node.type === "AssignmentPattern") { |
| 85 |
this._checkIdentifierOrVisit(node.left.typeAnnotation); |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
if (typeof options === "function") { |
| 90 |
callback = options; |
| 91 |
options = { processRightHandNodes: false }; |
| 92 |
} |
| 93 |
|
| 94 |
const visitor = new PatternVisitor(this.options, node, callback); |
| 95 |
visitor.visit(node); |
| 96 |
|
| 97 |
|
| 98 |
if (options.processRightHandNodes) { |
| 99 |
visitor.rightHandNodes.forEach(this.visit, this); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
visitClass(node) { |
| 105 |
|
| 106 |
this._visitArray(node.decorators); |
| 107 |
|
| 108 |
|
| 109 |
const typeParamScope = this._nestTypeParamScope(node); |
| 110 |
|
| 111 |
|
| 112 |
this._visitTypeAnnotation(node.implements); |
| 113 |
this._visitTypeAnnotation( |
| 114 |
node.superTypeParameters && node.superTypeParameters.params, |
| 115 |
); |
| 116 |
|
| 117 |
|
| 118 |
super.visitClass(node); |
| 119 |
|
| 120 |
|
| 121 |
if (typeParamScope) { |
| 122 |
this.close(node); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
visitFunction(node) { |
| 128 |
const typeParamScope = this._nestTypeParamScope(node); |
| 129 |
|
| 130 |
|
| 131 |
this._checkIdentifierOrVisit(node.returnType); |
| 132 |
|
| 133 |
|
| 134 |
super.visitFunction(node); |
| 135 |
|
| 136 |
|
| 137 |
if (typeParamScope) { |
| 138 |
this.close(node); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
visitProperty(node) { |
| 144 |
if (node.value?.type === "TypeCastExpression") { |
| 145 |
this._visitTypeAnnotation(node.value); |
| 146 |
} |
| 147 |
this._visitArray(node.decorators); |
| 148 |
super.visitProperty(node); |
| 149 |
} |
| 150 |
|
| 151 |
InterfaceDeclaration(node) { |
| 152 |
this._createScopeVariable(node, node.id); |
| 153 |
|
| 154 |
const typeParamScope = this._nestTypeParamScope(node); |
| 155 |
|
| 156 |
|
| 157 |
this._visitArray(node.extends); |
| 158 |
this.visit(node.body); |
| 159 |
|
| 160 |
if (typeParamScope) { |
| 161 |
this.close(node); |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
TypeAlias(node) { |
| 166 |
this._createScopeVariable(node, node.id); |
| 167 |
|
| 168 |
const typeParamScope = this._nestTypeParamScope(node); |
| 169 |
|
| 170 |
this.visit(node.right); |
| 171 |
|
| 172 |
if (typeParamScope) { |
| 173 |
this.close(node); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
ClassProperty(node) { |
| 178 |
this._visitClassProperty(node); |
| 179 |
} |
| 180 |
|
| 181 |
ClassPrivateProperty(node) { |
| 182 |
this._visitClassProperty(node); |
| 183 |
} |
| 184 |
|
| 185 |
PropertyDefinition(node) { |
| 186 |
this._visitClassProperty(node); |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
ClassPrivateMethod(node) { |
| 191 |
super.MethodDefinition(node); |
| 192 |
} |
| 193 |
|
| 194 |
DeclareModule(node) { |
| 195 |
this._visitDeclareX(node); |
| 196 |
} |
| 197 |
|
| 198 |
DeclareFunction(node) { |
| 199 |
this._visitDeclareX(node); |
| 200 |
} |
| 201 |
|
| 202 |
DeclareVariable(node) { |
| 203 |
this._visitDeclareX(node); |
| 204 |
} |
| 205 |
|
| 206 |
DeclareClass(node) { |
| 207 |
this._visitDeclareX(node); |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
OptionalMemberExpression(node) { |
| 212 |
super.MemberExpression(node); |
| 213 |
} |
| 214 |
|
| 215 |
_visitClassProperty(node) { |
| 216 |
this._visitTypeAnnotation(node.typeAnnotation); |
| 217 |
this.visitProperty(node); |
| 218 |
} |
| 219 |
|
| 220 |
_visitDeclareX(node) { |
| 221 |
if (node.id) { |
| 222 |
this._createScopeVariable(node, node.id); |
| 223 |
} |
| 224 |
|
| 225 |
const typeParamScope = this._nestTypeParamScope(node); |
| 226 |
if (typeParamScope) { |
| 227 |
this.close(node); |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
_createScopeVariable(node, name) { |
| 232 |
this.currentScope().variableScope.__define( |
| 233 |
name, |
| 234 |
new Definition("Variable", name, node, null, null, null), |
| 235 |
); |
| 236 |
} |
| 237 |
|
| 238 |
_nestTypeParamScope(node) { |
| 239 |
if (!node.typeParameters) { |
| 240 |
return null; |
| 241 |
} |
| 242 |
|
| 243 |
const parentScope = this.scopeManager.__currentScope; |
| 244 |
const scope = new Scope( |
| 245 |
this.scopeManager, |
| 246 |
"type-parameters", |
| 247 |
parentScope, |
| 248 |
node, |
| 249 |
false, |
| 250 |
); |
| 251 |
|
| 252 |
this.scopeManager.__nestScope(scope); |
| 253 |
for (let j = 0; j < node.typeParameters.params.length; j++) { |
| 254 |
const name = node.typeParameters.params[j]; |
| 255 |
scope.__define(name, new Definition("TypeParameter", name, name)); |
| 256 |
if (name.typeAnnotation) { |
| 257 |
this._checkIdentifierOrVisit(name); |
| 258 |
} |
| 259 |
} |
| 260 |
scope.__define = function () { |
| 261 |
return parentScope.__define.apply(parentScope, arguments); |
| 262 |
}; |
| 263 |
|
| 264 |
return scope; |
| 265 |
} |
| 266 |
|
| 267 |
_visitTypeAnnotation(node) { |
| 268 |
if (!node) { |
| 269 |
return; |
| 270 |
} |
| 271 |
if (Array.isArray(node)) { |
| 272 |
node.forEach(this._visitTypeAnnotation, this); |
| 273 |
return; |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
const visitorValues = getVisitorValues(node.type, this.#client); |
| 278 |
if (!visitorValues) { |
| 279 |
return; |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
for (let i = 0; i < visitorValues.length; i++) { |
| 284 |
const visitorValue = visitorValues[i]; |
| 285 |
const propertyType = propertyTypes[visitorValue]; |
| 286 |
const nodeProperty = node[visitorValue]; |
| 287 |
|
| 288 |
if (propertyType == null || nodeProperty == null) { |
| 289 |
continue; |
| 290 |
} |
| 291 |
if (propertyType.type === "loop") { |
| 292 |
for (let j = 0; j < nodeProperty.length; j++) { |
| 293 |
if (Array.isArray(propertyType.values)) { |
| 294 |
for (let k = 0; k < propertyType.values.length; k++) { |
| 295 |
const loopPropertyNode = nodeProperty[j][propertyType.values[k]]; |
| 296 |
if (loopPropertyNode) { |
| 297 |
this._checkIdentifierOrVisit(loopPropertyNode); |
| 298 |
} |
| 299 |
} |
| 300 |
} else { |
| 301 |
this._checkIdentifierOrVisit(nodeProperty[j]); |
| 302 |
} |
| 303 |
} |
| 304 |
} else if (propertyType.type === "single") { |
| 305 |
this._checkIdentifierOrVisit(nodeProperty); |
| 306 |
} else if (propertyType.type === "typeAnnotation") { |
| 307 |
this._visitTypeAnnotation(node.typeAnnotation); |
| 308 |
} else if (propertyType.type === "typeParameters") { |
| 309 |
for (let l = 0; l < node.typeParameters.params.length; l++) { |
| 310 |
this._checkIdentifierOrVisit(node.typeParameters.params[l]); |
| 311 |
} |
| 312 |
} else if (propertyType.type === "id") { |
| 313 |
if (node.id.type === "Identifier") { |
| 314 |
this._checkIdentifierOrVisit(node.id); |
| 315 |
} else { |
| 316 |
this._visitTypeAnnotation(node.id); |
| 317 |
} |
| 318 |
} |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
_checkIdentifierOrVisit(node) { |
| 323 |
if (node?.typeAnnotation) { |
| 324 |
this._visitTypeAnnotation(node.typeAnnotation); |
| 325 |
} else if (node?.type === "Identifier") { |
| 326 |
this.visit(node); |
| 327 |
} else { |
| 328 |
this._visitTypeAnnotation(node); |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
_visitArray(nodeList) { |
| 333 |
if (nodeList) { |
| 334 |
for (const node of nodeList) { |
| 335 |
this.visit(node); |
| 336 |
} |
| 337 |
} |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
module.exports = function analyzeScope(ast, parserOptions, client) { |
| 342 |
const options = { |
| 343 |
ignoreEval: true, |
| 344 |
optimistic: false, |
| 345 |
directive: false, |
| 346 |
nodejsScope: |
| 347 |
ast.sourceType === "script" && |
| 348 |
parserOptions.ecmaFeatures?.globalReturn === true, |
| 349 |
impliedStrict: false, |
| 350 |
sourceType: ast.sourceType, |
| 351 |
ecmaVersion: parserOptions.ecmaVersion, |
| 352 |
fallback, |
| 353 |
}; |
| 354 |
|
| 355 |
options.childVisitorKeys = client.getVisitorKeys(); |
| 356 |
|
| 357 |
const scopeManager = new ScopeManager(options); |
| 358 |
const referencer = new Referencer(options, scopeManager, client); |
| 359 |
|
| 360 |
referencer.visit(ast); |
| 361 |
|
| 362 |
return scopeManager; |
| 363 |
}; |