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