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