Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
feat(eslint-plugin): [no-unused-var] handle implicit exports in declaration files#8611
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import { | ||
ImplicitLibVariable, | ||
ScopeType, | ||
Variable, | ||
Visitor, | ||
} from '@typescript-eslint/scope-manager'; | ||
import type { TSESTree } from '@typescript-eslint/utils'; | ||
@@ -10,6 +11,7 @@ import { | ||
ESLintUtils, | ||
TSESLint, | ||
} from '@typescript-eslint/utils'; | ||
import { isDefinitionFile } from './misc'; | ||
class UnusedVarsVisitor< | ||
MessageIds extends string, | ||
@@ -22,6 +24,7 @@ class UnusedVarsVisitor< | ||
readonly #scopeManager: TSESLint.Scope.ScopeManager; | ||
// readonly #unusedVariables = new Set<TSESLint.Scope.Variable>(); | ||
readonly #isDefinitionFile: boolean; | ||
private constructor(context: TSESLint.RuleContext<MessageIds, Options>) { | ||
super({ | ||
@@ -32,6 +35,8 @@ class UnusedVarsVisitor< | ||
context.sourceCode.scopeManager, | ||
'Missing required scope manager', | ||
); | ||
this.#isDefinitionFile = isDefinitionFile(context.filename); | ||
} | ||
public static collectUnusedVariables< | ||
@@ -60,7 +65,12 @@ class UnusedVarsVisitor< | ||
scope: TSESLint.Scope.Scope, | ||
unusedVariables = new Set<TSESLint.Scope.Variable>(), | ||
): ReadonlySet<TSESLint.Scope.Variable> { | ||
const implicitlyExported = allVariablesImplicitlyExported( | ||
scope, | ||
this.#isDefinitionFile, | ||
); | ||
for (const variable of implicitlyExported ? [] : scope.variables) { | ||
if ( | ||
// skip function expression names, | ||
scope.functionExpressionScope || | ||
@@ -438,6 +448,47 @@ function isExported(variable: TSESLint.Scope.Variable): boolean { | ||
}); | ||
} | ||
function allVariablesImplicitlyExported( | ||
scope: TSESLint.Scope.Scope, | ||
isDefinitionFile: boolean, | ||
): boolean { | ||
// TODO: does this also happen in ambient module declarations? | ||
if ( | ||
!isDefinitionFile || | ||
!( | ||
scope.type === ScopeType.tsModule || | ||
scope.type === ScopeType.module || | ||
scope.type === ScopeType.global | ||
) | ||
) { | ||
return false; | ||
} | ||
// TODO: test modules, globals | ||
// TODO: look for `export {}` | ||
function isExportImportEquals(variable: Variable): boolean { | ||
for (const def of variable.defs) { | ||
if ( | ||
def.type === TSESLint.Scope.DefinitionType.ImportBinding && | ||
def.node.type === AST_NODE_TYPES.TSImportEqualsDeclaration && | ||
def.node.parent.type === AST_NODE_TYPES.ExportNamedDeclaration | ||
) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
for (const variable of scope.variables) { | ||
if (isExported(variable) && !isExportImportEquals(variable)) { | ||
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. declarenamespaceFoo{constx=1;exportconsty=3;}Foo.x;Foo.y; The 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. Hm, the rules for this are super annoying. What I have currently may just be true only for declaration files, but "ambient" ones with declare (in ts files only?) may have different rules? Member
| ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
const LOGICAL_ASSIGNMENT_OPERATORS = new Set(['&&=', '||=', '??=']); | ||
/** | ||