Skip to content

Commit 5342396

Browse files
authored
Add ESLint (#399)
1 parent 5df21c5 commit 5342396

30 files changed

+4389
-2575
lines changed

.vscodeignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ node_modules/**
1717
images/*.gif
1818
**/*.vsix
1919
**/package-lock.json
20+
eslint.config.mjs

client/src/commands.ts

Lines changed: 44 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ export async function overrideClassMembers() {
3333
let seenextends = false;
3434
for (let linenum = 0; linenum < openDoc.lineCount; linenum++) {
3535
const linetxt = openDoc.lineAt(linenum).text;
36-
if (linetxt.slice(0,5).toLowerCase() === "class") {
36+
if (linetxt.slice(0, 5).toLowerCase() === "class") {
3737
// This is the class definition line
38-
const linewords = linetxt.replace(/\s{2,}/g," ").split(" ");
38+
const linewords = linetxt.replace(/\s{2,}/g, " ").split(" ");
3939
if (linewords.length > 2 && linewords[2].toLowerCase() == "extends") {
4040
seenextends = true;
4141
}
@@ -44,7 +44,7 @@ export async function overrideClassMembers() {
4444
}
4545
if (!seenextends) {
4646
// This class has no superclasses, so tell the user and exit
47-
window.showInformationMessage("The current class has no superclasses.","Dismiss");
47+
window.showInformationMessage("The current class has no superclasses.", "Dismiss");
4848
return;
4949
}
5050

@@ -56,19 +56,19 @@ export async function overrideClassMembers() {
5656
cursorvalid = true;
5757
}
5858
if (cursorvalid) {
59-
docposvalid = await client.sendRequest("intersystems/refactor/validateOverrideCursor",{
59+
docposvalid = await client.sendRequest("intersystems/refactor/validateOverrideCursor", {
6060
uri: openDoc.uri.toString(),
6161
line: selection.active.line
6262
});
6363
}
6464
if (!cursorvalid || !docposvalid) {
6565
// We can't insert new class members at the cursor position, so tell the user and exit
66-
window.showInformationMessage("Cursor must be in the class definition body and with nothing selected.","Dismiss");
66+
window.showInformationMessage("Cursor must be in the class definition body and with nothing selected.", "Dismiss");
6767
return;
6868
}
6969

7070
// Ask the user to select the type of member that they want to override
71-
const selectedType = await window.showQuickPick(["Method","Parameter","Projection","Property","Query","Trigger","XData"],{
71+
const selectedType = await window.showQuickPick(["Method", "Parameter", "Projection", "Property", "Query", "Trigger", "XData"], {
7272
title: "Pick the type of class member to override"
7373
});
7474
if (!selectedType) {
@@ -88,18 +88,18 @@ export async function overrideClassMembers() {
8888
}
8989

9090
// Ask the server for all overridable members of the selected type
91-
const overridableMembers: QuickPickItem[] = await client.sendRequest("intersystems/refactor/listOverridableMembers",{
91+
const overridableMembers: QuickPickItem[] = await client.sendRequest("intersystems/refactor/listOverridableMembers", {
9292
uri: openDoc.uri.toString(),
9393
memberType: selectedType
9494
});
9595
if (!overridableMembers?.length) {
9696
// There are no members of this type to override, so tell the user and exit
97-
window.showInformationMessage(`There are no inherited ${plural} that are overridable.`,"Dismiss");
97+
window.showInformationMessage(`There are no inherited ${plural} that are overridable.`, "Dismiss");
9898
return;
9999
}
100100

101101
// Ask the user to select which members they want to override
102-
const selectedMembers = await window.showQuickPick(overridableMembers,{
102+
const selectedMembers = await window.showQuickPick(overridableMembers, {
103103
title: `Pick the ${plural} to override`,
104104
matchOnDescription: true,
105105
matchOnDetail: true,
@@ -111,7 +111,7 @@ export async function overrideClassMembers() {
111111
}
112112

113113
// Ask the server to compute the workspace edit that the client should apply
114-
const lspWorkspaceEdit: WorkspaceEdit = await client.sendRequest("intersystems/refactor/addOverridableMembers",{
114+
const lspWorkspaceEdit: WorkspaceEdit = await client.sendRequest("intersystems/refactor/addOverridableMembers", {
115115
uri: openDoc.uri.toString(),
116116
members: selectedMembers,
117117
cursor: selection.active,
@@ -130,7 +130,7 @@ export async function selectParameterType(uri: string, parameterRange: Range) {
130130
const allparametertypes: QuickPickItem[] = await client.sendRequest("intersystems/refactor/listParameterTypes");
131131

132132
// Ask the user to select a parameter type
133-
const selectedParameter = await window.showQuickPick(allparametertypes,{
133+
const selectedParameter = await window.showQuickPick(allparametertypes, {
134134
title: "Pick the Parameter type",
135135
matchOnDescription: true,
136136
canPickMany: false
@@ -152,31 +152,32 @@ export async function selectParameterType(uri: string, parameterRange: Range) {
152152
};
153153

154154
// Apply the workspace edit
155-
workspace.applyEdit(await client.protocol2CodeConverter.asWorkspaceEdit(edit));
155+
workspace.applyEdit(await client.protocol2CodeConverter.asWorkspaceEdit(edit));
156156
}
157157

158158
/**
159159
* Callback function for the `intersystems.language-server.selectImportPackage` command.
160160
*/
161161
export async function selectImportPackage(uri: string, classname: string) {
162162
// Ask for all import packages
163-
const allimportpackages: QuickPickItem[] = await client.sendRequest("intersystems/refactor/listImportPackages",{
163+
const allimportpackages: QuickPickItem[] = await client.sendRequest("intersystems/refactor/listImportPackages", {
164164
uri: uri,
165165
classmame: classname
166166
});
167167

168+
let selectedPackage: QuickPickItem;
168169
if (allimportpackages.length === 0) {
169170
// There are no packages of this class, so tell the user and exit
170-
window.showInformationMessage("There are no packages for \'" + classname + "\'","Dismiss");
171+
window.showInformationMessage("There are no packages for '" + classname + "'", "Dismiss");
171172
return;
172173
} else if (allimportpackages.length === 1) {
173174
// There is only one package, the user does not need to choose
174-
var selectedPackage = allimportpackages[0];
175+
selectedPackage = allimportpackages[0];
175176
} else {
176177
// Ask the user to select an import package
177-
var selectedPackage = await window.showQuickPick(allimportpackages,{
178+
selectedPackage = await window.showQuickPick(allimportpackages, {
178179
title: "Pick the package to import",
179-
canPickMany: false
180+
canPickMany: false
180181
});
181182
if (!selectedPackage) {
182183
// No package was selected
@@ -185,40 +186,41 @@ export async function selectImportPackage(uri: string, classname: string) {
185186
}
186187

187188
// Ask the server to compute the workspace edit
188-
const lspWorkspaceEdit: WorkspaceEdit = await client.sendRequest("intersystems/refactor/addImportPackage",{
189+
const lspWorkspaceEdit: WorkspaceEdit = await client.sendRequest("intersystems/refactor/addImportPackage", {
189190
uri: uri,
190191
packagename: selectedPackage.label,
191192
});
192193

193194
// Apply the workspace edit
194-
workspace.applyEdit(await client.protocol2CodeConverter.asWorkspaceEdit(lspWorkspaceEdit));
195+
workspace.applyEdit(await client.protocol2CodeConverter.asWorkspaceEdit(lspWorkspaceEdit));
195196
}
196197

197198
/**
198199
* Callback function for the `intersystems.language-server.extractMethod` command.
199200
*/
200201
export async function extractMethod(uri: string, lnstart: number, lnend: number, lnmethod: number, newmethodtype: string) {
201202
// Get the list of class member names
202-
const symbols = await commands.executeCommand("vscode.executeDocumentSymbolProvider", Uri.parse(uri));
203-
var clsmembers: string[] = [];
203+
const symbols = await commands.executeCommand("vscode.executeDocumentSymbolProvider", Uri.parse(uri));
204+
const clsmembers: string[] = [];
204205
for (let clsmember = 0; clsmember < symbols[0].children.length; clsmember++) {
205206
clsmembers.push(symbols[0].children[clsmember].name);
206207
}
207208

208-
var newmethodname = await window.showInputBox({
209+
let newmethodname = await window.showInputBox({
209210
title: "Enter the name of the new method",
210211
value: "newmethod",
211212
validateInput: (newmethodname: string) => {
212213
if (newmethodname === "") {
213214
return "Empty method name";
214215
}
215-
var testname: string = newmethodname;
216+
let testname: string = newmethodname;
216217
if (
217218
(newmethodname.charAt(0) !== '"' || newmethodname.charAt(newmethodname.length) !== '"') &&
219+
// eslint-disable-next-line no-control-regex
218220
newmethodname.match(/(^([A-Za-z]|%)$)|(^([A-Za-z]|%)([A-Za-z]|\d|[^\x00-\x7F])+$)/g) === null
219221
) {
220222
// Input contains forbidden characters so double exisiting " and add leading and trailing "
221-
testname = '"' + newmethodname.replace(/\"/g,'""') + '"';
223+
testname = '"' + newmethodname.replace('"', '""') + '"';
222224
}
223225
if (testname.length > 220) {
224226
return "Name is too long";
@@ -234,13 +236,14 @@ export async function extractMethod(uri: string, lnstart: number, lnend: number,
234236
return;
235237
}
236238
// Format name
239+
// eslint-disable-next-line no-control-regex
237240
if (newmethodname.match(/(^([A-Za-z]|%)$)|(^([A-Za-z]|%)([A-Za-z]|\d|[^\x00-\x7F])+$)/g) === null) {
238241
// Add quotes if the name does not start with a letter or %, then followed by letter/number/ascii>128
239-
newmethodname = '"' + newmethodname.replace(/\"/g,'""') + '"';
242+
newmethodname = '"' + newmethodname.replace('"', '""') + '"';
240243
}
241244

242245
// Ask the server to compute the workspace edit
243-
const lspWorkspaceEdit: WorkspaceEdit = await client.sendRequest("intersystems/refactor/addMethod",{
246+
const lspWorkspaceEdit: WorkspaceEdit = await client.sendRequest("intersystems/refactor/addMethod", {
244247
uri: uri,
245248
newmethodname: newmethodname,
246249
lnstart: lnstart,
@@ -250,40 +253,40 @@ export async function extractMethod(uri: string, lnstart: number, lnend: number,
250253
});
251254

252255
// Apply the workspace edit
253-
await workspace.applyEdit(await client.protocol2CodeConverter.asWorkspaceEdit(lspWorkspaceEdit));
256+
await workspace.applyEdit(await client.protocol2CodeConverter.asWorkspaceEdit(lspWorkspaceEdit));
254257

255258
// Highlight and scroll to new extracted method
256259
const activeEditor = window.activeTextEditor;
257260
if (activeEditor.document.uri.toString() === uri) {
258261
// Selection of the extracted method
259262
const anchor = lspWorkspaceEdit.changes[uri][0].range.start;
260-
var methodstring: string = "";
261-
for (let edit = 0; edit < lspWorkspaceEdit.changes[uri].length-2; edit++) {
263+
let methodstring: string = "";
264+
for (let edit = 0; edit < lspWorkspaceEdit.changes[uri].length - 2; edit++) {
262265
methodstring += lspWorkspaceEdit.changes[uri][edit].newText;
263266
}
264267
const methodsize = methodstring.split("\n").length - 1;
265-
const range: Range = new Range(new Position(anchor.line+1,0),new Position(anchor.line+methodsize,1));
266-
268+
const range: Range = new Range(new Position(anchor.line + 1, 0), new Position(anchor.line + methodsize, 1));
269+
267270
// Selection of the method call
268-
const anchor2 = lspWorkspaceEdit.changes[uri][lspWorkspaceEdit.changes[uri].length-1].range.start;
269-
const linesize = lspWorkspaceEdit.changes[uri][lspWorkspaceEdit.changes[uri].length-1].newText.length;
271+
const anchor2 = lspWorkspaceEdit.changes[uri][lspWorkspaceEdit.changes[uri].length - 1].range.start;
272+
const linesize = lspWorkspaceEdit.changes[uri][lspWorkspaceEdit.changes[uri].length - 1].newText.length;
270273
const range2: Range = new Range(
271-
new Position(anchor2.line+methodsize+1,anchor2.character),
272-
new Position(anchor2.line+methodsize+1,anchor2.character+linesize+1)
274+
new Position(anchor2.line + methodsize + 1, anchor2.character),
275+
new Position(anchor2.line + methodsize + 1, anchor2.character + linesize + 1)
273276
);
274277

275278
// Scroll to the extracted method
276279
activeEditor.revealRange(range);
277-
280+
278281
// Highlight extracted method and method call
279282
const color: string = "#ffff0020"; // Transparent yellow
280283
const timeout: number = 2000; // Highlight disapears after 2 seconds
281284
const decoration = window.createTextEditorDecorationType({
282285
backgroundColor: color
283286
});
284-
activeEditor.setDecorations(decoration,[range,range2]);
285-
await new Promise(r => setTimeout(r, timeout));
286-
setTimeout(function(){decoration.dispose();}, 0);
287+
activeEditor.setDecorations(decoration, [range, range2]);
288+
await new Promise(r => setTimeout(r, timeout));
289+
setTimeout(function () { decoration.dispose(); }, 0);
287290
}
288291
}
289292

@@ -310,15 +313,15 @@ export async function showSymbolInClass(uri: string, memberType: string, memberN
310313
editor = await window.showTextDocument(uriObj);
311314
}
312315
editor.selection = new Selection(symbol.selectionRange.start, symbol.selectionRange.end);
313-
editor.revealRange(symbol.selectionRange, TextEditorRevealType.InCenter);
316+
editor.revealRange(symbol.selectionRange, TextEditorRevealType.InCenter);
314317
}
315318
}
316319

317320
/**
318321
* Callback function for the `intersystems.language-server.setSelection` command.
319322
*/
320323
export function setSelection(editor: TextEditor, _edit: TextEditorEdit, startLine: number, startCharacter: number, endLine: number, endCharacter: number) {
321-
const range = new Range(startLine,startCharacter,endLine,endCharacter);
324+
const range = new Range(startLine, startCharacter, endLine, endCharacter);
322325
editor.selection = new Selection(range.start, range.end);
323326
editor.revealRange(range, TextEditorRevealType.InCenter);
324327
}

0 commit comments

Comments
 (0)