feat(eslint): add globals for jQuery

This commit is contained in:
Elian Doran 2025-03-09 21:38:16 +02:00
parent 064799e8cb
commit 1fb4634b7b
No known key found for this signature in database
3 changed files with 20 additions and 7 deletions

View File

@ -41,7 +41,7 @@
return [];
}
const errors = await glob.linter(text);
const errors = await glob.linter(text, glob.getActiveContextNote().mime);
console.log(errors);

View File

@ -45,4 +45,9 @@ describe("Linter", () => {
}
]);
});
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([]);
});
});

View File

@ -1,8 +1,19 @@
export async function lint(code: string) {
export async function lint(code: string, mimeType: string) {
const Linter = (await import("eslint-linter-browserify")).Linter;
const js = (await import("@eslint/js"));
const globals = (await import("globals"));
const globalDefinitions = (await import("globals"));
let globals: Record<string, any> = {
...globalDefinitions.browser,
api: "readonly"
};
if (mimeType === "application/javascript;env=frontend") {
globals = { ...globals, ...globalDefinitions.jquery };
} else if (mimeType === "application/javascript;env=backend") {
}
return new Linter().verify(code, [
js.configs.recommended,
@ -11,10 +22,7 @@ export async function lint(code: string) {
parserOptions: {
ecmaVersion: 2024
},
globals: {
...globals.browser,
api: "readonly"
},
globals
},
rules: {
"no-unused-vars": [ "warn", { vars: "local", args: "after-used" }]