From 8cbf209fe147e4e06e9ccd745aeafd7598a3be42 Mon Sep 17 00:00:00 2001 From: Ilya Golovin Date: Tue, 14 Nov 2023 01:13:11 +0300 Subject: [PATCH] fix: skip if call expression --- .../src/codeActions/custom/addDestructure.ts | 2 +- typescript/src/utils.ts | 15 +++++---------- typescript/test/codeActions.spec.ts | 16 ++++++++++++++++ 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/typescript/src/codeActions/custom/addDestructure.ts b/typescript/src/codeActions/custom/addDestructure.ts index 701bc3d..7594265 100644 --- a/typescript/src/codeActions/custom/addDestructure.ts +++ b/typescript/src/codeActions/custom/addDestructure.ts @@ -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() diff --git a/typescript/src/utils.ts b/typescript/src/utils.ts index cfe98e1..4c7ad77 100644 --- a/typescript/src/utils.ts +++ b/typescript/src/utils.ts @@ -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) } diff --git a/typescript/test/codeActions.spec.ts b/typescript/test/codeActions.spec.ts index 577df2e..a58c2cd 100644 --- a/typescript/test/codeActions.spec.ts +++ b/typescript/test/codeActions.spec.ts @@ -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', () => {