eslint-plugin-react-native icon indicating copy to clipboard operation
eslint-plugin-react-native copied to clipboard

Rule to detect JSX text not in a Text component

Open jer-sen opened this issue 6 years ago • 13 comments

In some components such as View, text can not be placed directly inside (it must be wrapped with a Text component).

Could you add a rule for this ? Ideally list of standard component forbidding direct use of text could be completed with a rule configuration.

jer-sen avatar Jan 31 '18 17:01 jer-sen

Excellent idea! I'll see if I can have a go at it

Intellicode avatar Feb 01 '18 17:02 Intellicode

Added this new rule https://github.com/Intellicode/eslint-plugin-react-native/pull/201

AleksandrZhukov avatar Oct 02 '18 13:10 AleksandrZhukov

Awesome! I was just searching for a rule like that. One question: Will this also prevent the following example:

const foo = ''
...
foo && <Component />

React Native does not evaluate foo as falsy but tries to render an empty string here.

lefloh avatar Oct 02 '18 13:10 lefloh

no, this one is the only case that can't be handled or I didn't understand how 😄 just use !!foo or foo.lenght > 0 for your case

AleksandrZhukov avatar Oct 02 '18 16:10 AleksandrZhukov

Yes, I know. Problem is to not forget about that while writing code... Would be great if eslint could remind you ;) Thanks anyways!

lefloh avatar Oct 02 '18 17:10 lefloh

Needs the ability to configure it. i.e. So you can add 'CustomStyledText' to valid text nodes if you use a Text wrapper to provide standardized text styles through your app.

Sadly even with that this quickly falls apart for me because I use styled.Text a lot to provide locally styled Text components. And there are a few outliers like react-native-paper providing a <Button> that accepts text.

dantman avatar Oct 12 '18 02:10 dantman

can you provide a code example?

yeah, seems this rule doesn't support styled components, but need to investigate

AleksandrZhukov avatar Oct 12 '18 09:10 AleksandrZhukov

Styled example:

const ContentText = styled.Text`
	${material.body1Object}
	color: ${colors.primaryText};
`;

Example of a "standard" <Text> wrapper: https://github.com/material-native/material-native/blob/master/src/MaterialText.js

<MaterialText body1>Foo</MaterialText>

Or a port of MUI's Typography: https://material-ui.com/style/typography/

dantman avatar Oct 12 '18 18:10 dantman

@dantman Fixed here https://github.com/Intellicode/eslint-plugin-react-native/pull/205

PS. Quite funny that you call your library as standard 😄

AleksandrZhukov avatar Oct 13 '18 16:10 AleksandrZhukov

Standardized within an app, anyone can make their own with the typography rules for their app. That's just an example of an old one I made to wrap Text with Material Design typography classes.

dantman avatar Oct 13 '18 18:10 dantman

#205 only works with styled components if the styled Text component is called StyledText, does not work if several StyledText are defined, unless each of them is skipped...

asmeikal avatar Nov 15 '19 17:11 asmeikal

@dantman Fixed here #205

PS. Quite funny that you call your library as standard 😄

Quite funny that you call it a fix 😄

the0ffh avatar Jun 09 '20 13:06 the0ffh

Awesome! I was just searching for a rule like that. One question: Will this also prevent the following example:

const foo = ''
...
foo && <Component />

React Native does not evaluate foo as falsy but tries to render an empty string here.

I am using eslint-plugin-jsx-expressions to manage this issue. Cf. https://github.com/yannickcr/eslint-plugin-react/issues/2073#issuecomment-945025341

pagkt2 avatar Jan 12 '22 18:01 pagkt2