Skip to content

Commit

Permalink
[Fix] boolean-prop-naming: avoid a crash with a non-TSTypeReference…
Browse files Browse the repository at this point in the history
… type
  • Loading branch information
developer-bandi authored and ljharb committed Mar 16, 2024
1 parent d50d886 commit 4467db5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
* [`no-unknown-property`]: support `popover`, `popovertarget`, `popovertargetaction` attributes ([#3707][] @ljharb)
* [`no-unknown-property`]: only match `data-*` attributes containing `-` ([#3713][] @silverwind)
* [`checked-requires-onchange-or-readonly`]: correct options that were behaving opposite ([#3715][] @jaesoekjjang)
* [`boolean-prop-naming`]: avoid a crash with a non-TSTypeReference type ([#3718][] @developer-bandi)

### Changed
* [`boolean-prop-naming`]: improve error message (@ljharb)

[#3718]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3718
[#3715]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3715
[#3713]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3713
[#3707]: https://github.com/jsx-eslint/eslint-plugin-react/issues/3707
Expand Down
6 changes: 5 additions & 1 deletion lib/rules/boolean-prop-naming.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,11 @@ module.exports = {
} else if (annotation.type === 'TSTypeReference') {
propType = objectTypeAnnotations.get(annotation.typeName.name);
} else if (annotation.type === 'TSIntersectionType') {
propType = flatMap(annotation.types, (type) => objectTypeAnnotations.get(type.typeName.name));
propType = flatMap(annotation.types, (type) => (
type.type === 'TSTypeReference'
? objectTypeAnnotations.get(type.typeName.name)
: type
));
}

if (propType) {
Expand Down
33 changes: 31 additions & 2 deletions tests/lib/rules/boolean-prop-naming.js
Original file line number Diff line number Diff line change
Expand Up @@ -1312,10 +1312,10 @@ ruleTester.run('boolean-prop-naming', rule, {
code: `
type Props = {
enabled: boolean
}
};
type BaseProps = {
semi: boolean
}
};
const Hello = (props: Props & BaseProps) => <div />;
`,
Expand All @@ -1338,5 +1338,34 @@ ruleTester.run('boolean-prop-naming', rule, {
},
],
},
{
code: `
type Props = {
enabled: boolean
};
const Hello = (props: Props & {
semi: boolean
}) => <div />;
`,
options: [{ rule: '^(is|has)[A-Z]([A-Za-z0-9]?)+' }],
features: ['ts', 'no-babel', 'no-ts-old'],
errors: [
{
messageId: 'patternMismatch',
data: {
propName: 'enabled',
pattern: '^(is|has)[A-Z]([A-Za-z0-9]?)+',
},
},
{
messageId: 'patternMismatch',
data: {
propName: 'semi',
pattern: '^(is|has)[A-Z]([A-Za-z0-9]?)+',
},
},
],
},
]),
});

0 comments on commit 4467db5

Please sign in to comment.