Skip to content

Commit b4320c8

Browse files
authored
fix: correct readonly checks and test cases (#957)
1 parent bea5318 commit b4320c8

File tree

4 files changed

+4
-73
lines changed

4 files changed

+4
-73
lines changed

packages/plugins/eslint-plugin-react-x/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@
5959
"@typescript-eslint/types": "^8.25.0",
6060
"@typescript-eslint/utils": "^8.25.0",
6161
"compare-versions": "^6.1.1",
62-
"is-immutable-type": "^5.0.1",
6362
"string-ts": "^2.2.1",
6463
"ts-pattern": "^5.6.2"
6564
},

packages/plugins/eslint-plugin-react-x/src/rules/prefer-read-only-props.spec.ts

+3-24
Original file line numberDiff line numberDiff line change
@@ -529,9 +529,9 @@ ruleTesterWithTypes.run(RULE_NAME, rule, {
529529
import * as React from "react";
530530
531531
interface HSV {
532-
h: number;
533-
s: number;
534-
v: number;
532+
readonly h: number;
533+
readonly s: number;
534+
readonly v: number;
535535
}
536536
interface ValuePickerProps {
537537
readonly Disabled: boolean;
@@ -542,27 +542,6 @@ ruleTesterWithTypes.run(RULE_NAME, rule, {
542542
return <div />
543543
}
544544
`,
545-
/* tsx */ `
546-
/// <reference types="react" />
547-
/// <reference types="react-dom" />
548-
549-
import * as React from "react";
550-
551-
interface HSV {
552-
h: number;
553-
s: number;
554-
v: number;
555-
}
556-
interface ValuePickerProps {
557-
readonly Disabled: boolean;
558-
readonly Hsv: HSV
559-
// TODO: Support checking if function is readonly
560-
onChange: () => void;
561-
}
562-
export function ValuePicker({ Disabled, Hsv, onChange }: ValuePickerProps) {
563-
return <div />
564-
}
565-
`,
566545
// memo with generic
567546
/* tsx */ `
568547
/// <reference types="react" />

packages/plugins/eslint-plugin-react-x/src/rules/prefer-read-only-props.ts

+1-11
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import type { RuleFeature } from "@eslint-react/shared";
33
import { getConstrainedTypeAtLocation, isTypeReadonly } from "@typescript-eslint/type-utils";
44
import type { ParserServicesWithTypeInformation } from "@typescript-eslint/utils";
55
import { ESLintUtils } from "@typescript-eslint/utils";
6-
import { getTypeImmutability, isImmutable, isReadonlyDeep, isReadonlyShallow, isUnknown } from "is-immutable-type";
76
import type { CamelCase } from "string-ts";
87
import type ts from "typescript";
98

@@ -18,15 +17,6 @@ export const RULE_FEATURES = [
1817

1918
export type MessageID = CamelCase<typeof RULE_NAME>;
2019

21-
function isReadonlyType(type: ts.Type, services: ParserServicesWithTypeInformation): boolean {
22-
try {
23-
const im = getTypeImmutability(services.program, type);
24-
return isUnknown(im) || isImmutable(im) || isReadonlyShallow(im) || isReadonlyDeep(im);
25-
} catch {
26-
return true;
27-
}
28-
}
29-
3020
export default createRule<[], MessageID>({
3121
meta: {
3222
type: "problem",
@@ -53,7 +43,7 @@ export default createRule<[], MessageID>({
5343
continue;
5444
}
5545
const propsType = getConstrainedTypeAtLocation(services, props);
56-
if (isTypeReadonly(services.program, propsType) || isReadonlyType(propsType, services)) {
46+
if (isTypeReadonly(services.program, propsType)) {
5747
continue;
5848
}
5949
context.report({ messageId: "preferReadOnlyProps", node: props });

pnpm-lock.yaml

-37
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)