Skip to content
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

Merged
merged 8 commits into from Feb 7, 2024

Conversation

tylerbutler
Copy link
Member

@tylerbutler tylerbutler commented Jan 5, 2024

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.

@tylerbutler tylerbutler requested review from a team as code owners January 5, 2024 01:38
@github-actions github-actions bot added area: build Build related issues base: main PRs targeted against main branch labels Jan 5, 2024
@msfluid-bot
Copy link
Collaborator

msfluid-bot commented Jan 5, 2024

Could not find a usable baseline build with search starting at CI 602b981

Generated by 🚫 dangerJS against b68f887

Copy link
Contributor

@jason-ha jason-ha left a 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.
Copy link
Contributor

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?

Copy link
Member Author

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.

Copy link
Contributor

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.

Copy link
Member Author

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

@tylerbutler tylerbutler enabled auto-merge (squash) February 7, 2024 02:21
@tylerbutler tylerbutler merged commit 0f2b8da into microsoft:main Feb 7, 2024
41 checks passed
@tylerbutler tylerbutler deleted the bt-no-js-files branch February 7, 2024 03:00
alexvy86 pushed a commit to alexvy86/FluidFramework that referenced this pull request Feb 13, 2024
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: build Build related issues base: main PRs targeted against main branch
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants