eslint-plugin-react
eslint-plugin-react copied to clipboard
`display-name` `ignoreTranspilerName` is too strict
The purpose of ignoreTranspilerName is to ignore inferred names:
const SFC = () => <div />; // inferred name
const Foo = class extends React.Component {} // inferred name
It should not, however, ignore explicit names:
function SFC() { return <div />; } // explicit name
class Foo extends React.Component {} // explicit name
I think that the ignoreTranspilerName option should be deprecated, and replaced with new options that cover the current use cases:
- always require an explicit
displayName(ignoreTranspilerName: true) - only require an explicit
displayNameonReact.createClasses (ignoreTranspilerName: false)
As well as cover these additional use cases:
- always require an explicit name - ie, an explicit displayName or a named function or named `class
- always require a name - either explicit, or ES6-inferred
- Never allow an explicit
displayName? (i don't really care about this one, but it seems useful for consistency)
@yannickcr @lencioni @EvHaus thoughts?
Voting for option 2!
These aren't exclusive options, they're use cases :-) also there's two lists, which "option 2" do you have in mind?
@ljharb if it can be inferred then I think not explicitly setting a displayName is fine. Anonymous functions or classes though should still be required to have a display name.
export default function() {
return <span>{'foo'}</span>;
}
This should throw an error.
Yes, that's what it's already doing - but in my example, I need "always require an explicit name", because I never want to rely on name inference.
@ljharb yeah you're talking about having three options right? I agree to that.
- Explicit everything.
- Inferred if possible otherwise you gotta do it bro!
- Do not set a display name.
This is the one that I like that's why I said I like option 2. Now I just realized it could mean the second item in both those lists. Hahaha. π
How about "always" | "only-unnamed" | "only-create-class"?
ββββββββββββββββββββββββββββββββββ¬βββββββββ¬βββββββββββββββ¬βββββββββββββββββββββ
β β always β only-unnamed β only-create-class β
ββββββββββββββββββββββββββββββββββΌβββββββββΌβββββββββββββββΌβββββββββββββββββββββ€
β function Cat() {} β fail β pass β pass β
β class Cat extends Component {} β fail β pass β pass β
β export default () => <></> β fail β fail β pass β
β exports.Cat = () => <></> β fail β fail β pass β
β const Cat = createClass({}) β fail β fail β fail β
ββββββββββββββββββββββββββββββββββ΄βββββββββ΄βββββββββββββββ΄βββββββββββββββββββββ
@golopot exports.Cat = () => {} doesn't have inference; maybe add const Cat = () => {} which should be fail/fail/pass?
Any update on this? Agree that it should not report positive on SFCs that have explicit names - which is almost every component in 2022.
function DontComplainAboutMyName() {
return <div></div>;
}
@SevenOutman it doesnβt report on those, as far as Iβm aware. This issue is about reporting on arrow function components that lack inferred names.
it doesnβt report on those, as far as Iβm aware.
Oops, turns out it was reporting anonymous arrow function components wrapped with forwardRef. My mistake.