Skip to content

Commit 6f8769b

Browse files
MariaSolOsJavaScriptBachdbaeumer
authored
Add eslint.codeActionsOnSave.options (#1999)
* Add eslint.codeActionsOnSave.options * fix * Nit fixes for `markdownDescription` * Whitespace fixes --------- Co-authored-by: Phillip Huang <[email protected]> Co-authored-by: Dirk Bäumer <[email protected]>
1 parent fc77e53 commit 6f8769b

File tree

5 files changed

+47
-20
lines changed

5 files changed

+47
-20
lines changed

$shared/settings.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,19 @@ export namespace CodeActionsOnSaveRules {
6464
}
6565
}
6666

67+
export namespace CodeActionsOnSaveOptions {
68+
export function from(value: object | undefined | null): ESLintOptions | undefined {
69+
if (value === undefined || value === null || typeof value !== 'object') {
70+
return undefined;
71+
}
72+
return value;
73+
}
74+
}
75+
6776
export type CodeActionsOnSaveSettings = {
6877
mode: CodeActionsOnSaveMode;
6978
rules?: string[];
79+
options?: ESLintOptions;
7080
};
7181

7282
export enum ESLintSeverity {

client/src/client.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {
2020

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

@@ -713,6 +713,7 @@ export namespace ESLintClient {
713713
settings.format = !!config.get<boolean>('format.enable', false);
714714
settings.codeActionOnSave.mode = CodeActionsOnSaveMode.from(config.get<CodeActionsOnSaveMode>('codeActionsOnSave.mode', CodeActionsOnSaveMode.all));
715715
settings.codeActionOnSave.rules = CodeActionsOnSaveRules.from(config.get<string[] | null>('codeActionsOnSave.rules', null));
716+
settings.codeActionOnSave.options = CodeActionsOnSaveOptions.from(config.get<ESLintOptions | null>('codeActionsOnSave.options', null));
716717
}
717718
if (workspaceFolder !== undefined) {
718719
settings.workspaceFolder = {

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,12 @@
458458
"default": null,
459459
"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"
460460
},
461+
"eslint.codeActionsOnSave.options": {
462+
"scope": "resource",
463+
"type": "object",
464+
"default": {},
465+
"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."
466+
},
461467
"eslint.format.enable": {
462468
"scope": "resource",
463469
"type": "boolean",

server/src/eslint.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
import { URI } from 'vscode-uri';
1919

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

2323
import * as Is from './is';
2424
import { LRUCache } from './linkedMap';
@@ -471,7 +471,7 @@ export class Fixes {
471471
}
472472
}
473473

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

476476
/**
477477
* Manages the special save rule configurations done in the VS Code settings.
@@ -491,8 +491,9 @@ export namespace SaveRuleConfigs {
491491
return result;
492492
}
493493
const rules = settings.codeActionOnSave.rules;
494+
const options = settings.codeActionOnSave.options;
494495
result = await ESLint.withClass(async (eslint) => {
495-
if (rules === undefined || eslint.isCLIEngine) {
496+
if ((rules === undefined && options === undefined) || eslint.isCLIEngine) {
496497
return undefined;
497498
}
498499
const config = await eslint.calculateConfigForFile(filePath);
@@ -501,18 +502,20 @@ export namespace SaveRuleConfigs {
501502
}
502503
const offRules: Set<string> = new Set();
503504
const onRules: Set<string> = new Set();
504-
if (rules.length === 0) {
505-
Object.keys(config.rules).forEach(ruleId => offRules.add(ruleId));
506-
} else {
507-
for (const ruleId of Object.keys(config.rules)) {
508-
if (isOff(ruleId, rules)) {
509-
offRules.add(ruleId);
510-
} else {
511-
onRules.add(ruleId);
505+
if (rules !== undefined) {
506+
if (rules.length === 0) {
507+
Object.keys(config.rules).forEach(ruleId => offRules.add(ruleId));
508+
} else {
509+
for (const ruleId of Object.keys(config.rules)) {
510+
if (isOff(ruleId, rules)) {
511+
offRules.add(ruleId);
512+
} else {
513+
onRules.add(ruleId);
514+
}
512515
}
513516
}
514517
}
515-
return offRules.size > 0 ? { offRules, onRules } : undefined;
518+
return (offRules.size > 0 || options) ? { offRules, onRules, options } : undefined;
516519
}, settings);
517520
if (result === undefined || result === null) {
518521
saveRuleConfigCache.set(uri, null);

server/src/eslintServer.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121
import { Validate, CodeActionsOnSaveMode } from './shared/settings';
2222

2323
import {
24-
CodeActions, ConfigData, ESLint, FixableProblem, Fixes, Problem, RuleMetaData, RuleSeverities,
24+
CodeActions, ESLint, ESLintClassOptions, FixableProblem, Fixes, Problem, RuleMetaData, RuleSeverities,
2525
SaveRuleConfigs, SuggestionsProblem, TextDocumentSettings,
2626
} from './eslint';
2727

@@ -771,11 +771,18 @@ async function computeAllFixes(identifier: VersionedTextDocumentIdentifier, mode
771771
} else {
772772
const saveConfig = filePath !== undefined && mode === AllFixesMode.onSave ? await SaveRuleConfigs.get(uri, settings) : undefined;
773773
const offRules = saveConfig?.offRules;
774-
let overrideConfig: Required<ConfigData> | undefined;
775-
if (offRules !== undefined) {
776-
overrideConfig = { rules: Object.create(null) };
777-
for (const ruleId of offRules) {
778-
overrideConfig.rules[ruleId] = 'off';
774+
const overrideOptions = saveConfig?.options;
775+
let eslintOptions: ESLintClassOptions = { fix: true };
776+
if (offRules !== undefined || overrideOptions !== undefined) {
777+
if (overrideOptions !== undefined) {
778+
eslintOptions = { ...eslintOptions, ...overrideOptions };
779+
}
780+
if (offRules !== undefined && offRules.size > 0) {
781+
const overrideConfig = { rules: Object.create(null) };
782+
for (const ruleId of offRules) {
783+
overrideConfig.rules[ruleId] = 'off';
784+
}
785+
eslintOptions.overrideConfig = overrideConfig;
779786
}
780787
}
781788
return ESLint.withClass(async (eslintClass) => {
@@ -800,7 +807,7 @@ async function computeAllFixes(identifier: VersionedTextDocumentIdentifier, mode
800807
}
801808
}
802809
return result;
803-
}, settings, overrideConfig !== undefined ? { fix: true, overrideConfig } : { fix: true });
810+
}, settings, eslintOptions);
804811
}
805812
}
806813

0 commit comments

Comments
 (0)