Skip to content

Commit 567b10d

Browse files
authored
Merge pull request #15545 from aozgaa/atTypesPrefixAndImportSuffix
At types prefix and import suffix
2 parents 1f8cf31 + 7282b9f commit 567b10d

File tree

6 files changed

+51
-13
lines changed

6 files changed

+51
-13
lines changed

src/compiler/core.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1783,7 +1783,8 @@ namespace ts {
17831783
return path.length > extension.length && endsWith(path, extension);
17841784
}
17851785

1786-
export function fileExtensionIsAny(path: string, extensions: string[]): boolean {
1786+
/* @internal */
1787+
export function fileExtensionIsOneOf(path: string, extensions: string[]): boolean {
17871788
for (const extension of extensions) {
17881789
if (fileExtensionIs(path, extension)) {
17891790
return true;
@@ -1983,7 +1984,7 @@ namespace ts {
19831984
for (const current of files) {
19841985
const name = combinePaths(path, current);
19851986
const absoluteName = combinePaths(absolutePath, current);
1986-
if (extensions && !fileExtensionIsAny(name, extensions)) continue;
1987+
if (extensions && !fileExtensionIsOneOf(name, extensions)) continue;
19871988
if (excludeRegex && excludeRegex.test(absoluteName)) continue;
19881989
if (!includeFileRegexes) {
19891990
results[0].push(name);

src/compiler/moduleNameResolver.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -971,10 +971,13 @@ namespace ts {
971971
}
972972
}
973973

974+
/** Double underscores are used in DefinitelyTyped to delimit scoped packages. */
975+
const mangledScopedPackageSeparator = "__";
976+
974977
/** For a scoped package, we must look in `@types/foo__bar` instead of `@types/@foo/bar`. */
975978
function mangleScopedPackage(moduleName: string, state: ModuleResolutionState): string {
976979
if (startsWith(moduleName, "@")) {
977-
const replaceSlash = moduleName.replace(ts.directorySeparator, "__");
980+
const replaceSlash = moduleName.replace(ts.directorySeparator, mangledScopedPackageSeparator);
978981
if (replaceSlash !== moduleName) {
979982
const mangled = replaceSlash.slice(1); // Take off the "@"
980983
if (state.traceEnabled) {
@@ -986,6 +989,17 @@ namespace ts {
986989
return moduleName;
987990
}
988991

992+
/* @internal */
993+
export function getPackageNameFromAtTypesDirectory(mangledName: string): string {
994+
const withoutAtTypePrefix = removePrefix(mangledName, "@types/");
995+
if (withoutAtTypePrefix !== mangledName) {
996+
return withoutAtTypePrefix.indexOf("__") !== -1 ?
997+
"@" + withoutAtTypePrefix.replace(mangledScopedPackageSeparator, ts.directorySeparator) :
998+
withoutAtTypePrefix;
999+
}
1000+
return mangledName;
1001+
}
1002+
9891003
function tryFindNonRelativeModuleNameInCache(cache: PerModuleNameCache | undefined, moduleName: string, containingDirectory: string, traceEnabled: boolean, host: ModuleResolutionHost): SearchResult<Resolved> {
9901004
const result = cache && cache.get(containingDirectory);
9911005
if (result) {

src/services/codefixes/importFixes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ namespace ts.codefix {
523523
catch (e) { }
524524
}
525525

526-
return relativeFileName;
526+
return getPackageNameFromAtTypesDirectory(relativeFileName);
527527
}
528528
}
529529

src/services/pathCompletions.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -245,41 +245,38 @@ namespace ts.Completions.PathCompletions {
245245

246246
// Get modules that the type checker picked up
247247
const ambientModules = map(typeChecker.getAmbientModules(), sym => stripQuotes(sym.name));
248-
let nonRelativeModules = filter(ambientModules, moduleName => startsWith(moduleName, fragment));
248+
let nonRelativeModuleNames = filter(ambientModules, moduleName => startsWith(moduleName, fragment));
249249

250250
// Nested modules of the form "module-name/sub" need to be adjusted to only return the string
251251
// after the last '/' that appears in the fragment because that's where the replacement span
252252
// starts
253253
if (isNestedModule) {
254254
const moduleNameWithSeperator = ensureTrailingDirectorySeparator(moduleNameFragment);
255-
nonRelativeModules = map(nonRelativeModules, moduleName => {
256-
if (startsWith(fragment, moduleNameWithSeperator)) {
257-
return moduleName.substr(moduleNameWithSeperator.length);
258-
}
259-
return moduleName;
255+
nonRelativeModuleNames = map(nonRelativeModuleNames, nonRelativeModuleName => {
256+
return removePrefix(nonRelativeModuleName, moduleNameWithSeperator);
260257
});
261258
}
262259

263260

264261
if (!options.moduleResolution || options.moduleResolution === ModuleResolutionKind.NodeJs) {
265262
for (const visibleModule of enumerateNodeModulesVisibleToScript(host, scriptPath)) {
266263
if (!isNestedModule) {
267-
nonRelativeModules.push(visibleModule.moduleName);
264+
nonRelativeModuleNames.push(visibleModule.moduleName);
268265
}
269266
else if (startsWith(visibleModule.moduleName, moduleNameFragment)) {
270267
const nestedFiles = tryReadDirectory(host, visibleModule.moduleDir, supportedTypeScriptExtensions, /*exclude*/ undefined, /*include*/ ["./*"]);
271268
if (nestedFiles) {
272269
for (let f of nestedFiles) {
273270
f = normalizePath(f);
274271
const nestedModule = removeFileExtension(getBaseFileName(f));
275-
nonRelativeModules.push(nestedModule);
272+
nonRelativeModuleNames.push(nestedModule);
276273
}
277274
}
278275
}
279276
}
280277
}
281278

282-
return deduplicate(nonRelativeModules);
279+
return deduplicate(nonRelativeModuleNames);
283280
}
284281

285282
export function getTripleSlashReferenceCompletion(sourceFile: SourceFile, position: number, compilerOptions: CompilerOptions, host: LanguageServiceHost): CompletionInfo {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
//// [|f1/*0*/();|]
4+
5+
// @Filename: node_modules/@types/myLib/index.d.ts
6+
//// export function f1() {}
7+
//// export var v1 = 5;
8+
9+
verify.importFixAtPosition([
10+
`import { f1 } from "myLib";
11+
12+
f1();`
13+
]);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
//// [|f1/*0*/();|]
4+
5+
// @Filename: node_modules/@types/myLib__scoped/index.d.ts
6+
//// export function f1() {}
7+
//// export var v1 = 5;
8+
9+
verify.importFixAtPosition([
10+
`import { f1 } from "@myLib/scoped";
11+
12+
f1();`
13+
]);

0 commit comments

Comments
 (0)