Skip to content

Conversation

ota-meshi
Copy link
Contributor

close #1376

I think it's still under discussion how to design the option, but I tried implementing it.
I think the test cases included in this PR will be useful for discussion.

@marijnh
Copy link
Member

marijnh commented Jun 11, 2025

I don't think we should go against the ESTree spec on what Program.sourceType can hold. I think setting that to "script" when the option is set to "commonjs" makes more sense.

@ota-meshi
Copy link
Contributor Author

Thank you for checking this PR! I changed Program.sourceType to script when option is commonjs.

@RReverser
Copy link
Member

This looks pretty good to me. The only other thing that comes to mind is it might be worth running all the sourceType: "script" tests in CommonJS mode as well, as for most of them it should be true.

@ota-meshi
Copy link
Contributor Author

Thank you for checking this PR!
I modified test/run.js to run all the sourceType: "script" tests and the same commonjs tests.
e610dd2
(Please let me know if this method should be avoided.)

After adding tests, I noticed that when using commonjs with allowAwaitOutsideFunction, the top-level await couldn't be parsed, so I fixed that.
But maybe allowAwaitOutsideFunction should be ignored for commonjs. What do you think about this?

@marijnh
Copy link
Member

marijnh commented Jun 12, 2025

But maybe allowAwaitOutsideFunction should be ignored for commonjs. What do you think about this?

It might be reasonable to raise an error when allowAwaitOutsideFunction is set to true with moduleType: "commonjs". Silently ignoring it when it is explicitly set seems too confusing.

@ota-meshi
Copy link
Contributor Author

Thank you for the comment! I changed it to reject the combination of allowAwaitOutsideFunction and moduleType: "commonjs".

@marijnh marijnh merged commit 8bcc897 into acornjs:master Jun 13, 2025
2 checks passed
@marijnh
Copy link
Member

marijnh commented Jun 13, 2025

Thanks, this looks good now.

@@ -137,6 +137,9 @@ export function getOptions(opts) {
if (isArray(options.onComment))
options.onComment = pushComment(options, options.onComment)

if (options.sourceType === "commonjs" && options.allowAwaitOutsideFunction)
throw new Error("Cannot use allowAwaitOutsideFunction with sourceType: commonjs")

Copy link
Contributor

Choose a reason for hiding this comment

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

Should we also throw if allowReturnOutsideFunction: false is used with sourceType: "commonjs"?

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.

Feature Request: Add option to parse CommonJS
4 participants