analyze-scope.cjs 10 KB

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