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

[New] add enforce-node-protocol-usage rule #3024

Merged
merged 1 commit into from
Dec 11, 2024

Conversation

GoldStrikeArch
Copy link

@GoldStrikeArch GoldStrikeArch commented Jul 4, 2024

Fixes #2717
Added a new rule prefer-node-builtin-imports.
All test cases are taken from eslint-plugin-unicorn_test

…` setting

Co-authored-by: Mikhail Pertsev <mikhail.pertsev@brightpattern.com>
Co-authored-by: sevenc-nanashi <sevenc7c@sevenc7c.com>
@GoldStrikeArch GoldStrikeArch force-pushed the feat/prefer-node-protocol branch from d728d5c to 968e562 Compare July 4, 2024 08:51
config/recommended.js Outdated Show resolved Hide resolved
docs/rules/prefer-node-builtin-imports.md Outdated Show resolved Hide resolved
@ljharb ljharb marked this pull request as draft July 4, 2024 13:48
@ljharb
Copy link
Member

ljharb commented Oct 3, 2024

@GoldStrikeArch are you still interested in completing this PR?

@GoldStrikeArch
Copy link
Author

@GoldStrikeArch are you still interested in completing this PR?

Yeah, forgot about this. I will try to finish it on this weekend

@sevenc-nanashi
Copy link
Contributor

Any updates on this PR? I'm very excited about this rule.

(I'd like to take over this PR if needed)

@ljharb
Copy link
Member

ljharb commented Dec 4, 2024

@sevenc-nanashi if you'd like to do so, please do NOT make a new PR - instead, comment here with a link to your branch containing this PR, rebased and updated per #3024 (comment), and i'll pull in the changes.

@sevenc-nanashi
Copy link
Contributor

sevenc-nanashi commented Dec 5, 2024

@ljharb
I rebased and added some changes according to the review:
https://github.com/sevenc-nanashi/eslint-plugin-import/tree/feat/prefer-node-protocol?tab=readme-ov-file

  • This rule now accepts always or never option.
  • Removed this rule from recommended option.
  • Fixed tests.

I think the name should be prefer-node-protocol, as the name prefer-node-builtin-imports sounds like preferring using node's builtin over other packages.

@ljharb
Copy link
Member

ljharb commented Dec 7, 2024

@sevenc-nanashi i don't think it should have "prefer" in it at all, because the rule won't be expressing an opinion that using, or avoiding, the protocol is preferred, but I agree that "node protocol" is better wording.

Perhaps enforce-node-protocol-usage?

@sevenc-nanashi
Copy link
Contributor

@ljharb I renamed to enforce-node-protocol-usage.

@ljharb ljharb changed the title feat: added prefer-node-builtin-imports rule [New] add enforce-node-protocol-usage rule Dec 8, 2024
@ljharb
Copy link
Member

ljharb commented Dec 8, 2024

The rule should either default to “never” - the far more universal behavior - or have no default at all.

The docs will need a lot of work too - deno and hun only support the protocol, i believe, so you’d want to set the rule to “always”; if you’re supporting a node version, bundler, or tool that doesn’t understand the node protocol, you’d want to set it to “never”.

Additionally, prefix-only core modules, like node:test, node:sea, node:sqlite, etc need to remain unaffected by the “never” setting. Although, because module.builtinModules doesn’t include prefix-only modules, you may not need to do anything here but add test cases.

@sevenc-nanashi
Copy link
Contributor

@ljharb
I made these changes:

  • The rule will throw error when the option is not specified
  • Added test cases that require("node:test") is valid in 'never'

@ljharb ljharb force-pushed the feat/prefer-node-protocol branch from 968e562 to e897498 Compare December 8, 2024 22:49
@ljharb
Copy link
Member

ljharb commented Dec 8, 2024

k, i've pulled in and rebased the changes. I also made it use is-core-module since module.builtinModules isn't available in older node versions.

@ljharb
Copy link
Member

ljharb commented Dec 8, 2024

@sevenc-nanashi i haven't reviewed the tests yet; assuming that every module being tested is tested with both configurations, and that eg node:test and node:sea are tested, and that tests pass, then this should be good.

@ljharb ljharb marked this pull request as ready for review December 8, 2024 22:51
@ljharb ljharb force-pushed the feat/prefer-node-protocol branch from e897498 to 8b62c8b Compare December 8, 2024 22:52
@sevenc-nanashi
Copy link
Contributor

@ljharb I made some changes:

@ljharb ljharb force-pushed the feat/prefer-node-protocol branch from 8b62c8b to 7dccf82 Compare December 9, 2024 06:19
@ljharb
Copy link
Member

ljharb commented Dec 9, 2024

hmm, lots of the tests are failing for me locally, but i'm not sure why. the logic seems correct.

@ljharb ljharb marked this pull request as draft December 9, 2024 06:20
@sevenc-nanashi
Copy link
Contributor

@ljharb Looks like the issue caused by the guard of visitor.

The fix
diff --git a/src/rules/enforce-node-protocol-usage.js b/src/rules/enforce-node-protocol-usage.js
index f8f3ee39..548f303b 100644
--- a/src/rules/enforce-node-protocol-usage.js
+++ b/src/rules/enforce-node-protocol-usage.js
@@ -96,11 +96,16 @@ module.exports = {
       url: docsUrl('enforce-node-protocol-usage'),
     },
     fixable: 'code',
-    schema: [
-      {
-        enum: ['always', 'never'],
-      },
-    ],
+    schema: {
+      type: 'array',
+      minItems: 1,
+      maxItems: 1,
+      items: [
+        {
+          enum: ['always', 'never'],
+        },
+      ],
+    },
     messages,
   },
   create(context) {
@@ -115,18 +120,12 @@ module.exports = {
         return checkAndReport(arg, context);
       },
       ExportNamedDeclaration(node) {
-        if (!isStringLiteral(node)) { return; }
-
         return checkAndReport(node.source, context);
       },
       ImportDeclaration(node) {
-        if (!isStringLiteral(node)) { return; }
-
         return checkAndReport(node.source, context);
       },
       ImportExpression(node) {
-        if (!isStringLiteral(node)) { return; }
-
         return checkAndReport(node.source, context);
       },
     };

In my environment, all tests passed with this patch.

@ljharb ljharb force-pushed the feat/prefer-node-protocol branch 2 times, most recently from 3459368 to 165fb3b Compare December 9, 2024 21:38
@sevenc-nanashi

This comment was marked as off-topic.

@ljharb

This comment was marked as off-topic.

@ljharb ljharb force-pushed the feat/prefer-node-protocol branch 4 times, most recently from d11494f to 1f38d1b Compare December 10, 2024 00:21
@ljharb
Copy link
Member

ljharb commented Dec 10, 2024

So one thing to note: the list of core modules is dependent on the node version that eslint is running in.

I think perhaps it might make sense to add a settings option for "node version" that this rule uses, that will override the current eslint node version (isCoreModule has that API already). Thoughts?

@ljharb ljharb force-pushed the feat/prefer-node-protocol branch from 1f38d1b to ad0d6f5 Compare December 10, 2024 06:30
@ljharb
Copy link
Member

ljharb commented Dec 10, 2024

ok, updated. lmk what yall think

@ljharb ljharb force-pushed the feat/prefer-node-protocol branch 2 times, most recently from 8d2e510 to d7bafc3 Compare December 10, 2024 07:43
@ljharb ljharb force-pushed the feat/prefer-node-protocol branch 2 times, most recently from 6e08655 to a77f197 Compare December 10, 2024 23:02
Copy link

codecov bot commented Dec 10, 2024

Codecov Report

Attention: Patch coverage is 86.27451% with 7 lines in your changes missing coverage. Please review.

Project coverage is 95.10%. Comparing base (d5f2950) to head (8c8058c).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/rules/enforce-node-protocol-usage.js 86.27% 7 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3024      +/-   ##
==========================================
+ Coverage   94.72%   95.10%   +0.37%     
==========================================
  Files          83       84       +1     
  Lines        3583     3634      +51     
  Branches     1252     1279      +27     
==========================================
+ Hits         3394     3456      +62     
+ Misses        189      178      -11     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@ljharb ljharb force-pushed the feat/prefer-node-protocol branch from a77f197 to 8c8058c Compare December 10, 2024 23:52
@ljharb ljharb marked this pull request as ready for review December 11, 2024 04:18
@ljharb ljharb merged commit 8c8058c into import-js:main Dec 11, 2024
340 of 341 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

Suggestion: new rule to enforce usage of node: prefix for Node.js built-ins
5 participants