Skip to content

Commit

Permalink
fix(eslint-plugin): [prefer-readonly] correct issue with anonymus fun…
Browse files Browse the repository at this point in the history
…ctions #2590
  • Loading branch information
armano2 committed May 14, 2022
1 parent 1867728 commit 5c60eea
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
9 changes: 7 additions & 2 deletions packages/eslint-plugin/src/rules/prefer-readonly.ts
Expand Up @@ -3,6 +3,7 @@ import * as ts from 'typescript';
import * as util from '../util';
import { typeIsOrHasBaseType } from '../util';
import { ASTUtils, AST_NODE_TYPES, TSESTree } from '@typescript-eslint/utils';
import { isIntersectionType } from 'tsutils';

type MessageIds = 'preferReadonly';
type Options = [
Expand Down Expand Up @@ -270,8 +271,12 @@ class ClassScope {
classNode: ts.ClassLikeDeclaration,
private readonly onlyInlineLambdas?: boolean,
) {
this.checker = checker;
this.classType = checker.getTypeAtLocation(classNode);
const classType = checker.getTypeAtLocation(classNode);
if (isIntersectionType(classType)) {
this.classType = classType.types[0];
} else {
this.classType = classType;
}

for (const member of classNode.members) {
if (ts.isPropertyDeclaration(member)) {
Expand Down
13 changes: 13 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

0 comments on commit 5c60eea

Please sign in to comment.