Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Bug: [unbound-method] Does not fail for destructured parameters #8947

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

Closed
4 tasks done
steverep opened this issue Apr 18, 2024 · 7 comments · Fixed by #8952
Closed
4 tasks done

Bug: [unbound-method] Does not fail for destructured parameters #8947

steverep opened this issue Apr 18, 2024 · 7 comments · Fixed by #8952
Labels
accepting prs Go ahead, send a pull request that resolves this issue bug Something isn't working locked due to age Please open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing. package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@steverep
Copy link

Before You File a Bug Report Please Confirm You Have Done The Following...

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have searched for related issues and found none that matched my issue.
  • I have read the FAQ and my problem is not listed.

Playground Link

https://typescript-eslint.io/play/#ts=5.4.3&fileType=.ts&code=MYGwhgzhAEAKD2ECWAXJ8B20DeBYAUNNAMQAe0AvNAAwDcBRxAnpTffg9KQBQCUO0TkQBOAUxQBXYVhQALJBAB0ZdkQC%2BBTkz44h0MZOnQ5C5U1XQN%2BKwQBmEjMDSZot%2BPG4AHAIwAuOIio6BgANAKk-qQATGFM-kxRlv4IyM4Y-HiE0MCYECjhkd6x8d6WrD4WBlJYANqk3joA1FxRfLEN-M0JfAC67FZAA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6WJgIwHsOATWgFtE%2BABbc%2B6KImjRu0SODABfEMqA&tsconfig=&tokens=false

Repro Code

class Position {
  #x = 0;
  #y = 0;

  x() { 
    return this.#x;
  }

  y() {
    return this.#y;
  }
}

function foo(p1: Position, { x: x2, y: y2 }: Position) {
  const { x: x1, y: y1 } = p1;
  return [x1() + x2(), y1() + y2()];
}

ESLint Config

module.exports = {
  "rules": {
    "@typescript-eslint/unbound-method": "error"
  }
}

tsconfig

{
  "compilerOptions": {
    // ...
  }
}

Expected Result

I expect the rule to fail when destructuring x2 and y2 in the second function parameter.

Actual Result

There are errors when destructuring x1 and y1 in the function body as expected, but no error for the equivalent code within the function parameter.

Additional Info

No response

@steverep steverep added bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Apr 18, 2024
@kirkwaiblinger
Copy link
Member

To be clear, this is what you want to happen, right?:

type HasMethod = { method(): void };
// actual
function f({ method }: HasMethod) {
}
// expected
function f({ method }: HasMethod) {
             ^^^^^^
}

@steverep
Copy link
Author

Yes, that destructured assignment should trigger the rule the same way as if it were destructured in the body.

@kirkwaiblinger
Copy link
Member

Awesome, thanks for the report! Seems like a bug to me as well.

Looking at past work it looks like this was probably just a simple oversight when the code was added that handles other cases of destructuring (#2228). Might be as simple as adding ObjectPattern to the rule.

@kirkwaiblinger kirkwaiblinger added accepting prs Go ahead, send a pull request that resolves this issue and removed triage Waiting for team members to take a look labels Apr 18, 2024
@targos
Copy link

targos commented Jul 30, 2024

I understand that it can be seen as a bug fix, but this is probably going to break many consumers who use the rule. For example, in my project, this is now flagged:

import { expect, test } from '@playwright/experimental-ct-react';

import SomeComponent from './SomeComponent';

test('some component', async ({ mount }) => {
  const result = await mount(<SomeComponent />);
  // ...
});

Note that Playwright makes it mandatory to use destructuring here so the only escape hatch is to disable the rule in tests.


It also affects React components with props defined like this:

interface TestProps {
  renderSomething(): string;
}

export function Test(props: TestProps) {
  const { renderSomething } = props;
  return <div>{renderSomething()}</div>
}

In that second case, I could rewrite my interface to fix it:

interface TestProps {
  renderSomething: () => string;
}

@JoshuaKGoldberg
Copy link
Member

Note that Playwright makes it mandatory to use destructuring

Do you have a link for that?

In that second case, I could rewrite my interface to fix it:

Indeed, that would be a more correct way to write that type. +1 that this is a bug fix. The (): ... method declaration was incorrect to begin with.

@targos
Copy link

targos commented Jul 30, 2024

Note that Playwright makes it mandatory to use destructuring

Do you have a link for that?

I can't find a direct mention in the documentation, but this is part of the test fixtures feature.

This is where they check for it.

@kirkwaiblinger
Copy link
Member

I'm with @JoshuaKGoldberg - the change is a correct bug fix. So this issue is correctly closed as completed.

@targos Thanks for bringing this pain point up, though! Would you kindly spin off a new issue to continue the conversation? I think it merits discussion, so I wouldn't want this conversation to get buried by virtue of occurring in a closed issue.

@github-actions github-actions bot added the locked due to age Please open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing. label Aug 9, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 9, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepting prs Go ahead, send a pull request that resolves this issue bug Something isn't working locked due to age Please open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing. package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
4 participants