Mark references to dynamically-named properties as used (#21010)

* Mark references to dynamically-named properties as used

* Avoid showing the symbol ID

* Use symbolToString instead of showSymbol
This commit is contained in:
Andy 2018-01-08 16:44:58 -08:00 committed by Mohamed Hegazy
parent 7e6315075d
commit 37d4f6a69d
6 changed files with 129 additions and 2 deletions

View file

@ -8252,6 +8252,7 @@ namespace ts {
const prop = getPropertyOfType(objectType, propName);
if (prop) {
if (accessExpression) {
markPropertyAsReferenced(prop, accessExpression, /*isThisAccess*/ accessExpression.expression.kind === SyntaxKind.ThisKeyword);
if (isAssignmentTarget(accessExpression) && (isReferenceToReadonlyEntity(accessExpression, prop) || isReferenceThroughNamespaceImport(accessExpression))) {
error(accessExpression.argumentExpression, Diagnostics.Cannot_assign_to_0_because_it_is_a_constant_or_a_read_only_property, symbolToString(prop));
return unknownType;
@ -21356,8 +21357,9 @@ namespace ts {
// Already would have reported an error on the getter.
break;
}
if (!member.symbol.isReferenced && hasModifier(member, ModifierFlags.Private)) {
error(member.name, Diagnostics._0_is_declared_but_its_value_is_never_read, symbolName(member.symbol));
const symbol = getSymbolOfNode(member);
if (!symbol.isReferenced && hasModifier(member, ModifierFlags.Private)) {
error(member.name, Diagnostics._0_is_declared_but_its_value_is_never_read, symbolToString(symbol));
}
break;
case SyntaxKind.Constructor:

View file

@ -0,0 +1,17 @@
tests/cases/compiler/noUnusedLocals_writeOnlyProperty_dynamicNames.ts(4,13): error TS6133: '[x]' is declared but its value is never read.
==== tests/cases/compiler/noUnusedLocals_writeOnlyProperty_dynamicNames.ts (1 errors) ====
const x = Symbol("x");
const y = Symbol("y");
class C {
private [x]: number;
~~~
!!! error TS6133: '[x]' is declared but its value is never read.
private [y]: number;
m() {
this[x] = 0; // write-only
this[y];
}
}

View file

@ -0,0 +1,25 @@
//// [noUnusedLocals_writeOnlyProperty_dynamicNames.ts]
const x = Symbol("x");
const y = Symbol("y");
class C {
private [x]: number;
private [y]: number;
m() {
this[x] = 0; // write-only
this[y];
}
}
//// [noUnusedLocals_writeOnlyProperty_dynamicNames.js]
var x = Symbol("x");
var y = Symbol("y");
var C = /** @class */ (function () {
function C() {
}
C.prototype.m = function () {
this[x] = 0; // write-only
this[y];
};
return C;
}());

View file

@ -0,0 +1,31 @@
=== tests/cases/compiler/noUnusedLocals_writeOnlyProperty_dynamicNames.ts ===
const x = Symbol("x");
>x : Symbol(x, Decl(noUnusedLocals_writeOnlyProperty_dynamicNames.ts, 0, 5))
>Symbol : Symbol(Symbol, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --))
const y = Symbol("y");
>y : Symbol(y, Decl(noUnusedLocals_writeOnlyProperty_dynamicNames.ts, 1, 5))
>Symbol : Symbol(Symbol, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --))
class C {
>C : Symbol(C, Decl(noUnusedLocals_writeOnlyProperty_dynamicNames.ts, 1, 22))
private [x]: number;
>x : Symbol(x, Decl(noUnusedLocals_writeOnlyProperty_dynamicNames.ts, 0, 5))
private [y]: number;
>y : Symbol(y, Decl(noUnusedLocals_writeOnlyProperty_dynamicNames.ts, 1, 5))
m() {
>m : Symbol(C.m, Decl(noUnusedLocals_writeOnlyProperty_dynamicNames.ts, 4, 24))
this[x] = 0; // write-only
>this : Symbol(C, Decl(noUnusedLocals_writeOnlyProperty_dynamicNames.ts, 1, 22))
>x : Symbol(x, Decl(noUnusedLocals_writeOnlyProperty_dynamicNames.ts, 0, 5))
this[y];
>this : Symbol(C, Decl(noUnusedLocals_writeOnlyProperty_dynamicNames.ts, 1, 22))
>y : Symbol(y, Decl(noUnusedLocals_writeOnlyProperty_dynamicNames.ts, 1, 5))
}
}

View file

@ -0,0 +1,39 @@
=== tests/cases/compiler/noUnusedLocals_writeOnlyProperty_dynamicNames.ts ===
const x = Symbol("x");
>x : unique symbol
>Symbol("x") : unique symbol
>Symbol : SymbolConstructor
>"x" : "x"
const y = Symbol("y");
>y : unique symbol
>Symbol("y") : unique symbol
>Symbol : SymbolConstructor
>"y" : "y"
class C {
>C : C
private [x]: number;
>x : unique symbol
private [y]: number;
>y : unique symbol
m() {
>m : () => void
this[x] = 0; // write-only
>this[x] = 0 : 0
>this[x] : number
>this : this
>x : unique symbol
>0 : 0
this[y];
>this[y] : number
>this : this
>y : unique symbol
}
}

View file

@ -0,0 +1,13 @@
// @noUnusedLocals: true
// @lib: es6
const x = Symbol("x");
const y = Symbol("y");
class C {
private [x]: number;
private [y]: number;
m() {
this[x] = 0; // write-only
this[y];
}
}