Skip to content

Commit

Permalink
disallow from as identifier after export default
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Jun 4, 2020
1 parent 8d87929 commit ce6e6db
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/babel-parser/src/parser/error-message.js
Expand Up @@ -42,6 +42,8 @@ export const ErrorMessages = Object.freeze({
DuplicateRegExpFlags: "Duplicate regular expression flag",
ElementAfterRest: "Rest element must be last element",
EscapedCharNotAnIdentifier: "Invalid Unicode escape",
ExportDefaultFromAsIdentifier:
"'from' is not allowed as an identifier after 'export default'",
ForInOfLoopInitializer:
"%0 loop variable declaration may not have an initializer",
GeneratorInSingleStatementContext:
Expand Down
11 changes: 11 additions & 0 deletions packages/babel-parser/src/parser/statement.js
Expand Up @@ -1925,6 +1925,17 @@ export default class StatementParser extends ExpressionParser {
if (isDefault) {
// Default exports
this.checkDuplicateExports(node, "default");
if (this.hasPlugin("exportDefaultFrom")) {
const declaration = ((node: any): N.ExportDefaultDeclaration)
.declaration;
if (
declaration.type === "Identifier" &&
declaration.name === "from" &&
declaration.end - declaration.start === 4 // does not contain escape
) {
this.raise(declaration.start, Errors.ExportDefaultFromAsIdentifier);
}
}
} else if (node.specifiers && node.specifiers.length) {
// Named exports
for (const specifier of node.specifiers) {
Expand Down
@@ -0,0 +1,2 @@
export default from
"bar";
@@ -0,0 +1,4 @@
{
"sourceType": "module",
"plugins": ["exportDefaultFrom"]
}
@@ -0,0 +1,37 @@
{
"type": "File",
"start":0,"end":26,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":6}},
"program": {
"type": "Program",
"start":0,"end":26,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":6}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ExportNamedDeclaration",
"start":0,"end":26,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":6}},
"specifiers": [
{
"type": "ExportDefaultSpecifier",
"start":7,"end":14,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":14}},
"exported": {
"type": "Identifier",
"start":7,"end":14,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":14},"identifierName":"default"},
"name": "default"
}
}
],
"source": {
"type": "StringLiteral",
"start":20,"end":25,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":5}},
"extra": {
"rawValue": "bar",
"raw": "\"bar\""
},
"value": "bar"
}
}
],
"directives": []
}
}
@@ -1,6 +1,9 @@
{
"type": "File",
"start":0,"end":20,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":20}},
"errors": [
"SyntaxError: 'from' is not allowed as an identifier after 'export default' (1:15)"
],
"program": {
"type": "Program",
"start":0,"end":20,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":20}},
Expand Down

0 comments on commit ce6e6db

Please sign in to comment.