Skip to content

Commit

Permalink
fix(lexer): fixed a optional chaining token bug introduced earlier
Browse files Browse the repository at this point in the history
  • Loading branch information
KFlash committed Aug 2, 2019
1 parent 5f23e59 commit 79e8fa3
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 14 additions & 7 deletions src/lexer/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,17 +216,24 @@ export function scanSingleToken(parser: ParserState, context: Context, state: Le
return token;

case Token.QuestionMark: {
const ch = advanceChar(parser);
if ((context & Context.OptionsNext) < 1) return token;
let ch = advanceChar(parser);
if ((context & Context.OptionsNext) < 1) return Token.QuestionMark;
if (ch === Chars.QuestionMark) {
advanceChar(parser);
return Token.Coalesce;
} else if (ch === Chars.Period) {
const index = parser.index + 1;
// Check that it's not followed by any numbers
if (index < parser.end) {
ch = parser.source.charCodeAt(index);
if ((CharTypes[ch] & CharFlags.Decimal) === 0) {
advanceChar(parser);
return Token.OptionalChaining;
}
}
}
if (ch === Chars.Period) {
advanceChar(parser);
return Token.OptionalChaining;
}
return token;

return Token.QuestionMark;
}

// `<`, `<=`, `<<`, `<<=`, `</`, `<!--`
Expand Down
4 changes: 2 additions & 2 deletions src/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ export const enum Token {

// Stage #3 proposals
Coalesce = 123 | IsBinaryOp | IsCoalesc | 1 << PrecStart, // ??,
OptionalChaining = 124, // ?.,
OptionalChaining = 125, // ?.,

}

Expand Down Expand Up @@ -233,7 +233,7 @@ export const KeywordDescTable = [
/* Others */
'enum', 'eval', 'arguments', 'escaped reserved', 'escaped future reserved', 'reserved if strict', '#',

'BigIntLiteral', '??', 'WhiteSpace', 'Illegal', 'LineTerminator', 'PrivateField', 'Template', '@', 'target', 'LineFeed', 'Escaped', 'JSXText', 'JSXText'
'BigIntLiteral', '??', 'WhiteSpace', '?.', 'Illegal', 'LineTerminator', 'PrivateField', 'Template', '@', 'target', 'LineFeed', 'Escaped', 'JSXText', 'JSXText'
];

// Normal object is much faster than Object.create(null), even with typeof check to avoid Object.prototype interference
Expand Down
84 changes: 84 additions & 0 deletions test/parser/expressions/conditional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,90 @@ describe('Expressions - Conditional', () => {
]);

pass('Expressions - Conditional (pass)', [
[
'foo?.3:0',
Context.OptionsNext,
{
type: 'Program',
sourceType: 'script',
body: [
{
type: 'ExpressionStatement',
expression: {
type: 'ConditionalExpression',
test: {
type: 'Identifier',
name: 'foo'
},
consequent: {
type: 'Literal',
value: 0.3
},
alternate: {
type: 'Literal',
value: 0
}
}
}
]
}
],
[
'foo?.3:0',
Context.None,
{
type: 'Program',
sourceType: 'script',
body: [
{
type: 'ExpressionStatement',
expression: {
type: 'ConditionalExpression',
test: {
type: 'Identifier',
name: 'foo'
},
consequent: {
type: 'Literal',
value: 0.3
},
alternate: {
type: 'Literal',
value: 0
}
}
}
]
}
],
[
'foo ? .3 : 0',
Context.None,
{
type: 'Program',
sourceType: 'script',
body: [
{
type: 'ExpressionStatement',
expression: {
type: 'ConditionalExpression',
test: {
type: 'Identifier',
name: 'foo'
},
consequent: {
type: 'Literal',
value: 0.3
},
alternate: {
type: 'Literal',
value: 0
}
}
}
]
}
],
[
'a ? b : c = d',
Context.None,
Expand Down

0 comments on commit 79e8fa3

Please sign in to comment.