Skip to content

Commit

Permalink
fix: declare missing property now works
Browse files Browse the repository at this point in the history
fix: e.code suggestions work in switch-case
  • Loading branch information
zardoy committed Aug 22, 2023
1 parent 630b9d9 commit be05d62
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
30 changes: 17 additions & 13 deletions typescript/src/codeActions/extended/declareMissingProperties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
},
},
],
Expand All @@ -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/*|*/]] }) => {}
`)
}
2 changes: 1 addition & 1 deletion typescript/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit be05d62

Please sign in to comment.