|
| 1 | +function isUnless(node) { |
| 2 | + return node.path?.type === 'GlimmerPathExpression' && node.path.original === 'unless'; |
| 3 | +} |
| 4 | + |
| 5 | +function isIf(node) { |
| 6 | + return node.path?.type === 'GlimmerPathExpression' && node.path.original === 'if'; |
| 7 | +} |
| 8 | + |
| 9 | +/** @type {import('eslint').Rule.RuleModule} */ |
| 10 | +module.exports = { |
| 11 | + meta: { |
| 12 | + type: 'suggestion', |
| 13 | + docs: { |
| 14 | + description: 'require simple conditions in unless blocks', |
| 15 | + category: 'Best Practices', |
| 16 | + url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-simple-unless.md', |
| 17 | + templateMode: 'both', |
| 18 | + }, |
| 19 | + schema: [ |
| 20 | + { |
| 21 | + type: 'object', |
| 22 | + properties: { |
| 23 | + allowlist: { type: 'array', items: { type: 'string' } }, |
| 24 | + denylist: { type: 'array', items: { type: 'string' } }, |
| 25 | + maxHelpers: { type: 'integer' }, |
| 26 | + }, |
| 27 | + additionalProperties: false, |
| 28 | + }, |
| 29 | + ], |
| 30 | + messages: { |
| 31 | + followingElseBlock: 'Using an `else` block with `unless` should be avoided.', |
| 32 | + asElseUnlessBlock: 'Using an `else unless` block should be avoided.', |
| 33 | + withHelper: '{{message}}', |
| 34 | + }, |
| 35 | + originallyFrom: { |
| 36 | + name: 'ember-template-lint', |
| 37 | + rule: 'lib/rules/simple-unless.js', |
| 38 | + docs: 'docs/rule/simple-unless.md', |
| 39 | + tests: 'test/unit/rules/simple-unless-test.js', |
| 40 | + }, |
| 41 | + }, |
| 42 | + |
| 43 | + create(context) { |
| 44 | + const options = context.options[0] || {}; |
| 45 | + const allowlist = options.allowlist || []; |
| 46 | + const denylist = options.denylist || []; |
| 47 | + const maxHelpers = options.maxHelpers === undefined ? 1 : options.maxHelpers; |
| 48 | + const sourceCode = context.getSourceCode(); |
| 49 | + |
| 50 | + function isElseUnlessBlock(node) { |
| 51 | + if (!node) { |
| 52 | + return false; |
| 53 | + } |
| 54 | + if (node.path?.type === 'GlimmerPathExpression' && node.path.original === 'unless') { |
| 55 | + const text = sourceCode.getText(node); |
| 56 | + return text.startsWith('{{else '); |
| 57 | + } |
| 58 | + return false; |
| 59 | + } |
| 60 | + |
| 61 | + function checkWithHelper(node) { |
| 62 | + let helperCount = 0; |
| 63 | + let nextParams = node.params || []; |
| 64 | + |
| 65 | + do { |
| 66 | + const currentParams = nextParams; |
| 67 | + nextParams = []; |
| 68 | + |
| 69 | + for (const param of currentParams) { |
| 70 | + if (param.type === 'GlimmerSubExpression') { |
| 71 | + helperCount++; |
| 72 | + const helperName = param.path?.original || ''; |
| 73 | + |
| 74 | + if (maxHelpers > -1 && helperCount > maxHelpers) { |
| 75 | + context.report({ |
| 76 | + node: param, |
| 77 | + messageId: 'withHelper', |
| 78 | + data: { |
| 79 | + message: `Using {{unless}} in combination with other helpers should be avoided. MaxHelpers: ${maxHelpers}`, |
| 80 | + }, |
| 81 | + }); |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + if (allowlist.length > 0 && !allowlist.includes(helperName)) { |
| 86 | + context.report({ |
| 87 | + node: param, |
| 88 | + messageId: 'withHelper', |
| 89 | + data: { |
| 90 | + message: `Using {{unless}} in combination with other helpers should be avoided. Allowed helper${allowlist.length > 1 ? 's' : ''}: ${allowlist}`, |
| 91 | + }, |
| 92 | + }); |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + if (denylist.length > 0 && denylist.includes(helperName)) { |
| 97 | + context.report({ |
| 98 | + node: param, |
| 99 | + messageId: 'withHelper', |
| 100 | + data: { |
| 101 | + message: `Using {{unless}} in combination with other helpers should be avoided. Restricted helper${denylist.length > 1 ? 's' : ''}: ${denylist}`, |
| 102 | + }, |
| 103 | + }); |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + if (param.params) { |
| 108 | + nextParams.push(...param.params); |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + } while (nextParams.some((p) => p.type === 'GlimmerSubExpression')); |
| 113 | + } |
| 114 | + |
| 115 | + return { |
| 116 | + GlimmerMustacheStatement(node) { |
| 117 | + if (node.path?.type === 'GlimmerPathExpression' && node.path.original === 'unless') { |
| 118 | + if (node.params?.[0]?.path) { |
| 119 | + checkWithHelper(node); |
| 120 | + } |
| 121 | + } |
| 122 | + }, |
| 123 | + |
| 124 | + GlimmerBlockStatement(node) { |
| 125 | + const nodeInverse = node.inverse; |
| 126 | + |
| 127 | + if (nodeInverse && nodeInverse.body?.length > 0) { |
| 128 | + if (isUnless(node)) { |
| 129 | + // Check for {{#unless}}...{{else if}} |
| 130 | + if (nodeInverse.body[0] && isIf(nodeInverse.body[0])) { |
| 131 | + context.report({ |
| 132 | + node: node.program || node, |
| 133 | + messageId: 'followingElseBlock', |
| 134 | + }); |
| 135 | + } else { |
| 136 | + // {{#unless}}...{{else}} |
| 137 | + context.report({ |
| 138 | + node: node.program || node, |
| 139 | + messageId: 'followingElseBlock', |
| 140 | + }); |
| 141 | + } |
| 142 | + } else if (isElseUnlessBlock(nodeInverse.body[0])) { |
| 143 | + // {{#if}}...{{else unless}} |
| 144 | + context.report({ |
| 145 | + node: nodeInverse.body[0], |
| 146 | + messageId: 'asElseUnlessBlock', |
| 147 | + }); |
| 148 | + } |
| 149 | + } else if (isUnless(node) && node.params?.[0]?.path) { |
| 150 | + checkWithHelper(node); |
| 151 | + } |
| 152 | + }, |
| 153 | + }; |
| 154 | + }, |
| 155 | +}; |
0 commit comments