-
-
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
no-relative-parent-imports over-reporting when import/resolver = typescript #1392
Comments
the point of the rule is about resolved parent imports, not aliased ones - if all you want is to block I think it would work the same with a webpack alias. |
What TS parser are you using? I don't think it's the resolver, I think it's the TS parser rewriting the path before the lint rule runs. |
@benmosher |
I'm not sure what you mean. I think the parser always runs before the lint rule. The parser would need to choose not to process the paths first. I am not 100% sure it's doing this but when I looked at the code for |
Sorry, I just double-checked and I must have looked in the wrong place before. It is resolving the path inside the rule which means it is the plugin's problem. |
wait, @ljharb, by this
you mean that you think this rule shouldn't only fire on import paths that start with |
@benmosher no, what i mean is, the path you type in your code shouldn't matter - only what it resolves to should matter. |
@ljharb This is not a great option when extending a config such as And while some might be using this rule to prevent resolution of imports higher in the tree, I just want to prevent the use of |
@smably you're correct about the difficulty there; you could write your own rule using eslint-rule-composer and no-restricted-syntax, though.
|
It may be true that the path is equally "fragile" on a rename or move but if the paths are absolute (or pseudo-absolute by using an alias), when you move a file you have a much stronger understanding of where that path moved to. Take for example the two pieces of code: Relative import addTwoNumbers from '../addToNumbers'; Aliased import addTwoNumbers from '@backend/utils/addToNumbers'; Say i now move the function the new code will be Aliased Updated import addTwoNumbers from '@commons/utils/addToNumbers'; Both of the relative and aliased paths will require an update of course but I can now do a find and replace in my editor for However, I do understand if this is the wrong rule for this desired outcome. |
Yes - I think you want a custom rule that forces every specifier to be bare - ie, to forbid relative paths entirely. |
just ran into the same issue. an easy solution maybe: |
The unfortunate thing about the workaround in #669 is that it doesn't allow adding custom error messages, because |
I'm running into an issue that |
I would expect both to resolve to the same thing if the typescript resolver is set up properly. |
@ljharb they do both resolve to the same thing and import correctly at runtime. But one is reported as an error by ESLint when |
Yes, I agree with you. |
It was enabled to ban imports with '../'. In fact, the rule restricts all parent imports: import-js/eslint-plugin-import#1392
It was enabled to ban imports with '../'. In fact, the rule restricts all parent imports: import-js/eslint-plugin-import#1392
It was enabled to ban imports with '../'. In fact, the rule restricts all parent imports: import-js/eslint-plugin-import#1392
Any news for this problem? It is prohibiting the use of the |
Here is how I implemented this on a NestJS project with Typescript: module.exports = {
rules: {
'no-restricted-imports': ['error', {'patterns': ['..*']}],
},
}; It prevents any import statement that references the parent dir as relative, forcing the use of an alias. I didn't figure out how to add a custom message. |
Here's an example of a custom message (docs): module.exports = {
rules: {
'no-restricted-imports': [
'error',
{
patterns: [
{
group: ['../*'],
message: 'Usage of relative parent imports is not allowed.',
},
],
},
],
},
}; |
close in favor of https://github.com/import-js/eslint-import-resolver-typescript Feel free to comment if it still occurs. |
@JounQin this one seems to already be using the TS resolver |
Hah, sorry to misread the thread. But there is no reproduction was provided, so it's hard to tell what was the cause then. |
I have been using the following tslint rule:
to forbid any relative imports such as
import constants from '../constants'
I am trying to migrate my tslint rules to eslint, and it seems like the rule
import/no-relative-parent-imports
should be what I am looking for. However, when I turn the rule on, all of my imports that are path rewrites in my tsconfig file report errors. See image below.I have the following
tsconfig.json
And the following
eslintrc.js
...
When I log out the input before the error is reported here, I see the following paths getting reported:
It seems as though the typescript import/resolver is translating the paths before they are applied to this eslint check. Is there a way that I can configure my eslint to work with this use case? or is this not yet possible
The text was updated successfully, but these errors were encountered: