Skip to content

Commit 36c17f0

Browse files
committed
refactor: fix some eslint errors in typescript
fix: don't patch text suggestios on functions & static classes functions
1 parent 90d83d0 commit 36c17f0

File tree

4 files changed

+38
-36
lines changed

4 files changed

+38
-36
lines changed

src/configurationType.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { ScriptElementKind } from 'typescript/lib/tsserverlibrary'
33
type ReplaceRule = {
44
/** e.g. `readFile`, `^readFile` (global) or `fs.readFile` */
55
suggestion: string
6-
filter: {
6+
filter?: {
77
// package?: string
88
// TODO
99
kind?: keyof typeof ScriptElementKind

src/configurationTypeCache.jsonc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// GENERATED. DON'T EDIT MANUALLY
2-
// md5hash: 9c3812628516e353f3aa75256e99e8fa
2+
// md5hash: 0b8b4697ce68ebfe40346fd51700a786
33
{
44
"type": "object",
55
"properties": {
@@ -219,7 +219,6 @@
219219
}
220220
},
221221
"required": [
222-
"filter",
223222
"suggestion"
224223
]
225224
}

src/extension.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export const activate = async () => {
1616
const config = vscode.workspace.getConfiguration().get(process.env.IDS_PREFIX!)
1717
api.configurePlugin('ts-essential-plugins', config)
1818
}
19+
1920
vscode.workspace.onDidChangeConfiguration(({ affectsConfiguration }) => {
2021
if (affectsConfiguration(process.env.IDS_PREFIX!)) syncConfig()
2122
})

typescript/src/index.ts

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ import get from 'lodash.get'
22
import type tslib from 'typescript/lib/tsserverlibrary'
33
import * as emmet from '@vscode/emmet-helper'
44

5+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
56
//@ts-ignore
67
import type { Configuration } from '../../src/configurationType'
78

8-
export = function ({ typescript }: { typescript: typeof import('typescript/lib/tsserverlibrary') }) {
9+
export = function({ typescript }: { typescript: typeof import('typescript/lib/tsserverlibrary') }) {
910
const ts = typescript
1011
let _configuration: Configuration
1112
const c = <T extends keyof Configuration>(key: T): Configuration[T] => get(_configuration, key)
@@ -42,11 +43,11 @@ export = function ({ typescript }: { typescript: typeof import('typescript/lib/t
4243
}
4344

4445
let prevCompletionsMap: Record<string, { originalName: string }>
46+
// eslint-disable-next-line complexity
4547
proxy.getCompletionsAtPosition = (fileName, position, options) => {
4648
prevCompletionsMap = {}
47-
if (!_configuration) {
48-
console.log('no received configuration!')
49-
}
49+
if (!_configuration) console.log('no received configuration!')
50+
5051
// console.time('slow-down')
5152
const program = info.languageService.getProgram()
5253
const sourceFile = program?.getSourceFile(fileName)
@@ -79,13 +80,13 @@ export = function ({ typescript }: { typescript: typeof import('typescript/lib/t
7980
if (lastPart.startsWith('<')) lastPart = lastPart.slice(1)
8081
const isStartingWithUpperCase = (str: string) => str[0] === str[0]?.toUpperCase()
8182
// check if starts with lowercase
82-
if (isStartingWithUpperCase(lastPart)) {
83+
if (isStartingWithUpperCase(lastPart))
8384
// TODO! compare with suggestions from lib.dom
8485
prior.entries = prior.entries.filter(
8586
entry => isStartingWithUpperCase(entry.name) && ![typescript.ScriptElementKind.enumElement].includes(entry.kind),
8687
)
87-
}
8888
}
89+
8990
if (
9091
c('jsxEmmet.type') !== 'disabled' &&
9192
(emmetSyntaxKinds.includes(node.kind) ||
@@ -121,7 +122,7 @@ export = function ({ typescript }: { typescript: typeof import('typescript/lib/t
121122
'html',
122123
{},
123124
) ?? { items: [] }
124-
for (const completion of emmetCompletions.items) {
125+
for (const completion of emmetCompletions.items)
125126
prior.entries.push({
126127
kind: typescript.ScriptElementKind.label,
127128
name: completion.label.slice(1),
@@ -132,7 +133,6 @@ export = function ({ typescript }: { typescript: typeof import('typescript/lib/t
132133
sourceDisplay: completion.detail !== undefined ? [{ kind: 'text', text: completion.detail }] : undefined,
133134
// replacementSpan: { start: position - 5, length: 5 },
134135
})
135-
}
136136
} else {
137137
const tags = c('jsxPseudoEmmet.tags')
138138
for (let [tag, value] of Object.entries(tags)) {
@@ -149,9 +149,9 @@ export = function ({ typescript }: { typescript: typeof import('typescript/lib/t
149149
}
150150
}
151151
}
152-
if (!prior) {
153-
return
154-
}
152+
153+
if (!prior) return
154+
155155
// const fullText = scriptSnapshot.getText(0, scriptSnapshot.getLength())
156156
// const matchImport = /(import (.*)from )['"].*['"]/.exec(fullText.split('\n')[line]!)?.[1]
157157
// if (matchImport && character <= `import${matchImport}`.length) {
@@ -160,14 +160,13 @@ export = function ({ typescript }: { typescript: typeof import('typescript/lib/t
160160
// }
161161
// prior.isGlobalCompletion
162162
// prior.entries[0]
163-
const entryNames = prior.entries.map(({ name }) => name)
163+
const entryNames = new Set(prior.entries.map(({ name }) => name))
164164
if (c('removeUselessFunctionProps.enable')) prior.entries = prior.entries.filter(e => !['Symbol', 'caller', 'prototype'].includes(e.name))
165-
if (['bind', 'call', 'caller'].every(name => entryNames.includes(name))) {
166-
if (c('highlightNonFunctionMethods.enable')) {
167-
const standardProps = ['Symbol', 'apply', 'arguments', 'bind', 'call', 'caller', 'length', 'name', 'prototype', 'toString']
165+
if (['bind', 'call', 'caller'].every(name => entryNames.has(name)) && c('highlightNonFunctionMethods.enable')) {
166+
const standardProps = new Set(['Symbol', 'apply', 'arguments', 'bind', 'call', 'caller', 'length', 'name', 'prototype', 'toString'])
168167
// TODO lift up!
169168
prior.entries = prior.entries.map(entry => {
170-
if (!standardProps.includes(entry.name)) {
169+
if (!standardProps.has(entry.name) && entry.kind !== ts.ScriptElementKind.warning) {
171170
const newName = `☆${entry.name}`
172171
prevCompletionsMap[newName] = {
173172
originalName: entry.name,
@@ -178,28 +177,30 @@ export = function ({ typescript }: { typescript: typeof import('typescript/lib/t
178177
name: newName,
179178
}
180179
}
180+
181181
return entry
182182
})
183183
}
184-
}
184+
185185
if (c('patchToString.enable')) {
186186
// const indexToPatch = arrayMoveItemToFrom(
187187
// prior.entries,
188188
// ({ name }) => name === 'toExponential',
189189
// ({ name }) => name === 'toString',
190190
// )
191-
let indexToPatch = prior.entries.findIndex(({ name }) => name === 'toString')
191+
const indexToPatch = prior.entries.findIndex(({ name }) => name === 'toString')
192192
if (indexToPatch !== -1) {
193193
prior.entries[indexToPatch]!.insertText = `${prior.entries[indexToPatch]!.insertText ?? prior.entries[indexToPatch]!.name}()`
194194
prior.entries[indexToPatch]!.kind = typescript.ScriptElementKind.constElement
195195
// prior.entries[indexToPatch]!.isSnippet = true
196196
}
197197
}
198+
198199
const banAutoImportPackages = c('suggestions.banAutoImportPackages')
199200
if (banAutoImportPackages?.length)
200201
prior.entries = prior.entries.filter(entry => {
201202
if (!entry.sourceDisplay) return true
202-
const text = entry.sourceDisplay.map(item => item.text).join()
203+
const text = entry.sourceDisplay.map(item => item.text).join('')
203204
if (text.startsWith('.')) return true
204205
// TODO change to startsWith?
205206
return !banAutoImportPackages.includes(text)
@@ -214,20 +215,20 @@ export = function ({ typescript }: { typescript: typeof import('typescript/lib/t
214215
})
215216
if (!suggestion) continue
216217

217-
if (rule.delete) {
218+
if (rule.delete)
218219
prior.entries.splice(foundIndex!, 1)
219-
}
220220

221-
if (rule.duplicateOriginal) {
221+
222+
if (rule.duplicateOriginal)
222223
prior.entries.splice(rule.duplicateOriginal === 'above' ? foundIndex! : foundIndex! + 1, 0, { ...suggestion })
223-
}
224224

225225
Object.assign(suggestion, rule.patch ?? {})
226226
if (rule.patch?.insertText) suggestion.isSnippet = true
227227
}
228-
if (c('correctSorting.enable')) {
228+
229+
if (c('correctSorting.enable'))
229230
prior.entries = prior.entries.map((entry, index) => ({ ...entry, sortText: `${entry.sortText ?? ''}${index}` }))
230-
}
231+
231232
// console.log('signatureHelp', JSON.stringify(info.languageService.getSignatureHelpItems(fileName, position, {})))
232233
// console.timeEnd('slow-down')
233234
return prior
@@ -265,6 +266,7 @@ export = function ({ typescript }: { typescript: typeof import('typescript/lib/t
265266

266267
return prior
267268
}
269+
268270
proxy.getCodeFixesAtPosition = (fileName, start, end, errorCodes, formatOptions, preferences) => {
269271
let prior = info.languageService.getCodeFixesAtPosition(fileName, start, end, errorCodes, formatOptions, preferences)
270272

@@ -278,11 +280,12 @@ export = function ({ typescript }: { typescript: typeof import('typescript/lib/t
278280

279281
return prior
280282
}
281-
// @ts-expect-error
283+
284+
// @ts-expect-error some experiments
282285
proxy.ignored = (fileName: string, positionOrRange: number, preferences: any) => {
283-
if (typeof positionOrRange !== 'number') {
286+
if (typeof positionOrRange !== 'number')
284287
positionOrRange = positionOrRange
285-
}
288+
286289
// ts.createSourceFile(fileName, sourceText, languageVersion)
287290
const { textSpan } = proxy.getSmartSelectionRange(fileName, positionOrRange)
288291
console.log('textSpan.start', textSpan.start, textSpan.length)
@@ -298,6 +301,7 @@ export = function ({ typescript }: { typescript: typeof import('typescript/lib/t
298301
console.log('no node')
299302
return []
300303
}
304+
301305
// console.log(
302306
// 'special 1',
303307
// typescript.isJsxExpression(node),
@@ -339,19 +343,17 @@ const arrayMoveItemToFrom = <T>(array: T[], originalItem: ArrayPredicate<T>, ite
339343
return originalItemIndex
340344
}
341345

342-
const patchText = (input: string, start: number, end: number, newText: string) => {
343-
return input.slice(0, start) + newText + input.slice(end)
344-
}
346+
const patchText = (input: string, start: number, end: number, newText: string) => input.slice(0, start) + newText + input.slice(end)
345347

346348
function findChildContainingPosition(
347349
typescript: typeof import('typescript/lib/tsserverlibrary'),
348350
sourceFile: tslib.SourceFile,
349351
position: number,
350352
): tslib.Node | undefined {
351353
function find(node: ts.Node): ts.Node | undefined {
352-
if (position >= node.getStart() && position < node.getEnd()) {
354+
if (position >= node.getStart() && position < node.getEnd())
353355
return typescript.forEachChild(node, find) || node
354-
}
356+
355357
return
356358
}
357359
return find(sourceFile)

0 commit comments

Comments
 (0)