-
Notifications
You must be signed in to change notification settings - Fork 12.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
Unused identifiers compiler code #9200
Changes from 1 commit
617b819
a8bc8b4
01966ba
1e5ca92
890d178
69dbeea
c78d0e2
107b369
481baa3
cd1ce0f
a38149d
0571c19
e17ed58
2b7f3a7
5a34352
93b7490
7aba626
ed5052d
f5cdc9c
8c3c7b1
dfad7cc
c325625
6a711bc
d62a43f
043a625
8f9d4ae
f15448a
c82453f
bcd6fc4
49385f4
5993015
f464f92
3b5f8d2
ed282d7
e502ba0
0e2e43d
d6c2bcd
f93c6c8
4521058
5eb7153
1401506
5361e5f
7fc4616
9753d09
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14447,10 +14447,8 @@ namespace ts { | |
error(local.valueDeclaration, Diagnostics._0_is_declared_but_never_used, key); | ||
} | ||
else if (local.valueDeclaration.kind === SyntaxKind.Parameter && compilerOptions.noUnusedParameters) { | ||
if (local.valueDeclaration.modifiers) { | ||
if (getCombinedNodeFlags(local.valueDeclaration) & NodeFlags.Private) { | ||
error(local.valueDeclaration, Diagnostics._0_is_declared_but_never_used, key); | ||
} | ||
if (getCombinedNodeFlags(local.valueDeclaration) & NodeFlags.Private) { | ||
error(local.valueDeclaration, Diagnostics._0_is_declared_but_never_used, key); | ||
} | ||
else { | ||
error(local.valueDeclaration, Diagnostics._0_is_declared_but_never_used, key); | ||
|
@@ -14462,7 +14460,7 @@ namespace ts { | |
} | ||
} | ||
|
||
function checkUnusedModuleLocals(node: ModuleDeclaration): void { | ||
function checkUnusedModuleLocals(node: ModuleDeclaration | SourceFile): void { | ||
if (compilerOptions.noUnusedLocals && !isInAmbientContext(node)) { | ||
for (const key in node.locals) { | ||
if (hasProperty(node.locals, key)) { | ||
|
@@ -14471,13 +14469,12 @@ namespace ts { | |
if ((local.valueDeclaration && local.valueDeclaration.kind)) { | ||
error(local.valueDeclaration, Diagnostics._0_is_declared_but_never_used, key); | ||
} | ||
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. no need to check kind in 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. Removed it |
||
else if (local.declarations && | ||
(local.declarations[0].kind === SyntaxKind.InterfaceDeclaration || local.declarations[0].kind === SyntaxKind.ModuleDeclaration)) { | ||
error(local.declarations[0], Diagnostics._0_is_declared_but_never_used, key); | ||
} | ||
else if (local.declarations && local.declarations[0].kind === SyntaxKind.ImportEqualsDeclaration) { | ||
error(local.declarations[0].name, Diagnostics._0_is_declared_but_never_used, key); | ||
} | ||
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. why do we need these checks? why not just forEach(local.declarations, d => error(d, Diagnostics._0_is_declared_but_never_used, key)); 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. I have already tried this one. But I feel this might not work. The reason is that ImportEqualsDeclaration is a special case in which we need to use local.declarations[0].name instead of local.declarations[0]. Scenario: module B { Here 'a' must be indicated as error instead of "import a = A". But, I have optimized it a little so the condition does not look long. 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. i would say all errors should be on the declaration.name if it exists. |
||
else if (local.declarations) { | ||
error(local.declarations[0], Diagnostics._0_is_declared_but_never_used, key); | ||
} | ||
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. nit, can we rename this to 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. Done 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. if (local.declarations) {
const declaration = local.declarations[0];
error(declaration.name? declaration.name : declaation, Diagnostics._0_is_declared_but_never_used, key);
} |
||
} | ||
} | ||
} | ||
|
@@ -14498,7 +14495,7 @@ namespace ts { | |
} | ||
} | ||
|
||
function checkUnusedTypeParameters(node: ClassDeclaration | FunctionDeclaration | MethodDeclaration | FunctionExpression | ArrowFunction | ConstructorDeclaration | SignatureDeclaration) { | ||
function checkUnusedTypeParameters(node: ClassDeclaration | FunctionDeclaration | MethodDeclaration | FunctionExpression | ArrowFunction | ConstructorDeclaration | SignatureDeclaration | InterfaceDeclaration) { | ||
if (compilerOptions.noUnusedLocals && !isInAmbientContext(node)) { | ||
if (node.typeParameters) { | ||
for (const typeParameter of node.typeParameters) { | ||
|
@@ -14520,15 +14517,13 @@ namespace ts { | |
if (hasProperty(node.locals, local)) { | ||
const localValue = node.locals[local]; | ||
if (localValue.declarations && !localValue.exportSymbol) { | ||
for (const declaration of localValue.declarations) { | ||
const symbol = declaration.symbol; | ||
if (!symbol.hasReference) { | ||
if (declaration.kind === SyntaxKind.ImportSpecifier || declaration.kind === SyntaxKind.ImportClause || declaration.kind === SyntaxKind.NamespaceImport) { | ||
error(declaration, Diagnostics._0_is_declared_but_never_used, symbol.name); | ||
} | ||
else if (declaration.kind === SyntaxKind.ImportEqualsDeclaration) { | ||
error(declaration.name, Diagnostics._0_is_declared_but_never_used, symbol.name); | ||
} | ||
const declaration = localValue.declarations[0]; | ||
if (!localValue.hasReference) { | ||
if (declaration.kind === SyntaxKind.ImportSpecifier || declaration.kind === SyntaxKind.ImportClause || declaration.kind === SyntaxKind.NamespaceImport) { | ||
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. if (declaration.kind === SyntaxKind.ImportSpecifier || declaration.kind === SyntaxKind.ImportClause || declaration.kind === SyntaxKind.NamespaceImport || declaration.kind === SyntaxKind.ImportEqualsDeclaration) {
error(declaration.name, Diagnostics._0_is_declared_but_never_used, localValue.name);
} 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. i am still not sure i understand why checkUnusedImports is not part of the checkUnusedModuleLocals? |
||
error(declaration, Diagnostics._0_is_declared_but_never_used, localValue.name); | ||
} | ||
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. i do not think this is correct. you already have the symbol, and the symbol is not exported, so 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. Changed the code. |
||
else if (declaration.kind === SyntaxKind.ImportEqualsDeclaration) { | ||
error(declaration.name, Diagnostics._0_is_declared_but_never_used, localValue.name); | ||
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. no need to check 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. can we merge this function with the checkUnusedModulePrivates ? 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. Removed the ambient context check. I prefer to keep them separate for sake of clarity, 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. i am not sure i see the point here. just merge them togather, and get rid of all the checks on the kinds. |
||
} | ||
} | ||
} | ||
|
@@ -16010,6 +16005,7 @@ namespace ts { | |
|
||
if (produceDiagnostics) { | ||
checkTypeForDuplicateIndexSignatures(node); | ||
checkUnusedTypeParameters(node); | ||
} | ||
} | ||
|
||
|
@@ -16934,6 +16930,7 @@ namespace ts { | |
forEach(node.statements, checkSourceElement); | ||
if (isExternalModule(node)) { | ||
checkUnusedImports(node); | ||
checkUnusedModuleLocals(node); | ||
} | ||
checkDeferredNodes(); | ||
deferredNodes = undefined; | ||
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.
export {}; // makes the file a module
var x; // should be reported as unused
export var y; // should not 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. Added the code and test code |
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
tests/cases/compiler/unusedTypeParameterInInterface1.ts(2,15): error TS6133: 'T' is declared but never used. | ||
|
||
|
||
==== tests/cases/compiler/unusedTypeParameterInInterface1.ts (1 errors) ==== | ||
|
||
interface int<T> { | ||
~ | ||
!!! error TS6133: 'T' is declared but never used. | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
//// [unusedTypeParameterInInterface1.ts] | ||
|
||
interface int<T> { | ||
|
||
} | ||
|
||
//// [unusedTypeParameterInInterface1.js] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
tests/cases/compiler/unusedTypeParameterInInterface2.ts(2,18): error TS6133: 'U' is declared but never used. | ||
|
||
|
||
==== tests/cases/compiler/unusedTypeParameterInInterface2.ts (1 errors) ==== | ||
|
||
interface int<T, U, V> { | ||
~ | ||
!!! error TS6133: 'U' is declared but never used. | ||
f1(a: T): string; | ||
c: V; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
//// [unusedTypeParameterInInterface2.ts] | ||
|
||
interface int<T, U, V> { | ||
f1(a: T): string; | ||
c: V; | ||
} | ||
|
||
//// [unusedTypeParameterInInterface2.js] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
tests/cases/compiler/unusedVariablesinModules1.ts(4,5): error TS6133: 'x' is declared but never used. | ||
|
||
|
||
==== tests/cases/compiler/unusedVariablesinModules1.ts (1 errors) ==== | ||
|
||
export {}; | ||
|
||
var x: string; | ||
~ | ||
!!! error TS6133: 'x' is declared but never used. | ||
|
||
export var y: string; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
//// [unusedVariablesinModules1.ts] | ||
|
||
export {}; | ||
|
||
var x: string; | ||
|
||
export var y: string; | ||
|
||
//// [unusedVariablesinModules1.js] | ||
"use strict"; | ||
var x; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
//@noUnusedLocals:true | ||
//@noUnusedParameters:true | ||
|
||
interface int<T> { | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
//@noUnusedLocals:true | ||
//@noUnusedParameters:true | ||
|
||
interface int<T, U, V> { | ||
f1(a: T): string; | ||
c: V; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
//@noUnusedLocals:true | ||
//@noUnusedParameters:true | ||
|
||
export {}; | ||
|
||
var x: string; | ||
|
||
export var y: string; |
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.
do not think you need this condition, the getCombinedNodeFlags should be sufficient.
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.
Agreed. Removed the condition