Skip to content

#1125 Fix parsing of unusual trailing expressions separated by whitespace #2252

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
20 changes: 16 additions & 4 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -939,7 +939,7 @@ export class Parser extends DiagnosticEmitter {
} while (tn.skip(Token.Comma));

let ret = Node.createVariableStatement(decorators, declarations, tn.range(startPos, tn.pos));
tn.skip(Token.Semicolon);
if (!tn.skip(Token.Semicolon) && !isFor) this.checkASI(tn);
return ret;
}

Expand Down Expand Up @@ -1122,7 +1122,7 @@ export class Parser extends DiagnosticEmitter {
}

let ret = Node.createReturnStatement(expr, tn.range(startPos, tn.pos));
tn.skip(Token.Semicolon);
if (!tn.skip(Token.Semicolon)) this.checkASI(tn);
return ret;
}

Expand Down Expand Up @@ -3009,7 +3009,7 @@ export class Parser extends DiagnosticEmitter {
}
}
let ret = Node.createBlockStatement(statements, tn.range(startPos, tn.pos));
tn.skip(Token.Semicolon);
if (topLevel) tn.skip(Token.Semicolon);
return ret;
}

Expand Down Expand Up @@ -3417,7 +3417,7 @@ export class Parser extends DiagnosticEmitter {
let expression = this.parseExpression(tn);
if (!expression) return null;
let ret = Node.createThrowStatement(expression, tn.range(startPos, tn.pos));
tn.skip(Token.Semicolon);
if (!tn.skip(Token.Semicolon)) this.checkASI(tn);
return ret;
}

Expand Down Expand Up @@ -4401,6 +4401,18 @@ export class Parser extends DiagnosticEmitter {
return expr;
}

private checkASI(
tn: Tokenizer
): void {
// see: https://tc39.es/ecma262/#sec-automatic-semicolon-insertion
let token = tn.peek(true);
if (tn.nextTokenOnNewLine || token == Token.EndOfFile || token == Token.CloseBrace) return;
this.error(
DiagnosticCode.Unexpected_token,
tn.range(tn.nextTokenPos)
);
}

/** Skips over a statement on errors in an attempt to reduce unnecessary diagnostic noise. */
skipStatement(tn: Tokenizer): void {
tn.peek(true);
Expand Down
19 changes: 19 additions & 0 deletions tests/parser/asi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function failLet(y: i32): i32 {
let x = y 234;
return x + y;
}

function failReturn(): i32 {
return 123 456;
}

function failThrow(): i32 {
throw 123 456;
}

function successCloseBrace(): i32 {
return 123 }

function successCloseParen(): i32 {
return ( 123 )
}
22 changes: 22 additions & 0 deletions tests/parser/asi.ts.fixture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function failLet(y: i32): i32 {
let x = y;
234;
return x + y;
}
function failReturn(): i32 {
return 123;
456;
}
function failThrow(): i32 {
throw 123;
456;
}
function successCloseBrace(): i32 {
return 123;
}
function successCloseParen(): i32 {
return (123);
}
// ERROR 1012: "Unexpected token." in asi.ts(2,13+0)
// ERROR 1012: "Unexpected token." in asi.ts(7,14+0)
// ERROR 1012: "Unexpected token." in asi.ts(11,13+0)