-
Notifications
You must be signed in to change notification settings - Fork 960
feat: support using
and await using
#1369
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
Conversation
6b5e69d
to
4f37116
Compare
acorn/src/statement.js
Outdated
decl.id = this.parseBindingAtom() | ||
this.checkLValPattern(decl.id, kind === "var" ? BIND_VAR : BIND_LEXICAL, false) | ||
|
||
if (kind === "using" || kind === "await using") { | ||
if (decl.id.type === "ObjectPattern") { | ||
this.raise(decl.id.start, "Using declaration cannot have destructuring patterns") | ||
} | ||
|
||
if (decl.id.type === "ArrayPattern") { | ||
for (let elem of decl.id.elements) { | ||
if (elem && elem.type === "RestElement") { | ||
this.raise(elem.start, "Unexpected token '...'") | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for working on this PR!
I know you're still working in progress, but let me comment.
I think this method handles LexicalBinding.
Following the spec, I thought it would be more direct to write it like this, but what do you think?
decl.id = kind === "using" || kind === "await using"
? this.parseIdent()
: this.parseBindingAtom()
this.checkLValPattern(decl.id, kind === "var" ? BIND_VAR : BIND_LEXICAL, false)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
b9d2a8f
to
84c17e0
Compare
pp.parseForAfterInit = function(node, init, awaitAt) { | ||
if ((this.type === tt._in || (this.options.ecmaVersion >= 6 && this.isContextual("of"))) && init.declarations.length === 1) { | ||
if (this.options.ecmaVersion >= 9) { | ||
if (this.type === tt._in) { | ||
if (awaitAt > -1) this.unexpected(awaitAt) | ||
} else node.await = awaitAt > -1 | ||
} | ||
return this.parseForIn(node, init) | ||
} | ||
if (awaitAt > -1) this.unexpected(awaitAt) | ||
return this.parseFor(node, init) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I moved this from line number 226 ~ 235.
e0d34af
to
f73fe47
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@baseballyama Thank you for making progress on this PR!
I'm not familiar with the specs so I may be wrong, but let me comment to the best of my understanding.
// --- ECMAScript version compatibility --- | ||
|
||
// ES16: using should be treated as regular identifier (assignment) | ||
test("using = 5;", { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be good to have a test for this in ES17.
Also, I think it would be good to have some of the following tests in ES17.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added: 7babfd3
acorn/src/statement.js
Outdated
return isIdentifierStart(ch, true) || ch === 92 // '\' | ||
} | ||
|
||
pp.isAwaitUsingDeclaration = function() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The naming seemed inconsistent to me.
The next method name is isUsing
, so I think isAwaitUsing
would be better here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated: 17b3f85
}, {"ecmaVersion": 17}); | ||
|
||
// Top-level using declaration | ||
test("using x = resource;", { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed: 5f1f124
test/tests-using.js
Outdated
}] | ||
}], | ||
sourceType: "script" | ||
}, {"ecmaVersion": 17}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please add the following test?
As I understand it can be parsed, but it is not an unsing
declaration.
for (using of x) {}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed: c395a8b
@ota-meshi Thank you very much for your review!!! It was really helpful! |
acorn/src/statement.js
Outdated
this.raise(this.start, "Using declaration cannot appear in the top level when source type is `script`") | ||
} | ||
this.next() | ||
if (usingKind === "await using") this.next() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would think that {await using a = x}
in sourceType: script
would be a syntax error, but it doesn't seem to be the case currently. I think maybe we should check this.canAwait
here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you! fixed: e59ede7
FEATURE: Support `using` and `await using` syntax. Issue #1369
Thanks for your work on this @baseballyama! And for the reviews, @ota-meshi. I have no further issues with this. Merged as b4ae0d2. I'll cut a release in a few days if no further issues come up. |
close: #1366