Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,56 @@ import rule, { RULE_NAME } from "./no-missing-button-type";

ruleTester.run(RULE_NAME, rule, {
invalid: [
{
code: tsx`<button />;`,
errors: [
{
messageId: "noMissingButtonType",
suggestions: [
{
messageId: "addButtonType",
data: { type: "button" },
output: tsx`<button type="button" />;`,
},
{
messageId: "addButtonType",
data: { type: "submit" },
output: tsx`<button type="submit" />;`,
},
{
messageId: "addButtonType",
data: { type: "reset" },
output: tsx`<button type="reset" />;`,
},
],
},
],
},
{
code: tsx`<button type />;`,
errors: [
{
messageId: "noMissingButtonType",
suggestions: [
{
messageId: "addButtonType",
data: { type: "button" },
output: tsx`<button type="button" />;`,
},
{
messageId: "addButtonType",
data: { type: "submit" },
output: tsx`<button type="submit" />;`,
},
{
messageId: "addButtonType",
data: { type: "reset" },
output: tsx`<button type="reset" />;`,
},
],
},
],
},
{
code: tsx`<button>Click me</button>;`,
errors: [
Expand Down Expand Up @@ -39,17 +89,17 @@ ruleTester.run(RULE_NAME, rule, {
{
messageId: "addButtonType",
data: { type: "button" },
output: tsx`<PolyComponent as="button" type="button">Click me</PolyComponent>;`,
output: tsx`<PolyComponent type="button" as="button">Click me</PolyComponent>;`,
},
{
messageId: "addButtonType",
data: { type: "submit" },
output: tsx`<PolyComponent as="button" type="submit">Click me</PolyComponent>;`,
output: tsx`<PolyComponent type="submit" as="button">Click me</PolyComponent>;`,
},
{
messageId: "addButtonType",
data: { type: "reset" },
output: tsx`<PolyComponent as="button" type="reset">Click me</PolyComponent>;`,
output: tsx`<PolyComponent type="reset" as="button">Click me</PolyComponent>;`,
},
],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ export const RULE_FEATURES = [

export const BUTTON_TYPES = ["button", "submit", "reset"] as const;

export type MessageID =
| CamelCase<typeof RULE_NAME>
| "addButtonType";
export type MessageID = CamelCase<typeof RULE_NAME> | RuleSuggestMessageID;

export type RuleSuggestMessageID = "addButtonType";

export default createRule<[], MessageID>({
meta: {
Expand Down Expand Up @@ -55,12 +55,12 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
attributeNode,
propNameOnJsx,
);
if (attributeValue.kind === "some" && typeof attributeValue.value !== "string") {
if (attributeValue.kind !== "some" || typeof attributeValue.value !== "string") {
context.report({
messageId: "noMissingButtonType",
node: attributeNode,
suggest: getSuggest((type) => (fixer: RuleFixer) => {
return fixer.replaceText(node, `${propNameOnJsx}="${type}"`);
return fixer.replaceText(attributeNode, `${propNameOnJsx}="${type}"`);
}),
});
}
Expand All @@ -71,9 +71,7 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
messageId: "noMissingButtonType",
node,
suggest: getSuggest((type) => (fixer: RuleFixer) => {
const lastToken = context.sourceCode.getLastToken(node.openingElement);
if (lastToken == null) return null;
return fixer.insertTextBefore(lastToken, ` type="${type}"`);
return fixer.insertTextAfter(node.openingElement.name, ` type="${type}"`);
}),
});
}
Expand Down
Loading