-
Notifications
You must be signed in to change notification settings - Fork 13k
Feat/58561 allow leading underscore to bypass no unused locals #62323
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
Open
jonshea
wants to merge
3
commits into
microsoft:main
Choose a base branch
from
jonshea:feat/58561_allow_leading_underscore_to_bypass_noUnusedLocals
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,151
−141
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
noUnusedLocals_types.ts(4,6): error TS6196: 'UnusedType1' is declared but never used. | ||
noUnusedLocals_types.ts(5,11): error TS6196: 'UnusedInterface1' is declared but never used. | ||
noUnusedLocals_types.ts(6,7): error TS6196: 'UnusedClass1' is declared but never used. | ||
|
||
|
||
==== noUnusedLocals_types.ts (3 errors) ==== | ||
// Test specifically for type declarations with underscore prefix | ||
|
||
// These should all produce errors (no underscore) | ||
type UnusedType1 = string; | ||
~~~~~~~~~~~ | ||
!!! error TS6196: 'UnusedType1' is declared but never used. | ||
interface UnusedInterface1 { x: number; } | ||
~~~~~~~~~~~~~~~~ | ||
!!! error TS6196: 'UnusedInterface1' is declared but never used. | ||
class UnusedClass1 { } | ||
~~~~~~~~~~~~ | ||
!!! error TS6196: 'UnusedClass1' is declared but never used. | ||
|
||
// These should NOT produce errors (underscore prefix) | ||
type _UnusedType2 = string; | ||
interface _UnusedInterface2 { x: number; } | ||
class _UnusedClass2 { } | ||
|
||
// Mixed usage - only the one without underscore should error | ||
type UsedInOther = number; | ||
type _Helper = UsedInOther; // _Helper is not an error, but it uses UsedInOther | ||
|
||
export {}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
//// [tests/cases/compiler/noUnusedLocals_types.ts] //// | ||
|
||
//// [noUnusedLocals_types.ts] | ||
// Test specifically for type declarations with underscore prefix | ||
|
||
// These should all produce errors (no underscore) | ||
type UnusedType1 = string; | ||
interface UnusedInterface1 { x: number; } | ||
class UnusedClass1 { } | ||
|
||
// These should NOT produce errors (underscore prefix) | ||
type _UnusedType2 = string; | ||
interface _UnusedInterface2 { x: number; } | ||
class _UnusedClass2 { } | ||
|
||
// Mixed usage - only the one without underscore should error | ||
type UsedInOther = number; | ||
type _Helper = UsedInOther; // _Helper is not an error, but it uses UsedInOther | ||
|
||
export {}; | ||
|
||
//// [noUnusedLocals_types.js] | ||
"use strict"; | ||
// Test specifically for type declarations with underscore prefix | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var UnusedClass1 = /** @class */ (function () { | ||
function UnusedClass1() { | ||
} | ||
return UnusedClass1; | ||
}()); | ||
var _UnusedClass2 = /** @class */ (function () { | ||
function _UnusedClass2() { | ||
} | ||
return _UnusedClass2; | ||
}()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
//// [tests/cases/compiler/noUnusedLocals_types.ts] //// | ||
|
||
=== noUnusedLocals_types.ts === | ||
// Test specifically for type declarations with underscore prefix | ||
|
||
// These should all produce errors (no underscore) | ||
type UnusedType1 = string; | ||
>UnusedType1 : Symbol(UnusedType1, Decl(noUnusedLocals_types.ts, 0, 0)) | ||
|
||
interface UnusedInterface1 { x: number; } | ||
>UnusedInterface1 : Symbol(UnusedInterface1, Decl(noUnusedLocals_types.ts, 3, 26)) | ||
>x : Symbol(UnusedInterface1.x, Decl(noUnusedLocals_types.ts, 4, 28)) | ||
|
||
class UnusedClass1 { } | ||
>UnusedClass1 : Symbol(UnusedClass1, Decl(noUnusedLocals_types.ts, 4, 41)) | ||
|
||
// These should NOT produce errors (underscore prefix) | ||
type _UnusedType2 = string; | ||
>_UnusedType2 : Symbol(_UnusedType2, Decl(noUnusedLocals_types.ts, 5, 22)) | ||
|
||
interface _UnusedInterface2 { x: number; } | ||
>_UnusedInterface2 : Symbol(_UnusedInterface2, Decl(noUnusedLocals_types.ts, 8, 27)) | ||
>x : Symbol(_UnusedInterface2.x, Decl(noUnusedLocals_types.ts, 9, 29)) | ||
|
||
class _UnusedClass2 { } | ||
>_UnusedClass2 : Symbol(_UnusedClass2, Decl(noUnusedLocals_types.ts, 9, 42)) | ||
|
||
// Mixed usage - only the one without underscore should error | ||
type UsedInOther = number; | ||
>UsedInOther : Symbol(UsedInOther, Decl(noUnusedLocals_types.ts, 10, 23)) | ||
|
||
type _Helper = UsedInOther; // _Helper is not an error, but it uses UsedInOther | ||
>_Helper : Symbol(_Helper, Decl(noUnusedLocals_types.ts, 13, 26)) | ||
>UsedInOther : Symbol(UsedInOther, Decl(noUnusedLocals_types.ts, 10, 23)) | ||
|
||
export {}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
//// [tests/cases/compiler/noUnusedLocals_types.ts] //// | ||
|
||
=== noUnusedLocals_types.ts === | ||
// Test specifically for type declarations with underscore prefix | ||
|
||
// These should all produce errors (no underscore) | ||
type UnusedType1 = string; | ||
>UnusedType1 : string | ||
> : ^^^^^^ | ||
|
||
interface UnusedInterface1 { x: number; } | ||
>x : number | ||
> : ^^^^^^ | ||
|
||
class UnusedClass1 { } | ||
>UnusedClass1 : UnusedClass1 | ||
> : ^^^^^^^^^^^^ | ||
|
||
// These should NOT produce errors (underscore prefix) | ||
type _UnusedType2 = string; | ||
>_UnusedType2 : string | ||
> : ^^^^^^ | ||
|
||
interface _UnusedInterface2 { x: number; } | ||
>x : number | ||
> : ^^^^^^ | ||
|
||
class _UnusedClass2 { } | ||
>_UnusedClass2 : _UnusedClass2 | ||
> : ^^^^^^^^^^^^^ | ||
|
||
// Mixed usage - only the one without underscore should error | ||
type UsedInOther = number; | ||
>UsedInOther : number | ||
> : ^^^^^^ | ||
|
||
type _Helper = UsedInOther; // _Helper is not an error, but it uses UsedInOther | ||
>_Helper : number | ||
> : ^^^^^^ | ||
|
||
export {}; |
164 changes: 149 additions & 15 deletions
164
tests/baselines/reference/unusedLocalsStartingWithUnderscore.errors.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,156 @@ | ||
/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. | ||
unusedLocalsStartingWithUnderscore.ts(2,7): error TS6133: 'unusedVar' is declared but its value is never read. | ||
unusedLocalsStartingWithUnderscore.ts(5,5): error TS6133: 'unusedLet' is declared but its value is never read. | ||
unusedLocalsStartingWithUnderscore.ts(8,5): error TS6133: 'unusedVar2' is declared but its value is never read. | ||
unusedLocalsStartingWithUnderscore.ts(11,9): error TS6133: 'a1' is declared but its value is never read. | ||
unusedLocalsStartingWithUnderscore.ts(15,10): error TS6133: 'unusedFunc' is declared but its value is never read. | ||
unusedLocalsStartingWithUnderscore.ts(18,7): error TS6133: 'unusedArrow' is declared but its value is never read. | ||
unusedLocalsStartingWithUnderscore.ts(22,7): error TS6196: 'UnusedClass' is declared but never used. | ||
unusedLocalsStartingWithUnderscore.ts(26,11): error TS6196: 'UnusedInterface' is declared but never used. | ||
unusedLocalsStartingWithUnderscore.ts(30,6): error TS6196: 'UnusedType' is declared but never used. | ||
unusedLocalsStartingWithUnderscore.ts(34,6): error TS6196: 'UnusedEnum' is declared but never used. | ||
unusedLocalsStartingWithUnderscore.ts(39,12): error TS6133: 'x' is declared but its value is never read. | ||
unusedLocalsStartingWithUnderscore.ts(41,12): error TS6133: 'x' is declared but its value is never read. | ||
unusedLocalsStartingWithUnderscore.ts(44,11): error TS6133: 'UnusedNamespace' is declared but its value is never read. | ||
unusedLocalsStartingWithUnderscore.ts(52,7): error TS6133: 'unusedA' is declared but its value is never read. | ||
unusedLocalsStartingWithUnderscore.ts(54,8): error TS6133: 'unusedC' is declared but its value is never read. | ||
unusedLocalsStartingWithUnderscore.ts(61,7): error TS6196: 'TestClass' is declared but never used. | ||
unusedLocalsStartingWithUnderscore.ts(62,13): error TS6133: 'unusedMember' is declared but its value is never read. | ||
unusedLocalsStartingWithUnderscore.ts(63,13): error TS6133: '_unusedMember' is declared but its value is never read. | ||
unusedLocalsStartingWithUnderscore.ts(65,13): error TS6133: 'unusedMethod' is declared but its value is never read. | ||
unusedLocalsStartingWithUnderscore.ts(66,13): error TS6133: '_unusedMethod' is declared but its value is never read. | ||
unusedLocalsStartingWithUnderscore.ts(69,6): error TS6196: 'TestEnum' is declared but never used. | ||
unusedLocalsStartingWithUnderscore.ts(74,11): error TS6196: 'TestInterface' is declared but never used. | ||
unusedLocalsStartingWithUnderscore.ts(79,7): error TS6133: 'obj' is declared but its value is never read. | ||
|
||
|
||
==== /a.ts (2 errors) ==== | ||
import * as _ from "./a"; | ||
==== unusedLocalsStartingWithUnderscore.ts (23 errors) ==== | ||
// Variables | ||
const unusedVar = 1; // error | ||
~~~~~~~~~ | ||
!!! error TS6133: 'unusedVar' is declared but its value is never read. | ||
const _unusedVar = 2; // ok | ||
|
||
for (const _ of []) { } | ||
let unusedLet = 3; // error | ||
~~~~~~~~~ | ||
!!! error TS6133: 'unusedLet' is declared but its value is never read. | ||
let _unusedLet = 4; // ok | ||
|
||
for (const _ in []) { } | ||
var unusedVar2 = 5; // error | ||
~~~~~~~~~~ | ||
!!! error TS6133: 'unusedVar2' is declared but its value is never read. | ||
var _unusedVar2 = 6; // ok | ||
|
||
namespace _ns { | ||
~~~ | ||
!!! error TS6133: '_ns' is declared but its value is never read. | ||
let _; | ||
~ | ||
!!! error TS6133: '_' is declared but its value is never read. | ||
for (const _ of []) { } | ||
const { a1, _b1 } = { a1: 1, _b1: 2 }; // error on a1 | ||
~~ | ||
!!! error TS6133: 'a1' is declared but its value is never read. | ||
const { _a2, _b2 } = { _a2: 1, _b2: 2 }; // ok | ||
|
||
for (const _ in []) { } | ||
// Functions | ||
function unusedFunc() { } // error | ||
~~~~~~~~~~ | ||
!!! error TS6133: 'unusedFunc' is declared but its value is never read. | ||
function _unusedFunc() { } // ok | ||
|
||
const unusedArrow = () => { }; // error | ||
~~~~~~~~~~~ | ||
!!! error TS6133: 'unusedArrow' is declared but its value is never read. | ||
const _unusedArrow = () => { }; // ok | ||
|
||
// Classes | ||
class UnusedClass { } // error | ||
~~~~~~~~~~~ | ||
!!! error TS6196: 'UnusedClass' is declared but never used. | ||
class _UnusedClass { } // ok | ||
|
||
// Interfaces | ||
interface UnusedInterface { } // error | ||
~~~~~~~~~~~~~~~ | ||
!!! error TS6196: 'UnusedInterface' is declared but never used. | ||
interface _UnusedInterface { } // ok | ||
|
||
// Type aliases | ||
type UnusedType = string; // error | ||
~~~~~~~~~~ | ||
!!! error TS6196: 'UnusedType' is declared but never used. | ||
type _UnusedType = string; // ok | ||
|
||
// Enums | ||
enum UnusedEnum { A } // error | ||
~~~~~~~~~~ | ||
!!! error TS6196: 'UnusedEnum' is declared but never used. | ||
enum _UnusedEnum { A } // ok | ||
|
||
// Declarations in for loops | ||
for (const _x of []) { } // ok | ||
for (const x of []) { } // error | ||
~ | ||
!!! error TS6133: 'x' is declared but its value is never read. | ||
for (const _x in []) { } // ok | ||
for (const x in []) { } // error | ||
~ | ||
!!! error TS6133: 'x' is declared but its value is never read. | ||
|
||
// Namespaces | ||
namespace UnusedNamespace { // error | ||
~~~~~~~~~~~~~~~ | ||
!!! error TS6133: 'UnusedNamespace' is declared but its value is never read. | ||
export const x = 1; | ||
} | ||
namespace _UnusedNamespace { // ok | ||
export const x = 1; | ||
} | ||
|
||
|
||
// Destructuring | ||
const { a: unusedA } = { a: 1 }; // error | ||
~~~~~~~~~~~~~~ | ||
!!! error TS6133: 'unusedA' is declared but its value is never read. | ||
const { b: _unusedB } = { b: 2 }; // ok | ||
const [unusedC] = [3]; // error | ||
~~~~~~~ | ||
!!! error TS6133: 'unusedC' is declared but its value is never read. | ||
const [_unusedD] = [4]; // ok | ||
|
||
// | ||
// The following declarations may _not_ use an underscore to bypass @noUnusedLocals | ||
// | ||
|
||
class TestClass { | ||
~~~~~~~~~ | ||
!!! error TS6196: 'TestClass' is declared but never used. | ||
private unusedMember = 1; // error | ||
~~~~~~~~~~~~ | ||
!!! error TS6133: 'unusedMember' is declared but its value is never read. | ||
private _unusedMember = 2; // still error | ||
~~~~~~~~~~~~~ | ||
!!! error TS6133: '_unusedMember' is declared but its value is never read. | ||
|
||
private unusedMethod() { } // error | ||
~~~~~~~~~~~~ | ||
!!! error TS6133: 'unusedMethod' is declared but its value is never read. | ||
private _unusedMethod() { } // still error | ||
~~~~~~~~~~~~~ | ||
!!! error TS6133: '_unusedMethod' is declared but its value is never read. | ||
} | ||
|
||
enum TestEnum { | ||
~~~~~~~~ | ||
!!! error TS6196: 'TestEnum' is declared but never used. | ||
UnusedMember = 1, // error | ||
_UnusedMember = 2, // still error | ||
} | ||
|
||
interface TestInterface { | ||
~~~~~~~~~~~~~ | ||
!!! error TS6196: 'TestInterface' is declared but never used. | ||
unusedProp: number; // error | ||
_unusedProp: number; // still error | ||
} | ||
|
||
const obj = { | ||
~~~ | ||
!!! error TS6133: 'obj' is declared but its value is never read. | ||
unusedProp: 1, // error | ||
_unusedProp: 2, // still error | ||
}; | ||
|
||
export { }; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I do not include this
isAmbientModule(declaration)
guard, then I get failures in two fourslash tests (moduleDeclarationDeprecated_suggestion1.ts
andmoduleDeclarationDeprecated_suggestion1.ts
), which both start unexpectedly surfacing"'__global' is declared but its value is never read.",
. I was not able to figure out how to fix those tests.But I also was not able to figure out any way to exercise this guard in
tests/case/compiler
tests. That is, I was unable to come up with a test case that would raise anunused declaration
ordeclared but its value is never read
, even when I did not include thisisAmbientModule(declaration)
. Here’s an example of what I tried.Probably this doesn’t matter. After all, there was no test validating that we don’t error on unused ambient modules even before this change, but I still thought I’d call it out.