-
-
Notifications
You must be signed in to change notification settings - Fork 211
Expand file tree
/
Copy pathtemplate-no-block-params-for-html-elements.js
More file actions
77 lines (70 loc) · 2.22 KB
/
template-no-block-params-for-html-elements.js
File metadata and controls
77 lines (70 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/** @type {import('eslint').Rule.RuleModule} */
// Mirror upstream ember-template-lint's inverse-of-isAngleBracketComponent logic.
// A tag is treated as an HTML element only when it:
// - does NOT contain ':' (named blocks like <:slot>)
// - does NOT contain '.' (path/namespaced invocations like <foo.bar>)
// - does NOT start with '@' (argument invocations like <@foo>)
// - has NO uppercase letters (component invocations like <MyThing>)
// - does NOT contain '-' (HTML custom elements like <my-element>)
// Everything else is a component / custom-element / slot — not a plain HTML element.
function isHtmlElement(tagName) {
if (!tagName) {
return false;
}
if (tagName.startsWith('@')) {
return false;
}
if (tagName.includes(':')) {
return false;
}
if (tagName.includes('.')) {
return false;
}
if (tagName.includes('-')) {
return false;
}
if (tagName !== tagName.toLowerCase()) {
return false;
}
return true;
}
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'disallow block params on HTML elements',
category: 'Best Practices',
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-block-params-for-html-elements.md',
},
fixable: null,
schema: [],
messages: {
noBlockParamsForHtmlElements:
'Block params can only be used with components, not HTML elements.',
},
},
create(context) {
const sourceCode = context.sourceCode;
return {
GlimmerElementNode(node) {
// Check if this is an HTML element (not a component / custom element / slot)
if (!isHtmlElement(node.tag)) {
return;
}
// If the tag name is a variable in scope, it's being used as a component, not an HTML element
const scope = sourceCode.getScope(node.parent);
const isVariable = scope.references.some((ref) => ref.identifier === node.parts[0]);
if (isVariable) {
return;
}
// Check for block params
if (node.blockParams && node.blockParams.length > 0) {
context.report({
node,
messageId: 'noBlockParamsForHtmlElements',
});
}
},
};
},
};