2025-05-11 15:29:03 +03:00
|
|
|
import { lint as _lint } from "./eslint.js";
|
2025-04-18 11:19:43 +03:00
|
|
|
import { trimIndentation } from "@triliumnext/commons";
|
2025-03-08 02:55:51 +02:00
|
|
|
import { describe, expect, it } from "vitest";
|
|
|
|
|
2025-05-11 15:29:03 +03:00
|
|
|
async function lint(code: string, mimeType: string) {
|
|
|
|
const linterData = await _lint(mimeType);
|
|
|
|
if (!("linter" in linterData)) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
const { linter, config } = linterData;
|
|
|
|
const result = linter.verify(code, config);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2025-03-08 02:55:51 +02:00
|
|
|
describe("Linter", () => {
|
|
|
|
it("reports some basic errors", async () => {
|
|
|
|
const result = await lint(trimIndentation`
|
|
|
|
for (const i = 0; i<10; i++) {
|
|
|
|
}
|
2025-03-10 09:25:33 +02:00
|
|
|
`, "application/javascript;env=frontend");
|
2025-03-08 02:55:51 +02:00
|
|
|
expect(result).toMatchObject([
|
|
|
|
{ message: "'i' is constant.", },
|
|
|
|
{ message: "Empty block statement." }
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("reports no error for correct script", async () => {
|
|
|
|
const result = await lint(trimIndentation`
|
|
|
|
const foo = "bar";
|
|
|
|
console.log(foo.toString());
|
|
|
|
for (const x of [ 1, 2, 3]) {
|
|
|
|
console.log(x?.toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
api.showMessage("Hi");
|
2025-03-10 09:25:33 +02:00
|
|
|
`, "application/javascript;env=frontend");
|
2025-03-08 02:55:51 +02:00
|
|
|
expect(result.length).toBe(0);
|
|
|
|
});
|
2025-03-08 03:02:25 +02:00
|
|
|
|
|
|
|
it("reports unused functions as warnings", async () => {
|
|
|
|
const result = await lint(trimIndentation`
|
|
|
|
function hello() { }
|
|
|
|
function world() { }
|
|
|
|
|
|
|
|
console.log("Hello world");
|
2025-03-10 09:25:33 +02:00
|
|
|
`, "application/javascript;env=frontend");
|
2025-03-08 03:02:25 +02:00
|
|
|
expect(result).toMatchObject([
|
|
|
|
{
|
|
|
|
message: "'hello' is defined but never used.",
|
|
|
|
severity: 1
|
|
|
|
},
|
|
|
|
{
|
|
|
|
message: "'world' is defined but never used.",
|
|
|
|
severity: 1
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
});
|
2025-03-09 21:38:16 +02:00
|
|
|
|
|
|
|
it("supports JQuery global", async () => {
|
|
|
|
expect(await lint(`$("<div>");`, "application/javascript;env=backend")).toMatchObject([{ "ruleId": "no-undef" }]);
|
|
|
|
expect(await lint(`console.log($("<div>"));`, "application/javascript;env=frontend")).toStrictEqual([]);
|
|
|
|
});
|
2025-03-09 21:41:13 +02:00
|
|
|
|
|
|
|
it("supports module.exports", async () => {
|
|
|
|
expect(await lint(`module.exports("Hi");`, "application/javascript;env=backend")).toStrictEqual([]);
|
|
|
|
expect(await lint(`module.exports("Hi");`, "application/javascript;env=frontend")).toStrictEqual([]);
|
|
|
|
});
|
2025-03-24 19:17:45 +02:00
|
|
|
|
|
|
|
it("ignores TypeScript file", async () => {
|
|
|
|
expect(await lint("export async function lint(code: string, mimeType: string) {}", "text/typescript-jsx")).toStrictEqual([]);
|
|
|
|
});
|
2025-03-08 02:55:51 +02:00
|
|
|
});
|