Handel Swtich statements
check for locals on for statments only mark private properties
This commit is contained in:
parent
b93407d11d
commit
54b4bef8c8
4 changed files with 105 additions and 10 deletions
|
@ -10241,9 +10241,12 @@ namespace ts {
|
|||
return unknownType;
|
||||
}
|
||||
|
||||
if (noUnusedIdentifiers && (prop.flags & SymbolFlags.ClassMember)) {
|
||||
if (noUnusedIdentifiers &&
|
||||
(prop.flags & SymbolFlags.ClassMember) &&
|
||||
prop.valueDeclaration && (prop.valueDeclaration.flags & NodeFlags.Private)) {
|
||||
if (prop.flags & SymbolFlags.Instantiated) {
|
||||
getSymbolLinks(prop).target.isReferenced = true;
|
||||
|
||||
}
|
||||
else {
|
||||
prop.isReferenced = true;
|
||||
|
@ -14513,10 +14516,11 @@ namespace ts {
|
|||
checkUnusedTypeParameters(<InterfaceDeclaration>node);
|
||||
break;
|
||||
case SyntaxKind.Block:
|
||||
case SyntaxKind.CaseBlock:
|
||||
case SyntaxKind.ForStatement:
|
||||
case SyntaxKind.ForInStatement:
|
||||
case SyntaxKind.ForOfStatement:
|
||||
checkUnusedLocalsAndParameters(<Block | ForInStatement | ForStatement | ForOfStatement>node);
|
||||
checkUnusedLocalsAndParameters(node);
|
||||
break;
|
||||
case SyntaxKind.Constructor:
|
||||
case SyntaxKind.FunctionExpression:
|
||||
|
@ -14543,7 +14547,7 @@ namespace ts {
|
|||
}
|
||||
}
|
||||
|
||||
function checkUnusedLocalsAndParameters(node: FunctionLikeDeclaration | ForStatement | Block): void {
|
||||
function checkUnusedLocalsAndParameters(node: Node): void {
|
||||
if (node.parent.kind !== SyntaxKind.InterfaceDeclaration && noUnusedIdentifiers && !isInAmbientContext(node)) {
|
||||
for (const key in node.locals) {
|
||||
if (hasProperty(node.locals, key)) {
|
||||
|
@ -14568,13 +14572,13 @@ namespace ts {
|
|||
if (node.members) {
|
||||
for (const member of node.members) {
|
||||
if (member.kind === SyntaxKind.MethodDeclaration || member.kind === SyntaxKind.PropertyDeclaration) {
|
||||
if (isPrivateNode(member) && !member.symbol.isReferenced) {
|
||||
if (!member.symbol.isReferenced && member.flags & NodeFlags.Private) {
|
||||
error(member.name, Diagnostics._0_is_declared_but_never_used, member.symbol.name);
|
||||
}
|
||||
}
|
||||
else if (member.kind === SyntaxKind.Constructor) {
|
||||
for (const parameter of (<ConstructorDeclaration>member).parameters) {
|
||||
if (isPrivateNode(parameter) && !parameter.symbol.isReferenced) {
|
||||
if (!parameter.symbol.isReferenced && parameter.flags & NodeFlags.Private) {
|
||||
error(parameter.name, Diagnostics._0_is_declared_but_never_used, parameter.symbol.name);
|
||||
}
|
||||
}
|
||||
|
@ -14596,10 +14600,6 @@ namespace ts {
|
|||
}
|
||||
}
|
||||
|
||||
function isPrivateNode(node: Node): boolean {
|
||||
return (node.flags & NodeFlags.Private) !== 0;
|
||||
}
|
||||
|
||||
function checkUnusedModuleMembers(node: ModuleDeclaration | SourceFile): void {
|
||||
if (compilerOptions.noUnusedLocals && !isInAmbientContext(node)) {
|
||||
for (const key in node.locals) {
|
||||
|
@ -15090,7 +15090,9 @@ namespace ts {
|
|||
if (node.condition) checkExpression(node.condition);
|
||||
if (node.incrementor) checkExpression(node.incrementor);
|
||||
checkSourceElement(node.statement);
|
||||
registerForUnusedIdentifiersCheck(node);
|
||||
if (node.locals) {
|
||||
registerForUnusedIdentifiersCheck(node);
|
||||
}
|
||||
}
|
||||
|
||||
function checkForOfStatement(node: ForOfStatement): void {
|
||||
|
@ -15556,6 +15558,9 @@ namespace ts {
|
|||
}
|
||||
forEach(clause.statements, checkSourceElement);
|
||||
});
|
||||
if (node.caseBlock.locals) {
|
||||
registerForUnusedIdentifiersCheck(node.caseBlock);
|
||||
}
|
||||
}
|
||||
|
||||
function checkLabeledStatement(node: LabeledStatement) {
|
||||
|
|
31
tests/baselines/reference/unusedSwitchStatment.errors.txt
Normal file
31
tests/baselines/reference/unusedSwitchStatment.errors.txt
Normal file
|
@ -0,0 +1,31 @@
|
|||
tests/cases/compiler/unusedSwitchStatment.ts(4,13): error TS6133: 'x' is declared but never used.
|
||||
tests/cases/compiler/unusedSwitchStatment.ts(7,15): error TS6133: 'c' is declared but never used.
|
||||
tests/cases/compiler/unusedSwitchStatment.ts(10,13): error TS6133: 'z' is declared but never used.
|
||||
|
||||
|
||||
==== tests/cases/compiler/unusedSwitchStatment.ts (3 errors) ====
|
||||
|
||||
switch (1) {
|
||||
case 0:
|
||||
let x;
|
||||
~
|
||||
!!! error TS6133: 'x' is declared but never used.
|
||||
break;
|
||||
case 1:
|
||||
const c = 1;
|
||||
~
|
||||
!!! error TS6133: 'c' is declared but never used.
|
||||
break;
|
||||
default:
|
||||
let z = 2;
|
||||
~
|
||||
!!! error TS6133: 'z' is declared but never used.
|
||||
}
|
||||
|
||||
|
||||
switch (2) {
|
||||
case 0:
|
||||
let x;
|
||||
case 1:
|
||||
x++;
|
||||
}
|
38
tests/baselines/reference/unusedSwitchStatment.js
Normal file
38
tests/baselines/reference/unusedSwitchStatment.js
Normal file
|
@ -0,0 +1,38 @@
|
|||
//// [unusedSwitchStatment.ts]
|
||||
|
||||
switch (1) {
|
||||
case 0:
|
||||
let x;
|
||||
break;
|
||||
case 1:
|
||||
const c = 1;
|
||||
break;
|
||||
default:
|
||||
let z = 2;
|
||||
}
|
||||
|
||||
|
||||
switch (2) {
|
||||
case 0:
|
||||
let x;
|
||||
case 1:
|
||||
x++;
|
||||
}
|
||||
|
||||
//// [unusedSwitchStatment.js]
|
||||
switch (1) {
|
||||
case 0:
|
||||
var x = void 0;
|
||||
break;
|
||||
case 1:
|
||||
var c = 1;
|
||||
break;
|
||||
default:
|
||||
var z = 2;
|
||||
}
|
||||
switch (2) {
|
||||
case 0:
|
||||
var x = void 0;
|
||||
case 1:
|
||||
x++;
|
||||
}
|
21
tests/cases/compiler/unusedSwitchStatment.ts
Normal file
21
tests/cases/compiler/unusedSwitchStatment.ts
Normal file
|
@ -0,0 +1,21 @@
|
|||
//@noUnusedLocals:true
|
||||
//@noUnusedParameters:true
|
||||
|
||||
switch (1) {
|
||||
case 0:
|
||||
let x;
|
||||
break;
|
||||
case 1:
|
||||
const c = 1;
|
||||
break;
|
||||
default:
|
||||
let z = 2;
|
||||
}
|
||||
|
||||
|
||||
switch (2) {
|
||||
case 0:
|
||||
let x;
|
||||
case 1:
|
||||
x++;
|
||||
}
|
Loading…
Reference in a new issue