Skip to content

Commit 633e249

Browse files
committed
feat: add codefix for const and let should be declared in block
1 parent 66d0a88 commit 633e249

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

typescript/src/index.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,30 @@ export = function ({ typescript }: { typescript: typeof import('typescript/lib/t
275275

276276
proxy.getCodeFixesAtPosition = (fileName, start, end, errorCodes, formatOptions, preferences) => {
277277
let prior = info.languageService.getCodeFixesAtPosition(fileName, start, end, errorCodes, formatOptions, preferences)
278+
// const scriptSnapshot = info.project.getScriptSnapshot(fileName)
279+
const diagnostics = info.languageService.getSemanticDiagnostics(fileName)
280+
281+
// https://github.com/Microsoft/TypeScript/blob/v4.5.5/src/compiler/diagnosticMessages.json#L458
282+
const appliableErrorCode = [1156, 1157].find(code => errorCodes.includes(code))
283+
if (appliableErrorCode) {
284+
const diagnostic = diagnostics.find(({ code }) => code === appliableErrorCode)!
285+
prior = [
286+
...prior,
287+
{
288+
fixName: 'wrapBlock',
289+
description: 'Wrap in block',
290+
changes: [
291+
{
292+
fileName,
293+
textChanges: [
294+
{ span: { start: diagnostic.start!, length: 0 }, newText: '{' },
295+
{ span: { start: diagnostic.start! + diagnostic.length!, length: 0 }, newText: '}' },
296+
],
297+
},
298+
],
299+
},
300+
]
301+
}
278302

279303
if (c('removeCodeFixes.enable')) {
280304
const toRemove = c('removeCodeFixes.codefixes')

0 commit comments

Comments
 (0)