-
-
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
[New]: add rule default-import-match-filename
#1476
base: main
Are you sure you want to change the base?
Conversation
Co-Authored-By: Chiawen Chen <[email protected]> Co-Authored-By: Armano <[email protected]> Co-Authored-By: Sharmila <[email protected]>
86d60cf
to
dcb5956
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this rule should also support a list of exceptions/aliases; ie, "fooBar" is allowed anywhere "foo" would otherwise be required
.
What does the "exception" option looks like? // When "name" and "path" both globly match an item in whitelist, the error is ignored.
{
whitelist: [
{
name: '*', // any name is allowed
path: '*/models/*',
},
{
name: 'reactDomSever',
path: 'react-dom/sever',
},
];
} |
dcb5956
to
4800988
Compare
4800988
to
217ec29
Compare
|
||
#### `ignorePaths` | ||
|
||
Set this option to `['some-dir/', 'bb']` to ignore import statements whose path contains either `some-dir/` or `bb` as a substring. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this support globs, and that should be in the example?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems that globs is not a good tool against relative paths.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you elaborate? it seems like a good tool to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To perform a glob matching a cwd is required, for example:
// Using a hypothetical glob library
glob.match({
pattern: 'tests/**/*.js'
target: '/home/john/project-a/tests/a.js'
cwd: '/home/john/project-a'
})
// true
What should the cwd be in this rule? The first thought is to use the location of eslintrc
as cwd, but it is problematic when there is another eslintrc
in a subdirectory, or when overrides
is used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the cwd for any eslint rule is always process.cwd()
, since you can't be sure where the config file is located.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using process.cwd()
taken as the cwd means that the rule will give different result depending on the cwd of the shell, is that fine?
// option for this rule
{
ignoredPaths: ['tests/**/*.js']
}
project-root$ npx eslint . # will ignore files in tests/
project-root/tests$ npx eslint . # will **not** ignore files in tests/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, that seems fine to me.
function isAncestorRelativePath(path) { | ||
return ( | ||
path.length > 0 && | ||
path[0] !== '/' && |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
path[0] !== '/' && | |
!path.startsWith('/') && |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
path[0] !== '/' && | |
(path[0] !== '/' || path[0] !== '\\') && |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On a windows machine, an absolute path would start with c://
, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, you can use C:\
or C:/
and all duplicated slashes and backslashes are reduced than C://
or C:////////////
is working,
you can also mix them up: C:/\/\/\/\
but who would do that
V:\> cd C:/\/\/\/\/\/\
C:\>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the test cases for absolute paths passes, I will leave the implementation as is.
https://github.com/benmosher/eslint-plugin-import/blob/64c6999a4627d2ea70fbb174194d66fc8b6af699/tests/src/rules/default-import-match-filename.js#L55-L57
ping @golopot, it'd be great to get this in :-) |
4c48012
to
b32abff
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be helpful for users to be able to specify that the binding should be in camelCase, or snake_case, so it won't conflict with the camelCase rule?
Let's add test cases that import from foo_bar
as well as fooBar
, for both cases.
Currently the rule is case-insesative, and underscores are ignored. Variable name |
d60737d
to
830a76b
Compare
I am having trouble in designing the option
|
Typically, process.cwd() is the current dir - because a rule can't know if its config came from a file (or which one), or from command line config, or an inline comment. With option 1, however - could that resolved path have |
It should be doable using the function path.relative(). That sounds like option 3 in #1476 (comment)? |
Yes, option 3 sounds right to me. |
Looking forward to this rule. Is there any autofix provided? Or not for now ? |
It would be great to see a little bit more control over how this rule operates in the parserOptions. For example, the ability to make this case sensitive or the ability to whitelist which characters are ignored.
For example:
pass - edit: typo |
@Pearce-Ropion while the casing should ideally match, remember that not all filesystems are case-sensitive. |
Of course, thats why I suggested it as an optional setting. My reasoning here that some people have the luxury of knowing what systems their code is being developed on (for example, every engineer at my company is required to use a linux machine). Also, after having a glance at the code, it looks like the Suggested changes snippet
|
It will be awesome to have this rule 👍 Let's merge this thing 🚀 |
I think that should be part of this. When would you ever want to enforce this for an import name and not the export name, granted it's an internal module. |
Does this need further work or is it done? |
It needs further work, as far as I'm aware. I'll mark it as a draft. |
I'm also working on an NPM package. But is quite some work because you need lots of options for customization. Will write when it's published. |
Is there any update on this? It would be great to have this as part of |
Closes #325 .
toLowercase
, and striping extensions, and striping characters "._-".notnow checked.