markalfred / eslint-plugin-no-constructor-bind

Prefer class arrow functions to binding in the constructor
12 stars 1 forks source link

Fix option does not work as expected and breaks the code. #57

Open Hastaroth1 opened 3 years ago

Hastaroth1 commented 3 years ago

When I apply the fix for someFunc in this code:

import { LitElement } from "lit";
import { customElement } from "lit/decorators.js";

class Something {
  constructor() {
    this.someFunc = this.someFunc.bind(this);
    this.someOtherFunc = this.someOtherFunc.bind(this);
  }

  private someFunc(_: boolean): void {
    console.log("");
  }
  private someOtherFunc(): void {
    console.log("");
  }
}

This is the result:

import { LitElement } from "lit";
import { customElement } from "lit/decorators.js";

class Something {
  constructor() {

    this.someOtherFunc = this.someOtherFunc.bind(this);
  }

  someFunc = import { LitElement } from "lit";
import { customElement } from "lit/decorators.js";

class Something {
  constructor() {
    this.someFunc = this.someFunc.bind(this);
    this.someOtherFunc = this.someOtherFunc.bind(this);
  }

  private someFunc(_: boolean): void {
    console.log("");
  }
  private someOtherFunc(): void {
    console.log("");
  }
}
 => {
    console.log("");
  }
  private someOtherFunc(): void {
    console.log("");
  }
}

For some reason the import and class declaration are copied and this breaks the code.

Using the fix on someOtherFunc works as expected.

The issue is related to the function having parameters. From my limited testing, it does not matter the name, type or number of parameters. As soon as there is one parameter, this breaks.

I've made a repo with the reproduction: https://github.com/Hastaroth1/no-constructor-bind-error

markalfred commented 2 years ago

Hey @Hastaroth1 sorry for not responding, this slipped past me. It looks like this might be caused by the fact that it's TypeScript. I didn't actually take into account that this plugin should work for TS too. I'll take a look into it as soon as I have time. Thanks for reporting!