From 345012e29d27a13fa3488892fc67938b2e3a22e6 Mon Sep 17 00:00:00 2001 From: Andy Date: Thu, 14 Jun 2018 12:55:12 -0700 Subject: [PATCH] Don't add diagnostic on unused import starting with underscore (#24958) * Don't add diagnostic on unused import starting with underscore * Fix lint --- src/compiler/checker.ts | 10 ++++--- ...sedLocalsStartingWithUnderscore.errors.txt | 13 +++++++--- .../unusedLocalsStartingWithUnderscore.js | 26 +++++++++++-------- ...unusedLocalsStartingWithUnderscore.symbols | 21 ++++++++------- .../unusedLocalsStartingWithUnderscore.types | 11 +++++--- .../unusedLocalsStartingWithUnderscore.ts | 8 +++--- 6 files changed, 54 insertions(+), 35 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 4f1b797e58a..c9d0a41f076 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -22895,7 +22895,11 @@ namespace ts { } for (const declaration of local.declarations) { - if (isAmbientModule(declaration)) continue; + if (isAmbientModule(declaration) || + (isVariableDeclaration(declaration) && isForInOrOfStatement(declaration.parent.parent) || isImportedDeclaration(declaration)) && isIdentifierThatStartsWithUnderScore(declaration.name!)) { + continue; + } + if (isImportedDeclaration(declaration)) { addToGroup(unusedImports, importClauseFromImported(declaration), declaration, getNodeId); } @@ -22907,9 +22911,7 @@ namespace ts { } } else if (isVariableDeclaration(declaration)) { - if (!isIdentifierThatStartsWithUnderScore(declaration.name) || !isForInOrOfStatement(declaration.parent.parent)) { - addToGroup(unusedVariables, declaration.parent, declaration, getNodeId); - } + addToGroup(unusedVariables, declaration.parent, declaration, getNodeId); } else { const parameter = local.valueDeclaration && tryGetRootParameterDeclaration(local.valueDeclaration); diff --git a/tests/baselines/reference/unusedLocalsStartingWithUnderscore.errors.txt b/tests/baselines/reference/unusedLocalsStartingWithUnderscore.errors.txt index b8a0a50fea6..4a708b6aa87 100644 --- a/tests/baselines/reference/unusedLocalsStartingWithUnderscore.errors.txt +++ b/tests/baselines/reference/unusedLocalsStartingWithUnderscore.errors.txt @@ -1,12 +1,17 @@ -tests/cases/compiler/unusedLocalsStartingWithUnderscore.ts(6,9): error TS6133: '_' is declared but its value is never read. +/a.ts(7,11): error TS6133: '_ns' is declared but its value is never read. +/a.ts(8,9): error TS6133: '_' is declared but its value is never read. -==== tests/cases/compiler/unusedLocalsStartingWithUnderscore.ts (1 errors) ==== +==== /a.ts (2 errors) ==== + import * as _ from "./a"; + for (const _ of []) { } for (const _ in []) { } - namespace M { + namespace _ns { + ~~~ +!!! error TS6133: '_ns' is declared but its value is never read. let _; ~ !!! error TS6133: '_' is declared but its value is never read. @@ -14,4 +19,4 @@ tests/cases/compiler/unusedLocalsStartingWithUnderscore.ts(6,9): error TS6133: ' for (const _ in []) { } } - \ No newline at end of file + \ No newline at end of file diff --git a/tests/baselines/reference/unusedLocalsStartingWithUnderscore.js b/tests/baselines/reference/unusedLocalsStartingWithUnderscore.js index 90bbb7a4c02..54b029445e3 100644 --- a/tests/baselines/reference/unusedLocalsStartingWithUnderscore.js +++ b/tests/baselines/reference/unusedLocalsStartingWithUnderscore.js @@ -1,26 +1,30 @@ -//// [unusedLocalsStartingWithUnderscore.ts] +//// [a.ts] +import * as _ from "./a"; + for (const _ of []) { } for (const _ in []) { } -namespace M { +namespace _ns { let _; for (const _ of []) { } for (const _ in []) { } } - -//// [unusedLocalsStartingWithUnderscore.js] + +//// [a.js] +"use strict"; +exports.__esModule = true; for (var _i = 0, _a = []; _i < _a.length; _i++) { - var _ = _a[_i]; + var _1 = _a[_i]; } -for (var _ in []) { } -var M; -(function (M) { +for (var _2 in []) { } +var _ns; +(function (_ns) { var _; for (var _i = 0, _a = []; _i < _a.length; _i++) { - var _1 = _a[_i]; + var _3 = _a[_i]; } - for (var _2 in []) { } -})(M || (M = {})); + for (var _4 in []) { } +})(_ns || (_ns = {})); diff --git a/tests/baselines/reference/unusedLocalsStartingWithUnderscore.symbols b/tests/baselines/reference/unusedLocalsStartingWithUnderscore.symbols index df78d17e2af..4c13df877cd 100644 --- a/tests/baselines/reference/unusedLocalsStartingWithUnderscore.symbols +++ b/tests/baselines/reference/unusedLocalsStartingWithUnderscore.symbols @@ -1,20 +1,23 @@ -=== tests/cases/compiler/unusedLocalsStartingWithUnderscore.ts === +=== /a.ts === +import * as _ from "./a"; +>_ : Symbol(_, Decl(a.ts, 0, 6)) + for (const _ of []) { } ->_ : Symbol(_, Decl(unusedLocalsStartingWithUnderscore.ts, 0, 10)) +>_ : Symbol(_, Decl(a.ts, 2, 10)) for (const _ in []) { } ->_ : Symbol(_, Decl(unusedLocalsStartingWithUnderscore.ts, 2, 10)) +>_ : Symbol(_, Decl(a.ts, 4, 10)) -namespace M { ->M : Symbol(M, Decl(unusedLocalsStartingWithUnderscore.ts, 2, 23)) +namespace _ns { +>_ns : Symbol(_ns, Decl(a.ts, 4, 23)) let _; ->_ : Symbol(_, Decl(unusedLocalsStartingWithUnderscore.ts, 5, 7)) +>_ : Symbol(_, Decl(a.ts, 7, 7)) for (const _ of []) { } ->_ : Symbol(_, Decl(unusedLocalsStartingWithUnderscore.ts, 6, 14)) +>_ : Symbol(_, Decl(a.ts, 8, 14)) for (const _ in []) { } ->_ : Symbol(_, Decl(unusedLocalsStartingWithUnderscore.ts, 8, 14)) +>_ : Symbol(_, Decl(a.ts, 10, 14)) } - + diff --git a/tests/baselines/reference/unusedLocalsStartingWithUnderscore.types b/tests/baselines/reference/unusedLocalsStartingWithUnderscore.types index 9f6a58e8d1e..88aa923a3e4 100644 --- a/tests/baselines/reference/unusedLocalsStartingWithUnderscore.types +++ b/tests/baselines/reference/unusedLocalsStartingWithUnderscore.types @@ -1,4 +1,7 @@ -=== tests/cases/compiler/unusedLocalsStartingWithUnderscore.ts === +=== /a.ts === +import * as _ from "./a"; +>_ : typeof _ + for (const _ of []) { } >_ : any >[] : undefined[] @@ -7,8 +10,8 @@ for (const _ in []) { } >_ : string >[] : undefined[] -namespace M { ->M : typeof M +namespace _ns { +>_ns : typeof _ns let _; >_ : any @@ -21,4 +24,4 @@ namespace M { >_ : string >[] : undefined[] } - + diff --git a/tests/cases/compiler/unusedLocalsStartingWithUnderscore.ts b/tests/cases/compiler/unusedLocalsStartingWithUnderscore.ts index 4e6930a6282..431d4fd15d8 100644 --- a/tests/cases/compiler/unusedLocalsStartingWithUnderscore.ts +++ b/tests/cases/compiler/unusedLocalsStartingWithUnderscore.ts @@ -1,13 +1,15 @@ -//@noUnusedLocals:true +// @noUnusedLocals:true + +// @Filename: /a.ts +import * as _ from "./a"; for (const _ of []) { } for (const _ in []) { } -namespace M { +namespace _ns { let _; for (const _ of []) { } for (const _ in []) { } } - \ No newline at end of file