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/boolean-prop-naming interface does not work #3285

Open
DavidShefcik opened this issue May 9, 2022 · 12 comments
Open

react/boolean-prop-naming interface does not work #3285

DavidShefcik opened this issue May 9, 2022 · 12 comments

Comments

@DavidShefcik
Copy link

The react/boolean-prop-naming rule does not work when using an interface with TypeScript but it works with a type

This shows an error like it should:

type Props = {
  done: boolean; // Shows error
}

This doesn't show an error like it should:

interface Props {
  done: boolean; // No error
}

My usage of the rule:

"react/boolean-prop-naming": [
      "error",
      {
        "rule": "^(is|has)[A-Z]([A-Za-z0-9]?)+",
        "message": "It is better if your prop ({{ propName }}) starts with `is` or `has`"
      }
    ],

Versions:

  • eslint - 8.15.0
  • eslint-plugin-react 7.29.4
@AgentRBY
Copy link

Any updates?

@ljharb
Copy link
Member

ljharb commented Mar 26, 2023

@AgentRBY nope, if there were updates, they'd be posted in the issue, or there'd be a linked PR. (that's true everywhere on github)

@Gergely-H
Copy link

It does not work with inline-defined prop types either. I would not suggest anyone use inline types, but it might help locate this problem.

Example for getting no error even with @DavidShefcik's rule enabled:

const TestComponent: FC<{
  isEnabled: boolean;
  enabled: boolean;
}> = ({ isEnabled, enabled }) => {
//...
};

@leadq

This comment was marked as spam.

@fescherer
Copy link

It seems like now at "eslint": "^8.47.0" with "eslint-plugin-react": "^7.33.1" is not working this rule (react/boolean-prop-naming) neither as type or interface

@ljharb
Copy link
Member

ljharb commented Aug 12, 2023

@ofelipescherer can you provide a repro? what code and config isn't working as you expect on the command line?

@fescherer
Copy link

Yes, of course. I created a sandbox demo.

The prop enabled should give me an error, right? I want to give me as suggestion isEnabled.

@desjardinsm
Copy link

Yes, of course. I created a sandbox demo.

The prop enabled should give me an error, right? I want to give me as suggestion isEnabled.

I've never been able to get custom ESLint configuration working in CodeSandbox; I found a couple of issues asking for it but I don't think it ever has been added (at least, I have never been able to get it to recognize my config files, but maybe I'm just missing something). Using your sandbox as a baseline I can't get any other rules or plugins set in a config file to work either. /* eslint */ comments work, but you can't add rules from a plugin that hasn't been added to the config that way.

In a local test repo, it does work with a props declared with a type, but only if you use an options object (even if it is empty):

"react/boolean-prop-naming": ["warn", {}],

But I don't think that is related to the original issue of it not working on interface props.

@fescherer
Copy link

Yes, of course. I created a sandbox demo.
The prop enabled should give me an error, right? I want to give me as suggestion isEnabled.

I've never been able to get custom ESLint configuration working in CodeSandbox; I found a couple of issues asking for it but I don't think it ever has been added (at least, I have never been able to get it to recognize my config files, but maybe I'm just missing something). Using your sandbox as a baseline I can't get any other rules or plugins set in a config file to work either. /* eslint */ comments work, but you can't add rules from a plugin that hasn't been added to the config that way.

In a local test repo, it does work with a props declared with a type, but only if you use an options object (even if it is empty):

"react/boolean-prop-naming": ["warn", {}],

But I don't think that is related to the original issue of it not working on interface props.

Yeah, you are right. I tested again in local and worked. I probably made some mistake on the first time tried. Thanks

@FinnWinchester
Copy link

FinnWinchester commented Oct 9, 2023

It happens to me as well. It flags me the variable declared inside a type but not when declared inside an interface or inline within the component declaration.

boolean-prop-naming
My deps are:
"@babel/eslint-parser": "7.22.10",
"@typescript-eslint/eslint-plugin": "5.59.11",
"@typescript-eslint/parser": "5.59.11",
"eslint": "8.47.0",
"eslint-plugin-jsx-a11y": "6.7.1",
"eslint-plugin-react": "7.33.2",
"eslint-plugin-react-hooks": "4.6.0",
"eslint-plugin-simple-import-sort": "10.0.0",

And my settings are:
"react/boolean-prop-naming": [ "warn", { "propTypeNames": ["bool"], "rule": "^(is|has|are|can)[A-Z]([A-Za-z0-9]?)+|enabled|disabled", "validateNested": true } ],

@alerizzo
Copy link

alerizzo commented Feb 9, 2024

This is still an issue; also adding other broken scenarios:

Extending the type also fails:

type Props = {
  enabled: boolean
}
const Hello = (props: Props & BaseProps) => <div />;

Passing the type as a generic also fails

type Props = {
  enabled: boolean
}
const Hello: React.FC<Props> = (props) => <div />;

@ShawnFarsai
Copy link

Still an issue -- if i change any interface to a type -- then the rule correctly catches

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

10 participants