Skip to content

Commit 526766c

Browse files
authored
fix(addDestructure): support call expression argument destructuring (#226)
1 parent 8134dc1 commit 526766c

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"develop"
2020
],
2121
"cSpell.words": [
22+
"fourslash",
2223
"tsserverlibrary",
2324
"unpatch"
2425
],

typescript/src/codeActions/custom/addDestructure/addSplittedDestructure.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ export default (node: ts.Node, sourceFile: ts.SourceFile, formatOptions: ts.Form
1919
const highlightedNode = findChildContainingExactPosition(sourceFile, pos)
2020

2121
if (!highlightedNode) continue
22+
/**
23+
* Targets `/->/foo.map/<-/(newVariable.test)`
24+
*/
25+
const isInsideExpressionOfCallExpression =
26+
ts.isCallExpression(highlightedNode.parent.parent) && highlightedNode.parent.parent.expression === highlightedNode.parent
2227

23-
if (
24-
ts.isElementAccessExpression(highlightedNode.parent) ||
25-
ts.isCallExpression(highlightedNode.parent.parent) ||
26-
ts.isTypeQueryNode(highlightedNode.parent)
27-
)
28-
return
28+
if (ts.isElementAccessExpression(highlightedNode.parent) || ts.isTypeQueryNode(highlightedNode.parent) || isInsideExpressionOfCallExpression) return
2929

3030
if (ts.isIdentifier(highlightedNode) && ts.isPropertyAccessExpression(highlightedNode.parent)) {
3131
const accessorName = highlightedNode.parent.name.getText()

typescript/test/codeActions/addDestruct.spec.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,31 @@ describe('Add destructure', () => {
254254
})
255255
})
256256

257+
test('Adds destructure to argument of call expression', () => {
258+
const initial = /* ts */ `
259+
const /*t*/newVariable/*t*/ = { test: 1 }
260+
261+
const obj = {
262+
tag: foo.map(newVariable.test),
263+
}
264+
`
265+
const expected = /* ts */ `
266+
const { test } = { test: 1 }
267+
268+
const obj = {
269+
tag: foo.map(test),
270+
}
271+
`
272+
273+
const { codeAction } = fourslashLikeTester(initial, undefined, { dedent: true })
274+
275+
codeAction(0, {
276+
refactorName: 'Add Destruct',
277+
newContent: expected,
278+
})
279+
})
257280
describe('Skip cases', () => {
258-
test('Should skip if trying to destruct call expression', () => {
281+
test('Should skip if trying to destruct expression of call expression', () => {
259282
const initial = /* ts */ `
260283
const /*t*/newVariable/*t*/ = foo
261284

0 commit comments

Comments
 (0)