jsx-eslint / eslint-plugin-react

React-specific linting rules for ESLint
MIT License
8.87k stars 2.76k forks source link

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

Open e110c0 opened 5 years ago

e110c0 commented 5 years ago

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 commented 5 years ago

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

e110c0 commented 5 years ago

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

ljharb commented 5 years ago

oh sorry, i meant #2142. updated.

e110c0 commented 5 years ago

To summarize:

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