jsx-eslint / eslint-plugin-react

React-specific linting rules for ESLint
MIT License
9k stars 2.77k forks source link

createElement setting? #1074

Open ljharb opened 7 years ago

ljharb commented 7 years ago

We allow createClass to be overridden with a setting - should we allow createElement to be configured as well?

If so, any rules referencing React.createElement should be sure to use <pragma>.<createElement>

TrySound commented 7 years ago

This works for me

settings:
  react:
    pragma: 'createElement'

rules:
  no-unused-vars: 'error'
  react/jsx-uses-react: 'error'
  react/jsx-uses-vars: 'error'
ljharb commented 7 years ago

Doesn't that change React to createElement, which isn't the same thing? Or is the default pragma React.createElement?

TrySound commented 7 years ago

I guess pragma here is what imported. Maybe this can conflict with createClass. But it's still deprecated and doesn't matter IMHO. So this way just works.

ljharb commented 7 years ago

It's important to note the caveat that it only works when not using anything off of React except for createElement - don't forget React.Component, React.Children, React.isValidElement, React.PropTypes, etc.

TrySound commented 7 years ago

Maybe. In my opinion these cases should be processed with flow or typescript. It's just an api, not syntax stuff.

ljharb commented 7 years ago

Neither of those things is a part of the language, and "types" is entirely unrelated to the reasons eslint-plugin-react needs to know about the React and createClass pragmas (which relate to using alternative renderers to React itself).

jkrems commented 7 years ago

Would it make sense to also allow h(...) as an alternative to React.createElement(...)? It's a relatively common pattern exposed by preact.

ljharb commented 7 years ago

It seems like we’d need a setting to specify “createElement” and whether it should chain it off the pragma or not.