|
| 1 | +const HEADINGS = new Set(['h1', 'h2', 'h3', 'h4', 'h5', 'h6']); |
| 2 | + |
| 3 | +/** @type {import('eslint').Rule.RuleModule} */ |
| 4 | +module.exports = { |
| 5 | + meta: { |
| 6 | + type: 'problem', |
| 7 | + docs: { |
| 8 | + description: 'disallow empty heading elements', |
| 9 | + category: 'Accessibility', |
| 10 | + recommendedGjs: true, |
| 11 | + recommendedGts: true, |
| 12 | + url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-empty-headings.md', |
| 13 | + }, |
| 14 | + schema: [], |
| 15 | + messages: { |
| 16 | + emptyHeading: |
| 17 | + 'Headings must contain accessible text content (or helper/component that provides text).', |
| 18 | + }, |
| 19 | + }, |
| 20 | + create(context) { |
| 21 | + function hasTextContent(node) { |
| 22 | + if (!node.children || node.children.length === 0) return false; |
| 23 | + |
| 24 | + for (const child of node.children) { |
| 25 | + // Text nodes with content |
| 26 | + if (child.type === 'GlimmerTextNode' && child.chars.trim().length > 0) { |
| 27 | + return true; |
| 28 | + } |
| 29 | + // Mustache statements or helpers |
| 30 | + if ( |
| 31 | + child.type === 'GlimmerMustacheStatement' || |
| 32 | + child.type === 'GlimmerBlockStatement' |
| 33 | + ) { |
| 34 | + return true; |
| 35 | + } |
| 36 | + // Nested elements |
| 37 | + if (child.type === 'GlimmerElementNode' && hasTextContent(child)) { |
| 38 | + return true; |
| 39 | + } |
| 40 | + } |
| 41 | + return false; |
| 42 | + } |
| 43 | + |
| 44 | + return { |
| 45 | + GlimmerElementNode(node) { |
| 46 | + if (HEADINGS.has(node.tag)) { |
| 47 | + // Skip if hidden |
| 48 | + const hasHidden = node.attributes?.some((a) => a.name === 'hidden'); |
| 49 | + const ariaHidden = node.attributes?.find((a) => a.name === 'aria-hidden'); |
| 50 | + if ( |
| 51 | + hasHidden || |
| 52 | + (ariaHidden?.value?.type === 'GlimmerTextNode' && ariaHidden.value.chars === 'true') |
| 53 | + ) { |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + if (!hasTextContent(node)) { |
| 58 | + context.report({ node, messageId: 'emptyHeading' }); |
| 59 | + } |
| 60 | + } |
| 61 | + }, |
| 62 | + }; |
| 63 | + }, |
| 64 | +}; |
0 commit comments