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] prefer-read-only-props: support Flow $ReadOnly #2772

Merged
merged 1 commit into from Sep 1, 2020
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -12,8 +12,12 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
### Fixed
* [`function-component-definition`]: ignore object properties ([#2771][] @stefan-wullems)
* [`forbid-component-props`]: Implemented support for "namespaced" components ([#2767][] @mnn)
* [`prefer-read-only-props`]: support Flow `$ReadOnly` ([#2772][], [#2779][], [#2770][] @karolina-benitez)

[#2779]: https://github.com/yannickcr/eslint-plugin-react/pull/2779
[#2772]: https://github.com/yannickcr/eslint-plugin-react/pull/2772
[#2771]: https://github.com/yannickcr/eslint-plugin-react/pull/2771
[#2770]: https://github.com/yannickcr/eslint-plugin-react/pull/2770
[#2767]: https://github.com/yannickcr/eslint-plugin-react/pull/2767
[#2761]: https://github.com/yannickcr/eslint-plugin-react/pull/2761
[#2748]: https://github.com/yannickcr/eslint-plugin-react/pull/2748
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/prefer-read-only-props.js
Expand Up @@ -13,7 +13,7 @@ function isFlowPropertyType(node) {
}

function isCovariant(node) {
return node.variance && node.variance.kind === 'plus';
return node.variance && node.variance.kind === 'plus' || node.parent.parent.parent.id && node.parent.parent.parent.id.name === '$ReadOnly';
}

// ------------------------------------------------------------------------------
Expand Down
31 changes: 31 additions & 0 deletions tests/lib/rules/prefer-read-only-props.js
Expand Up @@ -131,6 +131,37 @@ ruleTester.run('prefer-read-only-props', rule, {
name: PropTypes.string,
};
`
},
{
// Class component with typed props argument
code: `
type Props = $ReadOnly<{
name: string,
}>
class Hello extends React.Component<Props> {
render () {
return <div>Hello {this.props.name}</div>;
}
}
`,
parser: parsers.BABEL_ESLINT
},
{
// Class component with typed props argument
code: `
type Props = $ReadOnly<{
+firstName: string,
lastName: string
}>
class Hello extends React.Component<Props> {
render () {
return <div>Hello {this.props.firstName} {this.props.lastName}</div>;
}
}
`,
parser: parsers.BABEL_ESLINT
}
],

Expand Down