diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 0bedc9f404a..33ff04697f9 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -10241,8 +10241,13 @@ namespace ts { return unknownType; } - if (noUnusedIdentifiers && !isInAmbientContext(node)) { - prop.isReferenced = true; + if (noUnusedIdentifiers && (prop.flags & SymbolFlags.ClassMember)) { + if (prop.flags & SymbolFlags.Instantiated) { + getSymbolLinks(prop).target.isReferenced = true; + } + else { + prop.isReferenced = true; + } } getNodeLinks(node).resolvedSymbol = prop; diff --git a/src/compiler/types.ts b/src/compiler/types.ts index a3d7fd4823d..7d7e92f27c3 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -2107,6 +2107,8 @@ namespace ts { PropertyOrAccessor = Property | Accessor, Export = ExportNamespace | ExportType | ExportValue, + ClassMember = Method | Accessor | Property, + /* @internal */ // The set of things we consider semantically classifiable. Used to speed up the LS during // classification. diff --git a/tests/baselines/reference/unusedPrivateMembers.js b/tests/baselines/reference/unusedPrivateMembers.js new file mode 100644 index 00000000000..cdb540349c3 --- /dev/null +++ b/tests/baselines/reference/unusedPrivateMembers.js @@ -0,0 +1,114 @@ +//// [unusedPrivateMembers.ts] + +class Test1 { + private initializeInternal() { + } + + public test() { + var x = new Test1(); + x.initializeInternal(); + } +} + +class Test2 { + private p = 0; + public test() { + var x = new Test2(); + x.p; + } +} + +class Test3 { + private get x () { + return 0; + } + + public test() { + var x = new Test3(); + x.x; + } +} + +class Test4 { + private set x(v) { + v; + } + + public test() { + var x = new Test4(); + x.x; + } +} + +class Test5 { + private p: T; + public test() { + var x = new Test5(); + x.p; + } +} + + +//// [unusedPrivateMembers.js] +var Test1 = (function () { + function Test1() { + } + Test1.prototype.initializeInternal = function () { + }; + Test1.prototype.test = function () { + var x = new Test1(); + x.initializeInternal(); + }; + return Test1; +}()); +var Test2 = (function () { + function Test2() { + this.p = 0; + } + Test2.prototype.test = function () { + var x = new Test2(); + x.p; + }; + return Test2; +}()); +var Test3 = (function () { + function Test3() { + } + Object.defineProperty(Test3.prototype, "x", { + get: function () { + return 0; + }, + enumerable: true, + configurable: true + }); + Test3.prototype.test = function () { + var x = new Test3(); + x.x; + }; + return Test3; +}()); +var Test4 = (function () { + function Test4() { + } + Object.defineProperty(Test4.prototype, "x", { + set: function (v) { + v; + }, + enumerable: true, + configurable: true + }); + Test4.prototype.test = function () { + var x = new Test4(); + x.x; + }; + return Test4; +}()); +var Test5 = (function () { + function Test5() { + } + Test5.prototype.test = function () { + var x = new Test5(); + x.p; + }; + return Test5; +}()); diff --git a/tests/baselines/reference/unusedPrivateMembers.symbols b/tests/baselines/reference/unusedPrivateMembers.symbols new file mode 100644 index 00000000000..0d4426a594a --- /dev/null +++ b/tests/baselines/reference/unusedPrivateMembers.symbols @@ -0,0 +1,113 @@ +=== tests/cases/compiler/unusedPrivateMembers.ts === + +class Test1 { +>Test1 : Symbol(Test1, Decl(unusedPrivateMembers.ts, 0, 0)) + + private initializeInternal() { +>initializeInternal : Symbol(Test1.initializeInternal, Decl(unusedPrivateMembers.ts, 1, 13)) + } + + public test() { +>test : Symbol(Test1.test, Decl(unusedPrivateMembers.ts, 3, 5)) + + var x = new Test1(); +>x : Symbol(x, Decl(unusedPrivateMembers.ts, 6, 11)) +>Test1 : Symbol(Test1, Decl(unusedPrivateMembers.ts, 0, 0)) + + x.initializeInternal(); +>x.initializeInternal : Symbol(Test1.initializeInternal, Decl(unusedPrivateMembers.ts, 1, 13)) +>x : Symbol(x, Decl(unusedPrivateMembers.ts, 6, 11)) +>initializeInternal : Symbol(Test1.initializeInternal, Decl(unusedPrivateMembers.ts, 1, 13)) + } +} + +class Test2 { +>Test2 : Symbol(Test2, Decl(unusedPrivateMembers.ts, 9, 1)) + + private p = 0; +>p : Symbol(Test2.p, Decl(unusedPrivateMembers.ts, 11, 13)) + + public test() { +>test : Symbol(Test2.test, Decl(unusedPrivateMembers.ts, 12, 18)) + + var x = new Test2(); +>x : Symbol(x, Decl(unusedPrivateMembers.ts, 14, 11)) +>Test2 : Symbol(Test2, Decl(unusedPrivateMembers.ts, 9, 1)) + + x.p; +>x.p : Symbol(Test2.p, Decl(unusedPrivateMembers.ts, 11, 13)) +>x : Symbol(x, Decl(unusedPrivateMembers.ts, 14, 11)) +>p : Symbol(Test2.p, Decl(unusedPrivateMembers.ts, 11, 13)) + } +} + +class Test3 { +>Test3 : Symbol(Test3, Decl(unusedPrivateMembers.ts, 17, 1)) + + private get x () { +>x : Symbol(Test3.x, Decl(unusedPrivateMembers.ts, 19, 13)) + + return 0; + } + + public test() { +>test : Symbol(Test3.test, Decl(unusedPrivateMembers.ts, 22, 5)) + + var x = new Test3(); +>x : Symbol(x, Decl(unusedPrivateMembers.ts, 25, 11)) +>Test3 : Symbol(Test3, Decl(unusedPrivateMembers.ts, 17, 1)) + + x.x; +>x.x : Symbol(Test3.x, Decl(unusedPrivateMembers.ts, 19, 13)) +>x : Symbol(x, Decl(unusedPrivateMembers.ts, 25, 11)) +>x : Symbol(Test3.x, Decl(unusedPrivateMembers.ts, 19, 13)) + } +} + +class Test4 { +>Test4 : Symbol(Test4, Decl(unusedPrivateMembers.ts, 28, 1)) + + private set x(v) { +>x : Symbol(Test4.x, Decl(unusedPrivateMembers.ts, 30, 13)) +>v : Symbol(v, Decl(unusedPrivateMembers.ts, 31, 18)) + + v; +>v : Symbol(v, Decl(unusedPrivateMembers.ts, 31, 18)) + } + + public test() { +>test : Symbol(Test4.test, Decl(unusedPrivateMembers.ts, 33, 5)) + + var x = new Test4(); +>x : Symbol(x, Decl(unusedPrivateMembers.ts, 36, 11)) +>Test4 : Symbol(Test4, Decl(unusedPrivateMembers.ts, 28, 1)) + + x.x; +>x.x : Symbol(Test4.x, Decl(unusedPrivateMembers.ts, 30, 13)) +>x : Symbol(x, Decl(unusedPrivateMembers.ts, 36, 11)) +>x : Symbol(Test4.x, Decl(unusedPrivateMembers.ts, 30, 13)) + } +} + +class Test5 { +>Test5 : Symbol(Test5, Decl(unusedPrivateMembers.ts, 39, 1)) +>T : Symbol(T, Decl(unusedPrivateMembers.ts, 41, 12)) + + private p: T; +>p : Symbol(Test5.p, Decl(unusedPrivateMembers.ts, 41, 16)) +>T : Symbol(T, Decl(unusedPrivateMembers.ts, 41, 12)) + + public test() { +>test : Symbol(Test5.test, Decl(unusedPrivateMembers.ts, 42, 17)) + + var x = new Test5(); +>x : Symbol(x, Decl(unusedPrivateMembers.ts, 44, 11)) +>Test5 : Symbol(Test5, Decl(unusedPrivateMembers.ts, 39, 1)) + + x.p; +>x.p : Symbol(Test5.p, Decl(unusedPrivateMembers.ts, 41, 16)) +>x : Symbol(x, Decl(unusedPrivateMembers.ts, 44, 11)) +>p : Symbol(Test5.p, Decl(unusedPrivateMembers.ts, 41, 16)) + } +} + diff --git a/tests/baselines/reference/unusedPrivateMembers.types b/tests/baselines/reference/unusedPrivateMembers.types new file mode 100644 index 00000000000..1d303068f24 --- /dev/null +++ b/tests/baselines/reference/unusedPrivateMembers.types @@ -0,0 +1,121 @@ +=== tests/cases/compiler/unusedPrivateMembers.ts === + +class Test1 { +>Test1 : Test1 + + private initializeInternal() { +>initializeInternal : () => void + } + + public test() { +>test : () => void + + var x = new Test1(); +>x : Test1 +>new Test1() : Test1 +>Test1 : typeof Test1 + + x.initializeInternal(); +>x.initializeInternal() : void +>x.initializeInternal : () => void +>x : Test1 +>initializeInternal : () => void + } +} + +class Test2 { +>Test2 : Test2 + + private p = 0; +>p : number +>0 : number + + public test() { +>test : () => void + + var x = new Test2(); +>x : Test2 +>new Test2() : Test2 +>Test2 : typeof Test2 + + x.p; +>x.p : number +>x : Test2 +>p : number + } +} + +class Test3 { +>Test3 : Test3 + + private get x () { +>x : number + + return 0; +>0 : number + } + + public test() { +>test : () => void + + var x = new Test3(); +>x : Test3 +>new Test3() : Test3 +>Test3 : typeof Test3 + + x.x; +>x.x : number +>x : Test3 +>x : number + } +} + +class Test4 { +>Test4 : Test4 + + private set x(v) { +>x : any +>v : any + + v; +>v : any + } + + public test() { +>test : () => void + + var x = new Test4(); +>x : Test4 +>new Test4() : Test4 +>Test4 : typeof Test4 + + x.x; +>x.x : any +>x : Test4 +>x : any + } +} + +class Test5 { +>Test5 : Test5 +>T : T + + private p: T; +>p : T +>T : T + + public test() { +>test : () => void + + var x = new Test5(); +>x : Test5 +>new Test5() : Test5 +>Test5 : typeof Test5 + + x.p; +>x.p : number +>x : Test5 +>p : number + } +} + diff --git a/tests/cases/compiler/unusedPrivateMembers.ts b/tests/cases/compiler/unusedPrivateMembers.ts new file mode 100644 index 00000000000..ab4e5ba9bca --- /dev/null +++ b/tests/cases/compiler/unusedPrivateMembers.ts @@ -0,0 +1,51 @@ +//@noUnusedLocals:true +//@noUnusedParameters:true +//@target:ES5 + +class Test1 { + private initializeInternal() { + } + + public test() { + var x = new Test1(); + x.initializeInternal(); + } +} + +class Test2 { + private p = 0; + public test() { + var x = new Test2(); + x.p; + } +} + +class Test3 { + private get x () { + return 0; + } + + public test() { + var x = new Test3(); + x.x; + } +} + +class Test4 { + private set x(v) { + v; + } + + public test() { + var x = new Test4(); + x.x; + } +} + +class Test5 { + private p: T; + public test() { + var x = new Test5(); + x.p; + } +}