|
| 1 | +import { |
| 2 | + TSESTree, |
| 3 | + AST_NODE_TYPES, |
| 4 | +} from '@typescript-eslint/experimental-utils'; |
| 5 | +import ts from 'typescript'; |
| 6 | +import * as tsutils from 'tsutils'; |
| 7 | +import * as util from '../util'; |
| 8 | + |
| 9 | +type ExpressionWithTest = |
| 10 | + | TSESTree.ConditionalExpression |
| 11 | + | TSESTree.DoWhileStatement |
| 12 | + | TSESTree.ForStatement |
| 13 | + | TSESTree.IfStatement |
| 14 | + | TSESTree.WhileStatement; |
| 15 | + |
| 16 | +export default util.createRule({ |
| 17 | + name: 'strict-boolean-expressions', |
| 18 | + meta: { |
| 19 | + type: 'suggestion', |
| 20 | + docs: { |
| 21 | + description: 'Restricts the types allowed in boolean expressions', |
| 22 | + category: 'Best Practices', |
| 23 | + recommended: false, |
| 24 | + }, |
| 25 | + schema: [], |
| 26 | + messages: { |
| 27 | + strictBooleanExpression: 'Unexpected non-boolean in conditional.', |
| 28 | + }, |
| 29 | + }, |
| 30 | + defaultOptions: [], |
| 31 | + create(context) { |
| 32 | + const service = util.getParserServices(context); |
| 33 | + const checker = service.program.getTypeChecker(); |
| 34 | + |
| 35 | + /** |
| 36 | + * Determines if the node has a boolean type. |
| 37 | + */ |
| 38 | + function isBooleanType(node: TSESTree.Node): boolean { |
| 39 | + const tsNode = service.esTreeNodeToTSNodeMap.get<ts.ExpressionStatement>( |
| 40 | + node, |
| 41 | + ); |
| 42 | + const type = util.getConstrainedTypeAtLocation(checker, tsNode); |
| 43 | + return tsutils.isTypeFlagSet(type, ts.TypeFlags.BooleanLike); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Asserts that a testable expression contains a boolean, reports otherwise. |
| 48 | + * Filters all LogicalExpressions to prevent some duplicate reports. |
| 49 | + */ |
| 50 | + function assertTestExpressionContainsBoolean( |
| 51 | + node: ExpressionWithTest, |
| 52 | + ): void { |
| 53 | + if ( |
| 54 | + node.test !== null && |
| 55 | + node.test.type !== AST_NODE_TYPES.LogicalExpression && |
| 56 | + !isBooleanType(node.test) |
| 57 | + ) { |
| 58 | + reportNode(node.test); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Asserts that a logical expression contains a boolean, reports otherwise. |
| 64 | + */ |
| 65 | + function assertLocalExpressionContainsBoolean( |
| 66 | + node: TSESTree.LogicalExpression, |
| 67 | + ): void { |
| 68 | + if (!isBooleanType(node.left) || !isBooleanType(node.right)) { |
| 69 | + reportNode(node); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Asserts that a unary expression contains a boolean, reports otherwise. |
| 75 | + */ |
| 76 | + function assertUnaryExpressionContainsBoolean( |
| 77 | + node: TSESTree.UnaryExpression, |
| 78 | + ): void { |
| 79 | + if (!isBooleanType(node.argument)) { |
| 80 | + reportNode(node.argument); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Reports an offending node in context. |
| 86 | + */ |
| 87 | + function reportNode(node: TSESTree.Node): void { |
| 88 | + context.report({ node, messageId: 'strictBooleanExpression' }); |
| 89 | + } |
| 90 | + |
| 91 | + return { |
| 92 | + ConditionalExpression: assertTestExpressionContainsBoolean, |
| 93 | + DoWhileStatement: assertTestExpressionContainsBoolean, |
| 94 | + ForStatement: assertTestExpressionContainsBoolean, |
| 95 | + IfStatement: assertTestExpressionContainsBoolean, |
| 96 | + WhileStatement: assertTestExpressionContainsBoolean, |
| 97 | + LogicalExpression: assertLocalExpressionContainsBoolean, |
| 98 | + 'UnaryExpression[operator="!"]': assertUnaryExpressionContainsBoolean, |
| 99 | + }; |
| 100 | + }, |
| 101 | +}); |
0 commit comments