Extract rule: template-require-button-type#2600
Conversation
NullVoxPopuli-ai-agent
left a comment
There was a problem hiding this comment.
Review: template-require-button-type (PR #2600)
Comparison with ember-template-lint require-button-type.js
General correctness:
-
Core logic matches: The rule checks
<button>elements for a validtypeattribute with valuesbutton,submit, orreset. Missing type or invalid type is reported. This matches the original. -
Form-aware fix: Both the original and the PR check for a parent
<form>element to decide the default fix value (submitinside form,buttonotherwise). The original useshasParentTag(path, 'form')from a shared helper. The PR implements its ownhasParentForm(node)that walksnode.parent. This is functionally equivalent. -
Fix for invalid type — behavior difference: The original's fix for invalid type values always uses
b.text('button')(hardcoded to"button"). The PR's fix useshasParentForm(node) ? 'submit' : 'button', so it's context-aware even for the invalid-type case. This is actually an improvement over the original — if a button inside a form hastype="foo", it makes more sense to fix it totype="submit"thantype="button". -
Dynamic type values: The original returns early for non-TextNode type values (
if (value.type !== 'TextNode') return). The PR has a comment "Dynamic type values cannot be validated at lint time" and similarly doesn't report them. Correct match. -
Self-closing button fix: The PR has special handling for
<button/>→<button type="button" />. The original usesb.attr(...)to push to attributes (AST builder approach). The PR's regex-based approach (/^<button\s*\/>/.test(text)) is a bit fragile — it wouldn't handle<button class="x"/>(button with other attributes but no type). However, since this code path is only reached whentypeAttris missing, a self-closing button with other attributes would still hit theopenTagregex path, which should work. -
Tests are comprehensive: Both gjs and hbs modes, valid/invalid cases with fixes, form context, edge cases like
type=42, and even a JS comment containing<button>to test parser robustness.
Scope analysis (gjs/gts):
This rule checks HTML <button> elements — pure structural/tag-name matching. No helper/component name resolution. No scope analysis needed.
Overall: Clean, well-tested migration with a nice improvement on the fix logic for invalid types inside forms.
🤖 Automated review comparing with ember-template-lint source
a7f216d to
cf53182
Compare
Split from #2371.