Skip to content

Commit

Permalink
Recover from exported using declaration (#16407)
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Apr 4, 2024
1 parent e96a05d commit 2838c13
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/babel-parser/src/parse-error/standard-errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ export default {
UnterminatedRegExp: "Unterminated regular expression.",
UnterminatedString: "Unterminated string constant.",
UnterminatedTemplate: "Unterminated template.",
UsingDeclarationExport: "Using declaration cannot be exported.",
UsingDeclarationHasBindingPattern:
"Using declaration cannot have destructuring patterns.",
VarRedeclaration: ({ identifierName }: { identifierName: string }) =>
Expand Down
10 changes: 10 additions & 0 deletions packages/babel-parser/src/parser/statement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2649,6 +2649,16 @@ export default abstract class StatementParser extends ExpressionParser {
}
}

if (this.isContextual(tt._using)) {
this.raise(Errors.UsingDeclarationExport, this.state.startLoc);
return true;
}

if (this.isContextual(tt._await) && this.startsAwaitUsing()) {
this.raise(Errors.UsingDeclarationExport, this.state.startLoc);
return true;
}

return (
type === tt._var ||
type === tt._const ||
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{
"sourceType": "module",
"throws": "Unexpected token, expected \"{\" (1:7)"
"sourceType": "module"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"type": "File",
"start":0,"end":36,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":36,"index":36}},
"errors": [
"SyntaxError: Using declaration cannot be exported. (1:7)"
],
"program": {
"type": "Program",
"start":0,"end":36,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":36,"index":36}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ExportNamedDeclaration",
"start":0,"end":36,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":36,"index":36}},
"specifiers": [],
"source": null,
"declaration": {
"type": "VariableDeclaration",
"start":7,"end":36,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":36,"index":36}},
"declarations": [
{
"type": "VariableDeclarator",
"start":19,"end":35,"loc":{"start":{"line":1,"column":19,"index":19},"end":{"line":1,"column":35,"index":35}},
"id": {
"type": "Identifier",
"start":19,"end":24,"loc":{"start":{"line":1,"column":19,"index":19},"end":{"line":1,"column":24,"index":24},"identifierName":"basic"},
"name": "basic"
},
"init": {
"type": "CallExpression",
"start":27,"end":35,"loc":{"start":{"line":1,"column":27,"index":27},"end":{"line":1,"column":35,"index":35}},
"callee": {
"type": "Identifier",
"start":27,"end":33,"loc":{"start":{"line":1,"column":27,"index":27},"end":{"line":1,"column":33,"index":33},"identifierName":"reader"},
"name": "reader"
},
"arguments": []
}
}
],
"kind": "await using"
}
}
],
"directives": []
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export using basic = reader();
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"sourceType": "module"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"type": "File",
"start":0,"end":30,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":30,"index":30}},
"errors": [
"SyntaxError: Using declaration cannot be exported. (1:7)"
],
"program": {
"type": "Program",
"start":0,"end":30,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":30,"index":30}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ExportNamedDeclaration",
"start":0,"end":30,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":30,"index":30}},
"specifiers": [],
"source": null,
"declaration": {
"type": "VariableDeclaration",
"start":7,"end":30,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":30,"index":30}},
"declarations": [
{
"type": "VariableDeclarator",
"start":13,"end":29,"loc":{"start":{"line":1,"column":13,"index":13},"end":{"line":1,"column":29,"index":29}},
"id": {
"type": "Identifier",
"start":13,"end":18,"loc":{"start":{"line":1,"column":13,"index":13},"end":{"line":1,"column":18,"index":18},"identifierName":"basic"},
"name": "basic"
},
"init": {
"type": "CallExpression",
"start":21,"end":29,"loc":{"start":{"line":1,"column":21,"index":21},"end":{"line":1,"column":29,"index":29}},
"callee": {
"type": "Identifier",
"start":21,"end":27,"loc":{"start":{"line":1,"column":21,"index":21},"end":{"line":1,"column":27,"index":27},"identifierName":"reader"},
"name": "reader"
},
"arguments": []
}
}
],
"kind": "using"
}
}
],
"directives": []
}
}

0 comments on commit 2838c13

Please sign in to comment.