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

fix(eslint-plugin): [unified-signatures] no parameters function #6940

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/eslint-plugin/src/rules/unified-signatures.ts
Expand Up @@ -215,6 +215,7 @@ export default util.createRule<Options, MessageIds>({
if (ignoreDifferentlyNamedParameters) {
for (let i = 0; i < a.params.length; i += 1) {
if (
b.params[i] &&
Copy link
Member

Choose a reason for hiding this comment

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

[Refactor] Asking for an element out of bounds of an array is a little confusing. It looks like this code is asking whether b.params[i] is a falsy element - even though the type of b.params indicates that not to be doable. I'm also under the impression accessing elements out of an array's bounds can cause a performance deoptimization (though this code almost certainly wouldn't suffer meaningfully).

I think a more proper fix would be capping the i to Math.min(a.params.length, b.params.length).

Copy link
Member

Choose a reason for hiding this comment

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

Just applied - normally I'd let you have the pleasure, but I feel bad taking up your time on such a small change 😄

a.params[i].type === b.params[i].type &&
getStaticParameterName(a.params[i]) !==
getStaticParameterName(b.params[i])
Expand Down
22 changes: 22 additions & 0 deletions packages/eslint-plugin/tests/rules/unified-signatures.test.ts
Expand Up @@ -177,6 +177,28 @@ function f(v: number, u?: string): void {}
},
{
code: `
function f(v: boolean): number;
function f(): string;
`,
options: [{ ignoreDifferentlyNamedParameters: true }],
},
{
code: `
function f(v: boolean, u: boolean): number;
function f(v: boolean): string;
`,
options: [{ ignoreDifferentlyNamedParameters: true }],
},
{
code: `
function f(v: number, u?: string): void {}
function f(v: number): void;
function f(): string;
`,
options: [{ ignoreDifferentlyNamedParameters: true }],
},
{
code: `
function f(a: boolean, ...c: number[]): void;
function f(a: boolean, ...d: string[]): void;
function f(a: boolean, ...c: (number | string)[]): void {}
Expand Down