From cb4c1bd7e4496fa1431db62a426e2e470d4ec75c Mon Sep 17 00:00:00 2001 From: Karolina Benitez Date: Wed, 26 Aug 2020 08:48:55 -0700 Subject: [PATCH 1/3] Adds test --- tests/lib/rules/prefer-read-only-props.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/tests/lib/rules/prefer-read-only-props.js b/tests/lib/rules/prefer-read-only-props.js index 9445605c68..c6cd2492e5 100644 --- a/tests/lib/rules/prefer-read-only-props.js +++ b/tests/lib/rules/prefer-read-only-props.js @@ -131,7 +131,20 @@ ruleTester.run('prefer-read-only-props', rule, { name: PropTypes.string, }; ` - } + }, + // Class component with typed props property wrapped in $ReadOnly + { + code: [ + 'type Props = $ReadOnly<{foo: number}>;', + 'class Hello extends React.Component {', + ' props: Props;', + ' render () {', + ' return
{this.props.foo}
;', + ' }', + '}' + ].join('\n'), + parser: parsers.BABEL_ESLINT + } ], invalid: [ From e51b9115e04de41a8c5e96160a8c889ab930c637 Mon Sep 17 00:00:00 2001 From: Karolina Benitez Date: Wed, 26 Aug 2020 08:52:33 -0700 Subject: [PATCH 2/3] Fix indentation --- tests/lib/rules/prefer-read-only-props.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/tests/lib/rules/prefer-read-only-props.js b/tests/lib/rules/prefer-read-only-props.js index c6cd2492e5..dbdfda6a10 100644 --- a/tests/lib/rules/prefer-read-only-props.js +++ b/tests/lib/rules/prefer-read-only-props.js @@ -134,17 +134,17 @@ ruleTester.run('prefer-read-only-props', rule, { }, // Class component with typed props property wrapped in $ReadOnly { - code: [ - 'type Props = $ReadOnly<{foo: number}>;', - 'class Hello extends React.Component {', - ' props: Props;', - ' render () {', - ' return
{this.props.foo}
;', - ' }', - '}' - ].join('\n'), - parser: parsers.BABEL_ESLINT - } + code: [ + 'type Props = $ReadOnly<{foo: number}>;', + 'class Hello extends React.Component {', + ' props: Props;', + ' render () {', + ' return
{this.props.foo}
;', + ' }', + '}' + ].join('\n'), + parser: parsers.BABEL_ESLINT + } ], invalid: [ From efd6cc3b47724c2aa368a906da5b0f9edbc9af83 Mon Sep 17 00:00:00 2001 From: Karolina Benitez Date: Wed, 26 Aug 2020 08:53:49 -0700 Subject: [PATCH 3/3] Checks if prop is --- lib/rules/prefer-read-only-props.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rules/prefer-read-only-props.js b/lib/rules/prefer-read-only-props.js index 8349435f24..3a1faff94a 100644 --- a/lib/rules/prefer-read-only-props.js +++ b/lib/rules/prefer-read-only-props.js @@ -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'; } // ------------------------------------------------------------------------------