Merge pull request #15051 from Microsoft/at_types

Support @types module resolution from scoped packages
This commit is contained in:
Mohamed Hegazy
2017-04-17 10:08:15 -07:00
committed by GitHub
17 changed files with 226 additions and 1 deletions

View File

@@ -3185,6 +3185,10 @@
"category": "Message",
"code": 6181
},
"Scoped package detected, looking in '{0}'": {
"category": "Message",
"code": "6182"
},
"Variable '{0}' implicitly has an '{1}' type.": {
"category": "Error",

View File

@@ -954,10 +954,25 @@ namespace ts {
}
nodeModulesAtTypesExists = false;
}
return loadModuleFromNodeModulesFolder(Extensions.DtsOnly, moduleName, nodeModulesAtTypes, nodeModulesAtTypesExists, failedLookupLocations, state);
return loadModuleFromNodeModulesFolder(Extensions.DtsOnly, mangleScopedPackage(moduleName, state), nodeModulesAtTypes, nodeModulesAtTypesExists, failedLookupLocations, state);
}
}
/** For a scoped package, we must look in `@types/foo__bar` instead of `@types/@foo/bar`. */
function mangleScopedPackage(moduleName: string, state: ModuleResolutionState): string {
if (startsWith(moduleName, "@")) {
const replaceSlash = moduleName.replace(ts.directorySeparator, "__");
if (replaceSlash !== moduleName) {
const mangled = replaceSlash.slice(1); // Take off the "@"
if (state.traceEnabled) {
trace(state.host, Diagnostics.Scoped_package_detected_looking_in_0, mangled);
}
return mangled;
}
}
return moduleName;
}
function tryFindNonRelativeModuleNameInCache(cache: PerModuleNameCache | undefined, moduleName: string, containingDirectory: string, traceEnabled: boolean, host: ModuleResolutionHost): SearchResult<Resolved> {
const result = cache && cache.get(containingDirectory);
if (result) {