Skip to content
This repository has been archived by the owner on Dec 25, 2023. It is now read-only.

Commit

Permalink
Add support for custom rules dirs
Browse files Browse the repository at this point in the history
Resolves #68
  • Loading branch information
apexskier committed Jan 19, 2021
1 parent 6a2955d commit 2b2db0b
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 12 deletions.
25 changes: 21 additions & 4 deletions ESLint.novaextension/extension.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,21 @@
},
{
"key": "apexskier.eslint.config.eslintPath",
"title": "Path to ESLint executable",
"title": "ESLint executable",
"type": "path"
},
{
"key": "apexskier.eslint.config.eslintConfigPath",
"title": "Path to ESLint configuration",
"title": "ESLint configuration",
"type": "path"
},
{
"key": "apexskier.eslint.config.eslintRulesDirs",
"title": "Rules directories",
"link": "https://eslint.org/docs/user-guide/command-line-interface#-rulesdir",
"type": "pathArray",
"allowFiles": false,
"allowFolders": true
}
],

Expand All @@ -54,13 +62,22 @@
},
{
"key": "apexskier.eslint.config.eslintPath",
"title": "Path to ESLint executable",
"title": "ESLint executable",
"type": "path"
},
{
"key": "apexskier.eslint.config.eslintConfigPath",
"title": "Path to ESLint configuration",
"title": "ESLint configuration",
"type": "path"
},
{
"key": "apexskier.eslint.config.eslintRulesDirs",
"title": "Rules directories",
"link": "https://eslint.org/docs/user-guide/command-line-interface#-rulesdir",
"type": "pathArray",
"relative": true,
"allowFiles": false,
"allowFolders": true
}
],

Expand Down
28 changes: 28 additions & 0 deletions src/getRulesDirs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// returns custom eslint rules directories
export function getRulesDirs(): Array<string> | null {
const rulesDirs: Array<string> =
nova.config.get("apexskier.eslint.config.eslintRulesDirs", "array") ?? [];
const workspaceRulesDirs = nova.workspace.config.get(
"apexskier.eslint.config.eslintRulesDirs",
"array"
);
if (workspaceRulesDirs) {
for (const dir of workspaceRulesDirs) {
if (!dir.trim()) {
continue;
}
if (nova.path.isAbsolute(dir)) {
rulesDirs.push(dir);
} else if (nova.workspace.path) {
rulesDirs.push(nova.path.join(nova.workspace.path, dir));
} else {
nova.workspace.showErrorMessage(
"Save your workspace before using a relative ESLint rules directories."
);
return null;
}
}
}

return rulesDirs.filter((d) => d.trim());
}
39 changes: 31 additions & 8 deletions src/process.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import type { Linter, ESLint } from "eslint";
import { getEslintPath } from "./getEslintPath";
import { getEslintConfig } from "./getEslintConfig";
import { getRulesDirs } from "./getRulesDirs";

let eslintPath: string | null = null;
let eslintConfigPath: string | null = null;
let eslintRulesDirs: Array<string> | null = null;

// TODO: Clean up these disposables on deactivation
nova.config.onDidChange("apexskier.eslint.config.eslintPath", async () => {
eslintPath = await getEslintPath();
console.log("Updating ESLint executable globally", eslintPath);
Expand All @@ -30,10 +34,24 @@ nova.workspace.config.onDidChange(
nova.commands.invoke("apexskier.eslint.config.lintAllEditors");
}
);
nova.config.onDidChange("apexskier.eslint.config.eslintRulesDirs", () => {
eslintRulesDirs = getRulesDirs();
console.log("Updating ESLint rules globally");
nova.commands.invoke("apexskier.eslint.config.lintAllEditors");
});
nova.workspace.config.onDidChange(
"apexskier.eslint.config.eslintRulesDirs",
() => {
eslintRulesDirs = getRulesDirs();
console.log("Updating ESLint rules for workspace");
nova.commands.invoke("apexskier.eslint.config.lintAllEditors");
}
);

export async function initialize() {
eslintPath = await getEslintPath();
eslintConfigPath = getEslintConfig();
eslintRulesDirs = getRulesDirs();
}

const syntaxToRequiredPlugin: { [syntax: string]: string | undefined } = {
Expand Down Expand Up @@ -161,6 +179,17 @@ class ESLintProcess implements Disposable {
}
}

function addConfigArguments(toArgs: Array<string>) {
if (eslintRulesDirs) {
for (const dir of eslintRulesDirs) {
toArgs.unshift("--rulesdir", dir);
}
}
if (eslintConfigPath) {
toArgs.unshift("--config", eslintConfigPath);
}
}

export function runLintPass(
content: string,
path: string | null,
Expand All @@ -179,7 +208,6 @@ export function runLintPass(
return disposable;
}
const eslint = eslintPath;
const eslintConfig = eslintConfigPath;
// remove file:/Volumes/Macintosh HD from uri
const cleanPath = path
? "/" + decodeURI(path).split("/").slice(5).join("/")
Expand All @@ -194,9 +222,7 @@ export function runLintPass(
if (cleanPath) {
args.unshift("--stdin-filename", cleanPath);
}
if (eslintConfig) {
args.unshift("--config", eslintConfig);
}
addConfigArguments(args);
const process = new ESLintProcess(eslint, args, callback);
disposable.add(process);
process.write(content);
Expand Down Expand Up @@ -224,7 +250,6 @@ export function runFixPass(
return disposable;
}
const eslint = eslintPath;
const eslintConfig = eslintConfigPath;
// remove file:/Volumes/Macintosh HD from uri
const cleanPath = "/" + decodeURI(path).split("/").slice(5).join("/");

Expand All @@ -235,9 +260,7 @@ export function runFixPass(
} else {
const args = ["--fix", "--format=json"];
args.unshift(cleanPath);
if (eslintConfig) {
args.unshift("--config", eslintConfig);
}
addConfigArguments(args);
const process = new ESLintProcess(eslint, args, callback);
disposable.add(process);
}
Expand Down

0 comments on commit 2b2db0b

Please sign in to comment.