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
feat(check:policy): Prevent .js file extension #19106
Conversation
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 like it. :)
| /** | ||
| * A policy handler that checks for JavaScript source files that just use the .js file extension. Such files may be | ||
| * interpreted by node as either CommonJS or ESM based on the `type` field in the nearest package.json file. However | ||
| * this is not deterministic. Using file extensions reduces ambiguity and makes the behavior deterministic. |
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.
What is not deterministic? You just mean that we might change the package type?
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 mean that you can break things that are seemingly unrelated by adding type: module to a package. It's not obvious that adding a field to a package.json would cause an unrelated config file like .eslintrc.js to stop being interpreted correctly.
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.
Doesn't seem not related. That is the system.
Brings to mind the missing fluid build dependency on package.json. Change package.json as you like. Build cache doesn't care.
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.
Clarified the comment. I understand that this is node's behavior, but it is not obvious, and it's also new(ish).
build-tools/packages/build-tools/src/repoPolicyCheck/handlers/noJsFiles.ts
Outdated
Show resolved
Hide resolved
build-tools/packages/build-tools/src/repoPolicyCheck/handlers/noJsFiles.ts
Outdated
Show resolved
Hide resolved
Adds a policy handler that checks for JavaScript source files that just use the .js file extension. Such files may be interpreted by node as either CommonJS or ESM based on the `type` field in the nearest package.json file. However, this is can lead to confusing behavior when updating packages to use `type`. Using explicit file extensions reduces ambiguity and ensures a CJS file isn't suddenly treated like an ESM file.
Adds a policy handler that checks for JavaScript source files that just use the .js file extension. Such files may be interpreted by node as either CommonJS or ESM based on the
typefield in the nearest package.json file. However, this is can lead to confusing behavior when updating packages to usetype. Using explicit file extensions reduces ambiguity and ensures a CJS file isn't suddenly treated like an ESM file.