-
Notifications
You must be signed in to change notification settings - Fork 14
[cli] autofix via mops check --fix
#381
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
Merged
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
7a86d97
Set up 'mops check'
rvanasa cb4ef40
Merge branch 'main' into mops-check
rvanasa fd76d93
Set up autofix logic
rvanasa 0ff22e0
Merge branch 'mops-check' of https://github.com/dfinity/mops into mop…
rvanasa e326569
Format
rvanasa a968812
Improve tests and add '--warnings' flag
rvanasa 409b896
Refactor tests
rvanasa c19f4d3
Update M0223 test case
rvanasa 54c0b89
Merge branch 'main' into mops-check
Kamirus 6ac097a
[cli] Integrate mops toolchain for build process + build test without…
Kamirus 8650c2a
use json suggestsions in autofix
Kamirus d2311fb
separate tests
Kamirus 038d7a0
integrate lint-staged for pre-commit checks
Kamirus ffd6568
Update package dependencies and refactor autofix logic to utilize vsc…
Kamirus 0b46c80
Refactor check requirements and autofix logic to utilize updated vers…
Kamirus 296da30
Refactor diagnostics logging in `check` command and normalize file pa…
Kamirus 8751edf
fixpoint up 10 iterations
Kamirus d58fdc7
Explain that the check command runs transitively on all imported file…
Kamirus 0f1fbca
Refactor `testCheckFix` to simplify fixture copying and remove conten…
Kamirus 8818d83
Refactor `supportsAllLibsFlag` to remove mocPath parameter and update…
Kamirus 73f9dfd
count all diagnostics in tests + fix nonfixable error
Kamirus ebfcf25
Enhance `check` command output to display detailed fix information, i…
Kamirus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| import { readFile, writeFile } from "node:fs/promises"; | ||
| import { resolve } from "node:path"; | ||
| import { execa } from "execa"; | ||
| import { | ||
| TextDocument, | ||
| type TextEdit, | ||
| } from "vscode-languageserver-textdocument"; | ||
|
|
||
| interface Fix { | ||
| file: string; | ||
| code: string; | ||
| edit: TextEdit; | ||
| } | ||
|
|
||
| interface MocSpan { | ||
| file: string; | ||
| line_start: number; | ||
| column_start: number; | ||
| line_end: number; | ||
| column_end: number; | ||
| is_primary: boolean; | ||
| label: string | null; | ||
| suggested_replacement: string | null; | ||
| suggestion_applicability: string | null; | ||
| } | ||
|
|
||
| export interface MocDiagnostic { | ||
| message: string; | ||
| code: string; | ||
| level: string; | ||
| spans: MocSpan[]; | ||
| notes: string[]; | ||
| } | ||
|
|
||
| export function parseDiagnostics(stdout: string): MocDiagnostic[] { | ||
| return stdout | ||
| .split("\n") | ||
| .filter((l) => l.trim()) | ||
| .map((l) => { | ||
| try { | ||
| return JSON.parse(l) as MocDiagnostic; | ||
| } catch { | ||
| return null; | ||
| } | ||
| }) | ||
| .filter((d) => d !== null); | ||
| } | ||
|
|
||
| function extractFixes(diagnostics: MocDiagnostic[]): Fix[] { | ||
| const fixes: Fix[] = []; | ||
| for (const diag of diagnostics) { | ||
| for (const span of diag.spans) { | ||
| if ( | ||
| span.suggestion_applicability === "MachineApplicable" && | ||
| span.suggested_replacement !== null | ||
| ) { | ||
| fixes.push({ | ||
| file: span.file, | ||
| code: diag.code, | ||
| edit: { | ||
| range: { | ||
| start: { | ||
| line: span.line_start - 1, | ||
| character: span.column_start - 1, | ||
| }, | ||
| end: { | ||
| line: span.line_end - 1, | ||
| character: span.column_end - 1, | ||
| }, | ||
| }, | ||
| newText: span.suggested_replacement, | ||
| }, | ||
| }); | ||
| } | ||
| } | ||
| } | ||
| return fixes; | ||
| } | ||
|
|
||
| const MAX_FIX_ITERATIONS = 10; | ||
|
|
||
| export interface AutofixResult { | ||
| /** Map of file path → diagnostic codes fixed in that file */ | ||
| fixedFiles: Map<string, string[]>; | ||
| totalFixCount: number; | ||
| } | ||
|
|
||
| export async function autofixMotoko( | ||
| mocPath: string, | ||
| files: string[], | ||
| mocArgs: string[], | ||
| ): Promise<AutofixResult | null> { | ||
| const fixedFilesCodes = new Map<string, string[]>(); | ||
|
|
||
| for (let iteration = 0; iteration < MAX_FIX_ITERATIONS; iteration++) { | ||
| const allFixes: Fix[] = []; | ||
|
|
||
| for (const file of files) { | ||
| const result = await execa( | ||
| mocPath, | ||
| [file, "--error-format=json", ...mocArgs], | ||
| { stdio: "pipe", reject: false }, | ||
| ); | ||
|
|
||
| const diagnostics = parseDiagnostics(result.stdout); | ||
| allFixes.push(...extractFixes(diagnostics)); | ||
| } | ||
|
|
||
| if (allFixes.length === 0) { | ||
| break; | ||
| } | ||
|
|
||
| const fixesByFile = new Map<string, Fix[]>(); | ||
| for (const fix of allFixes) { | ||
| const normalizedPath = resolve(fix.file); | ||
| const existing = fixesByFile.get(normalizedPath) ?? []; | ||
| existing.push(fix); | ||
| fixesByFile.set(normalizedPath, existing); | ||
| } | ||
|
|
||
| let progress = false; | ||
|
|
||
| for (const [file, fixes] of fixesByFile) { | ||
| const original = await readFile(file, "utf-8"); | ||
| const doc = TextDocument.create(`file://${file}`, "motoko", 0, original); | ||
|
|
||
| let result: string; | ||
| try { | ||
| result = TextDocument.applyEdits( | ||
| doc, | ||
| fixes.map((f) => f.edit), | ||
| ); | ||
| } catch (err) { | ||
| console.warn(`Warning: could not apply fixes to ${file}: ${err}`); | ||
| continue; | ||
| } | ||
|
|
||
| if (result === original) { | ||
| continue; | ||
| } | ||
|
|
||
| await writeFile(file, result, "utf-8"); | ||
| progress = true; | ||
|
|
||
| const existing = fixedFilesCodes.get(file) ?? []; | ||
| for (const fix of fixes) { | ||
| existing.push(fix.code); | ||
| } | ||
| fixedFilesCodes.set(file, existing); | ||
| } | ||
|
|
||
| if (!progress) { | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| if (fixedFilesCodes.size === 0) { | ||
| return null; | ||
| } | ||
|
|
||
| let totalFixCount = 0; | ||
| for (const codes of fixedFilesCodes.values()) { | ||
| totalFixCount += codes.length; | ||
| } | ||
|
|
||
| return { | ||
| fixedFiles: fixedFilesCodes, | ||
| totalFixCount, | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally we should just have
mops check, but it is ok for now to explicitly provide the entrypoint.It is important to note that this command will also check all transitively imported files.