Skip to content

Commit

Permalink
refactor: fix some eslint errors in typescript
Browse files Browse the repository at this point in the history
fix: don't patch text suggestios on functions & static classes functions
  • Loading branch information
zardoy committed May 19, 2022
1 parent 90d83d0 commit 36c17f0
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/configurationType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ScriptElementKind } from 'typescript/lib/tsserverlibrary'
type ReplaceRule = {
/** e.g. `readFile`, `^readFile` (global) or `fs.readFile` */
suggestion: string
filter: {
filter?: {
// package?: string
// TODO
kind?: keyof typeof ScriptElementKind
Expand Down
3 changes: 1 addition & 2 deletions src/configurationTypeCache.jsonc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// GENERATED. DON'T EDIT MANUALLY
// md5hash: 9c3812628516e353f3aa75256e99e8fa
// md5hash: 0b8b4697ce68ebfe40346fd51700a786
{
"type": "object",
"properties": {
Expand Down Expand Up @@ -219,7 +219,6 @@
}
},
"required": [
"filter",
"suggestion"
]
}
Expand Down
1 change: 1 addition & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const activate = async () => {
const config = vscode.workspace.getConfiguration().get(process.env.IDS_PREFIX!)
api.configurePlugin('ts-essential-plugins', config)
}

vscode.workspace.onDidChangeConfiguration(({ affectsConfiguration }) => {
if (affectsConfiguration(process.env.IDS_PREFIX!)) syncConfig()
})
Expand Down
68 changes: 35 additions & 33 deletions typescript/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import get from 'lodash.get'
import type tslib from 'typescript/lib/tsserverlibrary'
import * as emmet from '@vscode/emmet-helper'

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

export = function ({ typescript }: { typescript: typeof import('typescript/lib/tsserverlibrary') }) {
export = function({ typescript }: { typescript: typeof import('typescript/lib/tsserverlibrary') }) {
const ts = typescript
let _configuration: Configuration
const c = <T extends keyof Configuration>(key: T): Configuration[T] => get(_configuration, key)
Expand Down Expand Up @@ -42,11 +43,11 @@ export = function ({ typescript }: { typescript: typeof import('typescript/lib/t
}

let prevCompletionsMap: Record<string, { originalName: string }>
// eslint-disable-next-line complexity
proxy.getCompletionsAtPosition = (fileName, position, options) => {
prevCompletionsMap = {}
if (!_configuration) {
console.log('no received configuration!')
}
if (!_configuration) console.log('no received configuration!')

// console.time('slow-down')
const program = info.languageService.getProgram()
const sourceFile = program?.getSourceFile(fileName)
Expand Down Expand Up @@ -79,13 +80,13 @@ export = function ({ typescript }: { typescript: typeof import('typescript/lib/t
if (lastPart.startsWith('<')) lastPart = lastPart.slice(1)
const isStartingWithUpperCase = (str: string) => str[0] === str[0]?.toUpperCase()
// check if starts with lowercase
if (isStartingWithUpperCase(lastPart)) {
if (isStartingWithUpperCase(lastPart))
// TODO! compare with suggestions from lib.dom
prior.entries = prior.entries.filter(
entry => isStartingWithUpperCase(entry.name) && ![typescript.ScriptElementKind.enumElement].includes(entry.kind),
)
}
}

if (
c('jsxEmmet.type') !== 'disabled' &&
(emmetSyntaxKinds.includes(node.kind) ||
Expand Down Expand Up @@ -121,7 +122,7 @@ export = function ({ typescript }: { typescript: typeof import('typescript/lib/t
'html',
{},
) ?? { items: [] }
for (const completion of emmetCompletions.items) {
for (const completion of emmetCompletions.items)
prior.entries.push({
kind: typescript.ScriptElementKind.label,
name: completion.label.slice(1),
Expand All @@ -132,7 +133,6 @@ export = function ({ typescript }: { typescript: typeof import('typescript/lib/t
sourceDisplay: completion.detail !== undefined ? [{ kind: 'text', text: completion.detail }] : undefined,
// replacementSpan: { start: position - 5, length: 5 },
})
}
} else {
const tags = c('jsxPseudoEmmet.tags')
for (let [tag, value] of Object.entries(tags)) {
Expand All @@ -149,9 +149,9 @@ export = function ({ typescript }: { typescript: typeof import('typescript/lib/t
}
}
}
if (!prior) {
return
}

if (!prior) return

// const fullText = scriptSnapshot.getText(0, scriptSnapshot.getLength())
// const matchImport = /(import (.*)from )['"].*['"]/.exec(fullText.split('\n')[line]!)?.[1]
// if (matchImport && character <= `import${matchImport}`.length) {
Expand All @@ -160,14 +160,13 @@ export = function ({ typescript }: { typescript: typeof import('typescript/lib/t
// }
// prior.isGlobalCompletion
// prior.entries[0]
const entryNames = prior.entries.map(({ name }) => name)
const entryNames = new Set(prior.entries.map(({ name }) => name))
if (c('removeUselessFunctionProps.enable')) prior.entries = prior.entries.filter(e => !['Symbol', 'caller', 'prototype'].includes(e.name))
if (['bind', 'call', 'caller'].every(name => entryNames.includes(name))) {
if (c('highlightNonFunctionMethods.enable')) {
const standardProps = ['Symbol', 'apply', 'arguments', 'bind', 'call', 'caller', 'length', 'name', 'prototype', 'toString']
if (['bind', 'call', 'caller'].every(name => entryNames.has(name)) && c('highlightNonFunctionMethods.enable')) {
const standardProps = new Set(['Symbol', 'apply', 'arguments', 'bind', 'call', 'caller', 'length', 'name', 'prototype', 'toString'])
// TODO lift up!
prior.entries = prior.entries.map(entry => {
if (!standardProps.includes(entry.name)) {
if (!standardProps.has(entry.name) && entry.kind !== ts.ScriptElementKind.warning) {
const newName = `☆${entry.name}`
prevCompletionsMap[newName] = {
originalName: entry.name,
Expand All @@ -178,28 +177,30 @@ export = function ({ typescript }: { typescript: typeof import('typescript/lib/t
name: newName,
}
}

return entry
})
}
}

if (c('patchToString.enable')) {
// const indexToPatch = arrayMoveItemToFrom(
// prior.entries,
// ({ name }) => name === 'toExponential',
// ({ name }) => name === 'toString',
// )
let indexToPatch = prior.entries.findIndex(({ name }) => name === 'toString')
const indexToPatch = prior.entries.findIndex(({ name }) => name === 'toString')
if (indexToPatch !== -1) {
prior.entries[indexToPatch]!.insertText = `${prior.entries[indexToPatch]!.insertText ?? prior.entries[indexToPatch]!.name}()`
prior.entries[indexToPatch]!.kind = typescript.ScriptElementKind.constElement
// prior.entries[indexToPatch]!.isSnippet = true
}
}

const banAutoImportPackages = c('suggestions.banAutoImportPackages')
if (banAutoImportPackages?.length)
prior.entries = prior.entries.filter(entry => {
if (!entry.sourceDisplay) return true
const text = entry.sourceDisplay.map(item => item.text).join()
const text = entry.sourceDisplay.map(item => item.text).join('')
if (text.startsWith('.')) return true
// TODO change to startsWith?
return !banAutoImportPackages.includes(text)
Expand All @@ -214,20 +215,20 @@ export = function ({ typescript }: { typescript: typeof import('typescript/lib/t
})
if (!suggestion) continue

if (rule.delete) {
if (rule.delete)
prior.entries.splice(foundIndex!, 1)
}

if (rule.duplicateOriginal) {

if (rule.duplicateOriginal)
prior.entries.splice(rule.duplicateOriginal === 'above' ? foundIndex! : foundIndex! + 1, 0, { ...suggestion })
}

Object.assign(suggestion, rule.patch ?? {})
if (rule.patch?.insertText) suggestion.isSnippet = true
}
if (c('correctSorting.enable')) {

if (c('correctSorting.enable'))
prior.entries = prior.entries.map((entry, index) => ({ ...entry, sortText: `${entry.sortText ?? ''}${index}` }))
}

// console.log('signatureHelp', JSON.stringify(info.languageService.getSignatureHelpItems(fileName, position, {})))
// console.timeEnd('slow-down')
return prior
Expand Down Expand Up @@ -265,6 +266,7 @@ export = function ({ typescript }: { typescript: typeof import('typescript/lib/t

return prior
}

proxy.getCodeFixesAtPosition = (fileName, start, end, errorCodes, formatOptions, preferences) => {
let prior = info.languageService.getCodeFixesAtPosition(fileName, start, end, errorCodes, formatOptions, preferences)

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

return prior
}
// @ts-expect-error

// @ts-expect-error some experiments
proxy.ignored = (fileName: string, positionOrRange: number, preferences: any) => {
if (typeof positionOrRange !== 'number') {
if (typeof positionOrRange !== 'number')
positionOrRange = positionOrRange
}

// ts.createSourceFile(fileName, sourceText, languageVersion)
const { textSpan } = proxy.getSmartSelectionRange(fileName, positionOrRange)
console.log('textSpan.start', textSpan.start, textSpan.length)
Expand All @@ -298,6 +301,7 @@ export = function ({ typescript }: { typescript: typeof import('typescript/lib/t
console.log('no node')
return []
}

// console.log(
// 'special 1',
// typescript.isJsxExpression(node),
Expand Down Expand Up @@ -339,19 +343,17 @@ const arrayMoveItemToFrom = <T>(array: T[], originalItem: ArrayPredicate<T>, ite
return originalItemIndex
}

const patchText = (input: string, start: number, end: number, newText: string) => {
return input.slice(0, start) + newText + input.slice(end)
}
const patchText = (input: string, start: number, end: number, newText: string) => input.slice(0, start) + newText + input.slice(end)

function findChildContainingPosition(
typescript: typeof import('typescript/lib/tsserverlibrary'),
sourceFile: tslib.SourceFile,
position: number,
): tslib.Node | undefined {
function find(node: ts.Node): ts.Node | undefined {
if (position >= node.getStart() && position < node.getEnd()) {
if (position >= node.getStart() && position < node.getEnd())
return typescript.forEachChild(node, find) || node
}

return
}
return find(sourceFile)
Expand Down

0 comments on commit 36c17f0

Please sign in to comment.