estree.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _parseError = require("../parse-error");
  7. const {
  8. defineProperty
  9. } = Object;
  10. const toUnenumerable = (object, key) => defineProperty(object, key, {
  11. enumerable: false,
  12. value: object[key]
  13. });
  14. function toESTreeLocation(node) {
  15. node.loc.start && toUnenumerable(node.loc.start, "index");
  16. node.loc.end && toUnenumerable(node.loc.end, "index");
  17. return node;
  18. }
  19. var _default = superClass => class ESTreeParserMixin extends superClass {
  20. parse() {
  21. const file = toESTreeLocation(super.parse());
  22. if (this.options.tokens) {
  23. file.tokens = file.tokens.map(toESTreeLocation);
  24. }
  25. return file;
  26. }
  27. parseRegExpLiteral({
  28. pattern,
  29. flags
  30. }) {
  31. let regex = null;
  32. try {
  33. regex = new RegExp(pattern, flags);
  34. } catch (e) {}
  35. const node = this.estreeParseLiteral(regex);
  36. node.regex = {
  37. pattern,
  38. flags
  39. };
  40. return node;
  41. }
  42. parseBigIntLiteral(value) {
  43. let bigInt;
  44. try {
  45. bigInt = BigInt(value);
  46. } catch (_unused) {
  47. bigInt = null;
  48. }
  49. const node = this.estreeParseLiteral(bigInt);
  50. node.bigint = String(node.value || value);
  51. return node;
  52. }
  53. parseDecimalLiteral(value) {
  54. const decimal = null;
  55. const node = this.estreeParseLiteral(decimal);
  56. node.decimal = String(node.value || value);
  57. return node;
  58. }
  59. estreeParseLiteral(value) {
  60. return this.parseLiteral(value, "Literal");
  61. }
  62. parseStringLiteral(value) {
  63. return this.estreeParseLiteral(value);
  64. }
  65. parseNumericLiteral(value) {
  66. return this.estreeParseLiteral(value);
  67. }
  68. parseNullLiteral() {
  69. return this.estreeParseLiteral(null);
  70. }
  71. parseBooleanLiteral(value) {
  72. return this.estreeParseLiteral(value);
  73. }
  74. directiveToStmt(directive) {
  75. const expression = directive.value;
  76. delete directive.value;
  77. expression.type = "Literal";
  78. expression.raw = expression.extra.raw;
  79. expression.value = expression.extra.expressionValue;
  80. const stmt = directive;
  81. stmt.type = "ExpressionStatement";
  82. stmt.expression = expression;
  83. stmt.directive = expression.extra.rawValue;
  84. delete expression.extra;
  85. return stmt;
  86. }
  87. initFunction(node, isAsync) {
  88. super.initFunction(node, isAsync);
  89. node.expression = false;
  90. }
  91. checkDeclaration(node) {
  92. if (node != null && this.isObjectProperty(node)) {
  93. this.checkDeclaration(node.value);
  94. } else {
  95. super.checkDeclaration(node);
  96. }
  97. }
  98. getObjectOrClassMethodParams(method) {
  99. return method.value.params;
  100. }
  101. isValidDirective(stmt) {
  102. var _stmt$expression$extr;
  103. return stmt.type === "ExpressionStatement" && stmt.expression.type === "Literal" && typeof stmt.expression.value === "string" && !((_stmt$expression$extr = stmt.expression.extra) != null && _stmt$expression$extr.parenthesized);
  104. }
  105. parseBlockBody(node, allowDirectives, topLevel, end, afterBlockParse) {
  106. super.parseBlockBody(node, allowDirectives, topLevel, end, afterBlockParse);
  107. const directiveStatements = node.directives.map(d => this.directiveToStmt(d));
  108. node.body = directiveStatements.concat(node.body);
  109. delete node.directives;
  110. }
  111. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  112. this.parseMethod(method, isGenerator, isAsync, isConstructor, allowsDirectSuper, "ClassMethod", true);
  113. if (method.typeParameters) {
  114. method.value.typeParameters = method.typeParameters;
  115. delete method.typeParameters;
  116. }
  117. classBody.body.push(method);
  118. }
  119. parsePrivateName() {
  120. const node = super.parsePrivateName();
  121. {
  122. if (!this.getPluginOption("estree", "classFeatures")) {
  123. return node;
  124. }
  125. }
  126. return this.convertPrivateNameToPrivateIdentifier(node);
  127. }
  128. convertPrivateNameToPrivateIdentifier(node) {
  129. const name = super.getPrivateNameSV(node);
  130. node = node;
  131. delete node.id;
  132. node.name = name;
  133. node.type = "PrivateIdentifier";
  134. return node;
  135. }
  136. isPrivateName(node) {
  137. {
  138. if (!this.getPluginOption("estree", "classFeatures")) {
  139. return super.isPrivateName(node);
  140. }
  141. }
  142. return node.type === "PrivateIdentifier";
  143. }
  144. getPrivateNameSV(node) {
  145. {
  146. if (!this.getPluginOption("estree", "classFeatures")) {
  147. return super.getPrivateNameSV(node);
  148. }
  149. }
  150. return node.name;
  151. }
  152. parseLiteral(value, type) {
  153. const node = super.parseLiteral(value, type);
  154. node.raw = node.extra.raw;
  155. delete node.extra;
  156. return node;
  157. }
  158. parseFunctionBody(node, allowExpression, isMethod = false) {
  159. super.parseFunctionBody(node, allowExpression, isMethod);
  160. node.expression = node.body.type !== "BlockStatement";
  161. }
  162. parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope = false) {
  163. let funcNode = this.startNode();
  164. funcNode.kind = node.kind;
  165. funcNode = super.parseMethod(funcNode, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope);
  166. funcNode.type = "FunctionExpression";
  167. delete funcNode.kind;
  168. node.value = funcNode;
  169. if (type === "ClassPrivateMethod") {
  170. node.computed = false;
  171. }
  172. return this.finishNode(node, "MethodDefinition");
  173. }
  174. parseClassProperty(...args) {
  175. const propertyNode = super.parseClassProperty(...args);
  176. {
  177. if (!this.getPluginOption("estree", "classFeatures")) {
  178. return propertyNode;
  179. }
  180. }
  181. propertyNode.type = "PropertyDefinition";
  182. return propertyNode;
  183. }
  184. parseClassPrivateProperty(...args) {
  185. const propertyNode = super.parseClassPrivateProperty(...args);
  186. {
  187. if (!this.getPluginOption("estree", "classFeatures")) {
  188. return propertyNode;
  189. }
  190. }
  191. propertyNode.type = "PropertyDefinition";
  192. propertyNode.computed = false;
  193. return propertyNode;
  194. }
  195. parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) {
  196. const node = super.parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor);
  197. if (node) {
  198. node.type = "Property";
  199. if (node.kind === "method") {
  200. node.kind = "init";
  201. }
  202. node.shorthand = false;
  203. }
  204. return node;
  205. }
  206. parseObjectProperty(prop, startLoc, isPattern, refExpressionErrors) {
  207. const node = super.parseObjectProperty(prop, startLoc, isPattern, refExpressionErrors);
  208. if (node) {
  209. node.kind = "init";
  210. node.type = "Property";
  211. }
  212. return node;
  213. }
  214. isValidLVal(type, isUnparenthesizedInAssign, binding) {
  215. return type === "Property" ? "value" : super.isValidLVal(type, isUnparenthesizedInAssign, binding);
  216. }
  217. isAssignable(node, isBinding) {
  218. if (node != null && this.isObjectProperty(node)) {
  219. return this.isAssignable(node.value, isBinding);
  220. }
  221. return super.isAssignable(node, isBinding);
  222. }
  223. toAssignable(node, isLHS = false) {
  224. if (node != null && this.isObjectProperty(node)) {
  225. const {
  226. key,
  227. value
  228. } = node;
  229. if (this.isPrivateName(key)) {
  230. this.classScope.usePrivateName(this.getPrivateNameSV(key), key.loc.start);
  231. }
  232. this.toAssignable(value, isLHS);
  233. } else {
  234. super.toAssignable(node, isLHS);
  235. }
  236. }
  237. toAssignableObjectExpressionProp(prop, isLast, isLHS) {
  238. if (prop.kind === "get" || prop.kind === "set") {
  239. this.raise(_parseError.Errors.PatternHasAccessor, {
  240. at: prop.key
  241. });
  242. } else if (prop.method) {
  243. this.raise(_parseError.Errors.PatternHasMethod, {
  244. at: prop.key
  245. });
  246. } else {
  247. super.toAssignableObjectExpressionProp(prop, isLast, isLHS);
  248. }
  249. }
  250. finishCallExpression(unfinished, optional) {
  251. const node = super.finishCallExpression(unfinished, optional);
  252. if (node.callee.type === "Import") {
  253. node.type = "ImportExpression";
  254. node.source = node.arguments[0];
  255. if (this.hasPlugin("importAttributes") || this.hasPlugin("importAssertions")) {
  256. var _node$arguments$;
  257. node.attributes = (_node$arguments$ = node.arguments[1]) != null ? _node$arguments$ : null;
  258. }
  259. delete node.arguments;
  260. delete node.callee;
  261. }
  262. return node;
  263. }
  264. toReferencedArguments(node) {
  265. if (node.type === "ImportExpression") {
  266. return;
  267. }
  268. super.toReferencedArguments(node);
  269. }
  270. parseExport(unfinished, decorators) {
  271. const exportStartLoc = this.state.lastTokStartLoc;
  272. const node = super.parseExport(unfinished, decorators);
  273. switch (node.type) {
  274. case "ExportAllDeclaration":
  275. node.exported = null;
  276. break;
  277. case "ExportNamedDeclaration":
  278. if (node.specifiers.length === 1 && node.specifiers[0].type === "ExportNamespaceSpecifier") {
  279. node.type = "ExportAllDeclaration";
  280. node.exported = node.specifiers[0].exported;
  281. delete node.specifiers;
  282. }
  283. case "ExportDefaultDeclaration":
  284. {
  285. var _declaration$decorato;
  286. const {
  287. declaration
  288. } = node;
  289. if ((declaration == null ? void 0 : declaration.type) === "ClassDeclaration" && ((_declaration$decorato = declaration.decorators) == null ? void 0 : _declaration$decorato.length) > 0 && declaration.start === node.start) {
  290. this.resetStartLocation(node, exportStartLoc);
  291. }
  292. }
  293. break;
  294. }
  295. return node;
  296. }
  297. parseSubscript(base, startLoc, noCalls, state) {
  298. const node = super.parseSubscript(base, startLoc, noCalls, state);
  299. if (state.optionalChainMember) {
  300. if (node.type === "OptionalMemberExpression" || node.type === "OptionalCallExpression") {
  301. node.type = node.type.substring(8);
  302. }
  303. if (state.stop) {
  304. const chain = this.startNodeAtNode(node);
  305. chain.expression = node;
  306. return this.finishNode(chain, "ChainExpression");
  307. }
  308. } else if (node.type === "MemberExpression" || node.type === "CallExpression") {
  309. node.optional = false;
  310. }
  311. return node;
  312. }
  313. hasPropertyAsPrivateName(node) {
  314. if (node.type === "ChainExpression") {
  315. node = node.expression;
  316. }
  317. return super.hasPropertyAsPrivateName(node);
  318. }
  319. isObjectProperty(node) {
  320. return node.type === "Property" && node.kind === "init" && !node.method;
  321. }
  322. isObjectMethod(node) {
  323. return node.method || node.kind === "get" || node.kind === "set";
  324. }
  325. finishNodeAt(node, type, endLoc) {
  326. return toESTreeLocation(super.finishNodeAt(node, type, endLoc));
  327. }
  328. resetStartLocation(node, startLoc) {
  329. super.resetStartLocation(node, startLoc);
  330. toESTreeLocation(node);
  331. }
  332. resetEndLocation(node, endLoc = this.state.lastTokEndLoc) {
  333. super.resetEndLocation(node, endLoc);
  334. toESTreeLocation(node);
  335. }
  336. };
  337. exports.default = _default;
  338. //# sourceMappingURL=estree.js.map