Skip to content

Commit be05d62

Browse files
committed
fix: declare missing property now works
fix: e.code suggestions work in switch-case
1 parent 630b9d9 commit be05d62

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

typescript/src/codeActions/extended/declareMissingProperties.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ export default {
66
kind: 'quickfix',
77
title: 'Declare missing property',
88
tryToApply({ sourceFile, node }) {
9-
const param = matchParents(node, ['Identifier', 'ObjectBindingPattern', 'Parameter'])
9+
const param = matchParents(node, ['Identifier', 'BindingElement', 'ObjectBindingPattern', 'Parameter'])
1010
if (param) {
1111
// special react pattern
1212
if (ts.isArrowFunction(param.parent) && ts.isVariableDeclaration(param.parent.parent)) {
1313
const variableDecl = param.parent.parent
1414
if (variableDecl.type?.getText().match(/(React\.)?FC/)) {
15-
// handle interface
15+
// todo handle interface
1616
}
1717
}
1818
// general patterns
@@ -24,21 +24,12 @@ export default {
2424
if (insertComma) insertText = `, ${insertText}`
2525
// alternatively only one snippetEdit could be used with tsFull.escapeSnippetText(insertText) + $0
2626
return {
27-
edits: [
28-
{
29-
newText: insertText,
30-
span: {
31-
length: 0,
32-
start: insertPos,
33-
},
34-
},
35-
],
3627
snippetEdits: [
3728
{
38-
newText: '$0',
29+
newText: `${tsFull.escapeSnippetText(insertText)}$0`,
3930
span: {
4031
length: 0,
41-
start: insertPos + insertText.length - 1,
32+
start: insertPos,
4233
},
4334
},
4435
],
@@ -48,3 +39,16 @@ export default {
4839
return
4940
},
5041
} as ExtendedCodeAction
42+
43+
const testCode = () => {
44+
const tester = (code: string) => {
45+
// ^ - problem location in which quickfix needs to be tested (applied)
46+
// | - cursor position after quickfix is applied
47+
// [[...]] - applied part of the code
48+
/* TODO */
49+
}
50+
51+
tester(/* ts */ `
52+
const b = ({ b, ^a }: { b[[, a/*|*/]] }) => {}
53+
`)
54+
}

typescript/src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,12 +303,12 @@ export const matchParents: MatchParentsType = (node, treeToCompare) => {
303303
for (const toCompare of treeToCompare) {
304304
if (!first) {
305305
node = node?.parent
306-
first = false
307306
}
308307
if (!node) return
309308
if (!(ts[`is${toCompare}` as keyof typeof ts] as (node) => boolean)(node)) {
310309
return
311310
}
311+
first = false
312312
}
313313
return node as any
314314
}

0 commit comments

Comments
 (0)