diff --git a/typescript/src/codeActions/extended/declareMissingProperties.ts b/typescript/src/codeActions/extended/declareMissingProperties.ts index 4ddd319..5c53bef 100644 --- a/typescript/src/codeActions/extended/declareMissingProperties.ts +++ b/typescript/src/codeActions/extended/declareMissingProperties.ts @@ -6,13 +6,13 @@ export default { kind: 'quickfix', title: 'Declare missing property', tryToApply({ sourceFile, node }) { - const param = matchParents(node, ['Identifier', 'ObjectBindingPattern', 'Parameter']) + const param = matchParents(node, ['Identifier', 'BindingElement', 'ObjectBindingPattern', 'Parameter']) if (param) { // special react pattern if (ts.isArrowFunction(param.parent) && ts.isVariableDeclaration(param.parent.parent)) { const variableDecl = param.parent.parent if (variableDecl.type?.getText().match(/(React\.)?FC/)) { - // handle interface + // todo handle interface } } // general patterns @@ -24,21 +24,12 @@ export default { if (insertComma) insertText = `, ${insertText}` // alternatively only one snippetEdit could be used with tsFull.escapeSnippetText(insertText) + $0 return { - edits: [ - { - newText: insertText, - span: { - length: 0, - start: insertPos, - }, - }, - ], snippetEdits: [ { - newText: '$0', + newText: `${tsFull.escapeSnippetText(insertText)}$0`, span: { length: 0, - start: insertPos + insertText.length - 1, + start: insertPos, }, }, ], @@ -48,3 +39,16 @@ export default { return }, } as ExtendedCodeAction + +const testCode = () => { + const tester = (code: string) => { + // ^ - problem location in which quickfix needs to be tested (applied) + // | - cursor position after quickfix is applied + // [[...]] - applied part of the code + /* TODO */ + } + + tester(/* ts */ ` + const b = ({ b, ^a }: { b[[, a/*|*/]] }) => {} + `) +} diff --git a/typescript/src/utils.ts b/typescript/src/utils.ts index 6a4ccbe..872e755 100644 --- a/typescript/src/utils.ts +++ b/typescript/src/utils.ts @@ -303,12 +303,12 @@ export const matchParents: MatchParentsType = (node, treeToCompare) => { for (const toCompare of treeToCompare) { if (!first) { node = node?.parent - first = false } if (!node) return if (!(ts[`is${toCompare}` as keyof typeof ts] as (node) => boolean)(node)) { return } + first = false } return node as any }