Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increase size of span for unused declaration #22388

Merged
1 commit merged into from
Mar 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21614,7 +21614,7 @@ namespace ts {
}

if (!isRemovedPropertyFromObjectSpread(node.kind === SyntaxKind.Identifier ? node.parent : node)) {
error(node, Diagnostics._0_is_declared_but_its_value_is_never_read, name);
diagnostics.add(createDiagnosticForNodeSpan(getSourceFileOfNode(declaration), declaration, node, Diagnostics._0_is_declared_but_its_value_is_never_read, name));
}
}

Expand All @@ -21623,7 +21623,7 @@ namespace ts {
}

function isIdentifierThatStartsWithUnderScore(node: Node) {
return node.kind === SyntaxKind.Identifier && idText(<Identifier>node).charCodeAt(0) === CharacterCodes._;
return isIdentifier(node) && idText(node).charCodeAt(0) === CharacterCodes._;
}

function checkUnusedClassMembers(node: ClassDeclaration | ClassExpression): void {
Expand Down
5 changes: 5 additions & 0 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,11 @@ namespace ts {
return createFileDiagnostic(sourceFile, span.start, span.length, message, arg0, arg1, arg2, arg3);
}

export function createDiagnosticForNodeSpan(sourceFile: SourceFile, startNode: Node, endNode: Node, message: DiagnosticMessage, arg0?: string | number, arg1?: string | number, arg2?: string | number, arg3?: string | number): Diagnostic {
const start = skipTrivia(sourceFile.text, startNode.pos);
return createFileDiagnostic(sourceFile, start, endNode.end - start, message, arg0, arg1, arg2, arg3);
}

export function createDiagnosticForNodeFromMessageChain(node: Node, messageChain: DiagnosticMessageChain): Diagnostic {
const sourceFile = getSourceFileOfNode(node);
const span = getErrorSpanForNode(sourceFile, node);
Expand Down
8 changes: 4 additions & 4 deletions src/services/codefixes/fixUnusedIdentifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace ts.codefix {
errorCodes,
getCodeActions(context) {
const { sourceFile } = context;
const token = getToken(sourceFile, context.span.start);
const token = getToken(sourceFile, textSpanEnd(context.span));
const result: CodeFixAction[] = [];

const deletion = textChanges.ChangeTracker.with(context, t => tryDeleteDeclaration(t, sourceFile, token));
Expand All @@ -30,7 +30,7 @@ namespace ts.codefix {
fixIds: [fixIdPrefix, fixIdDelete],
getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) => {
const { sourceFile } = context;
const token = getToken(diag.file!, diag.start!);
const token = findPrecedingToken(textSpanEnd(diag), diag.file!);
switch (context.fixId) {
case fixIdPrefix:
if (isIdentifier(token) && canPrefix(token)) {
Expand All @@ -47,9 +47,9 @@ namespace ts.codefix {
});

function getToken(sourceFile: SourceFile, pos: number): Node {
const token = getTokenAtPosition(sourceFile, pos, /*includeJsDocComment*/ false);
const token = findPrecedingToken(pos, sourceFile);
// this handles var ["computed"] = 12;
return token.kind === SyntaxKind.OpenBracketToken ? getTokenAtPosition(sourceFile, pos + 1, /*includeJsDocComment*/ false) : token;
return token.kind === SyntaxKind.CloseBracketToken ? findPrecedingToken(pos - 1, sourceFile) : token;
}

function tryPrefixDeclaration(changes: textChanges.ChangeTracker, errorCode: number, sourceFile: SourceFile, token: Node): void {
Expand Down
28 changes: 14 additions & 14 deletions tests/baselines/reference/noUnusedLocals_selfReference.errors.txt
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
tests/cases/compiler/noUnusedLocals_selfReference.ts(3,10): error TS6133: 'f' is declared but its value is never read.
tests/cases/compiler/noUnusedLocals_selfReference.ts(5,14): error TS6133: 'g' is declared but its value is never read.
tests/cases/compiler/noUnusedLocals_selfReference.ts(9,7): error TS6133: 'C' is declared but its value is never read.
tests/cases/compiler/noUnusedLocals_selfReference.ts(12,6): error TS6133: 'E' is declared but its value is never read.
tests/cases/compiler/noUnusedLocals_selfReference.ts(13,11): error TS6133: 'I' is declared but its value is never read.
tests/cases/compiler/noUnusedLocals_selfReference.ts(14,6): error TS6133: 'T' is declared but its value is never read.
tests/cases/compiler/noUnusedLocals_selfReference.ts(15,11): error TS6133: 'N' is declared but its value is never read.
tests/cases/compiler/noUnusedLocals_selfReference.ts(3,1): error TS6133: 'f' is declared but its value is never read.
tests/cases/compiler/noUnusedLocals_selfReference.ts(5,5): error TS6133: 'g' is declared but its value is never read.
tests/cases/compiler/noUnusedLocals_selfReference.ts(9,1): error TS6133: 'C' is declared but its value is never read.
tests/cases/compiler/noUnusedLocals_selfReference.ts(12,1): error TS6133: 'E' is declared but its value is never read.
tests/cases/compiler/noUnusedLocals_selfReference.ts(13,1): error TS6133: 'I' is declared but its value is never read.
tests/cases/compiler/noUnusedLocals_selfReference.ts(14,1): error TS6133: 'T' is declared but its value is never read.
tests/cases/compiler/noUnusedLocals_selfReference.ts(15,1): error TS6133: 'N' is declared but its value is never read.
tests/cases/compiler/noUnusedLocals_selfReference.ts(22,19): error TS6133: 'm' is declared but its value is never read.


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

function f() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about generic functions?

function f<T>() {
function f<T where T extends K> () {

Also what about async functions?

async function f() {

Also what about class methods?

private f() {

~
~~~~~~~~~~
!!! error TS6133: 'f' is declared but its value is never read.
f;
function g() {
~
~~~~~~~~~~
!!! error TS6133: 'g' is declared but its value is never read.
g;
}
}
class C {
~
~~~~~~~
!!! error TS6133: 'C' is declared but its value is never read.
m() { C; }
}
enum E { A = 0, B = E.A }
~
~~~~~~
!!! error TS6133: 'E' is declared but its value is never read.
interface I { x: I };
~
~~~~~~~~~~~
!!! error TS6133: 'I' is declared but its value is never read.
type T = { x: T };
~
~~~~~~
!!! error TS6133: 'T' is declared but its value is never read.
namespace N { N; }
~
~~~~~~~~~~~
!!! error TS6133: 'N' is declared but its value is never read.

// Avoid a false positive.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
tests/cases/compiler/noUnusedLocals_selfReference_skipsBlockLocations.ts(2,14): error TS6133: 'f' is declared but its value is never read.
tests/cases/compiler/noUnusedLocals_selfReference_skipsBlockLocations.ts(8,22): error TS6133: 'g' is declared but its value is never read.
tests/cases/compiler/noUnusedLocals_selfReference_skipsBlockLocations.ts(12,22): error TS6133: 'h' is declared but its value is never read.
tests/cases/compiler/noUnusedLocals_selfReference_skipsBlockLocations.ts(2,5): error TS6133: 'f' is declared but its value is never read.
tests/cases/compiler/noUnusedLocals_selfReference_skipsBlockLocations.ts(8,13): error TS6133: 'g' is declared but its value is never read.
tests/cases/compiler/noUnusedLocals_selfReference_skipsBlockLocations.ts(12,13): error TS6133: 'h' is declared but its value is never read.


==== tests/cases/compiler/noUnusedLocals_selfReference_skipsBlockLocations.ts (3 errors) ====
namespace n {
function f() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comments or extensive whitespace/newlines between function and f — will those be included in the span, or break off the function part out of the span?

~
~~~~~~~~~~
!!! error TS6133: 'f' is declared but its value is never read.
f;
}

switch (0) {
case 0:
function g() {
~
~~~~~~~~~~
!!! error TS6133: 'g' is declared but its value is never read.
g;
}
default:
function h() {
~
~~~~~~~~~~
!!! error TS6133: 'h' is declared but its value is never read.
h;
}
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/unusedClassesinModule1.errors.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
tests/cases/compiler/unusedClassesinModule1.ts(2,11): error TS6133: 'Calculator' is declared but its value is never read.
tests/cases/compiler/unusedClassesinModule1.ts(2,5): error TS6133: 'Calculator' is declared but its value is never read.


==== tests/cases/compiler/unusedClassesinModule1.ts (1 errors) ====
module A {
class Calculator {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's going to happen for generic class?

Calculator<T>
Calculator<T where T extends K>

~~~~~~~~~~
~~~~~~~~~~~~~~~~
!!! error TS6133: 'Calculator' is declared but its value is never read.
public handelChar() {
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
tests/cases/compiler/unusedClassesinNamespace1.ts(2,11): error TS6133: 'c1' is declared but its value is never read.
tests/cases/compiler/unusedClassesinNamespace1.ts(2,5): error TS6133: 'c1' is declared but its value is never read.


==== tests/cases/compiler/unusedClassesinNamespace1.ts (1 errors) ====
namespace Validation {
class c1 {
~~
~~~~~~~~
!!! error TS6133: 'c1' is declared but its value is never read.

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
tests/cases/compiler/unusedClassesinNamespace2.ts(2,11): error TS6133: 'c1' is declared but its value is never read.
tests/cases/compiler/unusedClassesinNamespace2.ts(2,5): error TS6133: 'c1' is declared but its value is never read.


==== tests/cases/compiler/unusedClassesinNamespace2.ts (1 errors) ====
namespace Validation {
class c1 {
~~
~~~~~~~~
!!! error TS6133: 'c1' is declared but its value is never read.

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
tests/cases/compiler/unusedClassesinNamespace4.ts(10,11): error TS6133: 'c3' is declared but its value is never read.
tests/cases/compiler/unusedClassesinNamespace4.ts(10,5): error TS6133: 'c3' is declared but its value is never read.


==== tests/cases/compiler/unusedClassesinNamespace4.ts (1 errors) ====
Expand All @@ -12,7 +12,7 @@ tests/cases/compiler/unusedClassesinNamespace4.ts(10,11): error TS6133: 'c3' is
}

class c3 extends c1 {
~~
~~~~~~~~
!!! error TS6133: 'c3' is declared but its value is never read.

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
tests/cases/compiler/unusedClassesinNamespace5.ts(10,11): error TS6133: 'c3' is declared but its value is never read.
tests/cases/compiler/unusedClassesinNamespace5.ts(10,5): error TS6133: 'c3' is declared but its value is never read.


==== tests/cases/compiler/unusedClassesinNamespace5.ts (1 errors) ====
Expand All @@ -12,7 +12,7 @@ tests/cases/compiler/unusedClassesinNamespace5.ts(10,11): error TS6133: 'c3' is
}

class c3 {
~~
~~~~~~~~
!!! error TS6133: 'c3' is declared but its value is never read.
public x: c1;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
tests/cases/compiler/unusedFunctionsinNamespaces1.ts(2,14): error TS6133: 'function1' is declared but its value is never read.
tests/cases/compiler/unusedFunctionsinNamespaces1.ts(2,5): error TS6133: 'function1' is declared but its value is never read.


==== tests/cases/compiler/unusedFunctionsinNamespaces1.ts (1 errors) ====
namespace Validation {
function function1() {
~~~~~~~~~
~~~~~~~~~~~~~~~~~~
!!! error TS6133: 'function1' is declared but its value is never read.
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
tests/cases/compiler/unusedFunctionsinNamespaces5.ts(9,14): error TS6133: 'function3' is declared but its value is never read.
tests/cases/compiler/unusedFunctionsinNamespaces5.ts(13,14): error TS6133: 'function4' is declared but its value is never read.
tests/cases/compiler/unusedFunctionsinNamespaces5.ts(9,5): error TS6133: 'function3' is declared but its value is never read.
tests/cases/compiler/unusedFunctionsinNamespaces5.ts(13,5): error TS6133: 'function4' is declared but its value is never read.


==== tests/cases/compiler/unusedFunctionsinNamespaces5.ts (2 errors) ====
Expand All @@ -12,13 +12,13 @@ tests/cases/compiler/unusedFunctionsinNamespaces5.ts(13,14): error TS6133: 'func
}

function function3() {
~~~~~~~~~
~~~~~~~~~~~~~~~~~~
!!! error TS6133: 'function3' is declared but its value is never read.
function1();
}

function function4() {
~~~~~~~~~
~~~~~~~~~~~~~~~~~~
!!! error TS6133: 'function4' is declared but its value is never read.

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
tests/cases/compiler/unusedFunctionsinNamespaces6.ts(13,14): error TS6133: 'function4' is declared but its value is never read.
tests/cases/compiler/unusedFunctionsinNamespaces6.ts(13,5): error TS6133: 'function4' is declared but its value is never read.


==== tests/cases/compiler/unusedFunctionsinNamespaces6.ts (1 errors) ====
Expand All @@ -15,7 +15,7 @@ tests/cases/compiler/unusedFunctionsinNamespaces6.ts(13,14): error TS6133: 'func
}

function function4() {
~~~~~~~~~
~~~~~~~~~~~~~~~~~~
!!! error TS6133: 'function4' is declared but its value is never read.

}
Expand Down
20 changes: 10 additions & 10 deletions tests/baselines/reference/unusedIdentifiersConsolidated1.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ tests/cases/compiler/unusedIdentifiersConsolidated1.ts(17,13): error TS6133: 'un
tests/cases/compiler/unusedIdentifiersConsolidated1.ts(24,13): error TS6133: 'unUsedPrivateFunction' is declared but its value is never read.
tests/cases/compiler/unusedIdentifiersConsolidated1.ts(37,11): error TS6133: 'numberRegexp' is declared but its value is never read.
tests/cases/compiler/unusedIdentifiersConsolidated1.ts(44,17): error TS6133: 'unUsedPrivateFunction' is declared but its value is never read.
tests/cases/compiler/unusedIdentifiersConsolidated1.ts(57,15): error TS6133: 'usedLocallyInterface2' is declared but its value is never read.
tests/cases/compiler/unusedIdentifiersConsolidated1.ts(64,11): error TS6133: 'dummy' is declared but its value is never read.
tests/cases/compiler/unusedIdentifiersConsolidated1.ts(67,15): error TS6133: 'unusedInterface' is declared but its value is never read.
tests/cases/compiler/unusedIdentifiersConsolidated1.ts(79,11): error TS6133: 'class3' is declared but its value is never read.
tests/cases/compiler/unusedIdentifiersConsolidated1.ts(99,15): error TS6133: 'interface5' is declared but its value is never read.
tests/cases/compiler/unusedIdentifiersConsolidated1.ts(57,5): error TS6133: 'usedLocallyInterface2' is declared but its value is never read.
tests/cases/compiler/unusedIdentifiersConsolidated1.ts(64,5): error TS6133: 'dummy' is declared but its value is never read.
tests/cases/compiler/unusedIdentifiersConsolidated1.ts(67,5): error TS6133: 'unusedInterface' is declared but its value is never read.
tests/cases/compiler/unusedIdentifiersConsolidated1.ts(79,5): error TS6133: 'class3' is declared but its value is never read.
tests/cases/compiler/unusedIdentifiersConsolidated1.ts(99,5): error TS6133: 'interface5' is declared but its value is never read.


==== tests/cases/compiler/unusedIdentifiersConsolidated1.ts (17 errors) ====
Expand Down Expand Up @@ -99,7 +99,7 @@ tests/cases/compiler/unusedIdentifiersConsolidated1.ts(99,15): error TS6133: 'in
}

interface usedLocallyInterface2 {
~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS6133: 'usedLocallyInterface2' is declared but its value is never read.
someFunction(s1: string): void;
}
Expand All @@ -108,12 +108,12 @@ tests/cases/compiler/unusedIdentifiersConsolidated1.ts(99,15): error TS6133: 'in
}

class dummy implements usedLocallyInterface {
~~~~~
~~~~~~~~~~~
!!! error TS6133: 'dummy' is declared but its value is never read.
}

interface unusedInterface {
~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS6133: 'unusedInterface' is declared but its value is never read.
}
}
Expand All @@ -127,7 +127,7 @@ tests/cases/compiler/unusedIdentifiersConsolidated1.ts(99,15): error TS6133: 'in
}

class class3 {
~~~~~~
~~~~~~~~~~~~
!!! error TS6133: 'class3' is declared but its value is never read.
}

Expand All @@ -149,7 +149,7 @@ tests/cases/compiler/unusedIdentifiersConsolidated1.ts(99,15): error TS6133: 'in
export let a: interface3;

interface interface5 {
~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~
!!! error TS6133: 'interface5' is declared but its value is never read.
}
}
4 changes: 2 additions & 2 deletions tests/baselines/reference/unusedImports10.errors.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
tests/cases/compiler/unusedImports10.ts(9,12): error TS6133: 'a' is declared but its value is never read.
tests/cases/compiler/unusedImports10.ts(9,5): error TS6133: 'a' is declared but its value is never read.


==== tests/cases/compiler/unusedImports10.ts (1 errors) ====
Expand All @@ -11,6 +11,6 @@ tests/cases/compiler/unusedImports10.ts(9,12): error TS6133: 'a' is declared but

module B {
import a = A;
~
~~~~~~~~
!!! error TS6133: 'a' is declared but its value is never read.
}
12 changes: 6 additions & 6 deletions tests/baselines/reference/unusedImports12.errors.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
tests/cases/compiler/a.ts(1,10): error TS6133: 'Member' is declared but its value is never read.
tests/cases/compiler/a.ts(2,8): error TS6133: 'd' is declared but its value is never read.
tests/cases/compiler/a.ts(2,23): error TS6133: 'M' is declared but its value is never read.
tests/cases/compiler/a.ts(3,13): error TS6133: 'ns' is declared but its value is never read.
tests/cases/compiler/a.ts(4,8): error TS6133: 'r' is declared but its value is never read.
tests/cases/compiler/a.ts(2,13): error TS6133: 'M' is declared but its value is never read.
tests/cases/compiler/a.ts(3,8): error TS6133: 'ns' is declared but its value is never read.
tests/cases/compiler/a.ts(4,1): error TS6133: 'r' is declared but its value is never read.


==== tests/cases/compiler/a.ts (5 errors) ====
Expand All @@ -12,13 +12,13 @@ tests/cases/compiler/a.ts(4,8): error TS6133: 'r' is declared but its value is n
import d, { Member as M } from './b';
~
!!! error TS6133: 'd' is declared but its value is never read.
~
~~~~~~~~~~~
!!! error TS6133: 'M' is declared but its value is never read.
import * as ns from './b';
~~
~~~~~~~
!!! error TS6133: 'ns' is declared but its value is never read.
import r = require("./b");
~
~~~~~~~~
!!! error TS6133: 'r' is declared but its value is never read.

==== tests/cases/compiler/b.ts (0 errors) ====
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/unusedImports7.errors.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
tests/cases/compiler/file2.ts(1,13): error TS6133: 'n' is declared but its value is never read.
tests/cases/compiler/file2.ts(1,8): error TS6133: 'n' is declared but its value is never read.


==== tests/cases/compiler/file1.ts (0 errors) ====
Expand All @@ -16,7 +16,7 @@ tests/cases/compiler/file2.ts(1,13): error TS6133: 'n' is declared but its value

==== tests/cases/compiler/file2.ts (1 errors) ====
import * as n from "./file1"
~
~~~~~~
!!! error TS6133: 'n' is declared but its value is never read.


Loading