-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e12b697
commit 4ba4cbc
Showing
3 changed files
with
65 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,55 @@ | ||
const { ESLintUtils } = require('@typescript-eslint/utils'); | ||
const _ = require('underscore'); | ||
const {ESLintUtils} = require('@typescript-eslint/utils'); | ||
|
||
module.exports = { | ||
name: 'boolean-conditional-rendering', | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'Enforce boolean conditions in React conditional rendering', | ||
recommended: 'error', | ||
name: 'boolean-conditional-rendering', | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'Enforce boolean conditions in React conditional rendering', | ||
recommended: 'error', | ||
}, | ||
schema: [], | ||
messages: { | ||
nonBooleanConditional: | ||
'The left side of conditional rendering should be a boolean, not "{{type}}".', | ||
}, | ||
}, | ||
schema: [], | ||
messages: { | ||
nonBooleanConditional: 'The left side of conditional rendering should be a boolean, not \"{{type}}\".', | ||
}, | ||
}, | ||
defaultOptions: [], | ||
create(context) { | ||
const parserServices = ESLintUtils.getParserServices(context); | ||
const typeChecker = parserServices.program.getTypeChecker(); | ||
return { | ||
LogicalExpression(node) { | ||
if (node.operator === '&&' && isJSXElement(node.right)) { | ||
const leftType = typeChecker.getTypeAtLocation( | ||
parserServices.esTreeNodeToTSNodeMap.get(node.left) | ||
); | ||
if (!isBoolean(leftType)) { | ||
const baseType = typeChecker.getBaseTypeOfLiteralType(leftType); | ||
context.report({ | ||
node: node.left, | ||
messageId: 'nonBooleanConditional', | ||
data: { | ||
type: typeChecker.typeToString(baseType), | ||
}, | ||
}); | ||
} | ||
defaultOptions: [], | ||
create(context) { | ||
function isJSXElement(node) { | ||
return node.type === 'JSXElement' || node.type === 'JSXFragment'; | ||
} | ||
function isBoolean(type) { | ||
return ( | ||
// eslint-disable-next-line no-bitwise | ||
(type.getFlags() & (16 | 528 | 512)) !== 0 // TypeFlags.Boolean | TypeFlags.BooleanLike | TypeFlags.BooleanLiteral | ||
|| (type.isUnion() | ||
// eslint-disable-next-line no-bitwise | ||
&& _.every(type.types, t => (t.getFlags() & (16 | 528 | 512)) !== 0)) | ||
); | ||
} | ||
}, | ||
}; | ||
function isJSXElement(node) { | ||
return node.type === 'JSXElement' || node.type === 'JSXFragment'; | ||
} | ||
function isBoolean(type) { | ||
return ( | ||
(type.getFlags() & (16 | 528 | 512)) !== 0 || // TypeFlags.Boolean | TypeFlags.BooleanLike | TypeFlags.BooleanLiteral | ||
(type.isUnion() && type.types.every(t => | ||
(t.getFlags() & (16 | 528 | 512)) !== 0 // TypeFlags.Boolean | TypeFlags.BooleanLike | TypeFlags.BooleanLiteral | ||
)) | ||
); | ||
} | ||
}, | ||
} | ||
const parserServices = ESLintUtils.getParserServices(context); | ||
const typeChecker = parserServices.program.getTypeChecker(); | ||
return { | ||
LogicalExpression(node) { | ||
if (!(node.operator === '&&' && isJSXElement(node.right))) { | ||
return; | ||
} | ||
const leftType = typeChecker.getTypeAtLocation( | ||
parserServices.esTreeNodeToTSNodeMap.get(node.left), | ||
); | ||
if (!isBoolean(leftType)) { | ||
const baseType = typeChecker.getBaseTypeOfLiteralType(leftType); | ||
context.report({ | ||
node: node.left, | ||
messageId: 'nonBooleanConditional', | ||
data: { | ||
type: typeChecker.typeToString(baseType), | ||
}, | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters