Skip to content
Open
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
17 changes: 17 additions & 0 deletions src/services/preparePasteEdits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,17 @@ import {
findAncestor,
forEachChild,
getTokenAtPosition,
isExportAssignment,
isExportDeclaration,
isExportSpecifier,
isIdentifier,
isImportClause,
isImportDeclaration,
isImportEqualsDeclaration,
isImportSpecifier,
isNamedExports,
isNamedImports,
isNamespaceImport,
rangeContainsPosition,
rangeContainsRange,
SourceFile,
Expand All @@ -25,6 +35,13 @@ export function preparePasteEdits(
ancestorNode => rangeContainsRange(ancestorNode, range),
);
if (!enclosingNode) return;
if (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do all these conditions work if only a an identifier is selected? What about part of the module specifier? The test only shows the case where an entire import declaration is selected.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, if only an identifier is selected, it works too because it doesn't enter this if block, instead tries to enter the forEachChild but since it doesn't have any children it just returns and moves to the next iteration. Same for moduleSpecifier. If this is selected { foo }, then it will enter the if block.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add tests to show that it works?

isImportSpecifier(enclosingNode) || isImportClause(enclosingNode) || isNamespaceImport(enclosingNode) || isImportEqualsDeclaration(enclosingNode)
|| isImportDeclaration(enclosingNode) || isNamedImports(enclosingNode) || isExportSpecifier(enclosingNode) || isNamedExports(enclosingNode)
|| isExportAssignment(enclosingNode) || isExportDeclaration(enclosingNode)
) {
return;
}
forEachChild(enclosingNode, function checkNameResolution(node) {
if (shouldProvidePasteEdits) return;
if (isIdentifier(node) && rangeContainsPosition(range, node.getStart(sourceFile))) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/// <reference path='./fourslash.ts' />

// @Filename: /file1.ts
//// import {[|abc|]} from "[|./file2|]";
//// import [|{q}|] from [|"./file3"|];
//// [|import * as a from "./file2";|]
////
//// const b = 1;
//// const c = 2;
//// [|export {[|b|], c} from "./file1";|]

// @Filename: /file2.ts
//// export const abc = 1;

// @Filename: /file3.ts
//// export const q = 1;
verify.preparePasteEdits({
copiedFromFile: "/file1.ts",
copiedTextRange: test.ranges(),
providePasteEdits: false,
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/// <reference path='./fourslash.ts' />

// @allowJs: true
// @module: commonjs

// @Filename: /file1.ts
//// [|import [|t|] = require("./file2");|]
//// function add(a: number, b: number): number {
//// return a + b;
//// }
//// [|export = add;|]

// @Filename: /file2.ts
//// export const t = 1;

verify.preparePasteEdits({
copiedFromFile: "/file1.ts",
copiedTextRange: test.ranges(),
providePasteEdits: false,
})
Loading