-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Comments
why do you have a getter with a constant value and a setter? |
No matter how I use getters, the rule should not break the code. But if you want an example, then I have a class 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. |
[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
[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
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. |
[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
[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
[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)
Before You File a Bug Report Please Confirm You Have Done The Following...
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
ESLint Config
tsconfig
Expected Result
Should be no errors because the setter exists
Actual Result
Error:
Literals should be exposed using readonly fields.
Auto-fix:
Then TypeScript error:
Duplicate identifier 'value'
Additional Info
No response
Versions
@typescript-eslint/eslint-plugin
5.42.1
@typescript-eslint/parser
5.42.1
TypeScript
4.8.4
ESLint
8.15.0
node
web
The text was updated successfully, but these errors were encountered: