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

Treat "await" as an invalid identifier #4954

Merged
merged 1 commit into from Mar 19, 2017
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
@@ -0,0 +1,3 @@
export {};

var obj = { await: function () {} };
@@ -0,0 +1,3 @@
export {};

var obj = { await: function _await() {} };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but this changes the fn.name

Copy link
Member

@boopathi boopathi Dec 8, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In chrome this happens - so defining await inside async function is syntax error. Not sure if it's a chrome bug that defining it outside is not a syntax error...

screenshot 2016-12-08 16 32 03

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fn.name also changes when it's derived to be arguments or another keyword; not much can be done about it other than omitting the name entirely and leaving it to ES6 engines to correctly derive the name...

And yes, Chrome's error is correct. You cannot use a keyword in a var/let/const binding, and explicitly named functions create a var binding with their name.

Copy link
Member Author

@Jessidhia Jessidhia Dec 8, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, defining it outside is not a syntax error in strict mode -- it is a syntax error with the module parse goal, which only exists in Chakra(?) and parsers for now.

3 changes: 3 additions & 0 deletions packages/babel-types/src/validators.js
Expand Up @@ -169,6 +169,9 @@ export function isReferenced(node: Object, parent: Object): boolean {
export function isValidIdentifier(name: string): boolean {
if (typeof name !== "string" || esutils.keyword.isReservedWordES6(name, true)) {
return false;
} else if (name === "await") {
// invalid in module, valid in script; better be safe (see #4952)
return false;
} else {
return esutils.keyword.isIdentifierNameES6(name);
}
Expand Down
4 changes: 4 additions & 0 deletions packages/babel-types/test/validators.js
Expand Up @@ -26,5 +26,9 @@ suite("validators", function () {

assert(t.isNodesEquivalent(parse(program), parse(program2)) === false);
});

it("rejects 'await' as an identifier", function () {
assert(t.isValidIdentifier("await") === false);
});
});
});