From 0488fcbaad123e1f5387ef86abf55cab7c84be86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Zl=C3=A1mal?= Date: Fri, 16 Apr 2021 20:27:17 +0200 Subject: [PATCH] fix: fix read-only React props error position (#478) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Example from our repo before this change: ``` error: Props must be $ReadOnly (flowtype/require-readonly-react-props) at src/kochka.com.mx/src/Layout.js:39:7: 37 | return ( 38 | <> > 39 | | ^ 40 | KOCHKA café · {props.title} 41 | 42 | ``` As you can see the error seems to be wrongly positioned. This is even more awkward when you want to suppress this error (and you have to do it on some unrelated line). After this fix: ``` error: Props must be $ReadOnly (flowtype/require-readonly-react-props) at src/kochka.com.mx/src/Layout.js:28:39: 26 | }; 27 | > 28 | export default function Layout(props: Props): React.Node { | ^ 29 | if (props.withHiddenTitle === true) { 30 | invariant(props.title == null, 'Cannot use `title` together with `withHiddenTitle` property.'); 31 | invariant( ``` --- src/rules/requireReadonlyReactProps.js | 2 +- tests/rules/assertions/requireReadonlyReactProps.js | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/rules/requireReadonlyReactProps.js b/src/rules/requireReadonlyReactProps.js index ab123b0..41ffee6 100644 --- a/src/rules/requireReadonlyReactProps.js +++ b/src/rules/requireReadonlyReactProps.js @@ -144,7 +144,7 @@ const create = (context) => { context.report({ message: identifier.name + ' must be $ReadOnly', - node, + node: identifier, }); reportedFunctionalComponents.push(identifier); diff --git a/tests/rules/assertions/requireReadonlyReactProps.js b/tests/rules/assertions/requireReadonlyReactProps.js index fec6610..3332013 100644 --- a/tests/rules/assertions/requireReadonlyReactProps.js +++ b/tests/rules/assertions/requireReadonlyReactProps.js @@ -85,9 +85,14 @@ export default { // functional components { + // vvvvv code: 'type Props = { }; function Foo(props: Props) { return

}', errors: [ { + column: 39, + endColumn: 44, + endLine: 1, + line: 1, message: 'Props must be $ReadOnly', }, ],