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

no-raw-text doesn't work for custom styled components

Open vjsingh opened this issue 4 years ago • 10 comments

Unless I'm doing something wrong, the no-raw-text rule doesn't seem to work when using styled-components. This makes it largely useless I think? Would be nice to be able to use this rule but not sure how to do so when using styled-components.

import styled from 'styled-components/native';

let view = () => (
  <MyText>This doesn't work</MyText>
);

const MyText = styled(Text)`
  font-size: 16;
`;

vjsingh avatar Dec 12 '19 15:12 vjsingh

Same problem... My codebase is using a lot of Typograph components made with styled-components and unfortunately, I had to disable this rule

LauraBeatris avatar Jul 25 '20 13:07 LauraBeatris

+1

adibas03 avatar Jul 29 '20 15:07 adibas03

First, @vjsingh You should rename the issue to 'no-raw-text doesn't work for custom styled components', as the rule actually works, but not for custom components.

Been doing some investigation into what it would take to implement this. At present, I am yet to get a direct reference to the type of StyledComponent from within the babel nodes which are passed into the rule checking function; only the Customized name of the component show up, so far. If anyone has some pointers or comment, that could help, I'd appreciate, so I can set up a PR, to fix this, once and for all

Previous issue: https://github.com/Intellicode/eslint-plugin-react-native/issues/204 Feature PR https://github.com/Intellicode/eslint-plugin-react-native/pull/201

adibas03 avatar Jul 31 '20 10:07 adibas03

Also running into this. One possible option to fix it would be to change the skip option to accept a regex instead of (or in addition to) an array of strings, so that you could do, for example:

'react-native/no-raw-text': ['error', {skip: '/Text$/'}]

to skip any component that is suffixed with Text.

mattpetrie avatar Aug 12 '20 21:08 mattpetrie

@mattpetrie That is actually a valid suggestion, we just need to consider the possible loopholes or drawbacks I was attempting to identify the styled -component component type, but not been able to get it from the babel generated resource for the lint checks

adibas03 avatar Aug 19 '20 10:08 adibas03

/Text$/

This does not work for me. I.e. I have a styled component called <SummaryText> if I add skip: ['SummaryText'] the error goes away so the rule is working. But /Text$/ doesn't work (nor did /^Text/ work for <TextSummary>). I've also tried does variations on this, ie. /Text.*/ /.Text../ Any suggestions?

This is the rule I have: 'react-native/no-raw-text': ['error', { skip: ['/Text$/'] }], I've also tried without the array.

Thanks in advance, sorry if I'm missing something simple.

fredlemieux avatar Mar 31 '21 09:03 fredlemieux

RegEx does not work for me with version: v3.10.0 String does work

ManAnRuck avatar Apr 17 '21 10:04 ManAnRuck

Regex isnt implemented, @mattpetrie was simply giving a suggestion.

bombillazo avatar Jun 18 '21 07:06 bombillazo

Hi. How are you all currently working around this problem?

janpe avatar Oct 25 '21 06:10 janpe

Hi. How are you all currently working around this problem?

You need to put the same tag name as the one you're using in the code, without the regex, something like:

// your styled components:
const Text = styled.Text``
const AnotherText = styled.Text``

// your rule:
'react-native/no-raw-text': [
          'error',
          {
            skip: [
              'Text',
              'AnotherText'
            ],
          },
        ],

PauloLuan avatar Oct 17 '22 13:10 PauloLuan