Skip to content

Commit 5dd9815

Browse files
committed
fix: resolve ESlint errors
1 parent e9ef20f commit 5dd9815

15 files changed

Lines changed: 85 additions & 103 deletions

src/DocumentWatcher.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as path from 'path'
1+
import * as path from 'node:path'
22
import {
33
Disposable,
44
TextDocument,
@@ -9,7 +9,6 @@ import {
99
workspace,
1010
} from 'vscode'
1111
import { KnownProps } from 'editorconfig'
12-
1312
import {
1413
InsertFinalNewline,
1514
PreSaveTransformation,
@@ -55,9 +54,6 @@ export default class DocumentWatcher {
5554
window.onDidChangeActiveTextEditor(async editor => {
5655
this.handleTextEditorChange(editor)
5756
}),
58-
)
59-
60-
subscriptions.push(
6157
window.onDidChangeWindowState(async state => {
6258
if (state.focused && this.doc) {
6359
const newOptions = await resolveTextEditorOptions(this.doc, {
@@ -69,19 +65,13 @@ export default class DocumentWatcher {
6965
})
7066
}
7167
}),
72-
)
73-
74-
subscriptions.push(
7568
workspace.onDidSaveTextDocument(doc => {
7669
if (path.basename(doc.fileName) === '.editorconfig') {
7770
this.log('.editorconfig file saved.')
7871
}
7972
// in case document was dirty on text editor change
8073
this.handleDocumentEncoding(doc)
8174
}),
82-
)
83-
84-
subscriptions.push(
8575
workspace.onWillSaveTextDocument(async e => {
8676
const transformations = this.calculatePreSaveTransformations(
8777
e.document,
@@ -199,7 +189,9 @@ export default class DocumentWatcher {
199189
return
200190
}
201191

202-
this.log(`${relativePath}: Re-opening document with ${targetEncoding} encoding...`)
192+
this.log(
193+
`${relativePath}: Re-opening document with ${targetEncoding} encoding...`,
194+
)
203195
await workspace.openTextDocument(document.uri, {
204196
encoding: targetEncoding,
205197
})

src/EditorConfigCompletionProvider.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ class EditorConfigCompletionProvider implements CompletionItemProvider {
9797
const textOfEntireLine = document.getText(
9898
document.lineAt(position.line).range,
9999
)
100-
const textOfLineUpToCursor = textOfEntireLine.substring(
100+
const textOfLineUpToCursor = textOfEntireLine.slice(
101101
0,
102-
position.character,
102+
Math.max(0, position.character),
103103
)
104104

105105
// conditionally generate autocomplete for property names or values
@@ -144,7 +144,7 @@ class EditorConfigCompletionProvider implements CompletionItemProvider {
144144
}
145145

146146
private hasEqualsSign(lineText: string): boolean {
147-
return lineText.indexOf('=') >= 0
147+
return lineText.includes('=')
148148
}
149149

150150
// =========================================================================

src/api.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import * as editorconfig from 'editorconfig'
21
import { TextDocument, TextEditorOptions, Uri, window, workspace } from 'vscode'
2+
import * as editorconfig from 'editorconfig'
33

44
/**
55
* Resolves `TextEditorOptions` for a `TextDocument`, combining the editor's
@@ -139,6 +139,13 @@ export function resolveFile(doc: TextDocument): {
139139
}
140140
}
141141

142+
/**
143+
* Convert vscode tabSize option into numeric value
144+
*/
145+
function resolveTabSize(tabSize: number | string) {
146+
return tabSize === 'auto' ? 4 : Number.parseInt(String(tabSize), 10)
147+
}
148+
142149
/**
143150
* Convert .editorconfig values to vscode editor options
144151
*/
@@ -192,27 +199,22 @@ export function toEditorConfig(options: TextEditorOptions) {
192199
const result: editorconfig.KnownProps = {}
193200

194201
switch (options.insertSpaces) {
195-
case true:
202+
case true: {
196203
result.indent_style = 'space'
197204
if (options.tabSize) {
198205
result.indent_size = resolveTabSize(options.tabSize)
199206
}
200207
break
208+
}
201209
case false:
202-
case 'auto':
210+
case 'auto': {
203211
result.indent_style = 'tab'
204212
if (options.tabSize) {
205213
result.tab_width = resolveTabSize(options.tabSize)
206214
}
207215
break
216+
}
208217
}
209218

210219
return result
211-
212-
/**
213-
* Convert vscode tabSize option into numeric value
214-
*/
215-
function resolveTabSize(tabSize: number | string) {
216-
return tabSize === 'auto' ? 4 : parseInt(String(tabSize), 10)
217-
}
218220
}

src/commands/generateEditorConfig.ts

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
import { readFile as _readFile } from 'fs'
2-
import { EOL } from 'os'
3-
import { resolve } from 'path'
4-
import { promisify } from 'util'
1+
import { readFile } from 'node:fs/promises'
2+
import { EOL } from 'node:os'
3+
import { resolve } from 'node:path'
54
import { FileType, Uri, window, workspace } from 'vscode'
65

7-
const readFile = promisify(_readFile)
8-
96
/**
107
* Generate a .editorconfig file in the root of the workspace based on the
118
* current vscode settings.
@@ -28,18 +25,18 @@ export async function generateEditorConfig(uri: Uri) {
2825
)
2926
return
3027
}
31-
} catch (err: unknown) {
28+
} catch (error: unknown) {
3229
if (
33-
typeof err === 'object' &&
34-
err !== null &&
35-
'name' in err &&
36-
'message' in err &&
37-
typeof err.message === 'string'
30+
typeof error === 'object' &&
31+
error !== null &&
32+
'name' in error &&
33+
'message' in error &&
34+
typeof error.message === 'string'
3835
) {
39-
if (err.name === 'EntryNotFound (FileSystemError)') {
36+
if (error.name === 'EntryNotFound (FileSystemError)') {
4037
writeFile()
4138
} else {
42-
window.showErrorMessage(err.message)
39+
window.showErrorMessage(error.message)
4340
}
4441
return
4542
}
@@ -132,7 +129,7 @@ export async function generateEditorConfig(uri: Uri) {
132129

133130
const encodingMap = {
134131
iso88591: 'latin1',
135-
utf8: 'utf-8',
132+
utf8: 'utff8-8',
136133
utf8bom: 'utf-8-bom',
137134
utf16be: 'utf-16-be',
138135
utf16le: 'utf-16-le',
@@ -159,17 +156,17 @@ export async function generateEditorConfig(uri: Uri) {
159156
editorConfigUri,
160157
Buffer.from(settingsLines.join(eolKey)),
161158
)
162-
} catch (err) {
159+
} catch (error) {
163160
if (
164-
typeof err !== 'object' ||
165-
err === null ||
166-
!('message' in err) ||
167-
typeof err.message !== 'string'
161+
typeof error !== 'object' ||
162+
error === null ||
163+
!('message' in error) ||
164+
typeof error.message !== 'string'
168165
) {
169166
return
170167
}
171168

172-
window.showErrorMessage(err.message)
169+
window.showErrorMessage(error.message)
173170
}
174171
}
175172
}

src/test/runTest.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as path from 'path'
1+
import * as path from 'node:path'
22

33
import { runTests } from '@vscode/test-electron'
44

@@ -39,4 +39,5 @@ async function main() {
3939
}
4040
}
4141

42+
// eslint-disable-next-line unicorn/prefer-top-level-await
4243
main()

src/test/suite/api.test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import * as assert from 'assert'
2-
import { KnownProps } from 'editorconfig'
1+
import * as assert from 'node:assert'
32
import { TextEditorOptions } from 'vscode'
4-
3+
import { KnownProps } from 'editorconfig'
54
import * as api from '../../api'
65

76
suite('EditorConfig extension', () => {

src/test/suite/index.test.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import * as assert from 'assert'
2-
import * as os from 'os'
1+
import * as assert from 'node:assert'
2+
import * as os from 'node:os'
33
import { Position, window, workspace, WorkspaceEdit } from 'vscode'
4-
import { getFixturePath, getOptionsForFixture, wait } from '../testUtils'
5-
64
import * as utils from 'vscode-test-utils'
5+
import { getFixturePath, getOptionsForFixture, wait } from '../testUtils'
76

87
suite('EditorConfig extension', function () {
98
this.retries(2)
@@ -372,26 +371,27 @@ function withSetting(
372371
) {
373372
return {
374373
async getText() {
375-
return (
376-
await this.createDoc(options.contents, options.fileName)
377-
).getText()
374+
const doc = await this.createDoc(options.contents, options.fileName)
375+
return doc.getText()
378376
},
379-
saveText(text: string) {
380-
return new Promise<string>(async resolve => {
381-
const doc = await this.createDoc(options.contents, options.fileName)
377+
async saveText(text: string) {
378+
const doc = await this.createDoc(options.contents, options.fileName)
379+
380+
const savePromise = new Promise<string>(resolve => {
382381
workspace.onDidChangeTextDocument(doc.save)
383382
workspace.onDidSaveTextDocument(savedDoc => {
384383
assert.strictEqual(savedDoc.isDirty, false, 'dirty saved doc')
385384
resolve(savedDoc.getText())
386385
})
387-
const edit = new WorkspaceEdit()
388-
edit.insert(doc.uri, new Position(0, 0), text)
389-
assert.strictEqual(
390-
await workspace.applyEdit(edit),
391-
true,
392-
'editor fails to apply edit',
393-
)
394386
})
387+
388+
const edit = new WorkspaceEdit()
389+
edit.insert(doc.uri, new Position(0, 0), text)
390+
391+
const result = await workspace.applyEdit(edit)
392+
assert.strictEqual(result, true, 'editor fails to apply edit')
393+
394+
return savePromise
395395
},
396396
async createDoc(contents = '', name = 'test') {
397397
const uri = await utils.createFile(

src/test/suite/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import * as path from 'node:path'
12
import { globSync } from 'glob'
23
import * as Mocha from 'mocha'
3-
import * as path from 'path'
44

55
export function run(): Promise<void> {
66
// Create the mocha test
@@ -25,8 +25,8 @@ export function run(): Promise<void> {
2525
c()
2626
}
2727
})
28-
} catch (err) {
29-
e(err)
28+
} catch (error) {
29+
e(error)
3030
}
3131
})
3232
}

src/test/testUtils.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import * as assert from 'assert'
2-
import * as path from 'path'
1+
import * as assert from 'node:assert'
2+
import * as path from 'node:path'
33
import { TextEditorOptions, Uri, window } from 'vscode'
44
import * as utils from 'vscode-test-utils'
55

@@ -10,7 +10,7 @@ export async function getOptionsForFixture(file: string[]) {
1010

1111
export function getFixturePath(file: string[]) {
1212
return path.resolve(
13-
path.join(...[__dirname, '..', 'test', 'suite', 'fixtures'].concat(file)),
13+
path.join(__dirname, '..', 'test', 'suite', 'fixtures', ...file),
1414
)
1515
}
1616

@@ -21,19 +21,18 @@ export function wait(ms: number) {
2121
}
2222

2323
async function getTextEditorOptions() {
24-
let resolved = false
25-
26-
return new Promise<TextEditorOptions>(async resolve => {
24+
const eventPromise = new Promise<TextEditorOptions>(resolve => {
2725
window.onDidChangeTextEditorOptions(e => {
28-
resolved = true
2926
assert.ok(e.options)
3027
resolve(e.options)
3128
})
29+
})
30+
31+
const fallbackPromise = (async () => {
3232
await wait(100)
33-
if (resolved) {
34-
return
35-
}
3633
assert.ok(window.activeTextEditor!.options)
37-
resolve(window.activeTextEditor!.options)
38-
})
34+
return window.activeTextEditor!.options
35+
})()
36+
37+
return await Promise.race([eventPromise, fallbackPromise])
3938
}

src/test/untitled-suite/index.test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import * as assert from 'assert'
1+
import * as assert from 'node:assert'
22
import { commands, window } from 'vscode'
3-
import { wait } from '../testUtils'
4-
53
import * as utils from 'vscode-test-utils'
4+
import { wait } from '../testUtils'
65

76
suite('EditorConfig extension untitled workspace', function () {
87
this.retries(2)

0 commit comments

Comments
 (0)