-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Suggestion: new rule to enforce usage of node:
prefix for Node.js built-ins
#2717
Comments
Why? What's the advantage to using the |
I updated the description with a few benefits I found in the web. Let me know if that's sufficient. |
The third point is already true for unprefixed core modules. You're correct that |
In my opinion, the upside of forcing the prefix is that it allows a more unique name to be chosen, without worrying about naming clashes with a package that is in |
That's certainly a benefit for node core, but i'm not sure how that'd be a benefit for everyone else? node already only adds core modules when they've reserved the package name on npm. |
Agreed. Will be looking forward to this rule. Or, if anyone has snippets for I think another plus for forcing the use of the |
This is a node project, as is eslint, and I'm not concerned with deno's incomplete support of node modules. |
All node documentation uses prefixed imports. Since it becomes the standard why keep two ways of importing? I think this rule would help with that. |
It's not "the standard", it's just that some node collabs want to push usage of the prefix. It's not actually better imo, altho I'm still evaluating the bullet points in the OP. |
I'm very much in favor of this rule, for the reasons stated above. Obviously it's a good idea to subject all rule proposals to some level of scrutiny to avoid feature creep. IMHO in this case there are enough benefits, even if not everybody may want to activate the rule.
This is an important one. It reduces cognitive burden for the developers.
Can you cite a source for that claim? The Node.js docs state the following:
This (to me) implies the cache isn't bypassed when unprefixed.
That's subjective, and even so, having a
This surprises me, since the README really implies this is a project for the whole of JS, not just Node — there's even talk about bundlers; and the
While I doubt Node.js will ever phase out the unprefixed imports due to backwards compatibility, I agree that newer code should certainly try to keep up with Node.js best practices.
AFAIK stuff like this goes through voting processes and the like, so it's as close to a standard as we're gonna get. |
You can tell by running "The whole of JS" is just node, for the tooling ecosystem. That may change in the future but it's certainly not anywhere close to changing yet. |
Additional points: Btw, the PR has a rule for forcing the use of the UPD: just saw your comment there, lol 😁 |
I just learned about the
I'm unsure if I understand this point. Please clarify if the following misses the point. While the tooling lives in To get back to context: from my perspective, the quote above was prompted by mention of Of course, any tool developer is free to limit the scope of their creation. However, the proposed rule seems to fit perfectly into this plugin, and it could just be opt-in as most rules here – available to the subset of users that want this functionality. |
I mean, what good reason is there to not prefix node std with |
@agent-ly for one, it doesn't work as cleanly with all tools yet, and it doesn't work on older node versions. |
This eslint rule is being discussed but not yet implemented -- import-js/eslint-plugin-import#2717. For the moment, we can just forbid the entire node standard library without prefix.
In my opinion, whether or not the prefix is useful is irrelevant here. What matters is that it exists and people may want to enforce consistency of either always using the prefix or never using the prefix. This plugin seems like a fine place to put such a rule. |
I would agree in a general sense, but I consider using the prefix to be a bad practice, and I'm very hesitant to have this plugin enable a bad practice at scale. |
Why do you consider it a bad practice? Does it not being 100% supported make it a bad practice? There isn't a single (supported) NodeJS version that doesn't support If what makes this a bad practice is it not being ~100% supported, then will it stop being a bad practice once it's closer to 100% support? Doesn't make much sense to me... |
This comment was marked as spam.
This comment was marked as spam.
@zettca no, support level has very little correlation to whether something is a good or bad practice; |
The The import plugin seems like the most logical place to add such a rule. @ljharb it sounds, for example, like you might want to set it to |
That is certainly a viable path forward, but I’m still not excited about even allowing “always” at all. |
There is a existing rule in eslint-plugin-unicorn. |
Adding a use case to support Reading through this, I don't see any argument for not using that format other than @ljharb dislikes it. If there is one, I would love to see an article or explanation as to what issues it introduces. And in that case, I would still like to see a rule for |
@Sivli-Embir your tool would need to be able to determine non-prefixed core modules regardless, since they could (and almost certainly do) exist in dependencies. This is trivial based on https://npmjs.com/is-core-module, so I don't think your use case holds up. |
@ljharb I have to disagree on that point. Adding another npm module increases our maintenance and security burden regardless of size or complexity, where a eslint rule is an elegant fix for us. We only scan our source files, not anything that lives in node_modules. Regardless, that misses the point I was trying to make. We could easily solve our issues in several ways, but the way we wish to solve it is the one I provided. I was only trying to provide a data point that this is more than for/against preference issue, and there are practical cases where an unopinionated rule would provide value. If this project shipped a |
That's because editors (I'm talking about VSCode, but very likely it's the LSP, so very likely all editors) used to add unprefixed imports in the past, however they recently (sometime in the past year) started to add prefixed imports. Often you just type the function or class you want to use and let the editor add the imports for you. I'm now getting both options in the fix menu (ctrl + dot), but the first one is the prefixed one, and I think if you let it add all missing imports automatically, they will always be prefixed. |
I think that whether imports are generated by an editor, copied from a code sample, or written by hand is another matter. We don't control these details of how our team authors code, only what gets checked-in, through automated jobs. I thought for sure I could configure my lint job to enforce consistency with eslint-plugin-import, since we're already using that. It's dissatisfying to have to configure another eslint plugin in conjunction with eslint-plugin-import, when the name of this plugin suggests it would be a comprehensive solution to linting imports. |
There is a clear concensus in the community in favor of having this rule. And there are two clear real world use cases which must be enforced by this rule. What's the drawback of having it implemented in this package? |
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as off-topic.
This comment was marked as off-topic.
Actually, there are a few dropbacks:
|
The suggestion is to make the rule dynamic, with a "never" and "always" option (at least) |
Unfortunately, making this rule dynamic and configurable is also not that easy. Remember, there are also Deno, Bun (uses Along with my original reply, these are reasons behind I rejected @benmccann's proposal (un-ts#97) of introducing |
I suppose the sole purpose of this feature is to avoid such cases, no? Like having a single configurable way to import built-in modules for the project |
In the context of Bun, you can/have to use both Node.js compatible API ( |
But you shouldn't be able to import it as |
Yeah, you can't (Due to being compatible with Node.js). |
So, this rule shouldn't conflict with bun then. Since the implementation would only check about node builtin modules anyway. |
i know it, but the sole purpose of ESLint itself is to restrict what dev can do to what he/she should do where it makes sense what are the cases when you want to specify both if the project should be runtime-agnostic there should be no prefixes to begin with. if project uses Bun-specific API then what is the motivation to use |
This change disables eslint-plugin-import's import resolution check, which doesn't understand the 'npm:' prefix required by Observable. Refs #31, c9d0f50, import-js/eslint-plugin-import#2717
If this rule is added it should have exceptions for |
fwiw, this is also a Biome linter's default(recommended) enabled rule https://biomejs.dev/linter/rules/use-nodejs-import-protocol/ |
I know the nodejs SQLite module is in active development, but 😬
|
indeed, there's now |
I searched a bit in the existing issues and didn't see someone asking for anything similar, also couldn't find in the existing rules. I think this could belong to this package.
Benefits (copied this from 3rd party resources, and added a few personal touches):
node:test
.The text was updated successfully, but these errors were encountered: