Skip to content

[naming-convention] Add the ability to lint imports #2106

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

Closed
vkrol opened this issue May 25, 2020 · 7 comments · Fixed by #7269
Closed

[naming-convention] Add the ability to lint imports #2106

vkrol opened this issue May 25, 2020 · 7 comments · Fixed by #7269
Labels
accepting prs Go ahead, send a pull request that resolves this issue enhancement: plugin rule option New rule option for an existing eslint-plugin rule package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@vkrol
Copy link
Contributor

vkrol commented May 25, 2020

Hello!

It's not a bug report, it's a feature request.

Repro

{
  "rules": {
    "@typescript-eslint/naming-convention": [
      "error",
      { "selector": "variableLike", "format": ["camelCase"] }
    ]
  }
}
import * as Foo from "./foo";

Expected Result
The error.

Actual Result
No error.

Additional Info
N/A.

Versions

package version
@typescript-eslint/eslint-plugin 3.0.0
@typescript-eslint/parser 3.0.0
TypeScript 3.9.3
ESLint 7.1.0
node X.Y.Z
npm X.Y.Z
@vkrol vkrol added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels May 25, 2020
@bradzacher
Copy link
Member

This definitely seems reasonable to check all imports.

I think the following would be good as a new selector: import.
Probably best to check both import namespace and import default.

@bradzacher bradzacher added enhancement: plugin rule option New rule option for an existing eslint-plugin rule and removed triage Waiting for team members to take a look labels May 26, 2020
@vkrol
Copy link
Contributor Author

vkrol commented May 26, 2020

Probably best to check both import namespace and import default.

I don't think it's a good idea to check default imports, because it is most likely that there will be no single rule for all default imports. In some cases, you will need PascalCase (classes) and in some cases you will need camelCase (functions).

@bradzacher
Copy link
Member

I would say the same about namespace imports. I think for the most part, the name of the thing depends on the name of the module you're importing.

For example, a lot of our modules names are strictCamelCase, so we import namespaces as such, but at FB it's a react codebase, so you have a lot of PascalCase files (and import namespace as such).

We can always use the modifier to distinguish between namespace and default should you want to apply different formatting to both.

Also worth noting that you can apply multiple format options to a selector if you want to allow either. This mostly just means you can ban unnderscores or consecutive capitals.

@vkrol
Copy link
Contributor Author

vkrol commented May 26, 2020

We can always use the modifier to distinguish between namespace and default should you want to apply different formatting to both.

If it will be possible to distinguish between default imports and namespaces, then no problem. Thanks!

@phaux
Copy link
Contributor

phaux commented Aug 7, 2020

For default import there's already a PR to eslint-plugin-import for the rule which enforces naming the import the same as export. I could also imagine somebody could want to name the default import after the filename, but that's also a better fit for eslint-plugin-import. I don't see any other reason for linting names of default imports, unless you actually want to always rename them, which is weird.

@bradzacher
Copy link
Member

Revisiting this - I'm not sure if it 100% makes sense for this to exist in the naming-convention rule.

On the one hand - it's trivial to add this to the rule, and you can validate that the name adheres to a format.

On the other hand - IMO the name of an import should be directly related to the thing being imported. I.e. it should be the filename (import * as foo from './foo', import foo from './foo').
This logic is not really possible to add to the rule itself.

Is there enough value in just ensuring the import name matches a format?

@vkrol
Copy link
Contributor Author

vkrol commented Nov 25, 2020

Is there enough value in just ensuring the import name matches a format?

I think yes, because this

IMO the name of an import should be directly related to the thing being imported. I.e. it should be the filename

is very opinionated (although I support this viewpoint) and in some teams the naming style may differ.

@JoshuaKGoldberg JoshuaKGoldberg added the accepting prs Go ahead, send a pull request that resolves this issue label Oct 25, 2021
@bradzacher bradzacher changed the title [naming-convention] Add the ability to lint namespace imports [naming-convention] Add the ability to lint imports Mar 22, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 25, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepting prs Go ahead, send a pull request that resolves this issue enhancement: plugin rule option New rule option for an existing eslint-plugin rule package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
4 participants