UNPKG

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