expression-scope.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. exports.newArrowHeadScope = newArrowHeadScope;
  7. exports.newAsyncArrowScope = newAsyncArrowScope;
  8. exports.newExpressionScope = newExpressionScope;
  9. exports.newParameterDeclarationScope = newParameterDeclarationScope;
  10. var _parseError = require("../parse-error");
  11. const kExpression = 0,
  12. kMaybeArrowParameterDeclaration = 1,
  13. kMaybeAsyncArrowParameterDeclaration = 2,
  14. kParameterDeclaration = 3;
  15. class ExpressionScope {
  16. constructor(type = kExpression) {
  17. this.type = void 0;
  18. this.type = type;
  19. }
  20. canBeArrowParameterDeclaration() {
  21. return this.type === kMaybeAsyncArrowParameterDeclaration || this.type === kMaybeArrowParameterDeclaration;
  22. }
  23. isCertainlyParameterDeclaration() {
  24. return this.type === kParameterDeclaration;
  25. }
  26. }
  27. class ArrowHeadParsingScope extends ExpressionScope {
  28. constructor(type) {
  29. super(type);
  30. this.declarationErrors = new Map();
  31. }
  32. recordDeclarationError(ParsingErrorClass, {
  33. at
  34. }) {
  35. const index = at.index;
  36. this.declarationErrors.set(index, [ParsingErrorClass, at]);
  37. }
  38. clearDeclarationError(index) {
  39. this.declarationErrors.delete(index);
  40. }
  41. iterateErrors(iterator) {
  42. this.declarationErrors.forEach(iterator);
  43. }
  44. }
  45. class ExpressionScopeHandler {
  46. constructor(parser) {
  47. this.parser = void 0;
  48. this.stack = [new ExpressionScope()];
  49. this.parser = parser;
  50. }
  51. enter(scope) {
  52. this.stack.push(scope);
  53. }
  54. exit() {
  55. this.stack.pop();
  56. }
  57. recordParameterInitializerError(toParseError, {
  58. at: node
  59. }) {
  60. const origin = {
  61. at: node.loc.start
  62. };
  63. const {
  64. stack
  65. } = this;
  66. let i = stack.length - 1;
  67. let scope = stack[i];
  68. while (!scope.isCertainlyParameterDeclaration()) {
  69. if (scope.canBeArrowParameterDeclaration()) {
  70. scope.recordDeclarationError(toParseError, origin);
  71. } else {
  72. return;
  73. }
  74. scope = stack[--i];
  75. }
  76. this.parser.raise(toParseError, origin);
  77. }
  78. recordArrowParameterBindingError(error, {
  79. at: node
  80. }) {
  81. const {
  82. stack
  83. } = this;
  84. const scope = stack[stack.length - 1];
  85. const origin = {
  86. at: node.loc.start
  87. };
  88. if (scope.isCertainlyParameterDeclaration()) {
  89. this.parser.raise(error, origin);
  90. } else if (scope.canBeArrowParameterDeclaration()) {
  91. scope.recordDeclarationError(error, origin);
  92. } else {
  93. return;
  94. }
  95. }
  96. recordAsyncArrowParametersError({
  97. at
  98. }) {
  99. const {
  100. stack
  101. } = this;
  102. let i = stack.length - 1;
  103. let scope = stack[i];
  104. while (scope.canBeArrowParameterDeclaration()) {
  105. if (scope.type === kMaybeAsyncArrowParameterDeclaration) {
  106. scope.recordDeclarationError(_parseError.Errors.AwaitBindingIdentifier, {
  107. at
  108. });
  109. }
  110. scope = stack[--i];
  111. }
  112. }
  113. validateAsPattern() {
  114. const {
  115. stack
  116. } = this;
  117. const currentScope = stack[stack.length - 1];
  118. if (!currentScope.canBeArrowParameterDeclaration()) return;
  119. currentScope.iterateErrors(([toParseError, loc]) => {
  120. this.parser.raise(toParseError, {
  121. at: loc
  122. });
  123. let i = stack.length - 2;
  124. let scope = stack[i];
  125. while (scope.canBeArrowParameterDeclaration()) {
  126. scope.clearDeclarationError(loc.index);
  127. scope = stack[--i];
  128. }
  129. });
  130. }
  131. }
  132. exports.default = ExpressionScopeHandler;
  133. function newParameterDeclarationScope() {
  134. return new ExpressionScope(kParameterDeclaration);
  135. }
  136. function newArrowHeadScope() {
  137. return new ArrowHeadParsingScope(kMaybeArrowParameterDeclaration);
  138. }
  139. function newAsyncArrowScope() {
  140. return new ArrowHeadParsingScope(kMaybeAsyncArrowParameterDeclaration);
  141. }
  142. function newExpressionScope() {
  143. return new ExpressionScope();
  144. }
  145. //# sourceMappingURL=expression-scope.js.map