Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Fix build #3853

Merged
merged 6 commits into from
Apr 26, 2018
Merged
Show file tree
Hide file tree
Changes from 5 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
28 changes: 28 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,28 @@ jobs:
key: dependency-cache-{{ checksum "yarn.lock" }}
- run: yarn add typescript@2.4
- run: yarn test
test2.7:
docker:
- image: circleci/node:6
steps:
- checkout
- attach_workspace:
at: '.'
- restore_cache:
key: dependency-cache-{{ checksum "yarn.lock" }}
- run: yarn add typescript@2.7
- run: yarn test
test2.8:
docker:
- image: circleci/node:6
steps:
- checkout
- attach_workspace:
at: '.'
- restore_cache:
key: dependency-cache-{{ checksum "yarn.lock" }}
- run: yarn add typescript@2.8
- run: yarn test
testNext:
docker:
- image: circleci/node:latest
Expand Down Expand Up @@ -92,6 +114,12 @@ workflows:
- test2.4:
requires:
- build
- test2.7:
requires:
- build
- test2.8:
requires:
- build
- testNext:
requires:
- build
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"tsutils": "^2.12.1"
},
"peerDependencies": {
"typescript": ">=2.1.0 || >=2.1.0-dev || >=2.2.0-dev || >=2.3.0-dev || >=2.4.0-dev || >=2.5.0-dev || >=2.6.0-dev || >=2.7.0-dev || >=2.8.0-dev"
"typescript": ">=2.1.0 || >=2.1.0-dev || >=2.2.0-dev || >=2.3.0-dev || >=2.4.0-dev || >=2.5.0-dev || >=2.6.0-dev || >=2.7.0-dev || >=2.8.0-dev || >=2.9.0-dev"
},
"devDependencies": {
"@types/babel-code-frame": "^6.20.0",
Expand All @@ -75,7 +75,7 @@
"ts-node": "^3.3.0",
"tslint": "^5.8.0",
"tslint-test-config-non-relative": "file:test/external/tslint-test-config-non-relative",
"typescript": "~2.7.2"
"typescript": "~2.8.3"
},
"license": "Apache-2.0",
"engines": {
Expand Down
86 changes: 62 additions & 24 deletions src/rules/noUnusedVariableRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,17 +126,19 @@ function walk(ctx: Lint.WalkContext<Options>, program: ts.Program): void {
}
}

if (kind === UnusedKind.VARIABLE_OR_PARAMETER) {
const importName = findImport(diag.start, sourceFile);
if (importName !== undefined) {
if (declaration && isImportUsed(importName, sourceFile, checker)) {
continue;
}
if (kind === UnusedKind.VARIABLE_OR_PARAMETER || kind === UnusedKind.DECLARATION) {
const importNames = findImports(diag.start, sourceFile, kind);
if (importNames.length > 0) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

logic changes to handle the array return type

for (const importName of importNames) {
if (declaration && isImportUsed(importName, sourceFile, checker)) {
continue;
}

if (importSpecifierFailures.has(importName)) {
throw new Error("Should not get 2 errors for the same import.");
if (importSpecifierFailures.has(importName)) {
throw new Error("Should not get 2 errors for the same import.");
}
importSpecifierFailures.set(importName, failure);
}
importSpecifierFailures.set(importName, failure);
continue;
}
}
Expand Down Expand Up @@ -337,12 +339,14 @@ function forEachImport<T>(sourceFile: ts.SourceFile, f: (i: ImportLike) => T | u
});
}

