Skip to content
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

Recover from "missing semicolon" errors #12437

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -1 +1 @@
SyntaxError: <CWD>\test.js: Unexpected token, expected ";" (2:10)
SyntaxError: <CWD>\test.js: Missing semicolon (2:10)
@@ -1 +1 @@
SyntaxError: <CWD>/test.js: Unexpected token, expected ";" (2:10)
SyntaxError: <CWD>/test.js: Missing semicolon (2:10)
@@ -1,3 +1,3 @@
{
"throws": "Unexpected token, expected \";\" (2:10)"
"throws": "Missing semicolon (2:10)"
}
1 change: 1 addition & 0 deletions packages/babel-parser/src/parser/error-message.js
Expand Up @@ -99,6 +99,7 @@ export const ErrorMessages = Object.freeze({
MissingClassName: "A class name is required",
MissingEqInAssignment:
"Only '=' operator can be used for specifying default value.",
MissingSemicolon: "Missing semicolon",
MissingUnicodeEscape: "Expecting Unicode escape sequence \\uXXXX",
MixingCoalesceWithLogical:
"Nullish coalescing operator(??) requires parens when mixing with logical operators",
Expand Down
4 changes: 2 additions & 2 deletions packages/babel-parser/src/parser/statement.js
Expand Up @@ -915,9 +915,9 @@ export default class StatementParser extends ExpressionParser {
init: ?(N.VariableDeclaration | N.Expression),
): N.ForStatement {
node.init = init;
this.expect(tt.semi);
this.semicolon(/* allowAsi */ false);
node.test = this.match(tt.semi) ? null : this.parseExpression();
this.expect(tt.semi);
this.semicolon(/* allowAsi */ false);
node.update = this.match(tt.parenR) ? null : this.parseExpression();
this.expect(tt.parenR);

Expand Down
5 changes: 3 additions & 2 deletions packages/babel-parser/src/parser/util.js
Expand Up @@ -105,8 +105,9 @@ export default class UtilParser extends Tokenizer {
// Consume a semicolon, or, failing that, see if we are allowed to
// pretend that there is a semicolon at this position.

semicolon(): void {
if (!this.isLineTerminator()) this.unexpected(null, tt.semi);
semicolon(allowAsi: boolean = true): void {
if (allowAsi ? this.isLineTerminator() : this.eat(tt.semi)) return;
this.raise(this.state.lastTokEnd, Errors.MissingSemicolon);
}

// Expect a token of a given type. If found, consume it, otherwise,
Expand Down
@@ -0,0 +1,7 @@
for (
var a = 1
a < 3
a++
) {

}
@@ -0,0 +1,82 @@
{
"type": "File",
"start":0,"end":38,"loc":{"start":{"line":1,"column":0},"end":{"line":7,"column":1}},
"errors": [
"SyntaxError: Missing semicolon (2:11)",
"SyntaxError: Missing semicolon (3:7)"
],
"program": {
"type": "Program",
"start":0,"end":38,"loc":{"start":{"line":1,"column":0},"end":{"line":7,"column":1}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ForStatement",
"start":0,"end":38,"loc":{"start":{"line":1,"column":0},"end":{"line":7,"column":1}},
"init": {
"type": "VariableDeclaration",
"start":8,"end":17,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":11}},
"declarations": [
{
"type": "VariableDeclarator",
"start":12,"end":17,"loc":{"start":{"line":2,"column":6},"end":{"line":2,"column":11}},
"id": {
"type": "Identifier",
"start":12,"end":13,"loc":{"start":{"line":2,"column":6},"end":{"line":2,"column":7},"identifierName":"a"},
"name": "a"
},
"init": {
"type": "NumericLiteral",
"start":16,"end":17,"loc":{"start":{"line":2,"column":10},"end":{"line":2,"column":11}},
"extra": {
"rawValue": 1,
"raw": "1"
},
"value": 1
}
}
],
"kind": "var"
},
"test": {
"type": "BinaryExpression",
"start":20,"end":25,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":7}},
"left": {
"type": "Identifier",
"start":20,"end":21,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":3},"identifierName":"a"},
"name": "a"
},
"operator": "<",
"right": {
"type": "NumericLiteral",
"start":24,"end":25,"loc":{"start":{"line":3,"column":6},"end":{"line":3,"column":7}},
"extra": {
"rawValue": 3,
"raw": "3"
},
"value": 3
}
},
"update": {
"type": "UpdateExpression",
"start":28,"end":31,"loc":{"start":{"line":4,"column":2},"end":{"line":4,"column":5}},
"operator": "++",
"prefix": false,
"argument": {
"type": "Identifier",
"start":28,"end":29,"loc":{"start":{"line":4,"column":2},"end":{"line":4,"column":3},"identifierName":"a"},
"name": "a"
}
},
"body": {
"type": "BlockStatement",
"start":34,"end":38,"loc":{"start":{"line":5,"column":2},"end":{"line":7,"column":1}},
"body": [],
"directives": []
}
}
],
"directives": []
}
}

This file was deleted.

@@ -0,0 +1,42 @@
{
"type": "File",
"start":0,"end":4,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":4}},
"errors": [
"SyntaxError: Missing semicolon (1:2)"
],
"program": {
"type": "Program",
"start":0,"end":4,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":4}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":2,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":2}},
"expression": {
"type": "NumericLiteral",
"start":0,"end":2,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":2}},
"extra": {
"rawValue": 7,
"raw": "07"
},
"value": 7
}
},
{
"type": "ExpressionStatement",
"start":2,"end":4,"loc":{"start":{"line":1,"column":2},"end":{"line":1,"column":4}},
"expression": {
"type": "NumericLiteral",
"start":2,"end":4,"loc":{"start":{"line":1,"column":2},"end":{"line":1,"column":4}},
"extra": {
"rawValue": 0.5,
"raw": ".5"
},
"value": 0.5
}
}
],
"directives": []
}
}

This file was deleted.

@@ -0,0 +1,45 @@
{
"type": "File",
"start":0,"end":9,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":9}},
"errors": [
"SyntaxError: Missing semicolon (1:5)"
],
"program": {
"type": "Program",
"start":0,"end":9,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":9}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "BlockStatement",
"start":0,"end":9,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":9}},
"body": [
{
"type": "ExpressionStatement",
"start":2,"end":5,"loc":{"start":{"line":1,"column":2},"end":{"line":1,"column":5}},
"expression": {
"type": "Identifier",
"start":2,"end":5,"loc":{"start":{"line":1,"column":2},"end":{"line":1,"column":5},"identifierName":"set"},
"name": "set"
}
},
{
"type": "ExpressionStatement",
"start":6,"end":7,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":7}},
"expression": {
"type": "NumericLiteral",
"start":6,"end":7,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":7}},
"extra": {
"rawValue": 1,
"raw": "1"
},
"value": 1
}
}
],
"directives": []
}
],
"directives": []
}
}

