Skip to content

Commit 54b4bef

Browse files
committed
Handel Swtich statements
check for locals on for statments only mark private properties
1 parent b93407d commit 54b4bef

File tree

4 files changed

+105
-10
lines changed

4 files changed

+105
-10
lines changed

Diff for: src/compiler/checker.ts

+15-10
Original file line numberDiff line numberDiff line change
@@ -10241,9 +10241,12 @@ namespace ts {
1024110241
return unknownType;
1024210242
}
1024310243

10244-
if (noUnusedIdentifiers && (prop.flags & SymbolFlags.ClassMember)) {
10244+
if (noUnusedIdentifiers &&
10245+
(prop.flags & SymbolFlags.ClassMember) &&
10246+
prop.valueDeclaration && (prop.valueDeclaration.flags & NodeFlags.Private)) {
1024510247
if (prop.flags & SymbolFlags.Instantiated) {
1024610248
getSymbolLinks(prop).target.isReferenced = true;
10249+
1024710250
}
1024810251
else {
1024910252
prop.isReferenced = true;
@@ -14513,10 +14516,11 @@ namespace ts {
1451314516
checkUnusedTypeParameters(<InterfaceDeclaration>node);
1451414517
break;
1451514518
case SyntaxKind.Block:
14519+
case SyntaxKind.CaseBlock:
1451614520
case SyntaxKind.ForStatement:
1451714521
case SyntaxKind.ForInStatement:
1451814522
case SyntaxKind.ForOfStatement:
14519-
checkUnusedLocalsAndParameters(<Block | ForInStatement | ForStatement | ForOfStatement>node);
14523+
checkUnusedLocalsAndParameters(node);
1452014524
break;
1452114525
case SyntaxKind.Constructor:
1452214526
case SyntaxKind.FunctionExpression:
@@ -14543,7 +14547,7 @@ namespace ts {
1454314547
}
1454414548
}
1454514549

14546-
function checkUnusedLocalsAndParameters(node: FunctionLikeDeclaration | ForStatement | Block): void {
14550+
function checkUnusedLocalsAndParameters(node: Node): void {
1454714551
if (node.parent.kind !== SyntaxKind.InterfaceDeclaration && noUnusedIdentifiers && !isInAmbientContext(node)) {
1454814552
for (const key in node.locals) {
1454914553
if (hasProperty(node.locals, key)) {
@@ -14568,13 +14572,13 @@ namespace ts {
1456814572
if (node.members) {
1456914573
for (const member of node.members) {
1457014574
if (member.kind === SyntaxKind.MethodDeclaration || member.kind === SyntaxKind.PropertyDeclaration) {
14571-
if (isPrivateNode(member) && !member.symbol.isReferenced) {
14575+
if (!member.symbol.isReferenced && member.flags & NodeFlags.Private) {
1457214576
error(member.name, Diagnostics._0_is_declared_but_never_used, member.symbol.name);
1457314577
}
1457414578
}
1457514579
else if (member.kind === SyntaxKind.Constructor) {
1457614580
for (const parameter of (<ConstructorDeclaration>member).parameters) {
14577-
if (isPrivateNode(parameter) && !parameter.symbol.isReferenced) {
14581+
if (!parameter.symbol.isReferenced && parameter.flags & NodeFlags.Private) {
1457814582
error(parameter.name, Diagnostics._0_is_declared_but_never_used, parameter.symbol.name);
1457914583
}
1458014584
}
@@ -14596,10 +14600,6 @@ namespace ts {
1459614600
}
1459714601
}
1459814602

14599-
function isPrivateNode(node: Node): boolean {
14600-
return (node.flags & NodeFlags.Private) !== 0;
14601-
}
14602-
1460314603
function checkUnusedModuleMembers(node: ModuleDeclaration | SourceFile): void {
1460414604
if (compilerOptions.noUnusedLocals && !isInAmbientContext(node)) {
1460514605
for (const key in node.locals) {
@@ -15090,7 +15090,9 @@ namespace ts {
1509015090
if (node.condition) checkExpression(node.condition);
1509115091
if (node.incrementor) checkExpression(node.incrementor);
1509215092
checkSourceElement(node.statement);
15093-
registerForUnusedIdentifiersCheck(node);
15093+
if (node.locals) {
15094+
registerForUnusedIdentifiersCheck(node);
15095+
}
1509415096
}
1509515097

1509615098
function checkForOfStatement(node: ForOfStatement): void {
@@ -15556,6 +15558,9 @@ namespace ts {
1555615558
}
1555715559
forEach(clause.statements, checkSourceElement);
1555815560
});
15561+
if (node.caseBlock.locals) {
15562+
registerForUnusedIdentifiersCheck(node.caseBlock);
15563+
}
1555915564
}
1556015565

1556115566
function checkLabeledStatement(node: LabeledStatement) {
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
tests/cases/compiler/unusedSwitchStatment.ts(4,13): error TS6133: 'x' is declared but never used.
2+
tests/cases/compiler/unusedSwitchStatment.ts(7,15): error TS6133: 'c' is declared but never used.
3+
tests/cases/compiler/unusedSwitchStatment.ts(10,13): error TS6133: 'z' is declared but never used.
4+
5+
6+
==== tests/cases/compiler/unusedSwitchStatment.ts (3 errors) ====
7+
8+
switch (1) {
9+
case 0:
10+
let x;
11+
~
12+
!!! error TS6133: 'x' is declared but never used.
13+
break;
14+
case 1:
15+
const c = 1;
16+
~
17+
!!! error TS6133: 'c' is declared but never used.
18+
break;
19+
default:
20+
let z = 2;
21+
~
22+
!!! error TS6133: 'z' is declared but never used.
23+
}
24+
25+
26+
switch (2) {
27+
case 0:
28+
let x;
29+
case 1:
30+
x++;
31+
}

Diff for: tests/baselines/reference/unusedSwitchStatment.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//// [unusedSwitchStatment.ts]
2+
3+
switch (1) {
4+
case 0:
5+
let x;
6+
break;
7+
case 1:
8+
const c = 1;
9+
break;
10+
default:
11+
let z = 2;
12+
}
13+
14+
15+
switch (2) {
16+
case 0:
17+
let x;
18+
case 1:
19+
x++;
20+
}
21+
22+
//// [unusedSwitchStatment.js]
23+
switch (1) {
24+
case 0:
25+
var x = void 0;
26+
break;
27+
case 1:
28+
var c = 1;
29+
break;
30+
default:
31+
var z = 2;
32+
}
33+
switch (2) {
34+
case 0:
35+
var x = void 0;
36+
case 1:
37+
x++;
38+
}

Diff for: tests/cases/compiler/unusedSwitchStatment.ts

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//@noUnusedLocals:true
2+
//@noUnusedParameters:true
3+
4+
switch (1) {
5+
case 0:
6+
let x;
7+
break;
8+
case 1:
9+
const c = 1;
10+
break;
11+
default:
12+
let z = 2;
13+
}
14+
15+
16+
switch (2) {
17+
case 0:
18+
let x;
19+
case 1:
20+
x++;
21+
}

0 commit comments

Comments
 (0)