estree.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _types = require("../tokenizer/types");
  7. var N = _interopRequireWildcard(require("../types"));
  8. var _scopeflags = require("../util/scopeflags");
  9. var _error = require("../parser/error");
  10. function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
  11. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
  12. function isSimpleProperty(node) {
  13. return node != null && node.type === "Property" && node.kind === "init" && node.method === false;
  14. }
  15. var _default = superClass => class extends superClass {
  16. estreeParseRegExpLiteral({
  17. pattern,
  18. flags
  19. }) {
  20. let regex = null;
  21. try {
  22. regex = new RegExp(pattern, flags);
  23. } catch (e) {}
  24. const node = this.estreeParseLiteral(regex);
  25. node.regex = {
  26. pattern,
  27. flags
  28. };
  29. return node;
  30. }
  31. estreeParseBigIntLiteral(value) {
  32. const bigInt = typeof BigInt !== "undefined" ? BigInt(value) : null;
  33. const node = this.estreeParseLiteral(bigInt);
  34. node.bigint = String(node.value || value);
  35. return node;
  36. }
  37. estreeParseDecimalLiteral(value) {
  38. const decimal = null;
  39. const node = this.estreeParseLiteral(decimal);
  40. node.decimal = String(node.value || value);
  41. return node;
  42. }
  43. estreeParseLiteral(value) {
  44. return this.parseLiteral(value, "Literal");
  45. }
  46. directiveToStmt(directive) {
  47. const directiveLiteral = directive.value;
  48. const stmt = this.startNodeAt(directive.start, directive.loc.start);
  49. const expression = this.startNodeAt(directiveLiteral.start, directiveLiteral.loc.start);
  50. expression.value = directiveLiteral.value;
  51. expression.raw = directiveLiteral.extra.raw;
  52. stmt.expression = this.finishNodeAt(expression, "Literal", directiveLiteral.end, directiveLiteral.loc.end);
  53. stmt.directive = directiveLiteral.extra.raw.slice(1, -1);
  54. return this.finishNodeAt(stmt, "ExpressionStatement", directive.end, directive.loc.end);
  55. }
  56. initFunction(node, isAsync) {
  57. super.initFunction(node, isAsync);
  58. node.expression = false;
  59. }
  60. checkDeclaration(node) {
  61. if (isSimpleProperty(node)) {
  62. this.checkDeclaration(node.value);
  63. } else {
  64. super.checkDeclaration(node);
  65. }
  66. }
  67. getObjectOrClassMethodParams(method) {
  68. return method.value.params;
  69. }
  70. checkLVal(expr, bindingType = _scopeflags.BIND_NONE, checkClashes, contextDescription, disallowLetBinding) {
  71. switch (expr.type) {
  72. case "ObjectPattern":
  73. expr.properties.forEach(prop => {
  74. this.checkLVal(prop.type === "Property" ? prop.value : prop, bindingType, checkClashes, "object destructuring pattern", disallowLetBinding);
  75. });
  76. break;
  77. default:
  78. super.checkLVal(expr, bindingType, checkClashes, contextDescription, disallowLetBinding);
  79. }
  80. }
  81. checkProto(prop, isRecord, protoRef, refExpressionErrors) {
  82. if (prop.method) {
  83. return;
  84. }
  85. super.checkProto(prop, isRecord, protoRef, refExpressionErrors);
  86. }
  87. isValidDirective(stmt) {
  88. return stmt.type === "ExpressionStatement" && stmt.expression.type === "Literal" && typeof stmt.expression.value === "string" && !stmt.expression.extra?.parenthesized;
  89. }
  90. stmtToDirective(stmt) {
  91. const directive = super.stmtToDirective(stmt);
  92. const value = stmt.expression.value;
  93. directive.value.value = value;
  94. return directive;
  95. }
  96. parseBlockBody(node, allowDirectives, topLevel, end) {
  97. super.parseBlockBody(node, allowDirectives, topLevel, end);
  98. const directiveStatements = node.directives.map(d => this.directiveToStmt(d));
  99. node.body = directiveStatements.concat(node.body);
  100. delete node.directives;
  101. }
  102. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  103. this.parseMethod(method, isGenerator, isAsync, isConstructor, allowsDirectSuper, "ClassMethod", true);
  104. if (method.typeParameters) {
  105. method.value.typeParameters = method.typeParameters;
  106. delete method.typeParameters;
  107. }
  108. classBody.body.push(method);
  109. }
  110. parseExprAtom(refExpressionErrors) {
  111. switch (this.state.type) {
  112. case _types.types.num:
  113. case _types.types.string:
  114. return this.estreeParseLiteral(this.state.value);
  115. case _types.types.regexp:
  116. return this.estreeParseRegExpLiteral(this.state.value);
  117. case _types.types.bigint:
  118. return this.estreeParseBigIntLiteral(this.state.value);
  119. case _types.types.decimal:
  120. return this.estreeParseDecimalLiteral(this.state.value);
  121. case _types.types._null:
  122. return this.estreeParseLiteral(null);
  123. case _types.types._true:
  124. return this.estreeParseLiteral(true);
  125. case _types.types._false:
  126. return this.estreeParseLiteral(false);
  127. default:
  128. return super.parseExprAtom(refExpressionErrors);
  129. }
  130. }
  131. parseLiteral(value, type, startPos, startLoc) {
  132. const node = super.parseLiteral(value, type, startPos, startLoc);
  133. node.raw = node.extra.raw;
  134. delete node.extra;
  135. return node;
  136. }
  137. parseFunctionBody(node, allowExpression, isMethod = false) {
  138. super.parseFunctionBody(node, allowExpression, isMethod);
  139. node.expression = node.body.type !== "BlockStatement";
  140. }
  141. parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope = false) {
  142. let funcNode = this.startNode();
  143. funcNode.kind = node.kind;
  144. funcNode = super.parseMethod(funcNode, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope);
  145. funcNode.type = "FunctionExpression";
  146. delete funcNode.kind;
  147. node.value = funcNode;
  148. type = type === "ClassMethod" ? "MethodDefinition" : type;
  149. return this.finishNode(node, type);
  150. }
  151. parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) {
  152. const node = super.parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor);
  153. if (node) {
  154. node.type = "Property";
  155. if (node.kind === "method") node.kind = "init";
  156. node.shorthand = false;
  157. }
  158. return node;
  159. }
  160. parseObjectProperty(prop, startPos, startLoc, isPattern, refExpressionErrors) {
  161. const node = super.parseObjectProperty(prop, startPos, startLoc, isPattern, refExpressionErrors);
  162. if (node) {
  163. node.kind = "init";
  164. node.type = "Property";
  165. }
  166. return node;
  167. }
  168. toAssignable(node) {
  169. if (isSimpleProperty(node)) {
  170. this.toAssignable(node.value);
  171. return node;
  172. }
  173. return super.toAssignable(node);
  174. }
  175. toAssignableObjectExpressionProp(prop, isLast) {
  176. if (prop.kind === "get" || prop.kind === "set") {
  177. throw this.raise(prop.key.start, _error.Errors.PatternHasAccessor);
  178. } else if (prop.method) {
  179. throw this.raise(prop.key.start, _error.Errors.PatternHasMethod);
  180. } else {
  181. super.toAssignableObjectExpressionProp(prop, isLast);
  182. }
  183. }
  184. finishCallExpression(node, optional) {
  185. super.finishCallExpression(node, optional);
  186. if (node.callee.type === "Import") {
  187. node.type = "ImportExpression";
  188. node.source = node.arguments[0];
  189. delete node.arguments;
  190. delete node.callee;
  191. }
  192. return node;
  193. }
  194. toReferencedArguments(node) {
  195. if (node.type === "ImportExpression") {
  196. return;
  197. }
  198. super.toReferencedArguments(node);
  199. }
  200. parseExport(node) {
  201. super.parseExport(node);
  202. switch (node.type) {
  203. case "ExportAllDeclaration":
  204. node.exported = null;
  205. break;
  206. case "ExportNamedDeclaration":
  207. if (node.specifiers.length === 1 && node.specifiers[0].type === "ExportNamespaceSpecifier") {
  208. node.type = "ExportAllDeclaration";
  209. node.exported = node.specifiers[0].exported;
  210. delete node.specifiers;
  211. }
  212. break;
  213. }
  214. return node;
  215. }
  216. parseSubscript(base, startPos, startLoc, noCalls, state) {
  217. const node = super.parseSubscript(base, startPos, startLoc, noCalls, state);
  218. if (state.optionalChainMember) {
  219. if (node.type === "OptionalMemberExpression" || node.type === "OptionalCallExpression") {
  220. node.type = node.type.substring(8);
  221. }
  222. if (state.stop) {
  223. const chain = this.startNodeAtNode(node);
  224. chain.expression = node;
  225. return this.finishNode(chain, "ChainExpression");
  226. }
  227. } else if (node.type === "MemberExpression" || node.type === "CallExpression") {
  228. node.optional = false;
  229. }
  230. return node;
  231. }
  232. };
  233. exports.default = _default;