Skip to content

Commit

Permalink
Handel Swtich statements
Browse files Browse the repository at this point in the history
check for locals on for statments
only mark private properties
  • Loading branch information
mhegazy committed Jun 30, 2016
1 parent b93407d commit 54b4bef
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 10 deletions.
25 changes: 15 additions & 10 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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:
Expand All @@ -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)) {
Expand All @@ -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);
}
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -15556,6 +15558,9 @@ namespace ts {
}
forEach(clause.statements, checkSourceElement);
});
if (node.caseBlock.locals) {
registerForUnusedIdentifiersCheck(node.caseBlock);
}
}

function checkLabeledStatement(node: LabeledStatement) {
Expand Down
31 changes: 31 additions & 0 deletions tests/baselines/reference/unusedSwitchStatment.errors.txt
Original file line number Diff line number Diff line change
@@ -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 changes: 38 additions & 0 deletions tests/baselines/reference/unusedSwitchStatment.js
Original file line number Diff line number Diff line change
@@ -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 changes: 21 additions & 0 deletions tests/cases/compiler/unusedSwitchStatment.ts
Original file line number Diff line number Diff line change
@@ -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++;
}

0 comments on commit 54b4bef

Please sign in to comment.