-
-
Notifications
You must be signed in to change notification settings - Fork 5
Add autofix support for the form-method-require rule
#337
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1966,6 +1966,108 @@ | |
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Create auto-fix action for form-method-require rule | ||
| */ | ||
| function createFormMethodRequireFix( | ||
| document: TextDocument, | ||
| diagnostic: Diagnostic, | ||
| ): CodeAction | null { | ||
| trace( | ||
| `[DEBUG] createFormMethodRequireFix called with diagnostic: ${JSON.stringify(diagnostic)}`, | ||
| ); | ||
|
|
||
| if (!diagnostic.data || diagnostic.data.ruleId !== "form-method-require") { | ||
| trace( | ||
| `[DEBUG] createFormMethodRequireFix: Invalid diagnostic data or ruleId`, | ||
| ); | ||
| return null; | ||
| } | ||
|
|
||
| const text = document.getText(); | ||
| const diagnosticOffset = document.offsetAt(diagnostic.range.start); | ||
|
|
||
| // Use robust tag boundary detection to find the form tag | ||
| const tagBoundaries = findTagBoundaries(text, diagnosticOffset); | ||
| if (!tagBoundaries) { | ||
| trace(`[DEBUG] createFormMethodRequireFix: Could not find tag boundaries`); | ||
| return null; | ||
| } | ||
|
|
||
| const { tagStart, tagEnd } = tagBoundaries; | ||
| const tagContent = text.substring(tagStart, tagEnd + 1); | ||
| trace(`[DEBUG] createFormMethodRequireFix: Found tag: ${tagContent}`); | ||
|
|
||
| // Verify this is a form tag | ||
| const formTagMatch = tagContent.match(/^<\s*form\b/i); | ||
| if (!formTagMatch) { | ||
| trace(`[DEBUG] createFormMethodRequireFix: Not a form tag`); | ||
| return null; | ||
| } | ||
|
|
||
| // Check if method attribute already exists | ||
| const methodAttrMatch = tagContent.match(/\bmethod\s*=/i); | ||
| if (methodAttrMatch) { | ||
| trace( | ||
| `[DEBUG] createFormMethodRequireFix: Method attribute already exists`, | ||
| ); | ||
| return null; | ||
| } | ||
|
|
||
| // Find the best position to insert the method attribute | ||
| // We'll add it after the opening form tag name but before the closing > | ||
| const formMatch = tagContent.match(/^(<\s*form)(\s+[^>]*?)?(\/?\s*>)$/i); | ||
| if (!formMatch) { | ||
| trace( | ||
| `[DEBUG] createFormMethodRequireFix: Could not parse form tag structure`, | ||
| ); | ||
| return null; | ||
| } | ||
|
|
||
| const beforeAttrs = formMatch[1]; // "<form" | ||
| const existingAttrs = formMatch[2] || ""; // existing attributes | ||
| const tagClose = formMatch[3]; // ">" or "/>" | ||
|
||
|
|
||
coliff marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // Calculate insertion position | ||
| let insertPosition: number; | ||
| let newText: string; | ||
|
|
||
| if (existingAttrs.trim()) { | ||
| // There are existing attributes, add method after them | ||
| insertPosition = tagStart + beforeAttrs.length + existingAttrs.length; | ||
| newText = ' method=""'; | ||
| } else { | ||
| // No existing attributes, add method right after "form" | ||
| insertPosition = tagStart + beforeAttrs.length; | ||
| newText = ' method=""'; | ||
coliff marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
Comment on lines
2032
to
2040
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The // Calculate insertion position
const newText = ' method=""';
let insertPosition: number;
if (existingAttrs.trim()) {
// There are existing attributes, add method after them
insertPosition = tagStart + beforeAttrs.length + existingAttrs.length;
} else {
// No existing attributes, add method right after "form"
insertPosition = tagStart + beforeAttrs.length;
} |
||
|
|
||
| const edit: TextEdit = { | ||
| range: { | ||
| start: document.positionAt(insertPosition), | ||
| end: document.positionAt(insertPosition), | ||
| }, | ||
| newText: newText, | ||
| }; | ||
|
|
||
| trace( | ||
| `[DEBUG] createFormMethodRequireFix: Will insert "${newText}" at position ${insertPosition}`, | ||
| ); | ||
|
|
||
| const workspaceEdit: WorkspaceEdit = { | ||
| changes: { | ||
| [document.uri]: [edit], | ||
| }, | ||
| }; | ||
|
|
||
| return { | ||
| title: 'Add method="" attribute to form', | ||
| kind: CodeActionKind.QuickFix, | ||
| edit: workspaceEdit, | ||
| isPreferred: true, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Create auto-fix actions for supported rules | ||
| */ | ||
|
|
@@ -2064,6 +2166,10 @@ | |
| trace(`[DEBUG] Calling createAttrNoDuplicationFix`); | ||
| fix = createAttrNoDuplicationFix(document, diagnostic); | ||
| break; | ||
| case "form-method-require": | ||
| trace(`[DEBUG] Calling createFormMethodRequireFix`); | ||
| fix = createFormMethodRequireFix(document, diagnostic); | ||
| break; | ||
| default: | ||
| trace(`[DEBUG] No autofix function found for rule: ${ruleId}`); | ||
| break; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.