|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +// See html-validate (https://html-validate.org) for the peer rule concept. |
| 4 | + |
| 5 | +const { isNativeElement } = require('../utils/is-native-element'); |
| 6 | + |
| 7 | +const VALID_TYPES = new Set([ |
| 8 | + 'button', |
| 9 | + 'checkbox', |
| 10 | + 'color', |
| 11 | + 'date', |
| 12 | + 'datetime-local', |
| 13 | + 'email', |
| 14 | + 'file', |
| 15 | + 'hidden', |
| 16 | + 'image', |
| 17 | + 'month', |
| 18 | + 'number', |
| 19 | + 'password', |
| 20 | + 'radio', |
| 21 | + 'range', |
| 22 | + 'reset', |
| 23 | + 'search', |
| 24 | + 'submit', |
| 25 | + 'tel', |
| 26 | + 'text', |
| 27 | + 'time', |
| 28 | + 'url', |
| 29 | + 'week', |
| 30 | +]); |
| 31 | + |
| 32 | +/** @type {import('eslint').Rule.RuleModule} */ |
| 33 | +module.exports = { |
| 34 | + meta: { |
| 35 | + type: 'problem', |
| 36 | + docs: { |
| 37 | + description: 'require input elements to have a valid type attribute', |
| 38 | + category: 'Best Practices', |
| 39 | + url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-require-input-type.md', |
| 40 | + templateMode: 'both', |
| 41 | + }, |
| 42 | + fixable: 'code', |
| 43 | + schema: [ |
| 44 | + { |
| 45 | + type: 'object', |
| 46 | + properties: { |
| 47 | + requireExplicit: { |
| 48 | + type: 'boolean', |
| 49 | + }, |
| 50 | + }, |
| 51 | + additionalProperties: false, |
| 52 | + }, |
| 53 | + ], |
| 54 | + messages: { |
| 55 | + missing: 'All `<input>` elements should have a `type` attribute', |
| 56 | + invalid: '`<input type="{{value}}">` is not a valid input type', |
| 57 | + }, |
| 58 | + }, |
| 59 | + |
| 60 | + create(context) { |
| 61 | + // Flagging a missing `type` is a style/consistency check, not a correctness |
| 62 | + // one: `<input>` without `type` is spec-compliant (defaults to the Text |
| 63 | + // state). Opt-in so teams that want parity with template-require-button- |
| 64 | + // type can enable it without imposing it on others. |
| 65 | + const requireExplicit = Boolean(context.options[0]?.requireExplicit); |
| 66 | + const sourceCode = context.sourceCode || context.getSourceCode(); |
| 67 | + |
| 68 | + return { |
| 69 | + GlimmerElementNode(node) { |
| 70 | + if (node.tag !== 'input') { |
| 71 | + return; |
| 72 | + } |
| 73 | + // In strict GJS, a lowercase local binding can shadow the native |
| 74 | + // `<input>` element. `isNativeElement` consults html/svg/mathml tag |
| 75 | + // lists and checks bindings in the scope chain to filter out |
| 76 | + // scope-shadowed cases. |
| 77 | + if (!isNativeElement(node, sourceCode)) { |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + const typeAttr = node.attributes?.find((attr) => attr.name === 'type'); |
| 82 | + |
| 83 | + if (!typeAttr) { |
| 84 | + if (!requireExplicit) { |
| 85 | + return; |
| 86 | + } |
| 87 | + context.report({ |
| 88 | + node, |
| 89 | + messageId: 'missing', |
| 90 | + fix(fixer) { |
| 91 | + // Insert right after `<input` so the new attribute is the first |
| 92 | + // one — avoids the fragile "find end of open tag" regex that can |
| 93 | + // mis-place the attribute past the `/` in self-closing syntax. |
| 94 | + const insertPos = node.range[0] + '<input'.length; |
| 95 | + return fixer.insertTextBeforeRange([insertPos, insertPos], ' type="text"'); |
| 96 | + }, |
| 97 | + }); |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + const value = typeAttr.value; |
| 102 | + |
| 103 | + // Valueless attribute form (`<input type />`) — per HTML spec, a |
| 104 | + // present-but-empty type attribute resolves to the missing-value |
| 105 | + // default ("Text state"). That's the same runtime result as |
| 106 | + // `type=""`, which we already flag. Treat them consistently: |
| 107 | + // flag as invalid('') and autofix to `type="text"`. |
| 108 | + if (!value) { |
| 109 | + context.report({ |
| 110 | + node: typeAttr, |
| 111 | + messageId: 'invalid', |
| 112 | + data: { value: '' }, |
| 113 | + fix(fixer) { |
| 114 | + return fixer.replaceText(typeAttr, 'type="text"'); |
| 115 | + }, |
| 116 | + }); |
| 117 | + return; |
| 118 | + } |
| 119 | + |
| 120 | + if (value.type === 'GlimmerTextNode') { |
| 121 | + const typeValue = value.chars.toLowerCase(); |
| 122 | + if (typeValue === '') { |
| 123 | + context.report({ |
| 124 | + node: typeAttr, |
| 125 | + messageId: 'invalid', |
| 126 | + data: { value: '' }, |
| 127 | + fix(fixer) { |
| 128 | + return fixer.replaceText(typeAttr, 'type="text"'); |
| 129 | + }, |
| 130 | + }); |
| 131 | + } else if (!VALID_TYPES.has(typeValue)) { |
| 132 | + context.report({ |
| 133 | + node: typeAttr, |
| 134 | + messageId: 'invalid', |
| 135 | + data: { value: value.chars }, |
| 136 | + fix(fixer) { |
| 137 | + return fixer.replaceText(typeAttr, 'type="text"'); |
| 138 | + }, |
| 139 | + }); |
| 140 | + } |
| 141 | + } |
| 142 | + }, |
| 143 | + }; |
| 144 | + }, |
| 145 | +}; |
0 commit comments