|
| 1 | +const ERROR_MESSAGE_FLIP_IF = |
| 2 | + 'Change `{{if (not condition)}} ... {{else}} ... {{/if}}` to `{{if condition}} ... {{else}} ... {{/if}}`.'; |
| 3 | +const ERROR_MESSAGE_USE_IF = 'Change `unless (not condition)` to `if condition`.'; |
| 4 | +const ERROR_MESSAGE_USE_UNLESS = 'Change `if (not condition)` to `unless condition`.'; |
| 5 | +const ERROR_MESSAGE_NEGATED_HELPER = 'Simplify unnecessary negation of helper.'; |
| 6 | + |
| 7 | +const INVERTIBLE_HELPERS = new Set(['not', 'eq', 'not-eq', 'gt', 'gte', 'lt', 'lte']); |
| 8 | + |
| 9 | +const HELPER_INVERSIONS = { |
| 10 | + not: null, // special case |
| 11 | + eq: 'not-eq', |
| 12 | + 'not-eq': 'eq', |
| 13 | + gt: 'lte', |
| 14 | + gte: 'lt', |
| 15 | + lt: 'gte', |
| 16 | + lte: 'gt', |
| 17 | +}; |
| 18 | + |
| 19 | +function isIf(node) { |
| 20 | + return node.path?.type === 'GlimmerPathExpression' && node.path.original === 'if'; |
| 21 | +} |
| 22 | + |
| 23 | +function isUnless(node) { |
| 24 | + return node.path?.type === 'GlimmerPathExpression' && node.path.original === 'unless'; |
| 25 | +} |
| 26 | + |
| 27 | +function hasNotHelper(node) { |
| 28 | + return ( |
| 29 | + node.params?.length > 0 && |
| 30 | + node.params[0].type === 'GlimmerSubExpression' && |
| 31 | + node.params[0].path?.type === 'GlimmerPathExpression' && |
| 32 | + node.params[0].path.original === 'not' |
| 33 | + ); |
| 34 | +} |
| 35 | + |
| 36 | +function hasNestedFixableHelper(node) { |
| 37 | + const inner = node.params[0]?.params?.[0]; |
| 38 | + return ( |
| 39 | + inner && |
| 40 | + inner.path?.type === 'GlimmerPathExpression' && |
| 41 | + INVERTIBLE_HELPERS.has(inner.path.original) |
| 42 | + ); |
| 43 | +} |
| 44 | + |
| 45 | +/** @type {import('eslint').Rule.RuleModule} */ |
| 46 | +module.exports = { |
| 47 | + meta: { |
| 48 | + type: 'suggestion', |
| 49 | + docs: { |
| 50 | + description: 'disallow negated conditions in if/unless', |
| 51 | + category: 'Best Practices', |
| 52 | + url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-negated-condition.md', |
| 53 | + templateMode: 'both', |
| 54 | + }, |
| 55 | + schema: [ |
| 56 | + { |
| 57 | + type: 'object', |
| 58 | + properties: { |
| 59 | + simplifyHelpers: { type: 'boolean' }, |
| 60 | + }, |
| 61 | + additionalProperties: false, |
| 62 | + }, |
| 63 | + ], |
| 64 | + messages: { |
| 65 | + flipIf: ERROR_MESSAGE_FLIP_IF, |
| 66 | + useIf: ERROR_MESSAGE_USE_IF, |
| 67 | + useUnless: ERROR_MESSAGE_USE_UNLESS, |
| 68 | + negatedHelper: ERROR_MESSAGE_NEGATED_HELPER, |
| 69 | + }, |
| 70 | + originallyFrom: { |
| 71 | + name: 'ember-template-lint', |
| 72 | + rule: 'lib/rules/no-negated-condition.js', |
| 73 | + docs: 'docs/rule/no-negated-condition.md', |
| 74 | + tests: 'test/unit/rules/no-negated-condition-test.js', |
| 75 | + }, |
| 76 | + }, |
| 77 | + |
| 78 | + create(context) { |
| 79 | + const options = context.options[0] || {}; |
| 80 | + const simplifyHelpers = options.simplifyHelpers === undefined ? true : options.simplifyHelpers; |
| 81 | + const sourceCode = context.getSourceCode(); |
| 82 | + |
| 83 | + // eslint-disable-next-line complexity |
| 84 | + function checkNode(node) { |
| 85 | + const nodeIsIf = isIf(node); |
| 86 | + const nodeIsUnless = isUnless(node); |
| 87 | + |
| 88 | + if (!nodeIsIf && !nodeIsUnless) { |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + // Skip `{{else if ...}}` / `{{else unless ...}}` chains for the outer check, |
| 93 | + // unless they have a fixable negated helper inside |
| 94 | + if (node.type === 'GlimmerBlockStatement') { |
| 95 | + const text = sourceCode.getText(node); |
| 96 | + if (text.startsWith('{{else ')) { |
| 97 | + if (!simplifyHelpers || !hasNotHelper(node) || !hasNestedFixableHelper(node)) { |
| 98 | + return; |
| 99 | + } |
| 100 | + context.report({ node: node.params[0], messageId: 'negatedHelper' }); |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + // Ignore `if ... else if ...` (chained) to avoid forcing negation |
| 105 | + if ( |
| 106 | + node.inverse?.body?.length > 0 && |
| 107 | + node.inverse.body[0].type === 'GlimmerBlockStatement' && |
| 108 | + nodeIsIf && |
| 109 | + isIf(node.inverse.body[0]) |
| 110 | + ) { |
| 111 | + return; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + if (!hasNotHelper(node)) { |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + const notExpr = node.params[0]; |
| 120 | + const hasFixableHelper = hasNestedFixableHelper(node); |
| 121 | + |
| 122 | + // If it's `if (not (someHelper ...))` and we can't simplify the helper, |
| 123 | + // don't suggest converting to `unless` (simple-unless rule would reject it) |
| 124 | + if ( |
| 125 | + nodeIsIf && |
| 126 | + notExpr.params?.[0]?.type === 'GlimmerSubExpression' && |
| 127 | + (!simplifyHelpers || !hasFixableHelper) |
| 128 | + ) { |
| 129 | + return; |
| 130 | + } |
| 131 | + |
| 132 | + // (not a b c) with multiple params — can't simply remove negation |
| 133 | + if (notExpr.params?.length > 1) { |
| 134 | + return; |
| 135 | + } |
| 136 | + |
| 137 | + // Determine message |
| 138 | + const isIfElseBlock = node.type === 'GlimmerBlockStatement' && node.inverse?.body?.length > 0; |
| 139 | + const isIfElseInline = node.type !== 'GlimmerBlockStatement' && node.params?.length === 3; |
| 140 | + const shouldFlip = isIfElseBlock || isIfElseInline; |
| 141 | + |
| 142 | + let messageId; |
| 143 | + if (hasFixableHelper && simplifyHelpers) { |
| 144 | + messageId = 'negatedHelper'; |
| 145 | + } else if (shouldFlip && nodeIsIf) { |
| 146 | + messageId = 'flipIf'; |
| 147 | + } else if (nodeIsUnless) { |
| 148 | + messageId = 'useIf'; |
| 149 | + } else { |
| 150 | + messageId = 'useUnless'; |
| 151 | + } |
| 152 | + |
| 153 | + context.report({ |
| 154 | + node: notExpr, |
| 155 | + messageId, |
| 156 | + }); |
| 157 | + } |
| 158 | + |
| 159 | + return { |
| 160 | + GlimmerBlockStatement: checkNode, |
| 161 | + GlimmerMustacheStatement: checkNode, |
| 162 | + GlimmerSubExpression: checkNode, |
| 163 | + }; |
| 164 | + }, |
| 165 | +}; |
0 commit comments