Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mark property referenced in the destructuring as referenced #11998

Merged
merged 2 commits into from
Nov 4, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;

Expand Down Expand Up @@ -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);
}
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);
}
}