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

Disallow "let" as name at lexical bindings #10099

Merged
merged 3 commits into from Jun 18, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
25 changes: 25 additions & 0 deletions packages/babel-parser/src/parser/statement.js
Expand Up @@ -1014,6 +1014,15 @@ export default class StatementParser extends ExpressionParser {
this.unexpected(null, "let is disallowed as a lexically bound name");
g-plane marked this conversation as resolved.
Show resolved Hide resolved
}
decl.id = this.parseBindingAtom();
if (kind === "const" || kind === "let") {
const invalid = this.checkVarIdHasLet(decl.id);
if (invalid) {
this.raise(
invalid.start,
"'let' is not allowed to be used as a name in 'let' or 'const' declarations.",
);
}
}
this.checkLVal(
g-plane marked this conversation as resolved.
Show resolved Hide resolved
decl.id,
kind === "var" ? BIND_VAR : BIND_LEXICAL,
Expand All @@ -1022,6 +1031,22 @@ export default class StatementParser extends ExpressionParser {
);
}

checkVarIdHasLet(id: ?N.Pattern): ?N.Identifier {
kaicataldo marked this conversation as resolved.
Show resolved Hide resolved
if (!id) {
return null;
} else if (id.type === "ArrayPattern") {
return id.elements.find(element => this.checkVarIdHasLet(element));
} else if (id.type === "ObjectPattern") {
return id.properties.find(property =>
this.checkVarIdHasLet(property.value),
);
} else if (id.type === "Identifier" && id.name === "let") {
return id;
} else {
return null;
}
}

// Parse a function declaration or literal (depending on the
// `isStatement` parameter).

Expand Down
@@ -0,0 +1 @@
let { let } = {};
@@ -0,0 +1,3 @@
{
"throws": "'let' is not allowed to be used as a name in 'let' or 'const' declarations. (1:6)"
}
@@ -0,0 +1 @@
const { let } = {};
@@ -0,0 +1,3 @@
{
"throws": "'let' is not allowed to be used as a name in 'let' or 'const' declarations. (1:8)"
}
@@ -0,0 +1 @@
let [let] = [];
@@ -0,0 +1,3 @@
{
"throws": "'let' is not allowed to be used as a name in 'let' or 'const' declarations. (1:5)"
}
@@ -0,0 +1 @@
const [let] = [];
@@ -0,0 +1,3 @@
{
"throws": "'let' is not allowed to be used as a name in 'let' or 'const' declarations. (1:7)"
}