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

Only enable Node lint rules for Node files #3672

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

mcmire
Copy link
Contributor

@mcmire mcmire commented Dec 15, 2023

Explanation

In a previous commit, we mistakenly introduced a Node-specific function for testing deep equality, and this ended up crashing the extension. This would usually have been caught by our ESLint rules, as they prohibit use of Node libraries by default. However, the ESLint configuration for this repo imports rules from our @metamask/eslint-config-nodejs package and applies them to all files, marking everything as Node-compatible, thereby allowing such usage. This is incorrect: these rules should only apply to test files and scripts.

This commit fix the ESLint configuration to match. Doing so revealed a couple of categories of violations, which this commit also fixes:

  • Some packages import and make use of EventEmitter from Node's events module. We add a notice to the README for these packages which advises consumers that these packages are designed to be used in a Node-compatible environment.
  • Some packages import and make use of Node's assert module to check values at runtime. It isn't strictly necessary to use this module, and so we replace its usage with simpler code.
  • Some packages import and make use of Node's inspect utility function. We don't strictly need this either, and we replace its usage with simpler code as well.

References

This issue was brought up in our company chat.

Changelog

(No functional changes to note here)

Checklist

  • I've updated the test suite for new or updated code as appropriate
  • I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate
  • I've highlighted breaking changes using the "BREAKING" category above as appropriate

@mcmire mcmire requested a review from a team as a code owner December 15, 2023 17:43
cryptodev-2s
cryptodev-2s previously approved these changes Dec 15, 2023
Copy link
Contributor

@cryptodev-2s cryptodev-2s left a comment

Choose a reason for hiding this comment

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

Lgtm!

@Mrtenz
Copy link
Member

Mrtenz commented Dec 15, 2023

However, the ESLint configuration for this repo imports rules from our @metamask/eslint-config-nodejs package and applies them to all files, marking everything as Node-compatible, thereby allowing such usage. This is incorrect: these rules should only apply to test files and scripts.

Does this mean that other files can be incompatible with Node.js? We use some packages in Node.js for Snaps.

@mcmire
Copy link
Contributor Author

mcmire commented Dec 18, 2023

Does this mean that other files can be incompatible with Node.js? We use some packages in Node.js for Snaps.

@Mrtenz Oh, sorry for the confusion, I just meant that these rules should only apply to test files and scripts in the context of this monorepo, not as a general statement for all monorepos. For Snaps, it sounds like we'd need some specific configuration to account for Node.js-compatible packages. That would make sense to me.

@mcmire mcmire requested a review from Gudahtt December 18, 2023 17:43
@Gudahtt
Copy link
Member

Gudahtt commented Dec 18, 2023

Does this mean that other files can be incompatible with Node.js? We use some packages in Node.js for Snaps.

The base configuration should ensure things work across Node.js and browsers. We're still applying the base config everywhere, so that should ensure compatibility with Node.js.

The Node.js rules are intended for scripts solely run in a Node.js environment, like scripts and tests.

@mcmire mcmire marked this pull request as draft June 25, 2024 23:50
@mcmire
Copy link
Contributor Author

mcmire commented Jun 25, 2024

I need to revive this. I'm also unsure we need the events imports.

@mcmire mcmire force-pushed the enable-nodejs-lint-rules-for-node-files branch 2 times, most recently from 1fa7c70 to 65259c9 Compare June 26, 2024 23:28
@mcmire
Copy link
Contributor Author

mcmire commented Jun 26, 2024

I've rebased this PR. Ready for a fresh review.

@mcmire mcmire marked this pull request as ready for review June 26, 2024 23:30
@mcmire mcmire requested a review from a team as a code owner June 26, 2024 23:30
@mcmire mcmire requested a review from a team June 26, 2024 23:30
@mcmire mcmire force-pushed the enable-nodejs-lint-rules-for-node-files branch 2 times, most recently from 5484629 to 7e7a8dd Compare June 27, 2024 16:16
@mcmire mcmire force-pushed the enable-nodejs-lint-rules-for-node-files branch from 7e7a8dd to bc0e538 Compare October 23, 2024 19:08
@mcmire mcmire requested review from a team as code owners October 23, 2024 19:08
@mcmire mcmire force-pushed the enable-nodejs-lint-rules-for-node-files branch from bc0e538 to 892e3f5 Compare October 23, 2024 19:09
@mcmire
Copy link
Contributor Author

