From 59eb9f8528bd1f00cb2b63fa46cca3dd8e299254 Mon Sep 17 00:00:00 2001 From: Ilya Golovin Date: Sun, 13 Jul 2025 18:51:53 +0300 Subject: [PATCH 1/2] fix(addDestruct): handle call expression param case Commit was originally cherry-picked from 4ae4e527ba230797fc71de4f50c96c4843d53a8e. --- .../addDestructure/addSplittedDestructure.ts | 12 ++++----- .../test/codeActions/addDestruct.spec.ts | 25 ++++++++++++++++++- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/typescript/src/codeActions/custom/addDestructure/addSplittedDestructure.ts b/typescript/src/codeActions/custom/addDestructure/addSplittedDestructure.ts index 3021d68c..00706f55 100644 --- a/typescript/src/codeActions/custom/addDestructure/addSplittedDestructure.ts +++ b/typescript/src/codeActions/custom/addDestructure/addSplittedDestructure.ts @@ -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() diff --git a/typescript/test/codeActions/addDestruct.spec.ts b/typescript/test/codeActions/addDestruct.spec.ts index 45433ebc..01a54e85 100644 --- a/typescript/test/codeActions/addDestruct.spec.ts +++ b/typescript/test/codeActions/addDestruct.spec.ts @@ -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 From f8c0c242e8a99f0f4dfba1c8860362d4f40e0792 Mon Sep 17 00:00:00 2001 From: Ilya Golovin Date: Sun, 13 Jul 2025 18:52:20 +0300 Subject: [PATCH 2/2] chore: add fourslash to cSpell config --- .vscode/settings.json | 1 + 1 file changed, 1 insertion(+) diff --git a/.vscode/settings.json b/.vscode/settings.json index 2f38b4d4..9d7fdcb6 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -19,6 +19,7 @@ "develop" ], "cSpell.words": [ + "fourslash", "tsserverlibrary", "unpatch" ],