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): [consistent-type-definitions] remove fixer when the interface is within a global module declaration #2739

Merged
merged 5 commits into from Nov 14, 2020
Merged

fix(eslint-plugin): [consistent-type-definitions] remove fixer when the interface is within a global module declaration #2739

merged 5 commits into from Nov 14, 2020

Conversation

ddubrava
Copy link
Contributor

@ddubrava ddubrava commented Nov 5, 2020

fixes #2707

@typescript-eslint
Copy link
Contributor

Thanks for the PR, @ddubrava!

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.

@bradzacher bradzacher added the bug Something isn't working label Nov 6, 2020
Comment on lines 71 to 73
':not(TSModuleDeclaration > TSModuleBlock) > TSInterfaceDeclaration'(
node: TSESTree.TSInterfaceDeclaration,
): void {
Copy link
Member

Choose a reason for hiding this comment

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

this will stop the rule reporting at all when an interface is within a module declaration.
Which is too wide of a change.

Instead, we still want to report on interface within a module declaration - but we want to just no automatically fix it if and only if it's within a declare global

Copy link
Contributor Author

@ddubrava ddubrava Nov 8, 2020

Choose a reason for hiding this comment

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

What's the manual fix for that case? I thought it's impossible to use the type within a declare in the case so we have to turn off it completely

Copy link
Member

Choose a reason for hiding this comment

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

the manual fix in that case would be for the engineer to review the lint and disable it in that case.

it's impossible for us to tell exactly what the intentions are so we have three options:

  • report with a fixer
    • this is bad because it means we can break the user's code.
  • do nothing
    • this isn't great because it means that the user can easily create code which should be marked as invalid.
    • it's impossible to tell if the code was written invalidly because it HAD to be this way, or if the author just wrote it wrong.
  • report without a fixer
    • this is a best-of-both worlds.
    • if the code is actually invalid - the user can fix it
    • if it's an edge case - the user can suppress the lint rule to show it's intentionally written this way.

I thought it's impossible to use the type within a declare in the case so we have to turn off it completely

Nope - it's perfectly acceptable to use type declarations or interface declarations.
In some cases the latter is better as it will allow specifically for augmentation via declaration merging.

@bradzacher bradzacher added the awaiting response Issues waiting for a reply from the OP or another party label Nov 8, 2020
…he interface is within a global module declaration
@ddubrava ddubrava changed the title fix(eslint-plugin): [consistent-type-definitions] ignore TSInterfaceDeclaration within TSModuleDeclaration (#2707) fix(eslint-plugin): [consistent-type-definitions] remove fixer when the interface is within a global module declaration (#2707) Nov 8, 2020
@codecov
Copy link

codecov bot commented Nov 8, 2020

Codecov Report

Merging #2739 (4d14d68) into master (e82698c) will increase coverage by 0.00%.
The diff coverage is 92.85%.

@@           Coverage Diff           @@
##           master    #2739   +/-   ##
=======================================
  Coverage   92.79%   92.80%           
=======================================
  Files         297      300    +3     
  Lines        9833     9852   +19     
  Branches     2762     2767    +5     
=======================================
+ Hits         9125     9143   +18     
  Misses        332      332           
- Partials      376      377    +1     
Flag Coverage Δ
unittest 92.80% <92.85%> (+<0.01%) ⬆️

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

Impacted Files Coverage Δ
...nt-plugin/src/rules/consistent-type-definitions.ts 91.17% <92.85%> (+0.85%) ⬆️
packages/eslint-plugin/src/rules/array-type.ts 97.33% <0.00%> (-1.30%) ⬇️
packages/scope-manager/src/lib/index.ts 100.00% <0.00%> (ø)
packages/scope-manager/src/lib/es2020.ts 100.00% <0.00%> (ø)
packages/scope-manager/src/lib/esnext.ts 100.00% <0.00%> (ø)
packages/visitor-keys/src/visitor-keys.ts 100.00% <0.00%> (ø)
...ackages/eslint-plugin/src/rules/no-implied-eval.ts 93.75% <0.00%> (ø)
...ckages/eslint-plugin/src/rules/no-throw-literal.ts 95.00% <0.00%> (ø)
...ckages/scope-manager/src/lib/webworker.iterable.ts 100.00% <0.00%> (ø)
...kages/scope-manager/src/lib/es2020.sharedmemory.ts 100.00% <0.00%> (ø)
... and 2 more

@ddubrava
Copy link
Contributor Author

ddubrava commented Nov 8, 2020

Should we handle only declare global {} or global {} is also shouldn't be fixed? And what about not global declarations?

Comment on lines 39 to 42
node.parent?.type === AST_NODE_TYPES.TSModuleBlock &&
node.parent.parent?.type === AST_NODE_TYPES.TSModuleDeclaration &&
node.parent.parent?.declare &&
node.parent.parent?.global
Copy link
Member

Choose a reason for hiding this comment

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

instead of just looking at the grandparent - we could instead look at all the ancestors so we can catch cases like:

declare global {
  namespace Foo {
    interface Bar {}
  }
}

context.getAncestors() will return you an array and you can traverse it from the start to go from the highest parent.

…g at the grandparent by looking at all the ancestors
@bradzacher bradzacher removed the awaiting response Issues waiting for a reply from the OP or another party label Nov 10, 2020
@bradzacher bradzacher changed the title fix(eslint-plugin): [consistent-type-definitions] remove fixer when the interface is within a global module declaration (#2707) fix(eslint-plugin): [consistent-type-definitions] remove fixer when the interface is within a global module declaration Nov 14, 2020
Copy link
Member

@bradzacher bradzacher left a comment

Choose a reason for hiding this comment

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

LGTM - thanks for this!

@bradzacher bradzacher merged commit 2326238 into typescript-eslint:master Nov 14, 2020
@ddubrava ddubrava deleted the consistent-type-definitions-exclude-TSModuleDeclaration branch November 15, 2020 15:08
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 16, 2020
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
2 participants