Skip to content

Commit 4a90614

Browse files
authored
Merge pull request #11998 from Microsoft/unusedProperty
Mark property referenced in the destructuring as referenced
2 parents defc053 + 13e8f7f commit 4a90614

File tree

5 files changed

+113
-11
lines changed

5 files changed

+113
-11
lines changed

src/compiler/checker.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11534,6 +11534,21 @@ namespace ts {
1153411534
diagnostics.add(createDiagnosticForNodeFromMessageChain(propNode, errorInfo));
1153511535
}
1153611536

11537+
function markPropertyAsReferenced(prop: Symbol) {
11538+
if (prop &&
11539+
noUnusedIdentifiers &&
11540+
(prop.flags & SymbolFlags.ClassMember) &&
11541+
prop.valueDeclaration && (getModifierFlags(prop.valueDeclaration) & ModifierFlags.Private)) {
11542+
if (prop.flags & SymbolFlags.Instantiated) {
11543+
getSymbolLinks(prop).target.isReferenced = true;
11544+
11545+
}
11546+
else {
11547+
prop.isReferenced = true;
11548+
}
11549+
}
11550+
}
11551+
1153711552
function checkPropertyAccessExpressionOrQualifiedName(node: PropertyAccessExpression | QualifiedName, left: Expression | QualifiedName, right: Identifier) {
1153811553
const type = checkNonNullExpression(left);
1153911554
if (isTypeAny(type) || type === silentNeverType) {
@@ -11553,17 +11568,7 @@ namespace ts {
1155311568
return unknownType;
1155411569
}
1155511570

11556-
if (noUnusedIdentifiers &&
11557-
(prop.flags & SymbolFlags.ClassMember) &&
11558-
prop.valueDeclaration && (getModifierFlags(prop.valueDeclaration) & ModifierFlags.Private)) {
11559-
if (prop.flags & SymbolFlags.Instantiated) {
11560-
getSymbolLinks(prop).target.isReferenced = true;
11561-
11562-
}
11563-
else {
11564-
prop.isReferenced = true;
11565-
}
11566-
}
11571+
markPropertyAsReferenced(prop);
1156711572

1156811573
getNodeLinks(node).resolvedSymbol = prop;
1156911574

@@ -16353,6 +16358,7 @@ namespace ts {
1635316358
const parentType = getTypeForBindingElementParent(parent);
1635416359
const name = node.propertyName || <Identifier>node.name;
1635516360
const property = getPropertyOfType(parentType, getTextOfPropertyName(name));
16361+
markPropertyAsReferenced(property);
1635616362
if (parent.initializer && property && getParentOfSymbol(property)) {
1635716363
checkClassPropertyAccess(parent, parent.initializer, parentType, property);
1635816364
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//// [unusedLocalProperty.ts]
2+
declare var console: { log(msg: any): void; }
3+
class Animal {
4+
constructor(private species: string) {
5+
}
6+
7+
printSpecies() {
8+
let { species } = this;
9+
console.log(species);
10+
}
11+
}
12+
13+
14+
15+
//// [unusedLocalProperty.js]
16+
var Animal = (function () {
17+
function Animal(species) {
18+
this.species = species;
19+
}
20+
Animal.prototype.printSpecies = function () {
21+
var species = this.species;
22+
console.log(species);
23+
};
24+
return Animal;
25+
}());
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
=== tests/cases/compiler/unusedLocalProperty.ts ===
2+
declare var console: { log(msg: any): void; }
3+
>console : Symbol(console, Decl(unusedLocalProperty.ts, 0, 11))
4+
>log : Symbol(log, Decl(unusedLocalProperty.ts, 0, 22))
5+
>msg : Symbol(msg, Decl(unusedLocalProperty.ts, 0, 27))
6+
7+
class Animal {
8+
>Animal : Symbol(Animal, Decl(unusedLocalProperty.ts, 0, 45))
9+
10+
constructor(private species: string) {
11+
>species : Symbol(Animal.species, Decl(unusedLocalProperty.ts, 2, 16))
12+
}
13+
14+
printSpecies() {
15+
>printSpecies : Symbol(Animal.printSpecies, Decl(unusedLocalProperty.ts, 3, 5))
16+
17+
let { species } = this;
18+
>species : Symbol(species, Decl(unusedLocalProperty.ts, 6, 13))
19+
>this : Symbol(Animal, Decl(unusedLocalProperty.ts, 0, 45))
20+
21+
console.log(species);
22+
>console.log : Symbol(log, Decl(unusedLocalProperty.ts, 0, 22))
23+
>console : Symbol(console, Decl(unusedLocalProperty.ts, 0, 11))
24+
>log : Symbol(log, Decl(unusedLocalProperty.ts, 0, 22))
25+
>species : Symbol(species, Decl(unusedLocalProperty.ts, 6, 13))
26+
}
27+
}
28+
29+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
=== tests/cases/compiler/unusedLocalProperty.ts ===
2+
declare var console: { log(msg: any): void; }
3+
>console : { log(msg: any): void; }
4+
>log : (msg: any) => void
5+
>msg : any
6+
7+
class Animal {
8+
>Animal : Animal
9+
10+
constructor(private species: string) {
11+
>species : string
12+
}
13+
14+
printSpecies() {
15+
>printSpecies : () => void
16+
17+
let { species } = this;
18+
>species : string
19+
>this : this
20+
21+
console.log(species);
22+
>console.log(species) : void
23+
>console.log : (msg: any) => void
24+
>console : { log(msg: any): void; }
25+
>log : (msg: any) => void
26+
>species : string
27+
}
28+
}
29+
30+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//@noUnusedLocals:true
2+
declare var console: { log(msg: any): void; }
3+
class Animal {
4+
constructor(private species: string) {
5+
}
6+
7+
printSpecies() {
8+
let { species } = this;
9+
console.log(species);
10+
}
11+
}
12+

0 commit comments

Comments
 (0)