function findImport(pos: number, sourceFile: ts.SourceFile): ts.Identifier | undefined {
return forEachImport(sourceFile, (i) => {
function findImports(pos: number, sourceFile: ts.SourceFile, kind: UnusedKind): ReadonlyArray<ts.Identifier> {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

changing to an array since in the case of kind === DECLARATION we want to return all the imports in the node

const imports = forEachImport(sourceFile, (i) => {
if (!isInRange(i, pos)) {
return undefined;
}

if (i.kind === ts.SyntaxKind.ImportEqualsDeclaration) {
if (i.name.getStart() === pos) {
return i.name;
}
return [i.name];
} else {
if (i.importClause === undefined) {
// Error node
Expand All @@ -351,37 +355,71 @@ function findImport(pos: number, sourceFile: ts.SourceFile): ts.Identifier | und

const { name: defaultName, namedBindings } = i.importClause;
if (namedBindings !== undefined && namedBindings.kind === ts.SyntaxKind.NamespaceImport) {
const { name } = namedBindings;
if (name.getStart() === pos) {
return name;
}
return undefined;
return [namedBindings.name];
}

if (defaultName !== undefined && defaultName.getStart() === pos) {
return defaultName;
// Starting from TS2.8, when all imports in an import node are not used,
// TS emits only 1 diagnostic object for the whole line as opposed
// to the previous behavior of outputting a diagnostic with kind == 6192
// (UnusedKind.VARIABLE_OR_PARAMETER) for every unused import.
// From TS2.8, in the case of none of the imports in a line being used,
// the single diagnostic TS outputs are different between the 1 import
// and 2+ imports cases:
// - 1 import in node:
// - diagnostic has kind == 6133 (UnusedKind.VARIABLE_OR_PARAMETER)
// - the text range is the whole node (`import { ... } from "..."`)
// whereas pre-TS2.8, the text range was for the import node. so
// `name.getStart()` won't equal `pos` like in pre-TS2.8
// - 2+ imports in node:
// - diagnostic has kind == 6192 (UnusedKind.DECLARATION)
// - we know that all of these are unused
if (kind === UnusedKind.DECLARATION) {
const imp: ts.Identifier[] = [];
if (defaultName !== undefined) {
imp.push(defaultName);
}
if (namedBindings !== undefined) {
imp.push(...namedBindings.elements.map((el) => el.name));
}
return imp.length > 0 ? imp : undefined;
} else if (defaultName !== undefined && (
isInRange(defaultName, pos) || namedBindings === undefined // defaultName is the only option
)) {
return [defaultName];
} else if (namedBindings !== undefined) {
for (const { name } of namedBindings.elements) {
if (name.getStart() === pos) {
return name;
if (namedBindings.elements.length === 1) {
return [namedBindings.elements[0].name];
}

for (const element of namedBindings.elements) {
if (isInRange(element, pos)) {
return [element.name];
}
}
}
}
return undefined;
});
return imports !== undefined ? imports : [];
}

function isInRange(range: ts.TextRange, pos: number): boolean {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: might belong in some utils file somewhere? isInTextRange?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hmm not sure where it would belong though

return range.pos <= pos && range.end >= pos;
}

const enum UnusedKind {
VARIABLE_OR_PARAMETER,
PROPERTY,
DECLARATION, // Introduced in TS 2.8
}
function getUnusedDiagnostic(diag: ts.Diagnostic): UnusedKind | undefined {
switch (diag.code) {
case 6133:
return UnusedKind.VARIABLE_OR_PARAMETER; // "'{0}' is declared but never used.
case 6138:
return UnusedKind.PROPERTY; // "Property '{0}' is declared but never used."
case 6192:
return UnusedKind.DECLARATION; // "All imports in import declaration are unused."
default:
return undefined;
}
Expand Down
1 change: 1 addition & 0 deletions src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ async function doLinting(options: Options, files: string[], program: ts.Program
} else {
contents = await tryReadFile(file, logger);
}

if (contents !== undefined) {
linter.lint(file, contents, configFile);
}
Expand Down
5 changes: 4 additions & 1 deletion test/rules/completed-docs/defaults/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ export class Aaa {
public set prop(value) { this.bbb = value; }
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Documentation must exist for public properties.]

// TODO: TypeScript API doesn't give us a symbol for this, so we must ignore it.
// TypeScript API doesn't give a symbol for this before version 2.8.0
// https://github.com/Microsoft/TypeScript/issues/14257
[Symbol.iterator]() {}
#if typescript >= 2.8.0
Copy link
Contributor

Choose a reason for hiding this comment

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

TIL this is possible. Is this documented anywhere? If not, should we file a followup issue for another PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

not sure if it has been documented anywhere, I found it while reading the tests. I think documenting it would be useful. Let's file a follow up issue

~~~~~~~~~~~~~~~~~~~~~~ [Documentation must exist for methods.]
#endif
}

export enum Ddd { }
Expand Down
2 changes: 0 additions & 2 deletions test/rules/no-unnecessary-type-assertion/test.ts.fix
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,6 @@ interface NotATuple {
declare const notATuple: NotATuple;
notATuple;

unknownName;
Copy link
Contributor

Choose a reason for hiding this comment

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

did this cause test errors?


function foo() {
let xx: 1 | 2 = 1;
const f = () => xx = 2;
Expand Down
3 changes: 0 additions & 3 deletions test/rules/no-unnecessary-type-assertion/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,6 @@ declare const notATuple: NotATuple;
<NotATuple>notATuple;
~~~~~~~~~~~~~~~~~~~~ [0]

unknownName!;
~~~~~~~~~~~~ [0]

function foo() {
let xx: 1 | 2 = 1;
const f = () => xx = 2;
Expand Down
7 changes: 7 additions & 0 deletions test/rules/no-unused-variable/check-parameters/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,15 @@ export class DestructuringTests {
}
}

// Could be a bug with 2.9-dev but getPreEmitDiagnostics isn't
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you link to the corresponding TypeScript issue?

Copy link
Contributor

Choose a reason for hiding this comment

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

👍 or if there isn't one, file a ticket with them and link it to this PR!

// emitting any diagnostics for classes.
abstract class AbstractTest {
#if typescript >= 2.8.0 < 2.9.0
~~~~~~~~~~~~~~~~~~~~~~~~~~~ [err % ('AbstractTest')]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

changes in compiler generated ranges

#endif
#if typescript < 2.8.0
~~~~~~~~~~~~ [err % ('AbstractTest')]
#endif
abstract foo(x);
}

Expand Down
12 changes: 10 additions & 2 deletions test/rules/no-unused-variable/default/false-positives.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
const fs = require("fs");

module Foo {
~~~ [err % ('Foo')]
#if typescript >= 2.8.0
Copy link
Contributor Author

Choose a reason for hiding this comment

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

changes in compiler generated ranges

~~~~~~~~~~ [err % ('Foo')]
#else
~~~ [err % ('Foo')]
#endif
const path = require("path");

console.log(fs);
Expand All @@ -24,7 +28,11 @@ hello.sayHello();
import Bar = whatever.that.Foo;

module some.module.blah {
~~~~ [err % ('some')]
#if typescript >= 2.8.0
~~~~~~~~~~~ [err % ('some')]
#else
~~~~ [err % ('some')]
#endif
export class bar {
private bar: Bar;
constructor() {
Expand Down
12 changes: 10 additions & 2 deletions test/rules/no-unused-variable/default/function.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ function func1(x: number, y: number) {
}

var func2 = () => {
~~~~~ [err % ('func2')]
~~~~~ [err % ('func2')]
//
};

function func3() {
~~~~~ [err % ('func3')]
#if typescript >= 2.8.0
Copy link
Contributor Author

Choose a reason for hiding this comment

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

changes in compiler generated ranges

~~~~~~~~~~~~~~ [err % ('func3')]
#else
~~~~~ [err % ('func3')]
#endif
return func1(1, 2);
}

Expand All @@ -17,7 +21,11 @@ export function func4() {
}

declare function func5(): any;
#if typescript >= 2.8.0
~~~~~~~~~~~~~~~~~~~~~~ [err % ('func5')]
#else
~~~~~ [err % ('func5')]
#endif

export default function () {
return 0;
Expand Down
1 change: 1 addition & 0 deletions test/rules/no-unused-variable/default/import.ts.fix
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,4 @@ import abc = require('a');
import def = abc.someVar;
console.log(def);


21 changes: 14 additions & 7 deletions test/rules/no-unused-variable/default/import.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import {a3 as aa3, a4 as aa4} from "a";
aa3;
// This import statement is unused and will be deleted along with this comment.
import {a5, a6} from "a";
~~~~~~~~~~~~~~~~~~~~~~~~~ [All imports on this line are unused.]
~~~~~~~~~~~~~~~~~~~~~~~~~ [allErr]
import {a7} from "a";
~~~~~~~~~~~~~~~~~~~~~ [All imports on this line are unused.]
~~~~~~~~~~~~~~~~~~~~~ [allErr]
import {a8, a9, a10} from "a";
~~ [err % ('a9')]
~~~ [err % ('a10')]
Expand All @@ -32,7 +32,7 @@ a16;

// Leading comment will not be deleted
import {unusedFunction} from "legacyDependency"; // Import unusedFunction because ....
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [All imports on this line are unused.]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [allErr]
// Next comment will not be deleted

export import a = require("a");
Expand All @@ -42,7 +42,11 @@ $(_.xyz());
/// <reference path="../externalFormatter.test.ts" />

module S {
#if typescript >= 2.8.0
Copy link
Contributor Author

Choose a reason for hiding this comment

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

changes in compiler generated ranges

~~~~~~~~ [err % ('S')]
#else
~ [err % ('S')]
#endif
var template = "";
~~~~~~~~ [err % ('template')]
}
Expand All @@ -54,17 +58,17 @@ import baz from "a";
import defaultExport, { namedExport } from "a";
~~~~~~~~~~~~~ [err % ('defaultExport')]
import d1, { d2 } from "a";
~~~~~~~~~~~~~~~~~~~~~~~~~~~ [All imports on this line are unused.]
~~~~~~~~~~~~~~~~~~~~~~~~~~~ [allErr]
import d3, { d4 } from "a";
~~~~~~ [All named bindings are unused.]
d3;

d3;import d5 from "a";
~~~~~~~~~~~~~~~~~~~ [All imports on this line are unused.]
~~~~~~~~~~~~~~~~~~~ [allErr]
import d6 from "a";d3;
~~~~~~~~~~~~~~~~~~~ [All imports on this line are unused.]
~~~~~~~~~~~~~~~~~~~ [allErr]
d3;import d7 from "a";d3;
~~~~~~~~~~~~~~~~~~~ [All imports on this line are unused.]
~~~~~~~~~~~~~~~~~~~ [allErr]

bar.someFunc();
baz();
Expand All @@ -81,3 +85,6 @@ console.log(def);
#else
[err]: '%s' is declared but never used.
#endif

[allErr]: All imports on this line are unused.
[allErrDecl]: All imports in import declaration are unused.
11 changes: 11 additions & 0 deletions test/rules/no-unused-variable/default/var.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,15 @@ var z;

export var abcd = 3;

// Could be a bug with 2.9-dev but getPreEmitDiagnostics isn't
// emitting any diagnostics for classes.
class ABCD {
#if typescript >= 2.8.0 < 2.9.0
~~~~~~~~~~ [err % ('ABCD')]
#endif
#if typescript < 2.8.0
~~~~ [err % ('ABCD')]
#endif
constructor() {
z = 3;
}
Expand Down Expand Up @@ -59,7 +66,11 @@ for(let e of [1,2,3]) {
export function testRenamingDestructure() {
var a = 2;
let {a: b} = {a: 4};
#if typescript >= 2.8.0
~~~~ [err % ('b')]
#else
~ [err % ('b')]
#endif
let {x: y} = {x: 7}; // false positive
return a + y;
}
Expand Down
Loading