|
| 1 | +/* eslint-disable eslint-plugin/prefer-placeholders */ |
| 2 | +const BUILT_INS = new Set([ |
| 3 | + 'action', |
| 4 | + 'array', |
| 5 | + 'component', |
| 6 | + 'concat', |
| 7 | + 'debugger', |
| 8 | + 'each', |
| 9 | + 'each-in', |
| 10 | + 'fn', |
| 11 | + 'get', |
| 12 | + 'hasBlock', |
| 13 | + 'has-block', |
| 14 | + 'has-block-params', |
| 15 | + 'hash', |
| 16 | + 'if', |
| 17 | + 'input', |
| 18 | + 'let', |
| 19 | + 'link-to', |
| 20 | + 'loc', |
| 21 | + 'log', |
| 22 | + 'mount', |
| 23 | + 'mut', |
| 24 | + 'on', |
| 25 | + 'outlet', |
| 26 | + 'partial', |
| 27 | + 'query-params', |
| 28 | + 'textarea', |
| 29 | + 'unbound', |
| 30 | + 'unique-id', |
| 31 | + 'unless', |
| 32 | + 'with', |
| 33 | + '-in-element', |
| 34 | + 'in-element', |
| 35 | + 'app-version', |
| 36 | + 'rootURL', |
| 37 | +]); |
| 38 | + |
| 39 | +const ALWAYS_CURLY = new Set(['yield']); |
| 40 | + |
| 41 | +function transformTagName(name) { |
| 42 | + // Convert kebab-case to PascalCase for angle bracket syntax |
| 43 | + const parts = name.split('/'); |
| 44 | + return parts |
| 45 | + .map((part) => { |
| 46 | + return part |
| 47 | + .split('-') |
| 48 | + .map((p) => p.charAt(0).toUpperCase() + p.slice(1)) |
| 49 | + .join(''); |
| 50 | + }) |
| 51 | + .join('::'); |
| 52 | +} |
| 53 | + |
| 54 | +function parseConfig(config) { |
| 55 | + const defaults = { |
| 56 | + allow: [], |
| 57 | + disallow: [], |
| 58 | + requireDash: false, |
| 59 | + noImplicitThis: true, |
| 60 | + }; |
| 61 | + |
| 62 | + if (config === true) { |
| 63 | + return defaults; |
| 64 | + } |
| 65 | + |
| 66 | + return { ...defaults, ...config }; |
| 67 | +} |
| 68 | + |
| 69 | +/** @type {import('eslint').Rule.RuleModule} */ |
| 70 | +module.exports = { |
| 71 | + meta: { |
| 72 | + type: 'suggestion', |
| 73 | + docs: { |
| 74 | + description: 'disallow curly component invocation, use angle bracket syntax instead', |
| 75 | + category: 'Best Practices', |
| 76 | + recommended: false, |
| 77 | + url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-curly-component-invocation.md', |
| 78 | + templateMode: 'loose', |
| 79 | + }, |
| 80 | + fixable: null, |
| 81 | + schema: [ |
| 82 | + { |
| 83 | + type: 'object', |
| 84 | + properties: { |
| 85 | + allow: { |
| 86 | + type: 'array', |
| 87 | + items: { type: 'string' }, |
| 88 | + }, |
| 89 | + disallow: { |
| 90 | + type: 'array', |
| 91 | + items: { type: 'string' }, |
| 92 | + }, |
| 93 | + requireDash: { |
| 94 | + type: 'boolean', |
| 95 | + }, |
| 96 | + noImplicitThis: { |
| 97 | + type: 'boolean', |
| 98 | + }, |
| 99 | + }, |
| 100 | + additionalProperties: false, |
| 101 | + }, |
| 102 | + ], |
| 103 | + messages: {}, |
| 104 | + originallyFrom: { |
| 105 | + name: 'ember-template-lint', |
| 106 | + rule: 'lib/rules/no-curly-component-invocation.js', |
| 107 | + docs: 'docs/rule/no-curly-component-invocation.md', |
| 108 | + tests: 'test/unit/rules/no-curly-component-invocation-test.js', |
| 109 | + }, |
| 110 | + }, |
| 111 | + |
| 112 | + create(context) { |
| 113 | + const config = parseConfig(context.options[0]); |
| 114 | + |
| 115 | + function shouldCheckComponent(pathOriginal) { |
| 116 | + // Check if in allow list |
| 117 | + if (config.allow.includes(pathOriginal)) { |
| 118 | + return false; |
| 119 | + } |
| 120 | + |
| 121 | + // Check if in disallow list - always report these |
| 122 | + if (config.disallow.includes(pathOriginal)) { |
| 123 | + return true; |
| 124 | + } |
| 125 | + |
| 126 | + // Always curly - don't report |
| 127 | + if (ALWAYS_CURLY.has(pathOriginal)) { |
| 128 | + return false; |
| 129 | + } |
| 130 | + |
| 131 | + // Built-in helpers - don't report |
| 132 | + if (BUILT_INS.has(pathOriginal)) { |
| 133 | + return false; |
| 134 | + } |
| 135 | + |
| 136 | + // If it looks like a component (has dash or slash), flag it |
| 137 | + if (pathOriginal.includes('-') || pathOriginal.includes('/')) { |
| 138 | + return true; |
| 139 | + } |
| 140 | + |
| 141 | + return false; |
| 142 | + } |
| 143 | + |
| 144 | + return { |
| 145 | + GlimmerMustacheStatement(node) { |
| 146 | + if (!node.path || node.path.type !== 'GlimmerPathExpression') { |
| 147 | + return; |
| 148 | + } |
| 149 | + |
| 150 | + const pathOriginal = node.path.original; |
| 151 | + |
| 152 | + // Skip if has positional params or hash arguments (can't be converted to angle brackets) |
| 153 | + if ( |
| 154 | + (node.params && node.params.length > 0) || |
| 155 | + (node.hash && node.hash.pairs && node.hash.pairs.length > 0) |
| 156 | + ) { |
| 157 | + return; |
| 158 | + } |
| 159 | + |
| 160 | + if (shouldCheckComponent(pathOriginal)) { |
| 161 | + const angleBracketName = transformTagName(pathOriginal); |
| 162 | + context.report({ |
| 163 | + node, |
| 164 | + message: `You are using the component {{${pathOriginal}}} with curly component syntax. You should use <${angleBracketName}> instead. If it is actually a helper you must manually add it to the 'no-curly-component-invocation' rule configuration, e.g. \`'no-curly-component-invocation': { allow: ['${pathOriginal}'] }\`.`, |
| 165 | + }); |
| 166 | + } |
| 167 | + }, |
| 168 | + |
| 169 | + GlimmerBlockStatement(node) { |
| 170 | + if (!node.path || node.path.type !== 'GlimmerPathExpression') { |
| 171 | + return; |
| 172 | + } |
| 173 | + |
| 174 | + const pathOriginal = node.path.original; |
| 175 | + |
| 176 | + // Skip if has positional params or hash arguments |
| 177 | + if ( |
| 178 | + (node.params && node.params.length > 0) || |
| 179 | + (node.hash && node.hash.pairs && node.hash.pairs.length > 0) |
| 180 | + ) { |
| 181 | + return; |
| 182 | + } |
| 183 | + |
| 184 | + if (shouldCheckComponent(pathOriginal)) { |
| 185 | + const angleBracketName = transformTagName(pathOriginal); |
| 186 | + context.report({ |
| 187 | + node, |
| 188 | + message: `You are using the component {{#${pathOriginal}}} with curly component syntax. You should use <${angleBracketName}> instead. If it is actually a helper you must manually add it to the 'no-curly-component-invocation' rule configuration, e.g. \`'no-curly-component-invocation': { allow: ['${pathOriginal}'] }\`.`, |
| 189 | + }); |
| 190 | + } |
| 191 | + }, |
| 192 | + }; |
| 193 | + }, |
| 194 | +}; |
| 195 | +/* eslint-enable eslint-plugin/prefer-placeholders */ |
0 commit comments