Skip to content
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

Add eslint.codeActionsOnSave.options #1945

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions $shared/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,19 @@ export namespace CodeActionsOnSaveRules {
}
}

export namespace CodeActionsOnSaveOptions {
export function from(value: object | undefined | null): ESLintOptions | undefined {
if (value === undefined || value === null || typeof value !== 'object') {
return undefined;
}
return value;
}
}

export type CodeActionsOnSaveSettings = {
mode: CodeActionsOnSaveMode;
rules?: string[];
options?: ESLintOptions;
};

export enum ESLintSeverity {
Expand Down
3 changes: 2 additions & 1 deletion client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {

import { LegacyDirectoryItem, Migration, PatternItem, ValidateItem } from './settings';
import { ExitCalled, NoConfigRequest, NoESLintLibraryRequest, OpenESLintDocRequest, ProbeFailedRequest, ShowOutputChannel, Status, StatusNotification, StatusParams } from './shared/customMessages';
import { CodeActionSettings, CodeActionsOnSaveMode, CodeActionsOnSaveRules, ConfigurationSettings, DirectoryItem, ESLintOptions, ESLintSeverity, ModeItem, PackageManagers, RuleCustomization, RunValues, Validate } from './shared/settings';
import { CodeActionSettings, CodeActionsOnSaveMode, CodeActionsOnSaveOptions, CodeActionsOnSaveRules, ConfigurationSettings, DirectoryItem, ESLintOptions, ESLintSeverity, ModeItem, PackageManagers, RuleCustomization, RunValues, Validate } from './shared/settings';
import { convert2RegExp, Is, Semaphore, toOSPath, toPosixPath } from './node-utils';
import { pickFolder } from './vscode-utils';

Expand Down Expand Up @@ -713,6 +713,7 @@ export namespace ESLintClient {
settings.format = !!config.get<boolean>('format.enable', false);
settings.codeActionOnSave.mode = CodeActionsOnSaveMode.from(config.get<CodeActionsOnSaveMode>('codeActionsOnSave.mode', CodeActionsOnSaveMode.all));
settings.codeActionOnSave.rules = CodeActionsOnSaveRules.from(config.get<string[] | null>('codeActionsOnSave.rules', null));
settings.codeActionOnSave.options = CodeActionsOnSaveOptions.from(config.get<ESLintOptions | null>('codeActionsOnSave.options', null));
}
if (workspaceFolder !== undefined) {
settings.workspaceFolder = {
Expand Down
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,12 @@
"default": null,
"markdownDescription": "The rules that should be executed when computing the code actions on save or formatting a file. Defaults to the rules configured via the ESLint configuration"
},
"eslint.codeActionsOnSave.options": {
"scope": "resource",
"type": "object",
"default": {},
"markdownDescription": "The eslint options object to use on save. (see https://eslint.org/docs/developer-guide/nodejs-api#eslint-class). eslint.codeActionsOnSave.rules, if specified, will take priority over any rule options here."
},
"eslint.format.enable": {
"scope": "resource",
"type": "boolean",
Expand Down
27 changes: 15 additions & 12 deletions server/src/eslint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
import { URI } from 'vscode-uri';

import { ProbeFailedParams, ProbeFailedRequest, NoESLintLibraryRequest, Status, NoConfigRequest, StatusNotification } from './shared/customMessages';
import { ConfigurationSettings, DirectoryItem, ESLintSeverity, ModeEnum, ModeItem, PackageManagers, RuleCustomization, RuleSeverity, Validate } from './shared/settings';
import { ConfigurationSettings, DirectoryItem, ESLintOptions, ESLintSeverity, ModeEnum, ModeItem, PackageManagers, RuleCustomization, RuleSeverity, Validate } from './shared/settings';

import * as Is from './is';
import { LRUCache } from './linkedMap';
Expand Down Expand Up @@ -471,7 +471,7 @@ export class Fixes {
}
}

export type SaveRuleConfigItem = { offRules: Set<string>; onRules: Set<string>};
export type SaveRuleConfigItem = { offRules: Set<string>; onRules: Set<string>; options: ESLintOptions | undefined};

/**
* Manages the special save rule configurations done in the VS Code settings.
Expand All @@ -491,8 +491,9 @@ export namespace SaveRuleConfigs {
return result;
}
const rules = settings.codeActionOnSave.rules;
const options = settings.codeActionOnSave.options;
result = await ESLint.withClass(async (eslint) => {
if (rules === undefined || eslint.isCLIEngine) {
if ((rules === undefined && options === undefined) || eslint.isCLIEngine) {
return undefined;
}
const config = await eslint.calculateConfigForFile(filePath);
Expand All @@ -501,18 +502,20 @@ export namespace SaveRuleConfigs {
}
const offRules: Set<string> = new Set();
const onRules: Set<string> = new Set();
if (rules.length === 0) {
Object.keys(config.rules).forEach(ruleId => offRules.add(ruleId));
} else {
for (const ruleId of Object.keys(config.rules)) {
if (isOff(ruleId, rules)) {
offRules.add(ruleId);
} else {
onRules.add(ruleId);
if (rules !== undefined) {
if (rules.length === 0) {
Object.keys(config.rules).forEach(ruleId => offRules.add(ruleId));
} else {
for (const ruleId of Object.keys(config.rules)) {
if (isOff(ruleId, rules)) {
offRules.add(ruleId);
} else {
onRules.add(ruleId);
}
}
}
}
return offRules.size > 0 ? { offRules, onRules } : undefined;
return (offRules.size > 0 || options) ? { offRules, onRules, options } : undefined;
}, settings);
if (result === undefined || result === null) {
saveRuleConfigCache.set(uri, null);
Expand Down
21 changes: 14 additions & 7 deletions server/src/eslintServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
import { Validate, CodeActionsOnSaveMode } from './shared/settings';

import {
CodeActions, ConfigData, ESLint, FixableProblem, Fixes, Problem, RuleMetaData, RuleSeverities,
CodeActions, ESLint, ESLintClassOptions, FixableProblem, Fixes, Problem, RuleMetaData, RuleSeverities,
SaveRuleConfigs, SuggestionsProblem, TextDocumentSettings,
} from './eslint';

Expand Down Expand Up @@ -771,11 +771,18 @@ async function computeAllFixes(identifier: VersionedTextDocumentIdentifier, mode
} else {
const saveConfig = filePath !== undefined && mode === AllFixesMode.onSave ? await SaveRuleConfigs.get(uri, settings) : undefined;
const offRules = saveConfig?.offRules;
let overrideConfig: Required<ConfigData> | undefined;
if (offRules !== undefined) {
overrideConfig = { rules: Object.create(null) };
for (const ruleId of offRules) {
overrideConfig.rules[ruleId] = 'off';
const overrideOptions = saveConfig?.options;
let eslintOptions: ESLintClassOptions = {fix: true};
if (offRules !== undefined || overrideOptions !== undefined) {
if (overrideOptions !== undefined) {
eslintOptions = {...overrideOptions, ...eslintOptions};
}
if (offRules !== undefined && offRules.size > 0) {
const overrideConfig = {rules: Object.create(null)};
for (const ruleId of offRules) {
overrideConfig.rules[ruleId] = 'off';
}
eslintOptions.overrideConfig = overrideConfig;
}
}
return ESLint.withClass(async (eslintClass) => {
Expand All @@ -800,7 +807,7 @@ async function computeAllFixes(identifier: VersionedTextDocumentIdentifier, mode
}
}
return result;
}, settings, overrideConfig !== undefined ? { fix: true, overrideConfig } : { fix: true });
}, settings, eslintOptions );
}
}

Expand Down