Skip to content

Conversation

baseballyama
Copy link
Contributor

close: #1366

Comment on lines 613 to 621
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 '...'")
}
}
}
}
Copy link
Contributor

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)
image

Copy link
Contributor Author

@baseballyama baseballyama May 30, 2025

Choose a reason for hiding this comment

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

@baseballyama baseballyama force-pushed the feat/using branch 7 times, most recently from b9d2a8f to 84c17e0 Compare May 30, 2025 13:03
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)
}
Copy link
Contributor Author

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.

@baseballyama baseballyama force-pushed the feat/using branch 14 times, most recently from e0d34af to f73fe47 Compare May 30, 2025 15:02
@baseballyama baseballyama marked this pull request as ready for review May 30, 2025 15:05
@baseballyama baseballyama requested a review from ota-meshi May 30, 2025 15:05
Copy link
Contributor

@ota-meshi ota-meshi left a 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;", {
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added: 7babfd3

return isIdentifierStart(ch, true) || ch === 92 // '\'
}

pp.isAwaitUsingDeclaration = function() {
Copy link
Contributor

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.

Copy link
Contributor Author

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;", {
Copy link
Contributor

Choose a reason for hiding this comment

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

From what I understand, this is an early error, because I don't think we can have a top-level using declaration for a script. However, I think it is allowed for modules.

image

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed: 5f1f124

}]
}],
sourceType: "script"
}, {"ecmaVersion": 17});
Copy link
Contributor

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) {}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed: c395a8b

@baseballyama
Copy link
Contributor Author

@ota-meshi Thank you very much for your review!!! It was really helpful!
I have addressed all of your comments.

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()
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you! fixed: e59ede7

marijnh pushed a commit that referenced this pull request Jun 2, 2025
FEATURE: Support `using` and `await using` syntax.

Issue #1369
@marijnh
Copy link
Member

marijnh commented Jun 2, 2025

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

using and await using declarations (ES2026)
3 participants