|
| 1 | +/** |
| 2 | + * Check if a tabindex value is statically verifiable as safe (0 or negative). |
| 3 | + * Returns true only if we can confirm the value is not positive. |
| 4 | + * Dynamic, non-numeric, or boolean values are considered unsafe. |
| 5 | + */ |
| 6 | +function isTabindexSafe(attrValue) { |
| 7 | + if (!attrValue) { |
| 8 | + return true; |
| 9 | + } |
| 10 | + |
| 11 | + // Handle simple text values like tabindex="0" or tabindex="-1" |
| 12 | + if (attrValue.type === 'GlimmerTextNode') { |
| 13 | + const value = Number.parseInt(attrValue.chars, 10); |
| 14 | + return !Number.isNaN(value) && value <= 0; |
| 15 | + } |
| 16 | + |
| 17 | + // Handle mustache statements like tabindex={{-1}} or tabindex={{someProperty}} |
| 18 | + if (attrValue.type === 'GlimmerMustacheStatement') { |
| 19 | + const path = attrValue.path; |
| 20 | + |
| 21 | + if (path.type === 'GlimmerNumberLiteral') { |
| 22 | + return Number.parseInt(path.original, 10) <= 0; |
| 23 | + } |
| 24 | + if (path.type === 'GlimmerStringLiteral') { |
| 25 | + const value = Number.parseInt(path.original, 10); |
| 26 | + return !Number.isNaN(value) && value <= 0; |
| 27 | + } |
| 28 | + |
| 29 | + // Handle conditional expressions like {{if this.show -1 0}} |
| 30 | + if ( |
| 31 | + path.type === 'GlimmerPathExpression' && |
| 32 | + (path.original === 'if' || path.original === 'unless') |
| 33 | + ) { |
| 34 | + return isConditionalTabindexSafe(attrValue.params); |
| 35 | + } |
| 36 | + |
| 37 | + // Any other dynamic value (variable, boolean, etc.) is not verifiably safe |
| 38 | + return false; |
| 39 | + } |
| 40 | + |
| 41 | + // Handle concat statements like tabindex="{{-1}}" or tabindex="{{false}}" |
| 42 | + if (attrValue.type === 'GlimmerConcatStatement') { |
| 43 | + const parts = attrValue.parts || []; |
| 44 | + if (parts.length > 0 && parts[0].type === 'GlimmerMustacheStatement') { |
| 45 | + return isTabindexSafe(parts[0]); |
| 46 | + } |
| 47 | + return false; |
| 48 | + } |
| 49 | + |
| 50 | + return false; |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | + * Check that all branches of a conditional (if/unless) expression are safe. |
| 55 | + */ |
| 56 | +function isConditionalTabindexSafe(params) { |
| 57 | + if (!params) { |
| 58 | + return false; |
| 59 | + } |
| 60 | + |
| 61 | + // Check the value branches (params[1] and optionally params[2]) |
| 62 | + for (let i = 1; i < params.length && i < 3; i++) { |
| 63 | + const param = params[i]; |
| 64 | + if (param.type === 'GlimmerNumberLiteral') { |
| 65 | + if (Number.parseInt(param.original, 10) > 0) { |
| 66 | + return false; |
| 67 | + } |
| 68 | + } else if (param.type === 'GlimmerStringLiteral') { |
| 69 | + const val = Number.parseInt(param.original, 10); |
| 70 | + if (Number.isNaN(val) || val > 0) { |
| 71 | + return false; |
| 72 | + } |
| 73 | + } else { |
| 74 | + // Dynamic value in branch — not verifiably safe |
| 75 | + return false; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + return true; |
| 80 | +} |
| 81 | + |
| 82 | +/** @type {import('eslint').Rule.RuleModule} */ |
| 83 | +module.exports = { |
| 84 | + meta: { |
| 85 | + type: 'suggestion', |
| 86 | + docs: { |
| 87 | + description: 'disallow positive tabindex values', |
| 88 | + category: 'Accessibility', |
| 89 | + url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-positive-tabindex.md', |
| 90 | + templateMode: 'both', |
| 91 | + }, |
| 92 | + fixable: null, |
| 93 | + schema: [], |
| 94 | + messages: { |
| 95 | + positive: 'Avoid positive integer values for tabindex.', |
| 96 | + }, |
| 97 | + originallyFrom: { |
| 98 | + name: 'ember-template-lint', |
| 99 | + rule: 'lib/rules/no-positive-tabindex.js', |
| 100 | + docs: 'docs/rule/no-positive-tabindex.md', |
| 101 | + tests: 'test/unit/rules/no-positive-tabindex-test.js', |
| 102 | + }, |
| 103 | + }, |
| 104 | + |
| 105 | + create(context) { |
| 106 | + return { |
| 107 | + GlimmerElementNode(node) { |
| 108 | + const tabindexAttr = node.attributes?.find((attr) => attr.name === 'tabindex'); |
| 109 | + |
| 110 | + if (!tabindexAttr || !tabindexAttr.value) { |
| 111 | + return; |
| 112 | + } |
| 113 | + |
| 114 | + if (!isTabindexSafe(tabindexAttr.value)) { |
| 115 | + context.report({ |
| 116 | + node: tabindexAttr, |
| 117 | + messageId: 'positive', |
| 118 | + }); |
| 119 | + } |
| 120 | + }, |
| 121 | + }; |
| 122 | + }, |
| 123 | +}; |
0 commit comments