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

react/jsx-no-literals: some strings are slipping through #2215

Open
e110c0 opened this issue Mar 26, 2019 · 4 comments
Open

react/jsx-no-literals: some strings are slipping through #2215

e110c0 opened this issue Mar 26, 2019 · 4 comments

Comments

@e110c0
Copy link

e110c0 commented Mar 26, 2019

some string literals are not detected by this rule and I wonder if there is a reason for it.

Example:

import React from 'react';

function TestComponent() {
  return (
    <div
      propError1={ `test` }
      propError2={ "test" }
      propError3={ 'test' }
      propNoError1="test"
      propNoError2='test'
    >
      {/* these are obvious errors */}
      Test
      { "Test" }
    </div>
  );
}

export default TestComponent;

as you can see, the props propNoError1 and propNoError2 slip through this rule (but are generally allowed code). I would expect 7 problems in this file, but we only get 5:

   6:20  warning  Strings not allowed in JSX files: “`test`”  react/jsx-no-literals
   7:20  warning  Strings not allowed in JSX files: “"test"”  react/jsx-no-literals
   8:20  warning  Strings not allowed in JSX files: “'test'”  react/jsx-no-literals
  12:39  warning  Strings not allowed in JSX files: “Test”    react/jsx-no-literals
  14:9   warning  Strings not allowed in JSX files: “"Test"”  react/jsx-no-literals

✖ 5 problems (0 errors, 5 warnings)

.eslintrc

{
  "plugins": [
    "eslint-plugin-react"
  ],
  "parser": "babel-eslint",
  "rules": {
    "react/jsx-no-literals": [
      1,
      {
        "noStrings": true
      }
    ]
  }
}

One of the use cases when it would be preferable for this rule to warn:

<img alt="untranslated alternate text" />

Is there any way to enforce this with this rule?

@ljharb
Copy link
Member

ljharb commented Mar 28, 2019

I believe #2146 addresses this, and this is a duplicate of #2142?

@e110c0
Copy link
Author

e110c0 commented Mar 28, 2019

Correct for #2146. not sure how #2141 is connected

@ljharb
Copy link
Member

ljharb commented Mar 28, 2019

oh sorry, i meant #2142. updated.

@e110c0
Copy link
Author

e110c0 commented Mar 28, 2019

To summarize:

  • the rule gets an option to allow/disallow strings in props
  • pure String props will be handled the same way as strings in curly braces and template strings

If so, this both clarifies how the rule is supposed to work and fix the issue in this ticket.

ljharb added a commit to ljharb/eslint-plugin-react that referenced this issue Feb 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants