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

Minor fixes for eslint.codeActionsOnSave.rules mechanism #1364

Merged
merged 5 commits into from
Nov 18, 2021
Merged
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
56 changes: 28 additions & 28 deletions server/src/eslintServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -553,44 +553,44 @@ function isOff(ruleId: string, matchers: string[]): boolean {
return true;
}

async function getSaveRuleConfig(filePath: string, settings: TextDocumentSettings & { library: ESLintModule }): Promise<SaveRuleConfigItem | undefined> {
let result = saveRuleConfigCache.get(filePath);
if (result === null) {
async function getSaveRuleConfig(uri: string, settings: TextDocumentSettings & { library: ESLintModule }): Promise<SaveRuleConfigItem | undefined> {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replaced filePath argument by the uri, as it is need here to get and set items on saveRuleConfigCache. Preferred this way instead of adding the uri to the arguments list to keep the function signature concise.

const filePath = getFilePath(uri);
let result = saveRuleConfigCache.get(uri);
if (filePath === undefined || result === null) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

filePath still required here to be used with ESLintClass.calculateConfigForFile method, so if the uri schema couldn't be resolved to a file path we return undefined in the same way of when there's no rules.

return undefined;
}
if (result !== undefined) {
return result;
}
const rules = settings.codeActionOnSave.rules;
if (rules === undefined || !ESLintModule.hasESLintClass(settings.library) || !settings.useESLintClass) {
result = undefined;
} else {
result = await withESLintClass(async (eslint) => {
const config = await eslint.calculateConfigForFile(filePath);
if (config === undefined || config.rules === undefined || config.rules.length === 0) {
return undefined;
}
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);
}
result = await withESLintClass(async (eslint) => {
if (rules === undefined || eslint.isCLIEngine) {
return undefined;
}
const config = await eslint.calculateConfigForFile(filePath);
if (config === undefined || config.rules === undefined || config.rules.length === 0) {
return undefined;
}
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);
}
}
return offRules.size > 0 ? { offRules, onRules } : undefined;
}, settings);
}
}
return offRules.size > 0 ? { offRules, onRules } : undefined;
}, settings);
if (result === undefined || result === null) {
saveRuleConfigCache.set(filePath, null);
saveRuleConfigCache.set(uri, null);
return undefined;
} else {
saveRuleConfigCache.set(filePath, result);
saveRuleConfigCache.set(uri, result);
return result;
}
}
Expand Down Expand Up @@ -2283,7 +2283,7 @@ async function computeAllFixes(identifier: VersionedTextDocumentIdentifier, mode
connection.tracer.log(`Computing all fixes took: ${Date.now() - start} ms.`);
return result;
} else {
const saveConfig = filePath !== undefined && mode === AllFixesMode.onSave ? await getSaveRuleConfig(filePath, settings) : undefined;
const saveConfig = filePath !== undefined && mode === AllFixesMode.onSave ? await getSaveRuleConfig(uri, settings) : undefined;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only call to getSaveRuleConfig function, so I changed the call to use the uri instead of the filePath.
As a personal decision I kept the filePath !== undefined condition, even if we are not using filePath in the call anymore. I thought that it could be more efficient by avoiding an asynchronous call if we already know its return value, but I'm not really sure.

const offRules = saveConfig?.offRules;
const onRules = saveConfig?.onRules;
let overrideConfig: Required<ConfigData> | undefined;
Expand Down