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

fix(eslint-plugin): [no-restricted-imports]: report type-only imports properly #3996

Merged

Conversation

rafaelss95
Copy link
Contributor

@rafaelss95 rafaelss95 commented Oct 12, 2021

Currently, the rule ignores type-only imports even if you don't specify the allowTypeImports option.

So this, for example:

import type { InvalidTestCase } from '@typescript-eslint/experimental-utils/dist/ts-eslint';

should be reported if you don't specify the allowTypeImports option, however it isn't with the following config:

"@typescript-eslint/no-restriced-imports": [
  "error",
  {
    "patterns": ["@typescript-eslint/experimental-utils/dist/*"],
  },
]

@typescript-eslint
Copy link
Contributor

Thanks for the PR, @rafaelss95!

typescript-eslint is a 100% community driven project, and we are incredibly grateful that you are contributing to that community.

The core maintainers work on this in their personal time, so please understand that it may not be possible for them to review your work immediately.

Thanks again!


🙏 Please, if you or your company is finding typescript-eslint valuable, help us sustain the project by sponsoring it transparently on https://opencollective.com/typescript-eslint. As a thank you, your profile/company logo will be added to our main README which receives thousands of unique visitors per day.

Comment on lines +154 to +157
allowedImportTypeMatchers.length > 0 &&
allowedImportTypeMatchers.every(matcher => {
return matcher.ignores(importSource);
})
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem was that allowedImportTypeMatchers remains empty when you don't specify allowTypeImports: true in options... and every returns true even for an empty array (ref: https://262.ecma-international.org/6.0/#sec-array.prototype.every).

@codecov
Copy link

codecov bot commented Oct 12, 2021

Codecov Report

Merging #3996 (6cd6f9c) into master (c51eef1) will decrease coverage by 0.05%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master    #3996      +/-   ##
==========================================
- Coverage   93.40%   93.35%   -0.06%     
==========================================
  Files         151      151              
  Lines        8093     7941     -152     
  Branches     2568     2544      -24     
==========================================
- Hits         7559     7413     -146     
+ Misses        181      179       -2     
+ Partials      353      349       -4     
Flag Coverage Δ
unittest 93.35% <100.00%> (-0.06%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
...s/eslint-plugin/src/rules/no-restricted-imports.ts 96.49% <100.00%> (+3.63%) ⬆️
packages/eslint-plugin/src/util/escapeRegExp.ts 75.00% <0.00%> (-5.00%) ⬇️
packages/eslint-plugin/src/util/propertyTypes.ts 87.50% <0.00%> (-1.39%) ⬇️
...ackages/eslint-plugin/src/rules/no-var-requires.ts 87.50% <0.00%> (-1.39%) ⬇️
...ages/eslint-plugin/src/rules/unified-signatures.ts 91.89% <0.00%> (-0.80%) ⬇️
...plugin/src/rules/no-unnecessary-type-constraint.ts 85.00% <0.00%> (-0.72%) ⬇️
packages/eslint-plugin/src/rules/quotes.ts 88.88% <0.00%> (-0.59%) ⬇️
packages/eslint-plugin/src/util/astUtils.ts 88.88% <0.00%> (-0.59%) ⬇️
...ges/eslint-plugin/src/rules/no-inferrable-types.ts 92.85% <0.00%> (-0.48%) ⬇️
...int-plugin/src/rules/consistent-type-assertions.ts 86.11% <0.00%> (-0.38%) ⬇️
... and 72 more

@JoshuaKGoldberg
Copy link
Member

@rafaelss95 could you please file an issue describing what's wrong with type-only imports in the no-restricted-imports, or failing that fill out this PR description with an explanation of what's wrong?

Most PRs generally are asked to correspond to an existing issue to make make sure the change is something that would be accepted before you spend too much time working on it. This one is a definite win IMO, so perhaps just a heads up for next time. 🙂

@bradzacher bradzacher added awaiting response Issues waiting for a reply from the OP or another party bug Something isn't working labels Oct 14, 2021
@rafaelss95
Copy link
Contributor Author

rafaelss95 commented Oct 14, 2021

@rafaelss95 could you please file an issue describing what's wrong with type-only imports in the no-restricted-imports, or failing that fill out this PR description with an explanation of what's wrong?

Hey @JoshuaKGoldberg, I've edited the PR description, let me know if it's enough for you.

Most PRs generally are asked to correspond to an existing issue to make make sure the change is something that would be accepted before you spend too much time working on it. This one is a definite win IMO, so perhaps just a heads up for next time. 🙂

I fully agree. I usually do this, however, this one was so simple to grasp and execute (basically a one-line change) that I didn't even bother to fill in the entire issue template and then implement it. But I'll keep in mind that it's always better to create an issue first, thanks.

I think to help with that, it'd be better to create PR templates to inform people to file an issue before create PRs, but it's just an idea.

@JoshuaKGoldberg
Copy link
Member

create PR templates

💯 , and 😱 thanks for the callout! I didn't register ours don't explicitly ask for that! #4010

Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great to me, thanks!

I don't know why CI is complaining about jest/globals...

@bradzacher bradzacher added 1 approval PR that a maintainer has LGTM'd - any maintainer can merge this when ready and removed awaiting response Issues waiting for a reply from the OP or another party 1 approval PR that a maintainer has LGTM'd - any maintainer can merge this when ready labels Oct 18, 2021
@bradzacher bradzacher merged commit 283cdf2 into typescript-eslint:master Oct 18, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 18, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants