diff --git a/.vscode/settings.json b/.vscode/settings.json index 2f38b4d..9d7fdcb 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -19,6 +19,7 @@ "develop" ], "cSpell.words": [ + "fourslash", "tsserverlibrary", "unpatch" ], diff --git a/typescript/src/codeActions/custom/addDestructure/addSplittedDestructure.ts b/typescript/src/codeActions/custom/addDestructure/addSplittedDestructure.ts index 3021d68..00706f5 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 45433eb..01a54e8 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