Skip to content

fix(addDestructure): support call expression argument destructuring #226

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"develop"
],
"cSpell.words": [
"fourslash",
"tsserverlibrary",
"unpatch"
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ export default (node: ts.Node, sourceFile: ts.SourceFile, formatOptions: ts.Form
const highlightedNode = findChildContainingExactPosition(sourceFile, pos)

if (!highlightedNode) continue
/**
* Targets `/->/foo.map/<-/(newVariable.test)`
*/
const isInsideExpressionOfCallExpression =
ts.isCallExpression(highlightedNode.parent.parent) && highlightedNode.parent.parent.expression === highlightedNode.parent

if (
ts.isElementAccessExpression(highlightedNode.parent) ||
ts.isCallExpression(highlightedNode.parent.parent) ||
ts.isTypeQueryNode(highlightedNode.parent)
)
return
if (ts.isElementAccessExpression(highlightedNode.parent) || ts.isTypeQueryNode(highlightedNode.parent) || isInsideExpressionOfCallExpression) return

if (ts.isIdentifier(highlightedNode) && ts.isPropertyAccessExpression(highlightedNode.parent)) {
const accessorName = highlightedNode.parent.name.getText()
Expand Down
25 changes: 24 additions & 1 deletion typescript/test/codeActions/addDestruct.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,31 @@ describe('Add destructure', () => {
})
})

test('Adds destructure to argument of call expression', () => {
const initial = /* ts */ `
const /*t*/newVariable/*t*/ = { test: 1 }

const obj = {
tag: foo.map(newVariable.test),
}
`
const expected = /* ts */ `
const { test } = { test: 1 }

const obj = {
tag: foo.map(test),
}
`

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

codeAction(0, {
refactorName: 'Add Destruct',
newContent: expected,
})
})
describe('Skip cases', () => {
test('Should skip if trying to destruct call expression', () => {
test('Should skip if trying to destruct expression of call expression', () => {
const initial = /* ts */ `
const /*t*/newVariable/*t*/ = foo

Expand Down
Loading