Skip to content

Commit

Permalink
Don't count self-reference when setting isReferenced
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Hanson committed Jul 28, 2017
1 parent 74e4903 commit da3ff37
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 38 deletions.
23 changes: 2 additions & 21 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1086,7 +1086,8 @@ namespace ts {
location = location.parent;
}

if (result && nameNotFoundMessage && noUnusedIdentifiers) {
// If result === lastLocation.symbol, this is a case of `lastLocation` referencing itself.
if (result && nameNotFoundMessage && noUnusedIdentifiers && result !== lastLocation.symbol) {
result.isReferenced = true;
}

Expand Down Expand Up @@ -10718,17 +10719,6 @@ namespace ts {
return undefined;
}

function getLeftmostIdentifierOrThis(node: Node): Node {
switch (node.kind) {
case SyntaxKind.Identifier:
case SyntaxKind.ThisKeyword:
return node;
case SyntaxKind.PropertyAccessExpression:
return getLeftmostIdentifierOrThis((<PropertyAccessExpression>node).expression);
}
return undefined;
}

function getBindingElementNameText(element: BindingElement): string | undefined {
if (element.parent.kind === SyntaxKind.ObjectBindingPattern) {
const name = element.propertyName || element.name;
Expand Down Expand Up @@ -18433,15 +18423,6 @@ namespace ts {
return forEachChild(n, containsSuperCall);
}

function markThisReferencesAsErrors(n: Node): void {
if (n.kind === SyntaxKind.ThisKeyword) {
error(n, Diagnostics.this_cannot_be_referenced_in_current_location);
}
else if (n.kind !== SyntaxKind.FunctionExpression && n.kind !== SyntaxKind.FunctionDeclaration) {
forEachChild(n, markThisReferencesAsErrors);
}
}

function isInstancePropertyWithInitializer(n: Node): boolean {
return n.kind === SyntaxKind.PropertyDeclaration &&
!(getModifierFlags(n) & ModifierFlags.Static) &&
Expand Down
28 changes: 28 additions & 0 deletions tests/baselines/reference/noUnusedLocals_selfReference.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
tests/cases/compiler/noUnusedLocals_selfReference.ts(3,10): error TS6133: 'f' is declared but never used.
tests/cases/compiler/noUnusedLocals_selfReference.ts(4,7): error TS6133: 'C' is declared but never used.
tests/cases/compiler/noUnusedLocals_selfReference.ts(7,6): error TS6133: 'E' is declared but never used.


==== tests/cases/compiler/noUnusedLocals_selfReference.ts (3 errors) ====
export {}; // Make this a module scope, so these are local variables.

function f() { f; }
~
!!! error TS6133: 'f' is declared but never used.
class C {
~
!!! error TS6133: 'C' is declared but never used.
m() { C; }
}
enum E { A = 0, B = E.A }
~
!!! error TS6133: 'E' is declared but never used.

// Does not detect mutual recursion.
function g() { D; }
class D { m() { g; } }

// Does not work on private methods.
class P { private m() { this.m; } }
P;

49 changes: 49 additions & 0 deletions tests/baselines/reference/noUnusedLocals_selfReference.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//// [noUnusedLocals_selfReference.ts]
export {}; // Make this a module scope, so these are local variables.

function f() { f; }
class C {
m() { C; }
}
enum E { A = 0, B = E.A }

// Does not detect mutual recursion.
function g() { D; }
class D { m() { g; } }

// Does not work on private methods.
class P { private m() { this.m; } }
P;


//// [noUnusedLocals_selfReference.js]
"use strict";
exports.__esModule = true;
function f() { f; }
var C = (function () {
function C() {
}
C.prototype.m = function () { C; };
return C;
}());
var E;
(function (E) {
E[E["A"] = 0] = "A";
E[E["B"] = 0] = "B";
})(E || (E = {}));
// Does not detect mutual recursion.
function g() { D; }
var D = (function () {
function D() {
}
D.prototype.m = function () { g; };
return D;
}());
// Does not work on private methods.
var P = (function () {
function P() {
}
P.prototype.m = function () { this.m; };
return P;
}());
P;
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
tests/cases/compiler/unusedLocalsAndParametersTypeAliases2.ts(2,6): error TS6133: 'handler1' is declared but never used.
tests/cases/compiler/unusedLocalsAndParametersTypeAliases2.ts(5,10): error TS6133: 'foo' is declared but never used.
tests/cases/compiler/unusedLocalsAndParametersTypeAliases2.ts(6,10): error TS6133: 'handler2' is declared but never used.


==== tests/cases/compiler/unusedLocalsAndParametersTypeAliases2.ts (2 errors) ====
==== tests/cases/compiler/unusedLocalsAndParametersTypeAliases2.ts (3 errors) ====
// unused
type handler1 = () => void;
~~~~~~~~
!!! error TS6133: 'handler1' is declared but never used.


function foo() {
~~~
!!! error TS6133: 'foo' is declared but never used.
type handler2 = () => void;
~~~~~~~~
!!! error TS6133: 'handler2' is declared but never used.
Expand Down
17 changes: 17 additions & 0 deletions tests/cases/compiler/noUnusedLocals_selfReference.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// @noUnusedLocals: true

export {}; // Make this a module scope, so these are local variables.

function f() { f; }
class C {
m() { C; }
}
enum E { A = 0, B = E.A }

// Does not detect mutual recursion.
function g() { D; }
class D { m() { g; } }

// Does not work on private methods.
class P { private m() { this.m; } }
P;
16 changes: 0 additions & 16 deletions tests/webTestServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,22 +125,6 @@ function dir(dirPath: string, spec?: string, options?: any) {
}
}

// fs.rmdirSync won't delete directories with files in it
function deleteFolderRecursive(dirPath: string) {
if (fs.existsSync(dirPath)) {
fs.readdirSync(dirPath).forEach((file) => {
const curPath = path.join(dirPath, file);
if (fs.statSync(curPath).isDirectory()) { // recurse
deleteFolderRecursive(curPath);
}
else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(dirPath);
}
};

function writeFile(path: string, data: any) {
ensureDirectoriesExist(getDirectoryPath(path));
fs.writeFileSync(path, data);
Expand Down

0 comments on commit da3ff37

Please sign in to comment.