-
Notifications
You must be signed in to change notification settings - Fork 30k
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
doc: add spec for contains module syntax #52059
doc: add spec for contains module syntax #52059
Conversation
Review requested:
|
doc/api/esm.md
Outdated
> * `import` statement (static only, _not_ dynamic `import()`) | ||
> * `export` statement | ||
> * `import.meta` | ||
> * `await` at the top level | ||
> * A lexical redeclaration of any of the CommonJS wrapper variables | ||
> (`require`, `exports`, `module`, `__filename`, `__dirname`) at the top | ||
> level |
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.
> * `import` statement (static only, _not_ dynamic `import()`) | |
> * `export` statement | |
> * `import.meta` | |
> * `await` at the top level | |
> * A lexical redeclaration of any of the CommonJS wrapper variables | |
> (`require`, `exports`, `module`, `__filename`, `__dirname`) at the top | |
> level | |
> * A static `import` statement. | |
> * An `export` statement. | |
> * Usage of the `import.meta` meta property. | |
> * An `await` expression at the top level. | |
> * A lexical redeclaration of any of the CommonJS wrapper variables | |
> (`require`, `exports`, `module`, `__filename`, `__dirname`) at the top | |
> level. |
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's important to clarify that this is only for import statements and not expressions.
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.
Note that [[HasTLA]]
is already an ECMAScript module field - https://tc39.es/ecma262/#sec-cyclic-module-records.
I worry about tying this detection design to the exact implementation, as opposed to defining what it is checking. Do you think it would be equivalent to claim the detection is the following:
What I prefer about this definition is that we are fully tying it to the exact check rules, as opposed to tying it to all the minutia of JS parser error behaviour. |
Yes, I think that would be equivalent. What steps would you replace with that text? I also think it should say “static |
I think implementers of all people are the least likely to miss that nuance. |
If that is the case, I'd suggest we replace the existing steps with the steps in #52059 (comment) to remove dependence on ECMA-262 parsing error behaviours (which may also change over time). |
So basically, to flip it around to not depend on a CJS parse in future here, and instead ensure the ESM path is the performance optimized one:
Then we can effectively leave "error reporting" itself as undefined for tooling and bundlers, since every environment can handle their own errors, so we don't need to strictly define that. By allowing that to be a grey area, this should allow us to switch between CJS-first and ESM-first parsing detections I believe? But I may also have missed something so let me know your thoughts. |
doc/api/esm.md
Outdated
@@ -1124,6 +1124,21 @@ _isImports_, _conditions_) | |||
> 1. Throw an _Invalid Package Configuration_ error. | |||
> 4. Return the parsed JSON source of the file at _pjsonURL_. | |||
|
|||
**CONTAINS\_MODULE\_SYNTAX**(_source_) |
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.
Perhaps DETECT_MODULE_SYNTAX
to capture that this is the "detection path".
Well our implementation has to parse as cjs first in order to avoid performance regression. If we define the spec as parsing as esm first then we'll be out of compliance, because ambiguous code that can parse as either would then need to run in strict mode per the spec. |
The compliance says nothing about error handling - only when to treat it as ESM or CJS. Ambiguous code always executes as CJS, regardless of parsing. So where is the non-compliance here? |
I guess you’re right; I was thinking of code like |
We instead frame things towards ESM being the optimized parse case, where CJS is an accommodation for the current parsing approach, so that we can optimize ESM as the primary parse in future. We could even document that the reason for the CJS variable handling is to allow for the CJS parsing preference today but that it is designed to work both ways? |
f969dfb
to
931460f
Compare
@guybedford I implemented your suggestion, please let me know what you think. |
Landed in f1949ac |
PR-URL: nodejs#52059 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Guy Bedford <[email protected]>
PR-URL: #52059 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Guy Bedford <[email protected]>
PR-URL: #52059 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Guy Bedford <[email protected]>
This PR updates the ESM resolution spec to correct the language about how we do the “contains module syntax” check.
@nodejs/loaders @guybedford