mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
Merge pull request #27980 from EECOLOR/leading-slash-imports
match leading slash imports with path mappings - fixes #13730
This commit is contained in:
@@ -730,6 +730,9 @@ namespace ts {
|
||||
function tryLoadModuleUsingOptionalResolutionSettings(extensions: Extensions, moduleName: string, containingDirectory: string, loader: ResolutionKindSpecificLoader,
|
||||
state: ModuleResolutionState): Resolved | undefined {
|
||||
|
||||
const resolved = tryLoadModuleUsingPathsIfEligible(extensions, moduleName, loader, state);
|
||||
if (resolved) return resolved.value;
|
||||
|
||||
if (!isExternalModuleNameRelative(moduleName)) {
|
||||
return tryLoadModuleUsingBaseUrl(extensions, moduleName, loader, state);
|
||||
}
|
||||
@@ -738,6 +741,17 @@ namespace ts {
|
||||
}
|
||||
}
|
||||
|
||||
function tryLoadModuleUsingPathsIfEligible(extensions: Extensions, moduleName: string, loader: ResolutionKindSpecificLoader, state: ModuleResolutionState) {
|
||||
const { baseUrl, paths } = state.compilerOptions;
|
||||
if (baseUrl && paths && !pathIsRelative(moduleName)) {
|
||||
if (state.traceEnabled) {
|
||||
trace(state.host, Diagnostics.baseUrl_option_is_set_to_0_using_this_value_to_resolve_non_relative_module_name_1, baseUrl, moduleName);
|
||||
trace(state.host, Diagnostics.paths_option_is_specified_looking_for_a_pattern_to_match_module_name_0, moduleName);
|
||||
}
|
||||
return tryLoadModuleUsingPaths(extensions, moduleName, baseUrl, paths, loader, /*onlyRecordFailures*/ false, state);
|
||||
}
|
||||
}
|
||||
|
||||
function tryLoadModuleUsingRootDirs(extensions: Extensions, moduleName: string, containingDirectory: string, loader: ResolutionKindSpecificLoader,
|
||||
state: ModuleResolutionState): Resolved | undefined {
|
||||
|
||||
@@ -816,22 +830,13 @@ namespace ts {
|
||||
}
|
||||
|
||||
function tryLoadModuleUsingBaseUrl(extensions: Extensions, moduleName: string, loader: ResolutionKindSpecificLoader, state: ModuleResolutionState): Resolved | undefined {
|
||||
const { baseUrl, paths } = state.compilerOptions;
|
||||
const { baseUrl } = state.compilerOptions;
|
||||
if (!baseUrl) {
|
||||
return undefined;
|
||||
}
|
||||
if (state.traceEnabled) {
|
||||
trace(state.host, Diagnostics.baseUrl_option_is_set_to_0_using_this_value_to_resolve_non_relative_module_name_1, baseUrl, moduleName);
|
||||
}
|
||||
if (paths) {
|
||||
if (state.traceEnabled) {
|
||||
trace(state.host, Diagnostics.paths_option_is_specified_looking_for_a_pattern_to_match_module_name_0, moduleName);
|
||||
}
|
||||
const resolved = tryLoadModuleUsingPaths(extensions, moduleName, baseUrl, paths, loader, /*onlyRecordFailures*/ false, state);
|
||||
if (resolved) {
|
||||
return resolved.value;
|
||||
}
|
||||
}
|
||||
const candidate = normalizePath(combinePaths(baseUrl, moduleName));
|
||||
if (state.traceEnabled) {
|
||||
trace(state.host, Diagnostics.Resolving_module_name_0_relative_to_base_url_1_2, moduleName, baseUrl, candidate);
|
||||
|
||||
@@ -247,19 +247,9 @@ namespace ts.Completions.StringCompletions {
|
||||
const scriptPath = sourceFile.path;
|
||||
const scriptDirectory = getDirectoryPath(scriptPath);
|
||||
|
||||
const extensionOptions = getExtensionOptions(compilerOptions);
|
||||
if (isPathRelativeToScript(literalValue) || isRootedDiskPath(literalValue)) {
|
||||
if (compilerOptions.rootDirs) {
|
||||
return getCompletionEntriesForDirectoryFragmentWithRootDirs(
|
||||
compilerOptions.rootDirs, literalValue, scriptDirectory, extensionOptions, compilerOptions, host, scriptPath);
|
||||
}
|
||||
else {
|
||||
return getCompletionEntriesForDirectoryFragment(literalValue, scriptDirectory, extensionOptions, host, scriptPath);
|
||||
}
|
||||
}
|
||||
else {
|
||||
return getCompletionEntriesForNonRelativeModules(literalValue, scriptDirectory, compilerOptions, host, typeChecker);
|
||||
}
|
||||
return isPathRelativeToScript(literalValue) || !compilerOptions.baseUrl && (isRootedDiskPath(literalValue) || isUrl(literalValue))
|
||||
? getCompletionEntriesForRelativeModules(literalValue, scriptDirectory, compilerOptions, host, scriptPath)
|
||||
: getCompletionEntriesForNonRelativeModules(literalValue, scriptDirectory, compilerOptions, host, typeChecker);
|
||||
}
|
||||
|
||||
interface ExtensionOptions {
|
||||
@@ -269,6 +259,17 @@ namespace ts.Completions.StringCompletions {
|
||||
function getExtensionOptions(compilerOptions: CompilerOptions, includeExtensions = false): ExtensionOptions {
|
||||
return { extensions: getSupportedExtensionsForModuleResolution(compilerOptions), includeExtensions };
|
||||
}
|
||||
function getCompletionEntriesForRelativeModules(literalValue: string, scriptDirectory: string, compilerOptions: CompilerOptions, host: LanguageServiceHost, scriptPath: Path) {
|
||||
const extensionOptions = getExtensionOptions(compilerOptions);
|
||||
if (compilerOptions.rootDirs) {
|
||||
return getCompletionEntriesForDirectoryFragmentWithRootDirs(
|
||||
compilerOptions.rootDirs, literalValue, scriptDirectory, extensionOptions, compilerOptions, host, scriptPath);
|
||||
}
|
||||
else {
|
||||
return getCompletionEntriesForDirectoryFragment(literalValue, scriptDirectory, extensionOptions, host, scriptPath);
|
||||
}
|
||||
}
|
||||
|
||||
function getSupportedExtensionsForModuleResolution(compilerOptions: CompilerOptions): ReadonlyArray<Extension> {
|
||||
const extensions = getSupportedExtensions(compilerOptions);
|
||||
return compilerOptions.resolveJsonModule && getEmitModuleResolutionKind(compilerOptions) === ModuleResolutionKind.NodeJs ?
|
||||
|
||||
@@ -757,6 +757,9 @@ import b = require("./moduleB");
|
||||
],
|
||||
"somefolder/*": [
|
||||
"someanotherfolder/*"
|
||||
],
|
||||
"/rooted/*": [
|
||||
"generated/*"
|
||||
]
|
||||
}
|
||||
};
|
||||
@@ -773,6 +776,7 @@ import b = require("./moduleB");
|
||||
"/root/folder1/file2/index.d.ts",
|
||||
// then first attempt on 'generated/*' was successful
|
||||
]);
|
||||
check("/rooted/folder1/file2", file2, []);
|
||||
check("folder2/file3", file3, [
|
||||
// first try '*'
|
||||
"/root/folder2/file3.ts",
|
||||
@@ -900,6 +904,9 @@ import b = require("./moduleB");
|
||||
],
|
||||
"somefolder/*": [
|
||||
"someanotherfolder/*"
|
||||
],
|
||||
"/rooted/*": [
|
||||
"generated/*"
|
||||
]
|
||||
}
|
||||
};
|
||||
@@ -911,6 +918,7 @@ import b = require("./moduleB");
|
||||
"/root/folder1/file2.d.ts",
|
||||
// success when using 'generated/*'
|
||||
]);
|
||||
check("/rooted/folder1/file2", file2, []);
|
||||
check("folder1/file3", file3, [
|
||||
// first try '*'
|
||||
"/root/folder1/file3.ts",
|
||||
|
||||
Reference in New Issue
Block a user