analyze-scope.cjs 9.6 KB

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