Skip to content

Commit b103d00

Browse files
committed
fix: remove lodash and fix debounce bugs
1 parent 472292e commit b103d00

12 files changed

+136
-123
lines changed

client/extension.ts

+49-40
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {updateStatusBar} from './status-bar';
77
import {xoRootCache} from './cache';
88
import {registerCommands} from './register-commands';
99

10-
let client: LanguageClient;
10+
let languageClient: LanguageClient;
1111

1212
const queue = new Queue({autostart: true, concurrency: 1});
1313

@@ -16,20 +16,19 @@ export async function activate(context: ExtensionContext) {
1616
xoRootCache.logger = logger;
1717

1818
logger.info(`[client] Activating XO extension v${pkg.version}`);
19-
logger.clear();
2019

21-
const shouldStartServer = await xoRootCache.get(window.activeTextEditor?.document.uri.fsPath);
2220
const xoConfig = workspace.getConfiguration('xo');
2321
const runtime = xoConfig.get<string>('runtime');
24-
let languages = xoConfig.get<string[]>('validate')!;
22+
const languages = xoConfig.get<string[]>('validate');
23+
const hasValidXoRoot = await xoRootCache.get(window.activeTextEditor?.document.uri.fsPath);
2524

26-
client = await createLanguageClient({context, outputChannel: logger, runtime, languages});
25+
languageClient = await createLanguageClient({context, outputChannel: logger, runtime, languages});
2726
/**
2827
* Update status bar on activation, and dispose of the status bar when the extension is deactivated
2928
*/
3029
const statusBar = await updateStatusBar(window.activeTextEditor?.document);
3130

32-
registerCommands({context, client, logger});
31+
registerCommands({context, client: languageClient, logger});
3332

3433
context.subscriptions.push(
3534
/**
@@ -38,6 +37,8 @@ export async function activate(context: ExtensionContext) {
3837
workspace.onDidChangeConfiguration((configChange: ConfigurationChangeEvent) => {
3938
queue.push(async () => {
4039
try {
40+
logger.debug('[client] Configuration change detected');
41+
4142
const isValidateChanged = configChange.affectsConfiguration('xo.validate');
4243

4344
if (isValidateChanged) {
@@ -48,40 +49,29 @@ export async function activate(context: ExtensionContext) {
4849
statusBar.text = '$(gear~spin)';
4950
statusBar.show();
5051

51-
languages = workspace.getConfiguration('xo').get<string[]>('validate', languages);
52+
const languages = workspace.getConfiguration('xo').get<string[]>('validate');
5253

53-
client.clientOptions.documentSelector = [];
54+
languageClient.clientOptions.documentSelector = [];
5455
if (languages && languages.length > 0)
5556
for (const language of languages) {
56-
(client.clientOptions.documentSelector as DocumentSelector).push(
57+
(languageClient.clientOptions.documentSelector as DocumentSelector).push(
5758
{language, scheme: 'file'},
5859
{language, scheme: 'untitled'}
5960
);
6061
}
6162

62-
await client.restart();
63+
await languageClient.restart();
6364

6465
statusBar.text = '$(xo-logo)';
6566

6667
statusBar.hide();
6768
logger.info('[client] Restarted client with new xo.validate options.');
6869
}
69-
70-
const isEnabledChanged = configChange.affectsConfiguration('xo.enable');
71-
72-
if (isEnabledChanged) {
73-
const enabled = workspace.getConfiguration('xo').get<boolean>('enable', true);
74-
75-
if (client.needsStart() && enabled) {
76-
await client.start();
77-
}
78-
79-
if (client.needsStop() && !enabled) {
80-
await client.dispose();
81-
}
82-
}
8370
} catch (error) {
84-
logger.error(`[client] Restarting client failed`, error);
71+
if (error instanceof Error) {
72+
logger.error(`[client] There was a problem handling the configuration change.`);
73+
logger.error(error);
74+
}
8575
}
8676
});
8777
}),
@@ -94,20 +84,33 @@ export async function activate(context: ExtensionContext) {
9484
queue.push(async () => {
9585
try {
9686
const {document: textDocument} = textEditor ?? {};
97-
logger.debug('[client] Active text editor changed', textDocument?.uri.fsPath);
87+
88+
logger.debug('[client] onDidChangeActiveTextEditor', textDocument?.uri.fsPath);
89+
90+
const isEnabled = workspace.getConfiguration('xo').get<boolean>('enable', true);
91+
92+
if (!isEnabled) {
93+
logger.debug('[client] onDidChangeActiveTextEditor > XO is not enabled');
94+
return;
95+
}
96+
9897
await updateStatusBar(textDocument);
99-
// if the client was not started
98+
10099
if (
100+
isEnabled &&
101101
textDocument &&
102-
client.needsStart() &&
102+
languageClient.needsStart() &&
103103
(await xoRootCache.get(textDocument.uri.fsPath))
104104
) {
105-
logger.debug('[client] Starting LSP client');
106-
await client.start();
105+
logger.debug('[client] Starting Language Client');
106+
await languageClient.start();
107107
}
108108
} catch (error) {
109-
statusBar.text = '$(xo-logo)';
110-
logger.error(`[client] There was a problem updating the statusbar`, error);
109+
if (error instanceof Error) {
110+
statusBar.text = '$(xo-logo)';
111+
logger.error(`[client] There was a problem handling the active text editor change.`);
112+
logger.error(error);
113+
}
111114
}
112115
});
113116
}),
@@ -120,22 +123,28 @@ export async function activate(context: ExtensionContext) {
120123
xoRootCache.delete(textDocument.uri.fsPath);
121124
});
122125
}),
126+
/**
127+
* Dispose of the status bar when the extension is deactivated
128+
*/
123129
statusBar
124130
);
125131

126-
if (shouldStartServer) {
127-
logger.info('[client] XO was present in the workspace, server is now starting.');
128-
await client.start();
129-
context.subscriptions.push(client);
130-
} else {
131-
logger.info('[client] XO was not present in the workspace, server will not be started.');
132+
if (hasValidXoRoot) {
133+
logger.info('[client] XO is enabled and is needed for linting file, server is now starting.');
134+
await languageClient.start();
135+
context.subscriptions.push(languageClient);
136+
return;
137+
}
138+
139+
if (!hasValidXoRoot) {
140+
logger.info('[client] XO is enabled and server will start when a relevant file is opened.');
132141
}
133142
}
134143

135144
export async function deactivate() {
136-
if (!client) {
145+
if (!languageClient) {
137146
return undefined;
138147
}
139148

140-
return client.stop();
149+
return languageClient.stop();
141150
}

package-lock.json

+18-16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-3
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
"title": "xo",
7272
"properties": {
7373
"xo.enable": {
74-
"scope": "window",
74+
"scope": "resource",
7575
"type": "boolean",
7676
"default": true,
7777
"description": "Control whether xo is enabled or not."
@@ -192,7 +192,7 @@
192192
"find-up": "^7.0.0",
193193
"is-string-and-not-blank": "^0.0.2",
194194
"load-json-file": "^6.2.0",
195-
"lodash": "^4.17.21",
195+
"p-debounce": "^4.0.0",
196196
"queue": "^6.0.2",
197197
"vscode-languageclient": "^9.0.1",
198198
"vscode-languageserver": "^9.0.1",
@@ -202,7 +202,6 @@
202202
"devDependencies": {
203203
"@commitlint/cli": "18.6.0",
204204
"@commitlint/config-conventional": "18.6.0",
205-
"@types/lodash": "^4.14.202",
206205
"@types/node": "^20.11.8",
207206
"@types/vscode": "^1.85.0",
208207
"@types/xo": "^0.39.8",

server/fix-builder.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import {Range, TextEdit} from 'vscode-languageserver/node';
2-
import isUndefined from 'lodash/isUndefined';
32
import {type TextDocument} from 'vscode-languageserver-textdocument';
43
import {type XoFix} from './types';
54

@@ -49,7 +48,7 @@ class Fix {
4948
for (const edit of this.edits.values()) {
5049
let isInRange = true;
5150

52-
if (!isUndefined(this.range)) {
51+
if (this.range !== undefined) {
5352
const startOffset = this.textDocument.offsetAt(this.range.start);
5453
const endOffset = this.textDocument.offsetAt(this.range.end);
5554

@@ -64,7 +63,7 @@ class Fix {
6463
(endOffset >= startEditPosition && endOffset <= endEditPosition);
6564
}
6665

67-
if (!isUndefined(edit.edit) && isInRange) result.push(edit);
66+
if (edit.edit !== undefined && isInRange) result.push(edit);
6867
}
6968

7069
return result.sort((a, b) => {

server/get-document-config.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import path from 'node:path';
2-
import isUndefined from 'lodash/isUndefined';
32
import {type TextDocumentIdentifier} from 'vscode-languageserver';
43
import {type XoConfig} from './types';
54
import type LintServer from './server';
@@ -17,7 +16,7 @@ async function getDocumentConfig(
1716
if (this.configurationCache.has(folderUri)) {
1817
const config: XoConfig = this.configurationCache.get(folderUri)!;
1918

20-
if (!isUndefined(config)) return config;
19+
if (config !== undefined) return config;
2120

2221
return {};
2322
}

server/get-document-formatting.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import isUndefined from 'lodash/isUndefined';
21
import {TextDocument} from 'vscode-languageserver-textdocument';
32
import {TextEdit, Range} from 'vscode-languageserver/node';
43
import Fix from './fix-builder';
@@ -20,11 +19,11 @@ async function getDocumentFormatting(
2019
edits: []
2120
};
2221

23-
if (isUndefined(cachedTextDocument)) return defaultResponse;
22+
if (cachedTextDocument === undefined) return defaultResponse;
2423

2524
const documentFixCache = this.documentFixCache.get(uri);
2625

27-
if (isUndefined(documentFixCache) || documentFixCache.size === 0) {
26+
if (documentFixCache === undefined || documentFixCache.size === 0) {
2827
return defaultResponse;
2928
}
3029

server/get-lint-results.ts

+8-10
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,17 @@ async function getLintResults(
4242
lintTextOptions.warnIgnored = false;
4343
lintTextOptions.fix = fix;
4444

45-
let report;
46-
45+
/**
46+
* Changing the current working directory to the folder
47+
*/
4748
const cwd = process.cwd();
4849

49-
try {
50-
process.chdir(lintTextOptions.cwd);
50+
process.chdir(lintTextOptions.cwd);
51+
52+
const report = await xo.lintText(contents, lintTextOptions);
5153

52-
// eslint-disable-next-line @typescript-eslint/await-thenable
53-
report = await xo.lintText(contents, lintTextOptions);
54-
} finally {
55-
if (cwd !== process.cwd()) {
56-
process.chdir(cwd);
57-
}
54+
if (cwd !== process.cwd()) {
55+
process.chdir(cwd);
5856
}
5957

6058
return report;

0 commit comments

Comments
 (0)