Skip to content
Merged
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
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -4145,6 +4145,10 @@
"category": "Error",
"code": 8030
},
"You cannot rename a module via a global import.": {
"category": "Error",
"code": 8031
},
"Only identifiers/qualified-names with optional type arguments are currently supported in a class 'extends' clause.": {
"category": "Error",
"code": 9002
Expand Down
9 changes: 8 additions & 1 deletion src/services/rename.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,18 @@ namespace ts.Rename {
}

function getRenameInfoForModule(node: StringLiteralLike, sourceFile: SourceFile, moduleSymbol: Symbol): RenameInfo | undefined {
if (!isExternalModuleNameRelative(node.text)) {
return getRenameInfoError(Diagnostics.You_cannot_rename_a_module_via_a_global_import);
}

const moduleSourceFile = find(moduleSymbol.declarations, isSourceFile);
if (!moduleSourceFile) return undefined;
const withoutIndex = node.text.endsWith("/index") || node.text.endsWith("/index.js") ? undefined : tryRemoveSuffix(removeFileExtension(moduleSourceFile.fileName), "/index");
const name = withoutIndex === undefined ? moduleSourceFile.fileName : withoutIndex;
const kind = withoutIndex === undefined ? ScriptElementKind.moduleElement : ScriptElementKind.directory;
const indexAfterLastSlash = node.text.lastIndexOf("/") + 1;
// Span should only be the last component of the path. + 1 to account for the quote character.
const triggerSpan = createTextSpan(node.getStart(sourceFile) + 1 + indexAfterLastSlash, node.text.length - indexAfterLastSlash);
return {
canRename: true,
fileToRename: name,
Expand All @@ -52,7 +59,7 @@ namespace ts.Rename {
localizedErrorMessage: undefined,
fullDisplayName: name,
kindModifiers: ScriptElementKindModifier.none,
triggerSpan: createTriggerSpanForNode(node, sourceFile),
triggerSpan,
};
}

Expand Down
8 changes: 4 additions & 4 deletions tests/cases/fourslash/findAllRefs_importType_exportEquals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
////export = [|T|];

// @Filename: /b.ts
////const x: import("[|./a|]") = 0;
////const y: import("[|./a|]").U = "";
////const x: import("[|./[|a|]|]") = 0;
////const y: import("[|./[|a|]|]").U = "";

verify.noErrors();

const [r0, r1, r2, r3, r4] = test.ranges();
const [r0, r1, r2, r3, r3b, r4, r4b] = test.ranges();
verify.referenceGroups(r0, [{ definition: "type T = number\nnamespace T", ranges: [r0, r2, r3] }]);
verify.referenceGroups(r1, [{ definition: "namespace T", ranges: [r1, r2] }]);
verify.referenceGroups(r2, [{ definition: "type T = number\nnamespace T", ranges: [r0, r1, r2, r3] }]);
Expand All @@ -25,7 +25,7 @@ verify.referenceGroups([r3, r4], [
verify.renameLocations(r0, [r0, r2]);
verify.renameLocations(r1, [r1, r2]);
verify.renameLocations(r2, [r0, r1, r2]);
for (const range of [r3, r4]) {
for (const range of [r3b, r4b]) {
goTo.rangeStart(range);
verify.renameInfoSucceeded(/*displayName*/ "/a.ts", /*fullDisplayName*/ "/a.ts", /*kind*/ "module", /*kindModifiers*/ "", /*fileToRename*/ "/a.ts", range);
}
17 changes: 12 additions & 5 deletions tests/cases/fourslash/renameImport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,24 @@

// @allowJs: true

// @Filename: /node_modules/global/index.d.ts
////export const x: number;

// @Filename: /a.ts
////export const x = 0;

// @Filename: /dir/index.ts
////export const x = 0;

// @Filename: /b.ts
////import * as a from "[|./a|]";
////import a2 = require("[|./a|]");
////import * as dir from "[|{| "target": "dir" |}./dir|]";
////import * as dir2 from "[|{| "target": "dir/index" |}./dir/index|]";
////import * as a from "./[|a|]";
////import a2 = require("./[|a|]");
////import * as dir from "./[|{| "target": "dir" |}dir|]";
////import * as dir2 from "./dir/[|{| "target": "dir/index" |}index|]";

// @Filename: /c.js
////const a = require("[|./a|]");
////const a = require("./[|a|]");
////const global = require("/*global*/global");

verify.noErrors();
goTo.eachRange(range => {
Expand All @@ -24,3 +28,6 @@ goTo.eachRange(range => {
const kind = target === "dir" ? "directory" : "module";
verify.renameInfoSucceeded(/*displayName*/ name, /*fullDisplayName*/ name, /*kind*/ kind, /*kindModifiers*/ "", /*fileToRename*/ name, range);
});

goTo.marker("global");
verify.renameInfoFailed("You cannot rename a module via a global import.");