Skip to content

Commit

Permalink
[Fix] boolean-prop-naming: detect TS interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
developer-bandi authored and ljharb committed Mar 5, 2024
1 parent 8c2bdb2 commit 901c794
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange

### Fixed
* [`jsx-no-leaked-render`]: prevent wrongly adding parens ([#3700][] @developer-bandi)
* [`boolean-prop-naming`]: detect TS interfaces ([#3701][] @developer-bandi)

[#3701]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3701
[#3700]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3700

## [7.34.0] - 2024.03.03
Expand Down
8 changes: 6 additions & 2 deletions lib/rules/boolean-prop-naming.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ module.exports = {
}

function findAllTypeAnnotations(identifier, node) {
if (node.type === 'TSTypeLiteral' || node.type === 'ObjectTypeAnnotation') {
if (node.type === 'TSTypeLiteral' || node.type === 'ObjectTypeAnnotation' || node.type === 'TSInterfaceBody') {
const currentNode = [].concat(
objectTypeAnnotations.get(identifier.name) || [],
node
Expand Down Expand Up @@ -363,6 +363,10 @@ module.exports = {
findAllTypeAnnotations(node.id, node.typeAnnotation);
},

TSInterfaceDeclaration(node) {
findAllTypeAnnotations(node.id, node.body);
},

// eslint-disable-next-line object-shorthand
'Program:exit'() {
if (!rule) {
Expand All @@ -386,7 +390,7 @@ module.exports = {
[].concat(propType).forEach((prop) => {
validatePropNaming(
component.node,
prop.properties || prop.members
prop.properties || prop.members || prop.body
);
});
}
Expand Down
15 changes: 15 additions & 0 deletions tests/lib/rules/boolean-prop-naming.js
Original file line number Diff line number Diff line change
Expand Up @@ -1239,5 +1239,20 @@ ruleTester.run('boolean-prop-naming', rule, {
},
],
},
{
code: `
interface TestFNType {
enabled: boolean
}
const HelloNew = (props: TestFNType) => { return <div /> };
`,
options: [{ rule: '^is[A-Z]([A-Za-z0-9]?)+' }],
features: ['ts', 'no-babel'],
errors: [
{
message: 'Prop name (enabled) doesn\'t match rule (^is[A-Z]([A-Za-z0-9]?)+)',
},
],
},
]),
});

0 comments on commit 901c794

Please sign in to comment.