From 16332a67ed9c32f496f8d37526b24c35fa94a016 Mon Sep 17 00:00:00 2001 From: navya9singh Date: Thu, 23 Jan 2025 10:38:48 -0800 Subject: [PATCH 1/4] adding check for imports in preparePasteEdits --- src/services/preparePasteEdits.ts | 13 +++++++++++++ .../fourslash/preparePasteEdits_returnFalse.ts | 3 +++ 2 files changed, 16 insertions(+) diff --git a/src/services/preparePasteEdits.ts b/src/services/preparePasteEdits.ts index 9d3879fe5428c..480426fa66788 100644 --- a/src/services/preparePasteEdits.ts +++ b/src/services/preparePasteEdits.ts @@ -2,7 +2,14 @@ import { findAncestor, forEachChild, getTokenAtPosition, + isBindingElement, isIdentifier, + isImportClause, + isImportDeclaration, + isImportEqualsDeclaration, + isImportSpecifier, + isNamedImports, + isNamespaceImport, rangeContainsPosition, rangeContainsRange, SourceFile, @@ -25,6 +32,12 @@ export function preparePasteEdits( ancestorNode => rangeContainsRange(ancestorNode, range), ); if (!enclosingNode) return; + if ( + isImportSpecifier(enclosingNode) || isImportClause(enclosingNode) || isNamespaceImport(enclosingNode) || isImportEqualsDeclaration(enclosingNode) + || isBindingElement(enclosingNode) || isImportDeclaration(enclosingNode) || isNamedImports(enclosingNode) + ) { + return; + } forEachChild(enclosingNode, function checkNameResolution(node) { if (shouldProvidePasteEdits) return; if (isIdentifier(node) && rangeContainsPosition(range, node.getStart(sourceFile))) { diff --git a/tests/cases/fourslash/preparePasteEdits_returnFalse.ts b/tests/cases/fourslash/preparePasteEdits_returnFalse.ts index 6c79c1e4c834c..a10ee5fa87c3a 100644 --- a/tests/cases/fourslash/preparePasteEdits_returnFalse.ts +++ b/tests/cases/fourslash/preparePasteEdits_returnFalse.ts @@ -1,11 +1,14 @@ /// // @Filename: /file1.ts +//// import [|{abc}|] from "./file2"; //// [|const a = 1;|] //// [|function foo() { //// console.log("testing");}|] //// [|//This is a comment|] +// @Filename: /file2.ts +//// export const abc = 1; verify.preparePasteEdits({ copiedFromFile: "/file1.ts", copiedTextRange: test.ranges(), From f98c142b00f2adc2237d5a86373f4cb5a00d16e6 Mon Sep 17 00:00:00 2001 From: navya9singh Date: Thu, 23 Jan 2025 10:44:26 -0800 Subject: [PATCH 2/4] fixing test --- tests/cases/fourslash/preparePasteEdits_returnFalse.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/cases/fourslash/preparePasteEdits_returnFalse.ts b/tests/cases/fourslash/preparePasteEdits_returnFalse.ts index a10ee5fa87c3a..61af94064fe05 100644 --- a/tests/cases/fourslash/preparePasteEdits_returnFalse.ts +++ b/tests/cases/fourslash/preparePasteEdits_returnFalse.ts @@ -1,7 +1,7 @@ /// // @Filename: /file1.ts -//// import [|{abc}|] from "./file2"; +//// [|import {abc} from "./file2";|] //// [|const a = 1;|] //// [|function foo() { //// console.log("testing");}|] From 9658e0a74579a95653082335a3238a13bd8aecf2 Mon Sep 17 00:00:00 2001 From: navya9singh Date: Thu, 23 Jan 2025 10:46:59 -0800 Subject: [PATCH 3/4] removing binding element check --- src/services/preparePasteEdits.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/services/preparePasteEdits.ts b/src/services/preparePasteEdits.ts index 480426fa66788..4b080bbfbd6d1 100644 --- a/src/services/preparePasteEdits.ts +++ b/src/services/preparePasteEdits.ts @@ -33,8 +33,8 @@ export function preparePasteEdits( ); if (!enclosingNode) return; if ( - isImportSpecifier(enclosingNode) || isImportClause(enclosingNode) || isNamespaceImport(enclosingNode) || isImportEqualsDeclaration(enclosingNode) - || isBindingElement(enclosingNode) || isImportDeclaration(enclosingNode) || isNamedImports(enclosingNode) + isImportSpecifier(enclosingNode) || isImportClause(enclosingNode) || isNamespaceImport(enclosingNode) + || isImportEqualsDeclaration(enclosingNode) || isImportDeclaration(enclosingNode) || isNamedImports(enclosingNode) ) { return; } From 7bbb51fea71f78eebf7d262b00662f24e08c94e1 Mon Sep 17 00:00:00 2001 From: navya9singh Date: Thu, 23 Jan 2025 11:01:35 -0800 Subject: [PATCH 4/4] removing import --- src/services/preparePasteEdits.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/services/preparePasteEdits.ts b/src/services/preparePasteEdits.ts index 4b080bbfbd6d1..cc413539f6b80 100644 --- a/src/services/preparePasteEdits.ts +++ b/src/services/preparePasteEdits.ts @@ -2,7 +2,6 @@ import { findAncestor, forEachChild, getTokenAtPosition, - isBindingElement, isIdentifier, isImportClause, isImportDeclaration,