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): [prefer-readonly] correct issue with anonymus functions #4974

Merged
merged 4 commits into from May 23, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 6 additions & 2 deletions packages/eslint-plugin/src/rules/prefer-readonly.ts
Expand Up @@ -270,8 +270,12 @@ class ClassScope {
classNode: ts.ClassLikeDeclaration,
private readonly onlyInlineLambdas?: boolean,
) {
this.checker = checker;
this.classType = checker.getTypeAtLocation(classNode);
const classType = checker.getTypeAtLocation(classNode);
if (tsutils.isIntersectionType(classType)) {
this.classType = classType.types[0];
} else {
this.classType = classType;
}

for (const member of classNode.members) {
if (ts.isPropertyDeclaration(member)) {
Expand Down
38 changes: 38 additions & 0 deletions packages/eslint-plugin/tests/rules/prefer-readonly.test.ts
Expand Up @@ -292,6 +292,19 @@ class Foo {
},
{
code: `
function ClassWithName<TBase extends new (...args: any[]) => {}>(Base: TBase) {
return class extends Base {
private _name: string;

public test(value: string) {
this._name = value;
}
};
}
`,
},
{
code: `
class Foo {
private value: Record<string, number> = {};

Expand Down Expand Up @@ -704,5 +717,30 @@ class Foo {
}
`,
},
{
code: `
function ClassWithName<TBase extends new (...args: any[]) => {}>(Base: TBase) {
return class extends Base {
private _name: string;
};
}
`,
output: `
function ClassWithName<TBase extends new (...args: any[]) => {}>(Base: TBase) {
return class extends Base {
private readonly _name: string;
};
}
`,
errors: [
{
data: {
name: '_name',
},
line: 4,
messageId: 'preferReadonly',
},
],
},
],
});