Skip to content

Commit 13e8f7f

Browse files
committed
Mark property referenced in the destructuring as referenced
Fixes #11324
1 parent 116c878 commit 13e8f7f

File tree

4 files changed

+76
-29
lines changed

4 files changed

+76
-29
lines changed

src/compiler/checker.ts

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

11507+
function markPropertyAsReferenced(prop: Symbol) {
11508+
if (prop &&
11509+
noUnusedIdentifiers &&
11510+
(prop.flags & SymbolFlags.ClassMember) &&
11511+
prop.valueDeclaration && (getModifierFlags(prop.valueDeclaration) & ModifierFlags.Private)) {
11512+
if (prop.flags & SymbolFlags.Instantiated) {
11513+
getSymbolLinks(prop).target.isReferenced = true;
11514+
11515+
}
11516+
else {
11517+
prop.isReferenced = true;
11518+
}
11519+
}
11520+
}
11521+
1150711522
function checkPropertyAccessExpressionOrQualifiedName(node: PropertyAccessExpression | QualifiedName, left: Expression | QualifiedName, right: Identifier) {
1150811523
const type = checkNonNullExpression(left);
1150911524
if (isTypeAny(type) || type === silentNeverType) {
@@ -11523,17 +11538,7 @@ namespace ts {
1152311538
return unknownType;
1152411539
}
1152511540

11526-
if (noUnusedIdentifiers &&
11527-
(prop.flags & SymbolFlags.ClassMember) &&
11528-
prop.valueDeclaration && (getModifierFlags(prop.valueDeclaration) & ModifierFlags.Private)) {
11529-
if (prop.flags & SymbolFlags.Instantiated) {
11530-
getSymbolLinks(prop).target.isReferenced = true;
11531-
11532-
}
11533-
else {
11534-
prop.isReferenced = true;
11535-
}
11536-
}
11541+
markPropertyAsReferenced(prop);
1153711542

1153811543
getNodeLinks(node).resolvedSymbol = prop;
1153911544

@@ -16323,6 +16328,7 @@ namespace ts {
1632316328
const parentType = getTypeForBindingElementParent(parent);
1632416329
const name = node.propertyName || <Identifier>node.name;
1632516330
const property = getPropertyOfType(parentType, getTextOfPropertyName(name));
16331+
markPropertyAsReferenced(property);
1632616332
if (parent.initializer && property && getParentOfSymbol(property)) {
1632716333
checkClassPropertyAccess(parent, parent.initializer, parentType, property);
1632816334
}

tests/baselines/reference/unusedLocalProperty.errors.txt

Lines changed: 0 additions & 18 deletions
This file was deleted.
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+

0 commit comments

Comments
 (0)