Skip to content

Commit

Permalink
feat: Add oxlint support (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
aklinker1 authored Aug 29, 2024
1 parent 7314018 commit f45f4e1
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pnpm check --fix
To enable checks for any of the following modules, just install them:

```sh
pnpm i -D typescript eslint prettier publint
pnpm i -D typescript oxlint prettier publint eslint
```

## Contributing
Expand Down
1 change: 1 addition & 0 deletions demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"eslint": "^8.56.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-only-warn": "^1.1.0",
"oxlint": "^0.9.1",
"vue-tsc": "^2.0.7"
}
}
82 changes: 82 additions & 0 deletions demo/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions src/tools/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { eslint } from "./eslint";
import { oxlint } from "./oxlint";
import { prettier } from "./prettier";
import { typescript } from "./typescript";
import { publint } from "./publint";
import { typescript } from "./typescript";

export const ALL_TOOLS = [publint, prettier, typescript, eslint];
export const ALL_TOOLS = [publint, prettier, typescript, oxlint, eslint];
59 changes: 59 additions & 0 deletions src/tools/oxlint.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { describe, it, expect } from "bun:test";
import { parseOuptut } from "./oxlint";

describe("Oxlint", () => {
it("should properly parse output", async () => {
const stdout = `
test.ts:1:7: Variable 'test' is declared but never used. [Warning/eslint(no-unused-vars)]
test.ts:3:7: Variable 'variable' is declared but never used. [Warning/eslint(no-unused-vars)]
test.ts:8:7: Variable 'two' is declared but never used. [Warning/eslint(no-unused-vars)]
test.ts:8:7: Missing initializer in const declaration [Error]
3 problems
`;
const stderr = "";
const code = 1;

expect(parseOuptut({ code, stdout, stderr })).toEqual([
{
file: "test.ts",
message: "Variable 'test' is declared but never used.",
location: {
line: 1,
column: 7,
},
rule: "eslint(no-unused-vars)",
kind: "warning",
},
{
file: "test.ts",
message: "Variable 'variable' is declared but never used.",
location: {
line: 3,
column: 7,
},
rule: "eslint(no-unused-vars)",
kind: "warning",
},
{
file: "test.ts",
message: "Variable 'two' is declared but never used.",
location: {
line: 8,
column: 7,
},
rule: "eslint(no-unused-vars)",
kind: "warning",
},
{
file: "test.ts",
message: "Missing initializer in const declaration",
location: {
line: 8,
column: 7,
},
kind: "error",
},
]);
});
});
53 changes: 53 additions & 0 deletions src/tools/oxlint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import type { OutputParser, Problem, ToolDefinition } from "../types";
import { execAndParse, isBinInstalled } from "../utils";
import { resolve } from "node:path";

export const oxlint: ToolDefinition = ({ binDir, root }) => {
const bin = resolve(root, binDir, "oxlint");
const checkArgs = ["--format=linux"];
const fixArgs = ["--format=linux", "--fix"];

return {
name: "Oxlint",
isInstalled: () => isBinInstalled(bin),
check: () => execAndParse(bin, checkArgs, root, parseOuptut),
fix: () => execAndParse(bin, fixArgs, root, parseOuptut),
};
};

export const parseOuptut: OutputParser = ({ stdout, stderr }) => {
if (stdout.trim()) {
return stdout.split(/\r?\n/).reduce<Problem[]>((acc, line) => {
const groups =
/^(?<file>.+?):(?<line>[0-9]+):(?<column>[0-9]):\s?(?<message>.*?)\s?\[(?<kind>Warning|Error)\/?(?<rule>.*?)\]\s?$/.exec(
line,
)?.groups;
if (groups) {
acc.push({
file: groups.file,
kind: groups.kind === "Error" ? "error" : "warning",
message: groups.message,
rule: groups.rule || undefined,
location: {
line: parseInt(groups.line, 10),
column: parseInt(groups.column, 10),
},
});
}
return acc;
}, []);
}

return stdout
.trim()
.split(/\r?\n/)
.map((line) => line.trim())
.filter((line) => !!line && !line.includes(" "))
.map(
(line): Problem => ({
file: line.trim(),
kind: "warning",
message: "Not formatted.",
}),
);
};

0 comments on commit f45f4e1

Please sign in to comment.