Skip to content

Commit

Permalink
Merge pull request microsoft#11998 from Microsoft/unusedProperty
Browse files Browse the repository at this point in the history
 Mark property referenced in the destructuring as referenced
  • Loading branch information
sheetalkamat authored Nov 4, 2016
2 parents defc053 + 13e8f7f commit 4a90614
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 11 deletions.
28 changes: 17 additions & 11 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11534,6 +11534,21 @@ namespace ts {
diagnostics.add(createDiagnosticForNodeFromMessageChain(propNode, errorInfo));
}

function markPropertyAsReferenced(prop: Symbol) {
if (prop &&
noUnusedIdentifiers &&
(prop.flags & SymbolFlags.ClassMember) &&
prop.valueDeclaration && (getModifierFlags(prop.valueDeclaration) & ModifierFlags.Private)) {
if (prop.flags & SymbolFlags.Instantiated) {
getSymbolLinks(prop).target.isReferenced = true;

}
else {
prop.isReferenced = true;
}
}
}

function checkPropertyAccessExpressionOrQualifiedName(node: PropertyAccessExpression | QualifiedName, left: Expression | QualifiedName, right: Identifier) {
const type = checkNonNullExpression(left);
if (isTypeAny(type) || type === silentNeverType) {
Expand All @@ -11553,17 +11568,7 @@ namespace ts {
return unknownType;
}

if (noUnusedIdentifiers &&
(prop.flags & SymbolFlags.ClassMember) &&
prop.valueDeclaration && (getModifierFlags(prop.valueDeclaration) & ModifierFlags.Private)) {
if (prop.flags & SymbolFlags.Instantiated) {
getSymbolLinks(prop).target.isReferenced = true;

}
else {
prop.isReferenced = true;
}
}
markPropertyAsReferenced(prop);

getNodeLinks(node).resolvedSymbol = prop;

Expand Down Expand Up @@ -16353,6 +16358,7 @@ namespace ts {
const parentType = getTypeForBindingElementParent(parent);
const name = node.propertyName || <Identifier>node.name;
const property = getPropertyOfType(parentType, getTextOfPropertyName(name));
markPropertyAsReferenced(property);
if (parent.initializer && property && getParentOfSymbol(property)) {
checkClassPropertyAccess(parent, parent.initializer, parentType, property);
}
Expand Down
25 changes: 25 additions & 0 deletions tests/baselines/reference/unusedLocalProperty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//// [unusedLocalProperty.ts]
declare var console: { log(msg: any): void; }
class Animal {
constructor(private species: string) {
}

printSpecies() {
let { species } = this;
console.log(species);
}
}



//// [unusedLocalProperty.js]
var Animal = (function () {
function Animal(species) {
this.species = species;
}
Animal.prototype.printSpecies = function () {
var species = this.species;
console.log(species);
};
return Animal;
}());
29 changes: 29 additions & 0 deletions tests/baselines/reference/unusedLocalProperty.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
=== tests/cases/compiler/unusedLocalProperty.ts ===
declare var console: { log(msg: any): void; }
>console : Symbol(console, Decl(unusedLocalProperty.ts, 0, 11))
>log : Symbol(log, Decl(unusedLocalProperty.ts, 0, 22))
>msg : Symbol(msg, Decl(unusedLocalProperty.ts, 0, 27))

class Animal {
>Animal : Symbol(Animal, Decl(unusedLocalProperty.ts, 0, 45))

constructor(private species: string) {
>species : Symbol(Animal.species, Decl(unusedLocalProperty.ts, 2, 16))
}

printSpecies() {
>printSpecies : Symbol(Animal.printSpecies, Decl(unusedLocalProperty.ts, 3, 5))

let { species } = this;
>species : Symbol(species, Decl(unusedLocalProperty.ts, 6, 13))
>this : Symbol(Animal, Decl(unusedLocalProperty.ts, 0, 45))

console.log(species);
>console.log : Symbol(log, Decl(unusedLocalProperty.ts, 0, 22))
>console : Symbol(console, Decl(unusedLocalProperty.ts, 0, 11))
>log : Symbol(log, Decl(unusedLocalProperty.ts, 0, 22))
>species : Symbol(species, Decl(unusedLocalProperty.ts, 6, 13))
}
}


30 changes: 30 additions & 0 deletions tests/baselines/reference/unusedLocalProperty.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
=== tests/cases/compiler/unusedLocalProperty.ts ===
declare var console: { log(msg: any): void; }
>console : { log(msg: any): void; }
>log : (msg: any) => void
>msg : any

class Animal {
>Animal : Animal

constructor(private species: string) {
>species : string
}

printSpecies() {
>printSpecies : () => void

let { species } = this;
>species : string
>this : this

console.log(species);
>console.log(species) : void
>console.log : (msg: any) => void
>console : { log(msg: any): void; }
>log : (msg: any) => void
>species : string
}
}


12 changes: 12 additions & 0 deletions tests/cases/compiler/unusedLocalProperty.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//@noUnusedLocals:true
declare var console: { log(msg: any): void; }
class Animal {
constructor(private species: string) {
}

printSpecies() {
let { species } = this;
console.log(species);
}
}

0 comments on commit 4a90614

Please sign in to comment.