Skip to content

Commit eb8de78

Browse files
committed
fix: fix builtin fixConvertConstToLet quickfix!
fix: wrap in block codeaction now places itself on a new line
1 parent bd12a14 commit eb8de78

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

typescript/src/index.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { GetConfig } from './types'
88
import { getCompletionsAtPosition, PrevCompletionMap } from './completionsAtPosition'
99
import { oneOf } from '@zardoy/utils'
1010
import { isGoodPositionMethodCompletion } from './isGoodPositionMethodCompletion'
11+
import { inspect } from 'util'
12+
import { getIndentFromPos } from './utils'
1113

1214
const thisPluginMarker = Symbol('__essentialPluginsMarker__')
1315

@@ -132,12 +134,25 @@ export = function ({ typescript }: { typescript: typeof import('typescript/lib/t
132134

133135
proxy.getCodeFixesAtPosition = (fileName, start, end, errorCodes, formatOptions, preferences) => {
134136
let prior = info.languageService.getCodeFixesAtPosition(fileName, start, end, errorCodes, formatOptions, preferences)
137+
// fix builtin codefixes/refactorings
138+
prior.forEach(fix => {
139+
if (fix.fixName === 'fixConvertConstToLet') {
140+
const { start, length } = fix.changes[0]!.textChanges[0]!.span
141+
const fixedLength = 'const'.length as 5
142+
fix.changes[0]!.textChanges[0]!.span.start = start + length - fixedLength
143+
fix.changes[0]!.textChanges[0]!.span.length = fixedLength
144+
}
145+
return fix
146+
})
135147
// const scriptSnapshot = info.project.getScriptSnapshot(fileName)
136148
const diagnostics = proxy.getSemanticDiagnostics(fileName)
137149

138150
// https://github.com/Microsoft/TypeScript/blob/v4.5.5/src/compiler/diagnosticMessages.json#L458
139151
const appliableErrorCode = [1156, 1157].find(code => errorCodes.includes(code))
140152
if (appliableErrorCode) {
153+
const program = info.languageService.getProgram()
154+
const sourceFile = program!.getSourceFile(fileName)!
155+
const startIndent = getIndentFromPos(typescript, sourceFile, end)
141156
const diagnostic = diagnostics.find(({ code }) => code === appliableErrorCode)!
142157
prior = [
143158
...prior,
@@ -148,8 +163,8 @@ export = function ({ typescript }: { typescript: typeof import('typescript/lib/t
148163
{
149164
fileName,
150165
textChanges: [
151-
{ span: { start: diagnostic.start!, length: 0 }, newText: '{' },
152-
{ span: { start: diagnostic.start! + diagnostic.length!, length: 0 }, newText: '}' },
166+
{ span: { start: diagnostic.start!, length: 0 }, newText: `{\n${startIndent}\t` },
167+
{ span: { start: diagnostic.start! + diagnostic.length!, length: 0 }, newText: `\n${startIndent}}` },
153168
],
154169
},
155170
],
@@ -197,7 +212,10 @@ export = function ({ typescript }: { typescript: typeof import('typescript/lib/t
197212
let prior = info.languageService.findReferences(fileName, position)
198213
if (!prior) return
199214
if (c('removeDefinitionFromReferences')) {
200-
prior = prior.map(({ references, ...other }) => ({ ...other, references: references.filter(({ isDefinition }) => !isDefinition) }))
215+
prior = prior.map(({ references, ...other }) => ({
216+
...other,
217+
references: references.filter(({ isDefinition }) => !isDefinition),
218+
}))
201219
}
202220
return prior
203221
}

typescript/src/utils.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,13 @@ export function findChildContainingPositionMaxDepth(
3232
}
3333
return find(sourceFile)
3434
}
35+
36+
export const getIndentFromPos = (typescript: typeof import('typescript/lib/tsserverlibrary'), sourceFile: tslib.SourceFile, position: number) => {
37+
const { character } = typescript.getLineAndCharacterOfPosition(sourceFile, position)
38+
return (
39+
sourceFile
40+
.getText()
41+
.slice(position - character, position)
42+
.match(/^\s+/)?.[0] ?? ''
43+
)
44+
}

0 commit comments

Comments
 (0)