Skip to content

Commit

Permalink
Recover from error for missing initializer in const declaration (#12120)
Browse files Browse the repository at this point in the history
  • Loading branch information
sosukesuzuki committed Sep 29, 2020
1 parent 3ba1ece commit 45f0cc2
Show file tree
Hide file tree
Showing 10 changed files with 175 additions and 15 deletions.
6 changes: 5 additions & 1 deletion packages/babel-parser/src/parser/statement.js
Expand Up @@ -1016,7 +1016,11 @@ export default class StatementParser extends ExpressionParser {
// `const` with no initializer is allowed in TypeScript.
// It could be a declaration like `const x: number;`.
if (!isTypescript) {
this.unexpected();
this.raise(
this.state.lastTokEnd,
Errors.DeclarationMissingInitializer,
"Const declarations",
);
}
} else if (
decl.id.type !== "Identifier" &&
Expand Down

This file was deleted.

@@ -0,0 +1,33 @@
{
"type": "File",
"start":0,"end":8,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":8}},
"errors": [
"SyntaxError: Const declarations require an initialization value (1:7)"
],
"program": {
"type": "Program",
"start":0,"end":8,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":8}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "VariableDeclaration",
"start":0,"end":8,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":8}},
"declarations": [
{
"type": "VariableDeclarator",
"start":6,"end":7,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":7}},
"id": {
"type": "Identifier",
"start":6,"end":7,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":7},"identifierName":"a"},
"name": "a"
},
"init": null
}
],
"kind": "const"
}
],
"directives": []
}
}
@@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:14)"
}
"throws": "Unexpected token, expected \";\" (1:14)"
}

This file was deleted.

@@ -0,0 +1,51 @@
{
"type": "File",
"start":0,"end":16,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":16}},
"errors": [
"SyntaxError: Const declarations require an initialization value (1:15)"
],
"program": {
"type": "Program",
"start":0,"end":16,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":16}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "VariableDeclaration",
"start":0,"end":16,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":16}},
"declarations": [
{
"type": "VariableDeclarator",
"start":6,"end":12,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":12}},
"id": {
"type": "Identifier",
"start":6,"end":7,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":7},"identifierName":"x"},
"name": "x"
},
"init": {
"type": "NumericLiteral",
"start":10,"end":12,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":12}},
"extra": {
"rawValue": 12,
"raw": "12"
},
"value": 12
}
},
{
"type": "VariableDeclarator",
"start":14,"end":15,"loc":{"start":{"line":1,"column":14},"end":{"line":1,"column":15}},
"id": {
"type": "Identifier",
"start":14,"end":15,"loc":{"start":{"line":1,"column":14},"end":{"line":1,"column":15},"identifierName":"y"},
"name": "y"
},
"init": null
}
],
"kind": "const"
}
],
"directives": []
}
}

This file was deleted.

@@ -0,0 +1,51 @@
{
"type": "File",
"start":0,"end":16,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":16}},
"errors": [
"SyntaxError: Const declarations require an initialization value (1:7)"
],
"program": {
"type": "Program",
"start":0,"end":16,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":16}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "VariableDeclaration",
"start":0,"end":16,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":16}},
"declarations": [
{
"type": "VariableDeclarator",
"start":6,"end":7,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":7}},
"id": {
"type": "Identifier",
"start":6,"end":7,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":7},"identifierName":"x"},
"name": "x"
},
"init": null
},
{
"type": "VariableDeclarator",
"start":9,"end":15,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":15}},
"id": {
"type": "Identifier",
"start":9,"end":10,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":10},"identifierName":"y"},
"name": "y"
},
"init": {
"type": "NumericLiteral",
"start":13,"end":15,"loc":{"start":{"line":1,"column":13},"end":{"line":1,"column":15}},
"extra": {
"rawValue": 12,
"raw": "12"
},
"value": 12
}
}
],
"kind": "const"
}
],
"directives": []
}
}

This file was deleted.

@@ -0,0 +1,33 @@
{
"type": "File",
"start":0,"end":8,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":8}},
"errors": [
"SyntaxError: Const declarations require an initialization value (1:7)"
],
"program": {
"type": "Program",
"start":0,"end":8,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":8}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "VariableDeclaration",
"start":0,"end":8,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":8}},
"declarations": [
{
"type": "VariableDeclarator",
"start":6,"end":7,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":7}},
"id": {
"type": "Identifier",
"start":6,"end":7,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":7},"identifierName":"x"},
"name": "x"
},
"init": null
}
],
"kind": "const"
}
],
"directives": []
}
}

0 comments on commit 45f0cc2

Please sign in to comment.