This file was deleted.

@@ -0,0 +1,45 @@
{
"type": "File",
"start":0,"end":9,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":9}},
"errors": [
"SyntaxError: Missing semicolon (1:5)"
],
"program": {
"type": "Program",
"start":0,"end":9,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":9}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "BlockStatement",
"start":0,"end":9,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":9}},
"body": [
{
"type": "ExpressionStatement",
"start":2,"end":5,"loc":{"start":{"line":1,"column":2},"end":{"line":1,"column":5}},
"expression": {
"type": "Identifier",
"start":2,"end":5,"loc":{"start":{"line":1,"column":2},"end":{"line":1,"column":5},"identifierName":"get"},
"name": "get"
}
},
{
"type": "ExpressionStatement",
"start":6,"end":7,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":7}},
"expression": {
"type": "NumericLiteral",
"start":6,"end":7,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":7}},
"extra": {
"rawValue": 2,
"raw": "2"
},
"value": 2
}
}
],
"directives": []
}
],
"directives": []
}
}

This file was deleted.

@@ -0,0 +1,34 @@
{
"type": "File",
"start":0,"end":4,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":4}},
"errors": [
"SyntaxError: Missing semicolon (1:1)"
],
"program": {
"type": "Program",
"start":0,"end":4,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":4}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":1,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":1}},
"expression": {
"type": "Identifier",
"start":0,"end":1,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":1},"identifierName":"a"},
"name": "a"
}
},
{
"type": "ExpressionStatement",
"start":2,"end":4,"loc":{"start":{"line":1,"column":2},"end":{"line":1,"column":4}},
"expression": {
"type": "Identifier",
"start":2,"end":3,"loc":{"start":{"line":1,"column":2},"end":{"line":1,"column":3},"identifierName":"b"},
"name": "b"
}
}
],
"directives": []
}
}
@@ -1,3 +1,3 @@
{
"throws": "Unexpected token, expected \";\" (1:2)"
}
"throws": "Unexpected token, expected \"(\" (1:4)"
}
@@ -1,3 +1,3 @@
{
"throws": "Unexpected token, expected \";\" (1:2)"
}
"throws": "A class name is required (1:7)"
}
@@ -1,3 +1,3 @@
{
"throws": "Unexpected token, expected \";\" (1:15)"
}
"throws": "Unexpected token (1:15)"
}
@@ -1,3 +1,3 @@
{
"throws": "Unexpected token, expected \";\" (1:14)"
}
"throws": "Unexpected token (1:14)"
}
@@ -1,3 +1,3 @@
{
"throws": "Unexpected token, expected \";\" (1:9)"
}
"throws": "Unexpected token (1:9)"
}
@@ -1,3 +1,3 @@
{
"throws": "Unexpected token, expected \";\" (1:8)"
}
"throws": "Unexpected token (1:9)"
}
@@ -1,3 +1,3 @@
{
"throws": "Unexpected token, expected \";\" (1:9)"
}
"throws": "Unexpected token (1:9)"
}

This file was deleted.