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

📎 Implement noGlobalDirnameFilename rule #4331

Open
kibertoad opened this issue Oct 18, 2024 · 11 comments · May be fixed by #4452
Open

📎 Implement noGlobalDirnameFilename rule #4331

kibertoad opened this issue Oct 18, 2024 · 11 comments · May be fixed by #4452
Assignees
Labels
A-Linter Area: linter L-JavaScript Language: JavaScript and super languages S-Feature Status: new feature to implement

Comments

@kibertoad
Copy link

kibertoad commented Oct 18, 2024

Description

__dirname doesn't work in ESM context, but is not easily caught by tests, as e. g. vitest injects __dirname, so it works correctly there, but then fails in production. __filename is also in a similar situation.

Prior art: https://github.com/sindresorhus/eslint-plugin-unicorn/blob/main/docs/rules/prefer-module.md

Also see #4245

@ematipico ematipico added A-Linter Area: linter L-JavaScript Language: JavaScript and super languages S-Feature Status: new feature to implement labels Oct 18, 2024
@kibertoad
Copy link
Author

@ematipico I don't know Rust, so unfortunately can't implement it myself.

@ematipico
Copy link
Member

Then why was this task open? Usually, these issues are open after a previous discussion, or by maintainers.

If there wasn't prior discussion, could you please open a discussion instead?

@ematipico ematipico closed this as not planned Won't fix, can't repro, duplicate, stale Oct 18, 2024
@kibertoad
Copy link
Author

kibertoad commented Oct 18, 2024

@ematipico I've linked previous discussion, see #4245. I believe you thumbs uped it as well :D

There was an ask to open a task for it from one of the collaborators.

@ematipico ematipico reopened this Oct 22, 2024
@unvalley
Copy link
Member

unvalley commented Oct 23, 2024

We have the noCommonJs rule, but it only applies to imports and exports.

If we want to add the prefer-modules rule, I think we should break it down into a few specific rules.

  • Disallows 'use strict' directive.
    • noRedundantUseStrict done this
  • Disallows “Global Return”.
    • noGlobalReturn?
  • Disallows the global variables `__dirname and __filename.
    • noGlobalDirnameFilename this issue
  • Disallows require(…).
    • noCommonJs done this
  • Disallows exports and module.exports.
    • noCommonJs done this

@unvalley unvalley self-assigned this Oct 23, 2024
@unvalley
Copy link
Member

noGlobalLegacyPath might be better, because we have already noGlobalXXX rules.

@dyc3
Copy link
Contributor

dyc3 commented Oct 23, 2024

IMO from a user's perspective "noGlobalLegacyPath" reads like it's banning something related to paths, not global variables

@unvalley
Copy link
Member

noGlobalDirnameAndFilename? It's not cool but expressing itself directly and easy to search.

@Conaclos
Copy link
Member

Conaclos commented Oct 23, 2024

Disallows 'use strict' directive.

Is this not covered by noRedundantUseStrict?

Disallows “Global Return”.

It is already a syntax error in module. Do I miss something?

Disallows the global variables `__dirname and __filename.

I wonder, is there reason to create a dedicated rule or should we ad this to noCommonJs?

noGlobalDirnameAndFilename?

noGlobalDirnameFilename should be fine.

@dyc3
Copy link
Contributor

dyc3 commented Oct 23, 2024

should we add this to noCommonJs

From a quick search, it looks like these globals are only valid in commonjs environments. So I think yeah, it would make sense to put it in the same rule. However, things against this that I can think of is:

  • that we could be taking some granularity away from users.
  • vitest injects these variables when it runs, so these can be available in esm looking environments, IIRC.

For those reasons I think it would make sense to keep this as a separate rule, or have options for the noCommonJs rule to enable/disable these different aspects of the rule. Adding rule options would be weird IMO, we generally tend to add rules and minimize the scope of those rules as much as possible.

@unvalley
Copy link
Member

unvalley commented Oct 24, 2024

Is this not covered by noRedundantUseStrict?

It's covered! I missed it.

@Conaclos
Copy link
Member

For those reasons I think it would make sense to keep this as a separate rule, or have options for the noCommonJs rule to enable/disable these different aspects of the rule. Adding rule options would be weird IMO, we generally tend to add rules and minimize the scope of those rules as much as possible.

You made a good point. Let's keep the rule separated. We should certainly add a pointer to the new rule in noCommonJs.

@unvalley unvalley changed the title 📎 Implement noLegacyPathGlobals rule 📎 Implement noGlobalDirnameFilename rule Oct 24, 2024
@unvalley unvalley linked a pull request Nov 3, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Linter Area: linter L-JavaScript Language: JavaScript and super languages S-Feature Status: new feature to implement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants