forked from ember-cli/eslint-plugin-ember
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate-no-heading-inside-button.js
More file actions
47 lines (45 loc) · 1.3 KB
/
template-no-heading-inside-button.js
File metadata and controls
47 lines (45 loc) · 1.3 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
const HEADING_ELEMENTS = new Set(['h1', 'h2', 'h3', 'h4', 'h5', 'h6']);
function hasButtonParent(node) {
let parent = node.parent;
while (parent) {
if (parent.type === 'GlimmerElementNode') {
// Check if it's a button element
if (parent.tag === 'button') {
return true;
}
// Check if it has role="button"
const roleAttr = parent.attributes?.find((a) => a.name === 'role');
if (roleAttr?.value?.type === 'GlimmerTextNode' && roleAttr.value.chars === 'button') {
return true;
}
}
parent = parent.parent;
}
return false;
}
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'disallow heading elements inside button elements',
category: 'Accessibility',
strictGjs: true,
strictGts: true,
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-heading-inside-button.md',
},
schema: [],
messages: {
noHeading: 'Buttons should not contain heading elements',
},
},
create(context) {
return {
GlimmerElementNode(node) {
if (HEADING_ELEMENTS.has(node.tag) && hasButtonParent(node)) {
context.report({ node, messageId: 'noHeading' });
}
},
};
},
};