placeholders.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
  9. 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; }
  10. _types.types.placeholder = new _types.TokenType("%%", {
  11. startsExpr: true
  12. });
  13. var _default = superClass => class extends superClass {
  14. parsePlaceholder(expectedNode) {
  15. if (this.match(_types.types.placeholder)) {
  16. const node = this.startNode();
  17. this.next();
  18. this.assertNoSpace("Unexpected space in placeholder.");
  19. node.name = super.parseIdentifier(true);
  20. this.assertNoSpace("Unexpected space in placeholder.");
  21. this.expect(_types.types.placeholder);
  22. return this.finishPlaceholder(node, expectedNode);
  23. }
  24. }
  25. finishPlaceholder(node, expectedNode) {
  26. const isFinished = !!(node.expectedNode && node.type === "Placeholder");
  27. node.expectedNode = expectedNode;
  28. return isFinished ? node : this.finishNode(node, "Placeholder");
  29. }
  30. getTokenFromCode(code) {
  31. if (code === 37 && this.input.charCodeAt(this.state.pos + 1) === 37) {
  32. return this.finishOp(_types.types.placeholder, 2);
  33. }
  34. return super.getTokenFromCode(...arguments);
  35. }
  36. parseExprAtom() {
  37. return this.parsePlaceholder("Expression") || super.parseExprAtom(...arguments);
  38. }
  39. parseIdentifier() {
  40. return this.parsePlaceholder("Identifier") || super.parseIdentifier(...arguments);
  41. }
  42. checkReservedWord(word) {
  43. if (word !== undefined) super.checkReservedWord(...arguments);
  44. }
  45. parseBindingAtom() {
  46. return this.parsePlaceholder("Pattern") || super.parseBindingAtom(...arguments);
  47. }
  48. checkLVal(expr) {
  49. if (expr.type !== "Placeholder") super.checkLVal(...arguments);
  50. }
  51. toAssignable(node) {
  52. if (node && node.type === "Placeholder" && node.expectedNode === "Expression") {
  53. node.expectedNode = "Pattern";
  54. return node;
  55. }
  56. return super.toAssignable(...arguments);
  57. }
  58. verifyBreakContinue(node) {
  59. if (node.label && node.label.type === "Placeholder") return;
  60. super.verifyBreakContinue(...arguments);
  61. }
  62. parseExpressionStatement(node, expr) {
  63. if (expr.type !== "Placeholder" || expr.extra && expr.extra.parenthesized) {
  64. return super.parseExpressionStatement(...arguments);
  65. }
  66. if (this.match(_types.types.colon)) {
  67. const stmt = node;
  68. stmt.label = this.finishPlaceholder(expr, "Identifier");
  69. this.next();
  70. stmt.body = this.parseStatement("label");
  71. return this.finishNode(stmt, "LabeledStatement");
  72. }
  73. this.semicolon();
  74. node.name = expr.name;
  75. return this.finishPlaceholder(node, "Statement");
  76. }
  77. parseBlock() {
  78. return this.parsePlaceholder("BlockStatement") || super.parseBlock(...arguments);
  79. }
  80. parseFunctionId() {
  81. return this.parsePlaceholder("Identifier") || super.parseFunctionId(...arguments);
  82. }
  83. parseClass(node, isStatement, optionalId) {
  84. const type = isStatement ? "ClassDeclaration" : "ClassExpression";
  85. this.next();
  86. this.takeDecorators(node);
  87. const oldStrict = this.state.strict;
  88. const placeholder = this.parsePlaceholder("Identifier");
  89. if (placeholder) {
  90. if (this.match(_types.types._extends) || this.match(_types.types.placeholder) || this.match(_types.types.braceL)) {
  91. node.id = placeholder;
  92. } else if (optionalId || !isStatement) {
  93. node.id = null;
  94. node.body = this.finishPlaceholder(placeholder, "ClassBody");
  95. return this.finishNode(node, type);
  96. } else {
  97. this.unexpected(null, "A class name is required");
  98. }
  99. } else {
  100. this.parseClassId(node, isStatement, optionalId);
  101. }
  102. this.parseClassSuper(node);
  103. node.body = this.parsePlaceholder("ClassBody") || this.parseClassBody(!!node.superClass, oldStrict);
  104. return this.finishNode(node, type);
  105. }
  106. parseExport(node) {
  107. const placeholder = this.parsePlaceholder("Identifier");
  108. if (!placeholder) return super.parseExport(...arguments);
  109. if (!this.isContextual("from") && !this.match(_types.types.comma)) {
  110. node.specifiers = [];
  111. node.source = null;
  112. node.declaration = this.finishPlaceholder(placeholder, "Declaration");
  113. return this.finishNode(node, "ExportNamedDeclaration");
  114. }
  115. this.expectPlugin("exportDefaultFrom");
  116. const specifier = this.startNode();
  117. specifier.exported = placeholder;
  118. node.specifiers = [this.finishNode(specifier, "ExportDefaultSpecifier")];
  119. return super.parseExport(node);
  120. }
  121. isExportDefaultSpecifier() {
  122. if (this.match(_types.types._default)) {
  123. const next = this.nextTokenStart();
  124. if (this.isUnparsedContextual(next, "from")) {
  125. if (this.input.startsWith(_types.types.placeholder.label, this.nextTokenStartSince(next + 4))) {
  126. return true;
  127. }
  128. }
  129. }
  130. return super.isExportDefaultSpecifier();
  131. }
  132. maybeParseExportDefaultSpecifier(node) {
  133. if (node.specifiers && node.specifiers.length > 0) {
  134. return true;
  135. }
  136. return super.maybeParseExportDefaultSpecifier(...arguments);
  137. }
  138. checkExport(node) {
  139. const {
  140. specifiers
  141. } = node;
  142. if (specifiers?.length) {
  143. node.specifiers = specifiers.filter(node => node.exported.type === "Placeholder");
  144. }
  145. super.checkExport(node);
  146. node.specifiers = specifiers;
  147. }
  148. parseImport(node) {
  149. const placeholder = this.parsePlaceholder("Identifier");
  150. if (!placeholder) return super.parseImport(...arguments);
  151. node.specifiers = [];
  152. if (!this.isContextual("from") && !this.match(_types.types.comma)) {
  153. node.source = this.finishPlaceholder(placeholder, "StringLiteral");
  154. this.semicolon();
  155. return this.finishNode(node, "ImportDeclaration");
  156. }
  157. const specifier = this.startNodeAtNode(placeholder);
  158. specifier.local = placeholder;
  159. this.finishNode(specifier, "ImportDefaultSpecifier");
  160. node.specifiers.push(specifier);
  161. if (this.eat(_types.types.comma)) {
  162. const hasStarImport = this.maybeParseStarImportSpecifier(node);
  163. if (!hasStarImport) this.parseNamedImportSpecifiers(node);
  164. }
  165. this.expectContextual("from");
  166. node.source = this.parseImportSource();
  167. this.semicolon();
  168. return this.finishNode(node, "ImportDeclaration");
  169. }
  170. parseImportSource() {
  171. return this.parsePlaceholder("StringLiteral") || super.parseImportSource(...arguments);
  172. }
  173. };
  174. exports.default = _default;