estree.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
  10. 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; }
  11. function isSimpleProperty(node) {
  12. return node != null && node.type === "Property" && node.kind === "init" && node.method === false;
  13. }
  14. var _default = superClass => class extends superClass {
  15. estreeParseRegExpLiteral({
  16. pattern,
  17. flags
  18. }) {
  19. let regex = null;
  20. try {
  21. regex = new RegExp(pattern, flags);
  22. } catch (e) {}
  23. const node = this.estreeParseLiteral(regex);
  24. node.regex = {
  25. pattern,
  26. flags
  27. };
  28. return node;
  29. }
  30. estreeParseBigIntLiteral(value) {
  31. const bigInt = typeof BigInt !== "undefined" ? BigInt(value) : null;
  32. const node = this.estreeParseLiteral(bigInt);
  33. node.bigint = String(node.value || value);
  34. return node;
  35. }
  36. estreeParseLiteral(value) {
  37. return this.parseLiteral(value, "Literal");
  38. }
  39. directiveToStmt(directive) {
  40. const directiveLiteral = directive.value;
  41. const stmt = this.startNodeAt(directive.start, directive.loc.start);
  42. const expression = this.startNodeAt(directiveLiteral.start, directiveLiteral.loc.start);
  43. expression.value = directiveLiteral.value;
  44. expression.raw = directiveLiteral.extra.raw;
  45. stmt.expression = this.finishNodeAt(expression, "Literal", directiveLiteral.end, directiveLiteral.loc.end);
  46. stmt.directive = directiveLiteral.extra.raw.slice(1, -1);
  47. return this.finishNodeAt(stmt, "ExpressionStatement", directive.end, directive.loc.end);
  48. }
  49. initFunction(node, isAsync) {
  50. super.initFunction(node, isAsync);
  51. node.expression = false;
  52. }
  53. checkDeclaration(node) {
  54. if (isSimpleProperty(node)) {
  55. this.checkDeclaration(node.value);
  56. } else {
  57. super.checkDeclaration(node);
  58. }
  59. }
  60. checkGetterSetterParams(method) {
  61. const prop = method;
  62. const paramCount = prop.kind === "get" ? 0 : 1;
  63. const start = prop.start;
  64. if (prop.value.params.length !== paramCount) {
  65. if (prop.kind === "get") {
  66. this.raise(start, "getter must not have any formal parameters");
  67. } else {
  68. this.raise(start, "setter must have exactly one formal parameter");
  69. }
  70. } else if (prop.kind === "set" && prop.value.params[0].type === "RestElement") {
  71. this.raise(start, "setter function argument must not be a rest parameter");
  72. }
  73. }
  74. checkLVal(expr, bindingType = _scopeflags.BIND_NONE, checkClashes, contextDescription, disallowLetBinding) {
  75. switch (expr.type) {
  76. case "ObjectPattern":
  77. expr.properties.forEach(prop => {
  78. this.checkLVal(prop.type === "Property" ? prop.value : prop, bindingType, checkClashes, "object destructuring pattern", disallowLetBinding);
  79. });
  80. break;
  81. default:
  82. super.checkLVal(expr, bindingType, checkClashes, contextDescription, disallowLetBinding);
  83. }
  84. }
  85. checkDuplicatedProto(prop, protoRef, refExpressionErrors) {
  86. if (prop.type === "SpreadElement" || prop.computed || prop.method || prop.shorthand) {
  87. return;
  88. }
  89. const key = prop.key;
  90. const name = key.type === "Identifier" ? key.name : String(key.value);
  91. if (name === "__proto__" && prop.kind === "init") {
  92. if (protoRef.used) {
  93. if (refExpressionErrors && refExpressionErrors.doubleProto === -1) {
  94. refExpressionErrors.doubleProto = key.start;
  95. } else {
  96. this.raise(key.start, "Redefinition of __proto__ property");
  97. }
  98. }
  99. protoRef.used = true;
  100. }
  101. }
  102. isValidDirective(stmt) {
  103. return stmt.type === "ExpressionStatement" && stmt.expression.type === "Literal" && typeof stmt.expression.value === "string" && (!stmt.expression.extra || !stmt.expression.extra.parenthesized);
  104. }
  105. stmtToDirective(stmt) {
  106. const directive = super.stmtToDirective(stmt);
  107. const value = stmt.expression.value;
  108. directive.value.value = value;
  109. return directive;
  110. }
  111. parseBlockBody(node, allowDirectives, topLevel, end) {
  112. super.parseBlockBody(node, allowDirectives, topLevel, end);
  113. const directiveStatements = node.directives.map(d => this.directiveToStmt(d));
  114. node.body = directiveStatements.concat(node.body);
  115. delete node.directives;
  116. }
  117. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  118. this.parseMethod(method, isGenerator, isAsync, isConstructor, allowsDirectSuper, "ClassMethod", true);
  119. if (method.typeParameters) {
  120. method.value.typeParameters = method.typeParameters;
  121. delete method.typeParameters;
  122. }
  123. classBody.body.push(method);
  124. }
  125. parseExprAtom(refExpressionErrors) {
  126. switch (this.state.type) {
  127. case _types.types.num:
  128. case _types.types.string:
  129. return this.estreeParseLiteral(this.state.value);
  130. case _types.types.regexp:
  131. return this.estreeParseRegExpLiteral(this.state.value);
  132. case _types.types.bigint:
  133. return this.estreeParseBigIntLiteral(this.state.value);
  134. case _types.types._null:
  135. return this.estreeParseLiteral(null);
  136. case _types.types._true:
  137. return this.estreeParseLiteral(true);
  138. case _types.types._false:
  139. return this.estreeParseLiteral(false);
  140. default:
  141. return super.parseExprAtom(refExpressionErrors);
  142. }
  143. }
  144. parseLiteral(value, type, startPos, startLoc) {
  145. const node = super.parseLiteral(value, type, startPos, startLoc);
  146. node.raw = node.extra.raw;
  147. delete node.extra;
  148. return node;
  149. }
  150. parseFunctionBody(node, allowExpression, isMethod = false) {
  151. super.parseFunctionBody(node, allowExpression, isMethod);
  152. node.expression = node.body.type !== "BlockStatement";
  153. }
  154. parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope = false) {
  155. let funcNode = this.startNode();
  156. funcNode.kind = node.kind;
  157. funcNode = super.parseMethod(funcNode, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope);
  158. funcNode.type = "FunctionExpression";
  159. delete funcNode.kind;
  160. node.value = funcNode;
  161. type = type === "ClassMethod" ? "MethodDefinition" : type;
  162. return this.finishNode(node, type);
  163. }
  164. parseObjectMethod(prop, isGenerator, isAsync, isPattern, containsEsc) {
  165. const node = super.parseObjectMethod(prop, isGenerator, isAsync, isPattern, containsEsc);
  166. if (node) {
  167. node.type = "Property";
  168. if (node.kind === "method") node.kind = "init";
  169. node.shorthand = false;
  170. }
  171. return node;
  172. }
  173. parseObjectProperty(prop, startPos, startLoc, isPattern, refExpressionErrors) {
  174. const node = super.parseObjectProperty(prop, startPos, startLoc, isPattern, refExpressionErrors);
  175. if (node) {
  176. node.kind = "init";
  177. node.type = "Property";
  178. }
  179. return node;
  180. }
  181. toAssignable(node) {
  182. if (isSimpleProperty(node)) {
  183. this.toAssignable(node.value);
  184. return node;
  185. }
  186. return super.toAssignable(node);
  187. }
  188. toAssignableObjectExpressionProp(prop, isLast) {
  189. if (prop.kind === "get" || prop.kind === "set") {
  190. throw this.raise(prop.key.start, "Object pattern can't contain getter or setter");
  191. } else if (prop.method) {
  192. throw this.raise(prop.key.start, "Object pattern can't contain methods");
  193. } else {
  194. super.toAssignableObjectExpressionProp(prop, isLast);
  195. }
  196. }
  197. finishCallExpression(node, optional) {
  198. super.finishCallExpression(node, optional);
  199. if (node.callee.type === "Import") {
  200. node.type = "ImportExpression";
  201. node.source = node.arguments[0];
  202. delete node.arguments;
  203. delete node.callee;
  204. }
  205. return node;
  206. }
  207. toReferencedListDeep(exprList, isParenthesizedExpr) {
  208. if (!exprList) {
  209. return;
  210. }
  211. super.toReferencedListDeep(exprList, isParenthesizedExpr);
  212. }
  213. };
  214. exports.default = _default;