Skip to content

Commit de2ec66

Browse files
karolina-benitezljharb
authored andcommitted
[Fix] prefer-read-only-props: support Flow $ReadOnly
Fixes #2472
1 parent d6ee945 commit de2ec66

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
1212
### Fixed
1313
* [`function-component-definition`]: ignore object properties ([#2771][] @stefan-wullems)
1414
* [`forbid-component-props`]: Implemented support for "namespaced" components ([#2767][] @mnn)
15+
* [`prefer-read-only-props`]: support Flow `$ReadOnly` ([#2772][], [#2779][], [#2770][] @karolina-benitez)
1516

17+
[#2779]: https://github.com/yannickcr/eslint-plugin-react/pull/2779
18+
[#2772]: https://github.com/yannickcr/eslint-plugin-react/pull/2772
1619
[#2771]: https://github.com/yannickcr/eslint-plugin-react/pull/2771
20+
[#2770]: https://github.com/yannickcr/eslint-plugin-react/pull/2770
1721
[#2767]: https://github.com/yannickcr/eslint-plugin-react/pull/2767
1822
[#2761]: https://github.com/yannickcr/eslint-plugin-react/pull/2761
1923
[#2748]: https://github.com/yannickcr/eslint-plugin-react/pull/2748

lib/rules/prefer-read-only-props.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function isFlowPropertyType(node) {
1313
}
1414

1515
function isCovariant(node) {
16-
return node.variance && node.variance.kind === 'plus';
16+
return node.variance && node.variance.kind === 'plus' || node.parent.parent.parent.id && node.parent.parent.parent.id.name === '$ReadOnly';
1717
}
1818

1919
// ------------------------------------------------------------------------------

tests/lib/rules/prefer-read-only-props.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,37 @@ ruleTester.run('prefer-read-only-props', rule, {
131131
name: PropTypes.string,
132132
};
133133
`
134+
},
135+
{
136+
// Class component with typed props argument
137+
code: `
138+
type Props = $ReadOnly<{
139+
name: string,
140+
}>
141+
142+
class Hello extends React.Component<Props> {
143+
render () {
144+
return <div>Hello {this.props.name}</div>;
145+
}
146+
}
147+
`,
148+
parser: parsers.BABEL_ESLINT
149+
},
150+
{
151+
// Class component with typed props argument
152+
code: `
153+
type Props = $ReadOnly<{
154+
+firstName: string,
155+
lastName: string
156+
}>
157+
158+
class Hello extends React.Component<Props> {
159+
render () {
160+
return <div>Hello {this.props.firstName} {this.props.lastName}</div>;
161+
}
162+
}
163+
`,
164+
parser: parsers.BABEL_ESLINT
134165
}
135166
],
136167

0 commit comments

Comments
 (0)