Skip to content

Commit

Permalink
fix: skip if call expression
Browse files Browse the repository at this point in the history
  • Loading branch information
Ilya Golovin committed Nov 13, 2023
1 parent c017eab commit 8cbf209
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
2 changes: 1 addition & 1 deletion typescript/src/codeActions/custom/addDestructure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const addDestructureToVariableWithSplittedPropertyAccessors = (
const highlightedNode = findChildContainingExactPosition(sourceFile, pos)

if (!highlightedNode) continue
if (ts.isElementAccessExpression(highlightedNode.parent)) return
if (ts.isElementAccessExpression(highlightedNode.parent) || ts.isCallExpression(highlightedNode.parent.parent)) return

if (ts.isIdentifier(highlightedNode) && ts.isPropertyAccessExpression(highlightedNode.parent)) {
const accessorName = highlightedNode.parent.name.getText()
Expand Down
15 changes: 5 additions & 10 deletions typescript/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -438,16 +438,11 @@ export const isTypeNode = (node: ts.Node) => {
// built-in types
return true
}
const isInTypeReference = (node: ts.Node) => {
if (ts.isTypeReferenceNode(node)) return true

if (inTypeReference(node)) return true

return false

function inTypeReference(node: ts.Node) {
if (ts.isTypeReferenceNode(node)) {
return true
}

return node.parent && inTypeReference(node.parent)
return node.parent && isInTypeReference(node.parent)
}

return isInTypeReference(node)
}
16 changes: 16 additions & 0 deletions typescript/test/codeActions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,22 @@ describe('Add destructure', () => {
})
})
})
test('Should skip if trying to destruct call expression', () => {
const initial = /* ts */ `
const /*t*/newVariable/*t*/ = foo
const obj = {
tag: newVariable.map(() => 10),
}
`

const { codeAction } = fourslashLikeTester(initial, undefined, { dedent: true })

codeAction(0, {
refactorName: 'Add Destruct',
newContent: null,
})
})
})

describe('From destructure', () => {
Expand Down

0 comments on commit 8cbf209

Please sign in to comment.