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: Add config to allow a set of strings #2377

Closed
benhollander opened this issue Aug 8, 2019 · 5 comments
Closed

react/jsx-no-literals: Add config to allow a set of strings #2377

benhollander opened this issue Aug 8, 2019 · 5 comments

Comments

@benhollander
Copy link

benhollander commented Aug 8, 2019

I propose adding a config option to allow a specified list of strings to pass the react/jsx-no-literals rule. I'm finding myself writing code similar to the below pretty often.

Semi-contrived examples

// This would throw an error because of " - "
// Can't do {' - '} because it will be rendered as text instead of an HTML entity
export const DisplayRangeComponent = (props) => (
  <div>
    {/* eslint-disable react/jsx-no-literals */}
    {props.min}&nbsp;-&nbsp;{props.max}
    {/* eslint-enable react/jsx-no-literals */}
  </div>
);

// This would throw because of '*'
export const AddAsterisk = ({ children }) => (
  {/* eslint-disable react/jsx-no-literals */}
  <span>{children}*</span>
  {/* eslint-enable react/jsx-no-literals */}
);

It could be convenient (at least in my use-case, which doesn't seem like it would be all that uncommon) to pass in a config like

rules: { 'react/jsx-no-literals': [ 'error', { ignoreStrings: [ '&nbsp;', '*' ] } ] }
@ljharb
Copy link
Member

ljharb commented Aug 8, 2019

Is there a reason you can't do: {this.props.min}{'&nbsp;-&nbsp;'}{this.props.max}?

@benhollander
Copy link
Author

benhollander commented Aug 9, 2019

Is there a reason you can't do: {this.props.min}{'&nbsp;-&nbsp;'}{this.props.max}?

That will cause the &nbsp; to be displayed in the browser rather than be escaped.

I made a codepen to maybe illustrate the problem more clearly: https://codepen.io/bhollander/pen/zgmGyj

image

renders as

image

@ljharb
Copy link
Member

ljharb commented Aug 9, 2019

ah right, then &nbsp;{'-'}&nbsp;?

@benhollander
Copy link
Author

benhollander commented Aug 9, 2019

That would still fail the eslint rule in question. 😅

@ljharb
Copy link
Member

ljharb commented Aug 9, 2019

Gotcha.

In that case, allowing an array of strings to ignore seems reasonable.

benhollander pushed a commit to benhollander/eslint-plugin-react that referenced this issue Aug 12, 2019
benhollander pushed a commit to benhollander/eslint-plugin-react that referenced this issue Aug 16, 2019
@ljharb ljharb closed this as completed in 2c6430d Aug 31, 2019
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