mcmire commented Oct 23, 2024

Rebased again and ready for another re-review.

In a previous commit, we mistakenly introduced a Node-specific function
for testing deep equality, and this ended up crashing the extension.
This would usually have been caught by our ESLint rules, as they
prohibit use of Node libraries by default. However, the ESLint
configuration for this repo imports rules from our
`@metamask/eslint-config-nodejs` package and applies them to all files,
marking everything as Node-compatible, thereby allowing such usage. This
is incorrect: these rules should only apply to test files and scripts.

This commit fix the ESLint configuration to match. Doing so revealed a
couple of categories of violations, which this commit also fixes:

- Some packages import and make use of EventEmitter from Node's `events`
  module. We add a notice to the README for these packages which advises
  consumers that these packages are designed to be used in a
  Node-compatible environment.
- Some packages import and make use of Node's `assert` module to check
  values at runtime. It isn't strictly necessary to use this module, and
  so we replace its usage with simpler code.
- Some packages import and make use of Node's `inspect` utility
  function. We don't strictly need this either, and we replace its
  usage with simpler code as well.
@mcmire mcmire force-pushed the enable-nodejs-lint-rules-for-node-files branch from 892e3f5 to 5f1f877 Compare October 23, 2024 19:56
@mcmire mcmire requested a review from a team as a code owner October 23, 2024 19:56
@mcmire mcmire self-assigned this Nov 8, 2024
mikesposito
mikesposito previously approved these changes Nov 11, 2024
Copy link
Member

@mikesposito mikesposito left a comment

Choose a reason for hiding this comment

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

Looks good!

@mcmire mcmire enabled auto-merge (squash) November 11, 2024 15:24
`NetworkController state is invalid: \`selectedNetworkClientId\` ${inspect(
state.selectedNetworkClientId,
)} does not refer to an RPC endpoint within a network configuration`,
// False negative - this is a string.
Copy link
Member

Choose a reason for hiding this comment

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

Huh. I can reproduce this locally as well. Strange bug.

I don't see any reports of this on the @typescript-eslint repo. Maybe this is caused by us using an incompatible version of TypeScript though.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I'd be curious how the situation is once we upgrade.

@mcmire mcmire disabled auto-merge November 11, 2024 18:33
Gudahtt
Gudahtt previously approved these changes Nov 11, 2024
Copy link
Member

@Gudahtt Gudahtt left a comment

Choose a reason for hiding this comment

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

LGTM!

@mcmire mcmire dismissed stale reviews from Gudahtt and mikesposito via 557c1a4 November 15, 2024 22:09
@mcmire mcmire requested a review from a team as a code owner November 15, 2024 22:09
@mcmire
Copy link
Contributor Author

mcmire commented Nov 15, 2024

Fixed conflicts and responded to feedback above. Ready for another review.

Gudahtt
Gudahtt previously approved these changes Nov 19, 2024
@mcmire mcmire enabled auto-merge (squash) November 21, 2024 20:34
@mcmire mcmire disabled auto-merge November 25, 2024 16:05
@mcmire mcmire enabled auto-merge (squash) November 25, 2024 16:06
@mcmire mcmire disabled auto-merge November 25, 2024 16:06
@mcmire mcmire enabled auto-merge (squash) November 25, 2024 16:06
@mcmire
Copy link
Contributor Author

mcmire commented Nov 26, 2024

Merge conflicts fixed.

Copy link
Member

@Gudahtt Gudahtt left a comment

Choose a reason for hiding this comment

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

LGTM!

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.

5 participants