lval.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. "use strict";Object.defineProperty(exports, "__esModule", {value: true});var _flow = require('../plugins/flow');
  2. var _typescript = require('../plugins/typescript');
  3. var _index = require('../tokenizer/index');
  4. var _keywords = require('../tokenizer/keywords');
  5. var _types = require('../tokenizer/types');
  6. var _base = require('./base');
  7. var _expression = require('./expression');
  8. var _util = require('./util');
  9. function parseSpread() {
  10. _index.next.call(void 0, );
  11. _expression.parseMaybeAssign.call(void 0, false);
  12. } exports.parseSpread = parseSpread;
  13. function parseRest(isBlockScope) {
  14. _index.next.call(void 0, );
  15. parseBindingAtom(isBlockScope);
  16. } exports.parseRest = parseRest;
  17. function parseBindingIdentifier(isBlockScope) {
  18. _expression.parseIdentifier.call(void 0, );
  19. markPriorBindingIdentifier(isBlockScope);
  20. } exports.parseBindingIdentifier = parseBindingIdentifier;
  21. function parseImportedIdentifier() {
  22. _expression.parseIdentifier.call(void 0, );
  23. _base.state.tokens[_base.state.tokens.length - 1].identifierRole = _index.IdentifierRole.ImportDeclaration;
  24. } exports.parseImportedIdentifier = parseImportedIdentifier;
  25. function markPriorBindingIdentifier(isBlockScope) {
  26. let identifierRole;
  27. if (_base.state.scopeDepth === 0) {
  28. identifierRole = _index.IdentifierRole.TopLevelDeclaration;
  29. } else if (isBlockScope) {
  30. identifierRole = _index.IdentifierRole.BlockScopedDeclaration;
  31. } else {
  32. identifierRole = _index.IdentifierRole.FunctionScopedDeclaration;
  33. }
  34. _base.state.tokens[_base.state.tokens.length - 1].identifierRole = identifierRole;
  35. } exports.markPriorBindingIdentifier = markPriorBindingIdentifier;
  36. // Parses lvalue (assignable) atom.
  37. function parseBindingAtom(isBlockScope) {
  38. switch (_base.state.type) {
  39. case _types.TokenType._this: {
  40. // In TypeScript, "this" may be the name of a parameter, so allow it.
  41. const oldIsType = _index.pushTypeContext.call(void 0, 0);
  42. _index.next.call(void 0, );
  43. _index.popTypeContext.call(void 0, oldIsType);
  44. return;
  45. }
  46. case _types.TokenType._yield:
  47. case _types.TokenType.name: {
  48. _base.state.type = _types.TokenType.name;
  49. parseBindingIdentifier(isBlockScope);
  50. return;
  51. }
  52. case _types.TokenType.bracketL: {
  53. _index.next.call(void 0, );
  54. parseBindingList(_types.TokenType.bracketR, isBlockScope, true /* allowEmpty */);
  55. return;
  56. }
  57. case _types.TokenType.braceL:
  58. _expression.parseObj.call(void 0, true, isBlockScope);
  59. return;
  60. default:
  61. _util.unexpected.call(void 0, );
  62. }
  63. } exports.parseBindingAtom = parseBindingAtom;
  64. function parseBindingList(
  65. close,
  66. isBlockScope,
  67. allowEmpty = false,
  68. allowModifiers = false,
  69. contextId = 0,
  70. ) {
  71. let first = true;
  72. let hasRemovedComma = false;
  73. const firstItemTokenIndex = _base.state.tokens.length;
  74. while (!_index.eat.call(void 0, close) && !_base.state.error) {
  75. if (first) {
  76. first = false;
  77. } else {
  78. _util.expect.call(void 0, _types.TokenType.comma);
  79. _base.state.tokens[_base.state.tokens.length - 1].contextId = contextId;
  80. // After a "this" type in TypeScript, we need to set the following comma (if any) to also be
  81. // a type token so that it will be removed.
  82. if (!hasRemovedComma && _base.state.tokens[firstItemTokenIndex].isType) {
  83. _base.state.tokens[_base.state.tokens.length - 1].isType = true;
  84. hasRemovedComma = true;
  85. }
  86. }
  87. if (allowEmpty && _index.match.call(void 0, _types.TokenType.comma)) {
  88. // Empty item; nothing further to parse for this item.
  89. } else if (_index.eat.call(void 0, close)) {
  90. break;
  91. } else if (_index.match.call(void 0, _types.TokenType.ellipsis)) {
  92. parseRest(isBlockScope);
  93. parseAssignableListItemTypes();
  94. // Support rest element trailing commas allowed by TypeScript <2.9.
  95. _index.eat.call(void 0, _types.TokenType.comma);
  96. _util.expect.call(void 0, close);
  97. break;
  98. } else {
  99. parseAssignableListItem(allowModifiers, isBlockScope);
  100. }
  101. }
  102. } exports.parseBindingList = parseBindingList;
  103. function parseAssignableListItem(allowModifiers, isBlockScope) {
  104. if (allowModifiers) {
  105. _typescript.tsParseModifiers.call(void 0, [
  106. _keywords.ContextualKeyword._public,
  107. _keywords.ContextualKeyword._protected,
  108. _keywords.ContextualKeyword._private,
  109. _keywords.ContextualKeyword._readonly,
  110. _keywords.ContextualKeyword._override,
  111. ]);
  112. }
  113. parseMaybeDefault(isBlockScope);
  114. parseAssignableListItemTypes();
  115. parseMaybeDefault(isBlockScope, true /* leftAlreadyParsed */);
  116. }
  117. function parseAssignableListItemTypes() {
  118. if (_base.isFlowEnabled) {
  119. _flow.flowParseAssignableListItemTypes.call(void 0, );
  120. } else if (_base.isTypeScriptEnabled) {
  121. _typescript.tsParseAssignableListItemTypes.call(void 0, );
  122. }
  123. }
  124. // Parses assignment pattern around given atom if possible.
  125. function parseMaybeDefault(isBlockScope, leftAlreadyParsed = false) {
  126. if (!leftAlreadyParsed) {
  127. parseBindingAtom(isBlockScope);
  128. }
  129. if (!_index.eat.call(void 0, _types.TokenType.eq)) {
  130. return;
  131. }
  132. const eqIndex = _base.state.tokens.length - 1;
  133. _expression.parseMaybeAssign.call(void 0, );
  134. _base.state.tokens[eqIndex].rhsEndIndex = _base.state.tokens.length;
  135. } exports.parseMaybeDefault = parseMaybeDefault;