-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
fix(eslint-plugin): [no-unused-vars] no is assigned a value but only used as a type
error when it has a same name
#11322
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
Open
nayounsang
wants to merge
4
commits into
typescript-eslint:main
Choose a base branch
from
nayounsang:type-unused
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+64
−4
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,8 @@ | |
import type { TSESTree } from '@typescript-eslint/utils'; | ||
|
||
import { | ||
DefinitionType, | ||
ESLintScopeVariable, | ||
ImplicitLibVariable, | ||
ScopeType, | ||
Visitor, | ||
|
@@ -188,7 +190,7 @@ | |
// basic exported variables | ||
isExported(variable) || | ||
// variables implicitly exported via a merged declaration | ||
isMergableExported(variable) || | ||
isMergeableExported(variable) || | ||
// used variables | ||
isUsedVariable(variable) | ||
) { | ||
|
@@ -415,6 +417,29 @@ | |
return false; | ||
} | ||
|
||
const exportExceptDefTypes: DefinitionType[] = [ | ||
DefinitionType.Variable, | ||
DefinitionType.Type, | ||
]; | ||
/** | ||
* In edge cases, the existing used logic does not work. | ||
* When the type and variable name of the variable are the same | ||
* @ref https://github.com/typescript-eslint/typescript-eslint/issues/10658 | ||
* @param variable the variable to check | ||
* @returns true if it is an edge case | ||
*/ | ||
function isSafeUnusedExportCondition(variable: ScopeVariable): boolean { | ||
if (variable instanceof ESLintScopeVariable) { | ||
return true; | ||
} | ||
if (variable.isTypeVariable && variable.isValueVariable) { | ||
return !variable.defs | ||
.map(d => d.type) | ||
.every(t => exportExceptDefTypes.includes(t)); | ||
} | ||
return true; | ||
} | ||
|
||
const MERGABLE_TYPES = new Set([ | ||
AST_NODE_TYPES.ClassDeclaration, | ||
AST_NODE_TYPES.FunctionDeclaration, | ||
|
@@ -426,7 +451,8 @@ | |
* Determine if the variable is directly exported | ||
* @param variable the variable to check | ||
*/ | ||
function isMergableExported(variable: ScopeVariable): boolean { | ||
function isMergeableExported(variable: ScopeVariable): boolean { | ||
const safeFlag = isSafeUnusedExportCondition(variable); | ||
// If all of the merged things are of the same type, TS will error if not all of them are exported - so we only need to find one | ||
for (const def of variable.defs) { | ||
// parameters can never be exported. | ||
|
@@ -441,7 +467,9 @@ | |
def.node.parent?.type === AST_NODE_TYPES.ExportNamedDeclaration) || | ||
def.node.parent?.type === AST_NODE_TYPES.ExportDefaultDeclaration | ||
) { | ||
return true; | ||
return ( | ||
safeFlag || def.node.type !== AST_NODE_TYPES.TSTypeAliasDeclaration | ||
); | ||
} | ||
} | ||
|
||
|
@@ -454,6 +482,7 @@ | |
* @returns True if the variable is exported, false if not. | ||
*/ | ||
function isExported(variable: ScopeVariable): boolean { | ||
const safeFlag = isSafeUnusedExportCondition(variable); | ||
return variable.defs.some(definition => { | ||
let node = definition.node; | ||
|
||
|
@@ -465,7 +494,12 @@ | |
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
return node.parent!.type.startsWith('Export'); | ||
const isExportedFlag = node.parent!.type.startsWith('Export'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. need adviceIs there a reason why didn't use optional chaining before? |
||
|
||
return ( | ||
isExportedFlag && | ||
(safeFlag || node.type !== AST_NODE_TYPES.TSTypeAliasDeclaration) | ||
); | ||
}); | ||
} | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
need advice
I don't know why this condition is necessary. Can't the arg type of
isExported
,isMergableExported
be used asVariable
?Actually, it is used as
Variable
only.