analyze-scope.cjs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. function _classPrivateFieldInitSpec(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); }
  2. function _checkPrivateRedeclaration(obj, privateCollection) { if (privateCollection.has(obj)) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } }
  3. function _classPrivateFieldGet(receiver, privateMap) { var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); return _classApplyDescriptorGet(receiver, descriptor); }
  4. function _classApplyDescriptorGet(receiver, descriptor) { if (descriptor.get) { return descriptor.get.call(receiver); } return descriptor.value; }
  5. function _classPrivateFieldSet(receiver, privateMap, value) { var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); _classApplyDescriptorSet(receiver, descriptor, value); return value; }
  6. function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!privateMap.has(receiver)) { throw new TypeError("attempted to " + action + " private field on non-instance"); } return privateMap.get(receiver); }
  7. function _classApplyDescriptorSet(receiver, descriptor, value) { if (descriptor.set) { descriptor.set.call(receiver, value); } else { if (!descriptor.writable) { throw new TypeError("attempted to set read only private field"); } descriptor.value = value; } }
  8. const escope = require("eslint-scope");
  9. const {
  10. Definition
  11. } = require("eslint-scope/lib/definition");
  12. const OriginalPatternVisitor = require("eslint-scope/lib/pattern-visitor");
  13. const OriginalReferencer = require("eslint-scope/lib/referencer");
  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(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, {
  90. writable: true,
  91. value: void 0
  92. });
  93. _classPrivateFieldSet(this, _client, client);
  94. }
  95. visitPattern(node, options, callback) {
  96. if (!node) {
  97. return;
  98. }
  99. this._checkIdentifierOrVisit(node.typeAnnotation);
  100. if (node.type === "AssignmentPattern") {
  101. this._checkIdentifierOrVisit(node.left.typeAnnotation);
  102. }
  103. if (typeof options === "function") {
  104. callback = options;
  105. options = {
  106. processRightHandNodes: false
  107. };
  108. }
  109. const visitor = new PatternVisitor(this.options, node, callback);
  110. visitor.visit(node);
  111. if (options.processRightHandNodes) {
  112. visitor.rightHandNodes.forEach(this.visit, this);
  113. }
  114. }
  115. visitClass(node) {
  116. this._visitArray(node.decorators);
  117. const typeParamScope = this._nestTypeParamScope(node);
  118. this._visitTypeAnnotation(node.implements);
  119. this._visitTypeAnnotation(node.superTypeParameters && node.superTypeParameters.params);
  120. super.visitClass(node);
  121. if (typeParamScope) {
  122. this.close(node);
  123. }
  124. }
  125. visitFunction(node) {
  126. const typeParamScope = this._nestTypeParamScope(node);
  127. this._checkIdentifierOrVisit(node.returnType);
  128. super.visitFunction(node);
  129. if (typeParamScope) {
  130. this.close(node);
  131. }
  132. }
  133. visitProperty(node) {
  134. var _node$value;
  135. if (((_node$value = node.value) == null ? void 0 : _node$value.type) === "TypeCastExpression") {
  136. this._visitTypeAnnotation(node.value);
  137. }
  138. this._visitArray(node.decorators);
  139. super.visitProperty(node);
  140. }
  141. InterfaceDeclaration(node) {
  142. this._createScopeVariable(node, node.id);
  143. const typeParamScope = this._nestTypeParamScope(node);
  144. this._visitArray(node.extends);
  145. this.visit(node.body);
  146. if (typeParamScope) {
  147. this.close(node);
  148. }
  149. }
  150. TypeAlias(node) {
  151. this._createScopeVariable(node, node.id);
  152. const typeParamScope = this._nestTypeParamScope(node);
  153. this.visit(node.right);
  154. if (typeParamScope) {
  155. this.close(node);
  156. }
  157. }
  158. ClassProperty(node) {
  159. this._visitClassProperty(node);
  160. }
  161. ClassPrivateProperty(node) {
  162. this._visitClassProperty(node);
  163. }
  164. PropertyDefinition(node) {
  165. this._visitClassProperty(node);
  166. }
  167. ClassPrivateMethod(node) {
  168. super.MethodDefinition(node);
  169. }
  170. DeclareModule(node) {
  171. this._visitDeclareX(node);
  172. }
  173. DeclareFunction(node) {
  174. this._visitDeclareX(node);
  175. }
  176. DeclareVariable(node) {
  177. this._visitDeclareX(node);
  178. }
  179. DeclareClass(node) {
  180. this._visitDeclareX(node);
  181. }
  182. OptionalMemberExpression(node) {
  183. super.MemberExpression(node);
  184. }
  185. _visitClassProperty(node) {
  186. this._visitTypeAnnotation(node.typeAnnotation);
  187. this.visitProperty(node);
  188. }
  189. _visitDeclareX(node) {
  190. if (node.id) {
  191. this._createScopeVariable(node, node.id);
  192. }
  193. const typeParamScope = this._nestTypeParamScope(node);
  194. if (typeParamScope) {
  195. this.close(node);
  196. }
  197. }
  198. _createScopeVariable(node, name) {
  199. this.currentScope().variableScope.__define(name, new Definition("Variable", name, node, null, null, null));
  200. }
  201. _nestTypeParamScope(node) {
  202. if (!node.typeParameters) {
  203. return null;
  204. }
  205. const parentScope = this.scopeManager.__currentScope;
  206. const scope = new escope.Scope(this.scopeManager, "type-parameters", parentScope, node, false);
  207. this.scopeManager.__nestScope(scope);
  208. for (let j = 0; j < node.typeParameters.params.length; j++) {
  209. const name = node.typeParameters.params[j];
  210. scope.__define(name, new Definition("TypeParameter", name, name));
  211. if (name.typeAnnotation) {
  212. this._checkIdentifierOrVisit(name);
  213. }
  214. }
  215. scope.__define = function () {
  216. return parentScope.__define.apply(parentScope, arguments);
  217. };
  218. return scope;
  219. }
  220. _visitTypeAnnotation(node) {
  221. if (!node) {
  222. return;
  223. }
  224. if (Array.isArray(node)) {
  225. node.forEach(this._visitTypeAnnotation, this);
  226. return;
  227. }
  228. const visitorValues = getVisitorValues(node.type, _classPrivateFieldGet(this, _client));
  229. if (!visitorValues) {
  230. return;
  231. }
  232. for (let i = 0; i < visitorValues.length; i++) {
  233. const visitorValue = visitorValues[i];
  234. const propertyType = propertyTypes[visitorValue];
  235. const nodeProperty = node[visitorValue];
  236. if (propertyType == null || nodeProperty == null) {
  237. continue;
  238. }
  239. if (propertyType.type === "loop") {
  240. for (let j = 0; j < nodeProperty.length; j++) {
  241. if (Array.isArray(propertyType.values)) {
  242. for (let k = 0; k < propertyType.values.length; k++) {
  243. const loopPropertyNode = nodeProperty[j][propertyType.values[k]];
  244. if (loopPropertyNode) {
  245. this._checkIdentifierOrVisit(loopPropertyNode);
  246. }
  247. }
  248. } else {
  249. this._checkIdentifierOrVisit(nodeProperty[j]);
  250. }
  251. }
  252. } else if (propertyType.type === "single") {
  253. this._checkIdentifierOrVisit(nodeProperty);
  254. } else if (propertyType.type === "typeAnnotation") {
  255. this._visitTypeAnnotation(node.typeAnnotation);
  256. } else if (propertyType.type === "typeParameters") {
  257. for (let l = 0; l < node.typeParameters.params.length; l++) {
  258. this._checkIdentifierOrVisit(node.typeParameters.params[l]);
  259. }
  260. } else if (propertyType.type === "id") {
  261. if (node.id.type === "Identifier") {
  262. this._checkIdentifierOrVisit(node.id);
  263. } else {
  264. this._visitTypeAnnotation(node.id);
  265. }
  266. }
  267. }
  268. }
  269. _checkIdentifierOrVisit(node) {
  270. if (node != null && node.typeAnnotation) {
  271. this._visitTypeAnnotation(node.typeAnnotation);
  272. } else if ((node == null ? void 0 : node.type) === "Identifier") {
  273. this.visit(node);
  274. } else {
  275. this._visitTypeAnnotation(node);
  276. }
  277. }
  278. _visitArray(nodeList) {
  279. if (nodeList) {
  280. for (const node of nodeList) {
  281. this.visit(node);
  282. }
  283. }
  284. }
  285. }
  286. module.exports = function analyzeScope(ast, parserOptions, client) {
  287. var _parserOptions$ecmaFe;
  288. const options = {
  289. ignoreEval: true,
  290. optimistic: false,
  291. directive: false,
  292. nodejsScope: ast.sourceType === "script" && ((_parserOptions$ecmaFe = parserOptions.ecmaFeatures) == null ? void 0 : _parserOptions$ecmaFe.globalReturn) === true,
  293. impliedStrict: false,
  294. sourceType: ast.sourceType,
  295. ecmaVersion: parserOptions.ecmaVersion,
  296. fallback
  297. };
  298. options.childVisitorKeys = client.getVisitorKeys();
  299. const scopeManager = new escope.ScopeManager(options);
  300. const referencer = new Referencer(options, scopeManager, client);
  301. referencer.visit(ast);
  302. return scopeManager;
  303. };