Skip to content

Commit

Permalink
fix(bool-prop-naming): handle the union type
Browse files Browse the repository at this point in the history
  • Loading branch information
mobily committed Mar 12, 2022
1 parent 83031b3 commit 38a7f77
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 11 deletions.
24 changes: 13 additions & 11 deletions lib/rules/boolean-prop-naming.js
Expand Up @@ -256,6 +256,18 @@ module.exports = {
}
}

function findTypeAnnotations(identifier, node) {
if (node.type === 'TSTypeLiteral') {
const currentNode = objectTypeAnnotations.get(identifier.name) || [];
currentNode.push(node);
objectTypeAnnotations.set(identifier.name, currentNode);
} else if (node.type === 'TSIntersectionType' || node.type === 'TSUnionType') {
node.types.forEach((type) => {
findTypeAnnotations(identifier, type);
});
}
}

// --------------------------------------------------------------------------
// Public
// --------------------------------------------------------------------------
Expand Down Expand Up @@ -327,17 +339,7 @@ module.exports = {
},

TSTypeAliasDeclaration(node) {
if (node.typeAnnotation.type === 'TSTypeLiteral') {
objectTypeAnnotations.set(node.id.name, node.typeAnnotation);
} else if (node.typeAnnotation.type === 'TSIntersectionType') {
node.typeAnnotation.types.forEach((type) => {
if (type.type === 'TSTypeLiteral') {
const currentNode = objectTypeAnnotations.get(node.id.name) || [];
currentNode.push(type);
objectTypeAnnotations.set(node.id.name, currentNode);
}
});
}
findTypeAnnotations(node.id, node.typeAnnotation);
},

// eslint-disable-next-line object-shorthand
Expand Down
71 changes: 71 additions & 0 deletions tests/lib/rules/boolean-prop-naming.js
Expand Up @@ -467,6 +467,36 @@ ruleTester.run('boolean-prop-naming', rule, {
features: ['types'],
errors: [],
},
{
code: `
type Props = {
isEnabled: boolean
} | {
hasLOL: boolean
}
const HelloNew = (props: Props) => { return <div /> };
`,
options: [{ rule: '^(is|has)[A-Z]([A-Za-z0-9]?)+' }],
features: ['types'],
errors: [],
},
{
code: `
type Props = {
isEnabled: boolean
} & ({
hasLOL: boolean
} | {
isLOL: boolean
})
const HelloNew = (props: Props) => { return <div /> };
`,
options: [{ rule: '^(is|has)[A-Z]([A-Za-z0-9]?)+' }],
features: ['types'],
errors: [],
},
]),

invalid: parsers.all([
Expand Down Expand Up @@ -1168,5 +1198,46 @@ ruleTester.run('boolean-prop-naming', rule, {
},
],
},
{
code: `
type Props = {
enabled: boolean
} | {
hasLOL: boolean
}
const HelloNew = (props: Props) => { return <div /> };
`,
options: [{ rule: '^(is|has)[A-Z]([A-Za-z0-9]?)+' }],
features: ['types', 'no-ts-old'],
errors: [
{
message: 'Prop name (enabled) doesn\'t match rule (^(is|has)[A-Z]([A-Za-z0-9]?)+)',
},
],
},
{
code: `
type Props = {
enabled: boolean
} & ({
hasLOL: boolean
} | {
lol: boolean
})
const HelloNew = (props: Props) => { return <div /> };
`,
options: [{ rule: '^(is|has)[A-Z]([A-Za-z0-9]?)+' }],
features: ['types', 'no-ts-old'],
errors: [
{
message: 'Prop name (enabled) doesn\'t match rule (^(is|has)[A-Z]([A-Za-z0-9]?)+)',
},
{
message: 'Prop name (lol) doesn\'t match rule (^(is|has)[A-Z]([A-Za-z0-9]?)+)',
},
],
},
]),
});

0 comments on commit 38a7f77

Please sign in to comment.