Skip to content

Commit 8bf1416

Browse files
committed
fix: custom declare missing propertly code fix was always displayed even when it couldn't be applied
1 parent 08caa2a commit 8bf1416

File tree

4 files changed

+14
-5
lines changed

4 files changed

+14
-5
lines changed

src/codeActionProvider.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export default () => {
4242
}
4343

4444
if (context.triggerKind !== vscode.CodeActionTriggerKind.Invoke) return
45-
const result = await getPossibleTwoStepRefactorings(range)
45+
const result = await getPossibleTwoStepRefactorings(range, document, context.diagnostics)
4646
if (!result) return
4747
const { turnArrayIntoObject, extendedCodeActions } = result
4848
const codeActions: vscode.CodeAction[] = []
@@ -147,12 +147,13 @@ export default () => {
147147
await vscode.workspace.applyEdit(edit)
148148
})
149149

150-
async function getPossibleTwoStepRefactorings(range: vscode.Range, document = vscode.window.activeTextEditor!.document) {
150+
async function getPossibleTwoStepRefactorings(range: vscode.Range, document: vscode.TextDocument, diagnostics: Readonly<vscode.Diagnostic[]>) {
151151
return sendCommand('getTwoStepCodeActions', {
152152
document,
153153
position: range.start,
154154
inputOptions: {
155155
range: vscodeRangeToTs(document, range),
156+
diagnostics: diagnostics.filter(({ source }) => source === 'ts').map(({ code }) => (typeof code === 'object' ? +code.value : +code!)),
156157
},
157158
})
158159
}

typescript/src/codeActions/getCodeActions.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ export const getExtendedCodeActions = <T extends string | undefined>(
8080
// languageServiceHost: ts.LanguageServiceHost,
8181
formatOptions: ts.FormatCodeSettings | undefined,
8282
applyCodeActionTitle: T,
83+
filterErrorCodes?: number[],
8384
): T extends undefined ? ExtendedCodeAction[] : ApplyExtendedCodeActionResult => {
8485
const range = typeof positionOrRange !== 'number' && positionOrRange.pos !== positionOrRange.end ? positionOrRange : undefined
8586
const position = typeof positionOrRange === 'number' ? positionOrRange : positionOrRange.pos
@@ -99,8 +100,14 @@ export const getExtendedCodeActions = <T extends string | undefined>(
99100
}
100101
return compact(
101102
extendedCodeActions.map(codeAction => {
102-
if (!codeAction.codes && !codeAction.tryToApply(tryToApplyOptions)) return
103-
return codeAction
103+
if (
104+
!filterErrorCodes ||
105+
!codeAction.codes ||
106+
(codeAction.codes.some(c => filterErrorCodes.includes(c)) && codeAction.tryToApply(tryToApplyOptions))
107+
) {
108+
return codeAction
109+
}
110+
return
104111
}),
105112
) as T extends undefined ? ExtendedCodeAction[] : never
106113
}

typescript/src/ipcTypes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export type RequestInputTypes = {
5151
}
5252
getTwoStepCodeActions: {
5353
range: [number, number]
54+
diagnostics: number[]
5455
}
5556
twoStepCodeActionSecondStep: {
5657
range: [number, number]

typescript/src/specialCommands/handle.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export default (
4040
const node = findChildContainingPosition(ts, sourceFile, position)
4141
const posEnd = { pos: specialCommandArg.range[0], end: specialCommandArg.range[1] }
4242

43-
const extendedCodeActions = getExtendedCodeActions(sourceFile, posEnd, languageService, undefined, undefined)
43+
const extendedCodeActions = getExtendedCodeActions(sourceFile, posEnd, languageService, undefined, undefined, specialCommandArg.diagnostics)
4444
return {
4545
turnArrayIntoObject: objectIntoArrayConverters(posEnd, node, undefined),
4646
extendedCodeActions,

0 commit comments

Comments
 (0)