Skip to content

Bug: [class-literal-property-style] A literal getter with a setter does not pass lint, and incorrectly auto-fix #5961

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
sviat9440 opened this issue Nov 10, 2022 · 3 comments · Fixed by #8277
Closed
4 tasks done
Labels
accepting prs Go ahead, send a pull request that resolves this issue bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@sviat9440
Copy link
Contributor

sviat9440 commented Nov 10, 2022

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=4.8.4&sourceType=module&code=MYGwhgzhAEAqCmEAu0DeBYAUNaF4oDcwQBXeACiJAC5ckAnASwDsBzASjSxxwHpfoAOmHdoAXyyjW+aFTLl2tZEzZdsPevhL1m0AORJESPQG5REzGKA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6M+AQ2WVsf0TRO8WsWgB7UtCK1kRJOiiCJ0SODABfEBqA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3QAacDUjoAHoizJ46IngCGqACL9YmNXl3Cwo9FIC+IM0A

Repro Code

class Test {
  set value(val: string) {
    // ...
  }

  get value(): string {
    return 'test';
  }
}

ESLint Config

module.exports = {
  "rules": {
    "@typescript-eslint/class-literal-property-style": "error"
  }
}

tsconfig

{
  "compilerOptions": {
    "strictNullChecks": true,
    "experimentalDecorators": true
  }
}

Expected Result

Should be no errors because the setter exists

Actual Result

Error: Literals should be exposed using readonly fields.

Auto-fix:

class Test {
  set value(val: string) {
    // ...
  }

  readonly value = 'test';
}

Then TypeScript error:
Duplicate identifier 'value'

Additional Info

No response

Versions

package version
@typescript-eslint/eslint-plugin 5.42.1
@typescript-eslint/parser 5.42.1
TypeScript 4.8.4
ESLint 8.15.0
node web
@sviat9440 sviat9440 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 Nov 10, 2022
@bradzacher
Copy link
Member

why do you have a getter with a constant value and a setter?
that really looks like a code smell, TBH.

@bradzacher bradzacher added awaiting response Issues waiting for a reply from the OP or another party and removed triage Waiting for team members to take a look labels Nov 10, 2022
@sviat9440
Copy link
Contributor Author

sviat9440 commented Nov 10, 2022

No matter how I use getters, the rule should not break the code.

But if you want an example, then I have a class Child with a getter and a setter, and I use it in another class Parent.

class Child {
  #value: string;   

  get value(): string {
      return this.#value;
  }

  set value(newValue: string) {
    this.#value = newValue;
    // some other not relevant logic
  }
}

class Parent {
  constructor(private child: Child) {}

  someMethod() {
    this.someOtherMethod(this.child.value);
  }

  someOtherMethod(value: string) {
     // not relevant logic
  }
}

Then I want to write a Unit-test for Parent. To do this, I need to mock Child.

class MockChild implements Child {
  set value(value: string) {
     // empty setter
  }

  get value(): string {
     return ''; // In order not to break anything
  }
}

Then my test looks like this:

let value: jasmine.Spy;
let parent: Parent;
beforeEach(() => {
  const mockChild = new MockChild();
  value = spyOnProperty(mockChild, 'value', 'get')
  parent = new Parent(mockChild);
});

describe('method "someMethod"', () => {
  let someOtherMethod: jasmine.Spy;
  beforeEach(() => {
    someOtherMethod = spyOn(parent, 'someOtherMethod');
  });

  it('should call someOtherMethod with value from Child', () => {
    value.and.returnValue('some text');
    parent.someMethod();
    expect(someOtherMethod).toHaveBeenCalledWith('some text');
    expect(value).toHaveBeenCalledTimes(1);
  });
});

This is a simple abstract example. But there are more complex tests in which you need to check that the value is taken only after set or whatever.

sviat9440 added a commit to MillerSvt/typescript-eslint that referenced this issue Nov 13, 2022
[prefer-optional-chain] A literal getter with a setter does not pass lint, and incorrectly auto-fix (typescript-eslint#5961)

[prefer-optional-chain] The overridden literal getter does not pass lint and incorrectly auto-fix (typescript-eslint#5962)

BREAKING CHANGE
sviat9440 added a commit to MillerSvt/typescript-eslint that referenced this issue Nov 14, 2022
[prefer-optional-chain] A literal getter with a setter does not pass lint, and incorrectly auto-fix (typescript-eslint#5961)

[prefer-optional-chain] The overridden literal getter does not pass lint and incorrectly auto-fix (typescript-eslint#5962)

BREAKING CHANGE
@JoshuaKGoldberg JoshuaKGoldberg added triage Waiting for team members to take a look and removed awaiting response Issues waiting for a reply from the OP or another party labels Nov 15, 2022
@bradzacher
Copy link
Member

I'm happy for this rule to be upgraded to be smarter such that it doesn't error if there is a setter of the same name defined on the class.

@bradzacher bradzacher 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 Nov 16, 2022
sviat9440 added a commit to MillerSvt/typescript-eslint that referenced this issue Jan 29, 2023
[prefer-optional-chain] A literal getter with a setter does not pass lint, and incorrectly auto-fix (typescript-eslint#5961)

[prefer-optional-chain] The overridden literal getter does not pass lint and incorrectly auto-fix (typescript-eslint#5962)

BREAKING CHANGE
sviat9440 added a commit to MillerSvt/typescript-eslint that referenced this issue Jan 29, 2023
[prefer-optional-chain] A literal getter with a setter does not pass lint, and incorrectly auto-fix (typescript-eslint#5961)

[prefer-optional-chain] The overridden literal getter does not pass lint and incorrectly auto-fix (typescript-eslint#5962)

BREAKING CHANGE
sviat9440 added a commit to MillerSvt/typescript-eslint that referenced this issue Jan 29, 2023
[prefer-optional-chain] A literal getter with a setter does not pass lint, and incorrectly auto-fix (typescript-eslint#5961)

[prefer-optional-chain] The overridden literal getter does not pass lint and incorrectly auto-fix (typescript-eslint#5962)
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 10, 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 package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
3 participants