-
-
Notifications
You must be signed in to change notification settings - Fork 963
fix(noShadow): ts overloads #9360
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@biomejs/biome": patch | ||
| --- | ||
|
|
||
| Fixed [#7125](https://github.com/biomejs/biome/issues/7125): The rule `noShadow` no longer incorrectly flags parameters in TypeScript constructor and method overload signatures. |
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
16 changes: 16 additions & 0 deletions
16
crates/biome_js_analyze/tests/specs/nursery/noShadow/invalid-overloads.ts
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,16 @@ | ||
| /* should generate diagnostics */ | ||
|
|
||
| // Shadowing overload implementation parameter inside body with let/const | ||
| class ShadowInBody { | ||
| constructor(a: string); | ||
| constructor(a: number); | ||
| constructor(a: unknown) { | ||
| let a = 4; | ||
| } | ||
| } | ||
|
|
||
| // Rest parameter in implementation shadows outer variable | ||
| const items = [1, 2, 3]; | ||
| function restShadow(...items: string[]): void; | ||
| function restShadow(...items: number[]): void; | ||
| function restShadow(...items: (string | number)[]) {} |
79 changes: 79 additions & 0 deletions
79
crates/biome_js_analyze/tests/specs/nursery/noShadow/invalid-overloads.ts.snap
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,79 @@ | ||
| --- | ||
| source: crates/biome_js_analyze/tests/spec_tests.rs | ||
| expression: invalid-overloads.ts | ||
| --- | ||
| # Input | ||
| ```ts | ||
| /* should generate diagnostics */ | ||
|
|
||
| // Shadowing overload implementation parameter inside body with let/const | ||
| class ShadowInBody { | ||
| constructor(a: string); | ||
| constructor(a: number); | ||
| constructor(a: unknown) { | ||
| let a = 4; | ||
| } | ||
| } | ||
|
|
||
| // Rest parameter in implementation shadows outer variable | ||
| const items = [1, 2, 3]; | ||
| function restShadow(...items: string[]): void; | ||
| function restShadow(...items: number[]): void; | ||
| function restShadow(...items: (string | number)[]) {} | ||
|
|
||
| ``` | ||
|
|
||
| # Diagnostics | ||
| ``` | ||
| invalid-overloads.ts:8:13 lint/nursery/noShadow ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
|
||
| ! This variable shadows another variable with the same name in the outer scope. | ||
|
|
||
| 6 │ constructor(a: number); | ||
| 7 │ constructor(a: unknown) { | ||
| > 8 │ let a = 4; | ||
| │ ^ | ||
| 9 │ } | ||
| 10 │ } | ||
|
|
||
| i This is the shadowed variable, which is now inaccessible in the inner scope. | ||
|
|
||
| 5 │ constructor(a: string); | ||
| 6 │ constructor(a: number); | ||
| > 7 │ constructor(a: unknown) { | ||
| │ ^ | ||
| 8 │ let a = 4; | ||
| 9 │ } | ||
|
|
||
| i Consider renaming this variable. It's easy to confuse the origin of variables if they share the same name. | ||
|
|
||
| i This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information. | ||
|
|
||
|
|
||
| ``` | ||
|
|
||
| ``` | ||
| invalid-overloads.ts:16:24 lint/nursery/noShadow ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
|
||
| ! This variable shadows another variable with the same name in the outer scope. | ||
|
|
||
| 14 │ function restShadow(...items: string[]): void; | ||
| 15 │ function restShadow(...items: number[]): void; | ||
| > 16 │ function restShadow(...items: (string | number)[]) {} | ||
| │ ^^^^^ | ||
| 17 │ | ||
|
|
||
| i This is the shadowed variable, which is now inaccessible in the inner scope. | ||
|
|
||
| 12 │ // Rest parameter in implementation shadows outer variable | ||
| > 13 │ const items = [1, 2, 3]; | ||
| │ ^^^^^ | ||
| 14 │ function restShadow(...items: string[]): void; | ||
| 15 │ function restShadow(...items: number[]): void; | ||
|
|
||
| i Consider renaming this variable. It's easy to confuse the origin of variables if they share the same name. | ||
|
|
||
| i This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information. | ||
|
|
||
|
|
||
| ``` |
78 changes: 78 additions & 0 deletions
78
crates/biome_js_analyze/tests/specs/nursery/noShadow/valid-overloads.ts
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,78 @@ | ||
| /* should not generate diagnostics */ | ||
|
|
||
| // Constructor overloads: same parameter name in overloads and implementation | ||
| class Foo { | ||
| prop: number | boolean; | ||
|
|
||
| constructor(param1: number); | ||
| constructor(otherName: boolean); | ||
| constructor(dodo: boolean); | ||
| constructor(param1: boolean | number) { | ||
| this.prop = param1; | ||
| const otherName = 5; | ||
| } | ||
|
|
||
| param1 = 5; | ||
|
|
||
| // Method using same names as constructor overload parameters | ||
| method(param1: string): void; | ||
| method(otherName: boolean): void; | ||
| method(param1: string | boolean): void { | ||
| let dodo: number; | ||
| } | ||
| } | ||
|
|
||
| // Constructor overloads with different parameter names in implementation | ||
| class Bar { | ||
| constructor(a: number); | ||
| constructor(b: number) {} | ||
|
|
||
| // Method using same name as constructor overload parameter | ||
| doThing(a: number) {} | ||
| } | ||
|
|
||
| // Class without overloads: parameter names can be reused in methods | ||
| class Baz { | ||
| private apple: null; | ||
| constructor(apple: null) {} | ||
| bakeAPie(apple: null) {} | ||
| } | ||
|
|
||
| // Regular overloaded functions | ||
| function overloaded(prop: string): void; | ||
| function overloaded(prop: number): void; | ||
| function overloaded(prop: string | number) {} | ||
|
|
||
| // Method overloads | ||
| class MethodOverloads { | ||
| method(x: string): void; | ||
| method(x: number): void; | ||
| method(x: string | number): void {} | ||
| } | ||
|
|
||
| // Rest parameters in function overload signatures | ||
| function withRest(...args: string[]): void; | ||
| function withRest(...args: number[]): void; | ||
| function withRest(...args: (string | number)[]) {} | ||
|
|
||
| // Rest parameters in constructor overload signatures | ||
| class RestConstructor { | ||
| constructor(...items: string[]); | ||
| constructor(...items: number[]); | ||
| constructor(...items: (string | number)[]) {} | ||
|
|
||
| doStuff(...items: boolean[]) {} | ||
| } | ||
|
|
||
| // Rest parameters in method overload signatures | ||
| class RestMethod { | ||
| method(...vals: string[]): void; | ||
| method(...vals: number[]): void; | ||
| method(...vals: (string | number)[]): void {} | ||
| } | ||
|
|
||
| // Overload signature parameters should not shadow outer-scope variables | ||
| // (only the overload signatures, the implementation is a real parameter) | ||
| const outerVar = 10; | ||
| function overloaded2(outerVar: string): void; | ||
| function overloaded2(outerVar: number): void; |
86 changes: 86 additions & 0 deletions
86
crates/biome_js_analyze/tests/specs/nursery/noShadow/valid-overloads.ts.snap
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,86 @@ | ||
| --- | ||
| source: crates/biome_js_analyze/tests/spec_tests.rs | ||
| expression: valid-overloads.ts | ||
| --- | ||
| # Input | ||
| ```ts | ||
| /* should not generate diagnostics */ | ||
|
|
||
| // Constructor overloads: same parameter name in overloads and implementation | ||
| class Foo { | ||
| prop: number | boolean; | ||
|
|
||
| constructor(param1: number); | ||
| constructor(otherName: boolean); | ||
| constructor(dodo: boolean); | ||
| constructor(param1: boolean | number) { | ||
| this.prop = param1; | ||
| const otherName = 5; | ||
| } | ||
|
|
||
| param1 = 5; | ||
|
|
||
| // Method using same names as constructor overload parameters | ||
| method(param1: string): void; | ||
| method(otherName: boolean): void; | ||
| method(param1: string | boolean): void { | ||
| let dodo: number; | ||
| } | ||
| } | ||
|
|
||
| // Constructor overloads with different parameter names in implementation | ||
| class Bar { | ||
| constructor(a: number); | ||
| constructor(b: number) {} | ||
|
|
||
| // Method using same name as constructor overload parameter | ||
| doThing(a: number) {} | ||
| } | ||
|
|
||
| // Class without overloads: parameter names can be reused in methods | ||
| class Baz { | ||
| private apple: null; | ||
| constructor(apple: null) {} | ||
| bakeAPie(apple: null) {} | ||
| } | ||
|
|
||
| // Regular overloaded functions | ||
| function overloaded(prop: string): void; | ||
| function overloaded(prop: number): void; | ||
| function overloaded(prop: string | number) {} | ||
|
|
||
| // Method overloads | ||
| class MethodOverloads { | ||
| method(x: string): void; | ||
| method(x: number): void; | ||
| method(x: string | number): void {} | ||
| } | ||
|
|
||
| // Rest parameters in function overload signatures | ||
| function withRest(...args: string[]): void; | ||
| function withRest(...args: number[]): void; | ||
| function withRest(...args: (string | number)[]) {} | ||
|
|
||
| // Rest parameters in constructor overload signatures | ||
| class RestConstructor { | ||
| constructor(...items: string[]); | ||
| constructor(...items: number[]); | ||
| constructor(...items: (string | number)[]) {} | ||
|
|
||
| doStuff(...items: boolean[]) {} | ||
| } | ||
|
|
||
| // Rest parameters in method overload signatures | ||
| class RestMethod { | ||
| method(...vals: string[]): void; | ||
| method(...vals: number[]): void; | ||
| method(...vals: (string | number)[]): void {} | ||
| } | ||
|
|
||
| // Overload signature parameters should not shadow outer-scope variables | ||
| // (only the overload signatures, the implementation is a real parameter) | ||
| const outerVar = 10; | ||
| function overloaded2(outerVar: string): void; | ||
| function overloaded2(outerVar: number): void; | ||
|
|
||
| ``` |
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
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.
Uh oh!
There was an error while loading. Please reload this page.