Mark property referenced in the destructuring as referenced

Fixes #11324
This commit is contained in:
Sheetal Nandi
2016-11-02 11:08:34 -07:00
parent 116c87819a
commit 13e8f7fada
4 changed files with 76 additions and 29 deletions

View File

@@ -11504,6 +11504,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) {
@@ -11523,17 +11538,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;
@@ -16323,6 +16328,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);
}

View File

@@ -1,18 +0,0 @@
tests/cases/compiler/unusedLocalProperty.ts(3,25): error TS6138: Property 'species' is declared but never used.
==== tests/cases/compiler/unusedLocalProperty.ts (1 errors) ====
declare var console: { log(msg: any): void; }
class Animal {
constructor(private species: string) {
~~~~~~~
!!! error TS6138: Property 'species' is declared but never used.
}
printSpecies() {
let { species } = this;
console.log(species);
}
}

View File

@@ -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))
}
}

View File

@@ -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
}
}