Skip to content

Commit

Permalink
fix(lexer): improved template literal scanning
Browse files Browse the repository at this point in the history
  • Loading branch information
KFlash committed May 23, 2019
1 parent a69476c commit 68175f6
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 55 deletions.
6 changes: 3 additions & 3 deletions src/lexer/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export function scanString(parser: ParserState, context: Context): any {
const code = parseEscape(parser, context, ch);

if (code >= 0) ret += fromCodePoint(code);
else handleStringError(parser, code as Escape);
else handleStringError(parser, code as Escape, /* isTemplate */ 0);
}
marker = parser.index + 1;
}
Expand Down Expand Up @@ -215,13 +215,13 @@ export function parseEscape(parser: ParserState, context: Context, first: number
}
}

export function handleStringError(state: ParserState, code: Escape): void {
export function handleStringError(state: ParserState, code: Escape, isTemplate: 0 | 1): void {
switch (code) {
case Escape.Empty:
return;

case Escape.StrictOctal:
report(state, Errors.StrictOctalEscape);
report(state, isTemplate ? Errors.TemplateOctalLiteral : Errors.StrictOctalEscape);

case Escape.EightOrNine:
report(state, Errors.InvalidEightAndNine);
Expand Down
4 changes: 2 additions & 2 deletions src/lexer/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function scanTemplate(parser: ParserState, context: Context): Token {
if (ch > 0x7e) {
ret += fromCodePoint(ch);
} else {
const code = parseEscape(parser, context, ch);
const code = parseEscape(parser, context | Context.Strict, ch);
if (code >= 0) {
ret += fromCodePoint(code);
} else if (code !== Escape.Empty && context & Context.TaggedTemplate) {
Expand All @@ -36,7 +36,7 @@ export function scanTemplate(parser: ParserState, context: Context): Token {
}
break;
} else {
handleStringError(parser, code as Escape);
handleStringError(parser, code as Escape, /* isTemplate */ 1);
}
}
} else {
Expand Down
4 changes: 1 addition & 3 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3283,7 +3283,6 @@ function parseArrayOrObjectAssignmentPattern(
}

reinterpretToPattern(parser, node);

const { token } = parser;
const right = parseExpression(parser, (context | Context.DisallowIn) ^ Context.DisallowIn, /* assignable */ 1);

Expand All @@ -3306,7 +3305,7 @@ function parseArrayOrObjectAssignmentPattern(
*
* @param parser Parser object
* @param context Context masks
* @param closingToken Token
* @param closingToken
* @param type Binding type
* @param isAsync
*/
Expand All @@ -3324,7 +3323,6 @@ function parseRestOrSpreadElement(

if (parser.token & (Token.Keyword | Token.IsIdentifier)) {
parser.assignable = AssignmentKind.IsAssignable;

destructible |= parser.token === Token.AwaitKeyword ? DestructuringKind.Await : 0;

argument = parsePrimaryExpressionExtended(parser, context, type, /* inNewExpression */ 0, /* assignable */ 1);
Expand Down
4 changes: 2 additions & 2 deletions test/lexer/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ describe('Lexer - Template', () => {
describe('Lexer - Tagged Template', () => {
const tokens: [Context, Token, string, string | void][] = [
//[Context.TaggedTemplate, Token.TemplateTail, '`\\u{70bc`', undefined],
[Context.TaggedTemplate, Token.TemplateContinuation, '`\\7${', '\u0007'],
[Context.TaggedTemplate, Token.TemplateContinuation, '`\\1${', '\u0001'],
[Context.TaggedTemplate, Token.TemplateContinuation, '`\\7${', undefined],
[Context.TaggedTemplate, Token.TemplateContinuation, '`\\1${', undefined],
[Context.TaggedTemplate, Token.TemplateContinuation, "`'${", "'"],
[Context.TaggedTemplate, Token.TemplateContinuation, '`"${', '"'],
[Context.TaggedTemplate, Token.TemplateContinuation, '`\\`${', '`'],
Expand Down
7 changes: 7 additions & 0 deletions test/parser/declarations/let.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ describe('Declarations - Let', () => {
for (const arg of [
'let [ , , ...x] = [1, 2, 3, 4, 5];',
'let test262id8;',
'let a1; [a1] = [1]',
'let [...rest2] = [1, 2, 3, 4, 5];',
'let [a4, b4, c4, ...rest4] = [1, 2, 3];',
'let a1; [[a1]] = [[1]];',
'let a1; [[a1, b1] = [1, 2]] = [];',
'let a1; [a1, b1, c1, d1, ...rest1] = "testing";',
'let arrow = () => {};',
`let x = class x {};
let x = class {};
Expand Down Expand Up @@ -150,6 +156,7 @@ describe('Declarations - Let', () => {
'let x = /* bef */5 + 3/* aft */;',
'let x = y + 5;',
'let x=y + 5;',
'let [[a]=[1]] = [[2]];',
'let/foo/g',
`{ let x = 5; let y = 6; }`,
'let {a,b=0,c:d,e:f=0,[g]:[h]}=0',
Expand Down
50 changes: 5 additions & 45 deletions test/parser/expressions/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,11 @@ describe('Expressions - Template', () => {
['[z``] = {}', Context.None],
['[`${"a"}`] = {}', Context.None],
['[`${""}`] = {}', Context.None],
['`\\7`', Context.None],
['`\\10`', Context.None],
['`\\01`', Context.None],
['`\\30`', Context.None],
['`\\001`', Context.None],
//['`a${await foo}d`', Context.None],
['`\\u{g}`', Context.None],
['`\\u00g0`', Context.None],
Expand All @@ -341,51 +346,6 @@ describe('Expressions - Template', () => {
['`\\u{11ffff}${', Context.None]
]);
pass('Expressions - Template (pass)', [
/* [
'f`x`\n/foo/',
Context.None,
{
body: [
{
expression: {
quasi: {
expressions: [],
quasis: [
{
tail: true,
type: 'TemplateElement',
value: {
cooked: 'x',
raw: 'x'
}
}
],
type: 'TemplateLiteral'
},
tag: {
name: 'f',
type: 'Identifier'
},
type: 'TaggedTemplateExpression'
},
type: 'ExpressionStatement'
},
{
expression: {
regex: {
flags: '',
pattern: 'foo'
},
type: 'Literal',
value: /foo/
},
type: 'ExpressionStatement'
}
],
sourceType: 'script',
type: 'Program'
}
],*/
[
'`${y, x`)`}`',
Context.None,
Expand Down
3 changes: 3 additions & 0 deletions test/parser/miscellaneous/grammar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2726,6 +2726,9 @@ describe('Miscellaneous - Cover grammar', () => {
'var d4 = (function( { a4, b4 } = { a4 : 20, b4 : 25 }) { return a4;})();',
'[...((a5))] = [1, 2, 3];',
'({} = undefined);',
'[[a]=[1]] = [];',
'function foo(x = [a, b = 2, ...c] = [1,,3,4,5,6,7]) {}',
'`${[a = 5, b, ...c] = [, 1, 3, 5, 7, 9]}`;',
'function f({}){}; f();',
'function f({}){}; f(null);'
]) {
Expand Down

0 comments on commit 68175f6

Please sign in to comment.