Skip to content

Commit 8830d76

Browse files
authored
Port PR#9867 to Release-2.0 (#10147)
* Change the shape of the shim layer to support getAutomaticTypeDirectives * Change the key for looking up automatic type-directives * Update baselines from change look-up name of type-directives * Add @CurrentDirectory into the test * Update baselines * Fix linting error * Address PR: fix spelling mistake * Instead of return path of the type directive names just return type directive names
1 parent d173bca commit 8830d76

27 files changed

+62
-26
lines changed

src/compiler/program.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,19 +1066,15 @@ namespace ts {
10661066
return resolutions;
10671067
}
10681068

1069-
function getInferredTypesRoot(options: CompilerOptions, rootFiles: string[], host: CompilerHost) {
1070-
return computeCommonSourceDirectoryOfFilenames(rootFiles, host.getCurrentDirectory(), f => host.getCanonicalFileName(f));
1071-
}
1072-
10731069
/**
1074-
* Given a set of options and a set of root files, returns the set of type directive names
1070+
* Given a set of options, returns the set of type directive names
10751071
* that should be included for this program automatically.
10761072
* This list could either come from the config file,
10771073
* or from enumerating the types root + initial secondary types lookup location.
10781074
* More type directives might appear in the program later as a result of loading actual source files;
10791075
* this list is only the set of defaults that are implicitly included.
10801076
*/
1081-
export function getAutomaticTypeDirectiveNames(options: CompilerOptions, rootFiles: string[], host: CompilerHost): string[] {
1077+
export function getAutomaticTypeDirectiveNames(options: CompilerOptions, host: ModuleResolutionHost): string[] {
10821078
// Use explicit type list from tsconfig.json
10831079
if (options.types) {
10841080
return options.types;
@@ -1091,7 +1087,10 @@ namespace ts {
10911087
if (typeRoots) {
10921088
for (const root of typeRoots) {
10931089
if (host.directoryExists(root)) {
1094-
result = result.concat(host.getDirectories(root));
1090+
for (const typeDirectivePath of host.getDirectories(root)) {
1091+
// Return just the type directive names
1092+
result = result.concat(getBaseFileName(normalizePath(typeDirectivePath)));
1093+
}
10951094
}
10961095
}
10971096
}
@@ -1166,11 +1165,11 @@ namespace ts {
11661165
forEach(rootNames, name => processRootFile(name, /*isDefaultLib*/ false));
11671166

11681167
// load type declarations specified via 'types' argument or implicitly from types/ and node_modules/@types folders
1169-
const typeReferences: string[] = getAutomaticTypeDirectiveNames(options, rootNames, host);
1168+
const typeReferences: string[] = getAutomaticTypeDirectiveNames(options, host);
11701169

11711170
if (typeReferences) {
1172-
const inferredRoot = getInferredTypesRoot(options, rootNames, host);
1173-
const containingFilename = combinePaths(inferredRoot, "__inferred type names__.ts");
1171+
// This containingFilename needs to match with the one used in managed-side
1172+
const containingFilename = combinePaths(host.getCurrentDirectory(), "__inferred type names__.ts");
11741173
const resolutions = resolveTypeReferenceDirectiveNamesWorker(typeReferences, containingFilename);
11751174
for (let i = 0; i < typeReferences.length; i++) {
11761175
processTypeReferenceDirective(typeReferences[i], resolutions[i]);

src/compiler/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2872,6 +2872,7 @@ namespace ts {
28722872
directoryExists?(directoryName: string): boolean;
28732873
realpath?(path: string): string;
28742874
getCurrentDirectory?(): string;
2875+
getDirectories?(path: string): string[];
28752876
}
28762877

28772878
export interface ResolvedModule {

src/services/services.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1794,7 +1794,7 @@ namespace ts {
17941794
};
17951795
}
17961796

1797-
// Cache host information about scrip Should be refreshed
1797+
// Cache host information about script should be refreshed
17981798
// at each language service public entry point, since we don't know when
17991799
// set of scripts handled by the host changes.
18001800
class HostCache {

src/services/shims.ts

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,18 +72,28 @@ namespace ts {
7272
directoryExists(directoryName: string): boolean;
7373
}
7474

75-
/** Public interface of the the of a config service shim instance.*/
76-
export interface CoreServicesShimHost extends Logger, ModuleResolutionHost {
75+
/** Public interface of the core-services host instance used in managed side */
76+
export interface CoreServicesShimHost extends Logger {
77+
directoryExists(directoryName: string): boolean;
78+
fileExists(fileName: string): boolean;
79+
getCurrentDirectory(): string;
80+
getDirectories(path: string): string;
81+
7782
/**
7883
* Returns a JSON-encoded value of the type: string[]
7984
*
8085
* @param exclude A JSON encoded string[] containing the paths to exclude
8186
* when enumerating the directory.
8287
*/
8388
readDirectory(rootDir: string, extension: string, basePaths?: string, excludeEx?: string, includeFileEx?: string, includeDirEx?: string, depth?: number): string;
84-
useCaseSensitiveFileNames?(): boolean;
85-
getCurrentDirectory(): string;
89+
90+
/**
91+
* Read arbitary text files on disk, i.e. when resolution procedure needs the content of 'package.json' to determine location of bundled typings for node modules
92+
*/
93+
readFile(fileName: string): string;
94+
realpath?(path: string): string;
8695
trace(s: string): void;
96+
useCaseSensitiveFileNames?(): boolean;
8797
}
8898

8999
///
@@ -240,6 +250,7 @@ namespace ts {
240250
}
241251

242252
export interface CoreServicesShim extends Shim {
253+
getAutomaticTypeDirectiveNames(compilerOptionsJson: string): string;
243254
getPreProcessedFileInfo(fileName: string, sourceText: IScriptSnapshot): string;
244255
getTSConfigFileInfo(fileName: string, sourceText: IScriptSnapshot): string;
245256
getDefaultCompilationSettings(): string;
@@ -492,6 +503,10 @@ namespace ts {
492503
private readDirectoryFallback(rootDir: string, extension: string, exclude: string[]) {
493504
return JSON.parse(this.shimHost.readDirectory(rootDir, extension, JSON.stringify(exclude)));
494505
}
506+
507+
public getDirectories(path: string): string[] {
508+
return JSON.parse(this.shimHost.getDirectories(path));
509+
}
495510
}
496511

497512
function simpleForwardCall(logger: Logger, actionDescription: string, action: () => any, logPerformance: boolean): any {
@@ -1003,7 +1018,7 @@ namespace ts {
10031018

10041019
public getPreProcessedFileInfo(fileName: string, sourceTextSnapshot: IScriptSnapshot): string {
10051020
return this.forwardJSONCall(
1006-
"getPreProcessedFileInfo('" + fileName + "')",
1021+
`getPreProcessedFileInfo('${fileName}')`,
10071022
() => {
10081023
// for now treat files as JavaScript
10091024
const result = preProcessFile(sourceTextSnapshot.getText(0, sourceTextSnapshot.getLength()), /* readImportFiles */ true, /* detectJavaScriptImports */ true);
@@ -1017,6 +1032,16 @@ namespace ts {
10171032
});
10181033
}
10191034

1035+
public getAutomaticTypeDirectiveNames(compilerOptionsJson: string): string {
1036+
return this.forwardJSONCall(
1037+
`getAutomaticTypeDirectiveNames('${compilerOptionsJson}')`,
1038+
() => {
1039+
const compilerOptions = <CompilerOptions>JSON.parse(compilerOptionsJson);
1040+
return getAutomaticTypeDirectiveNames(compilerOptions, this.host);
1041+
}
1042+
);
1043+
}
1044+
10201045
private convertFileReferences(refs: FileReference[]): IFileReference[] {
10211046
if (!refs) {
10221047
return undefined;

tests/baselines/reference/library-reference-13.trace.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[
2-
"======== Resolving type reference directive 'jquery', containing file '/a/b/__inferred type names__.ts', root directory '/a/types'. ========",
2+
"======== Resolving type reference directive 'jquery', containing file '/__inferred type names__.ts', root directory '/a/types'. ========",
33
"Resolving with primary search path '/a/types'",
44
"File '/a/types/jquery/package.json' does not exist.",
55
"File '/a/types/jquery/index.d.ts' exist - use it as a name resolution result.",

tests/baselines/reference/library-reference-14.trace.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[
2-
"======== Resolving type reference directive 'jquery', containing file '/a/b/__inferred type names__.ts', root directory '/a/types'. ========",
2+
"======== Resolving type reference directive 'jquery', containing file '/a/__inferred type names__.ts', root directory '/a/types'. ========",
33
"Resolving with primary search path '/a/types'",
44
"File '/a/types/jquery/package.json' does not exist.",
55
"File '/a/types/jquery/index.d.ts' exist - use it as a name resolution result.",

tests/baselines/reference/library-reference-15.trace.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[
2-
"======== Resolving type reference directive 'jquery', containing file '/a/b/__inferred type names__.ts', root directory 'types'. ========",
2+
"======== Resolving type reference directive 'jquery', containing file '/a/__inferred type names__.ts', root directory 'types'. ========",
33
"Resolving with primary search path 'types'",
44
"File 'types/jquery/package.json' does not exist.",
55
"File 'types/jquery/index.d.ts' exist - use it as a name resolution result.",

tests/baselines/reference/library-reference-2.trace.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"'package.json' has 'types' field 'jquery.d.ts' that references '/types/jquery/jquery.d.ts'.",
66
"File '/types/jquery/jquery.d.ts' exist - use it as a name resolution result.",
77
"======== Type reference directive 'jquery' was successfully resolved to '/types/jquery/jquery.d.ts', primary: true. ========",
8-
"======== Resolving type reference directive 'jquery', containing file '/__inferred type names__.ts', root directory '/types'. ========",
8+
"======== Resolving type reference directive 'jquery', containing file 'test/__inferred type names__.ts', root directory '/types'. ========",
99
"Resolving with primary search path '/types'",
1010
"Found 'package.json' at '/types/jquery/package.json'.",
1111
"'package.json' has 'types' field 'jquery.d.ts' that references '/types/jquery/jquery.d.ts'.",

tests/baselines/reference/library-reference-6.trace.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"File '/node_modules/@types/alpha/package.json' does not exist.",
55
"File '/node_modules/@types/alpha/index.d.ts' exist - use it as a name resolution result.",
66
"======== Type reference directive 'alpha' was successfully resolved to '/node_modules/@types/alpha/index.d.ts', primary: true. ========",
7-
"======== Resolving type reference directive 'alpha', containing file '/src/__inferred type names__.ts', root directory '/node_modules/@types'. ========",
7+
"======== Resolving type reference directive 'alpha', containing file '/__inferred type names__.ts', root directory '/node_modules/@types'. ========",
88
"Resolving with primary search path '/node_modules/@types'",
99
"File '/node_modules/@types/alpha/package.json' does not exist.",
1010
"File '/node_modules/@types/alpha/index.d.ts' exist - use it as a name resolution result.",

tests/baselines/reference/typeReferenceDirectives1.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
//// [index.d.ts]
44

5-
65
interface $ { x }
76

87
//// [app.ts]

tests/baselines/reference/typeReferenceDirectives1.symbols

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ interface A {
99
}
1010
=== /types/lib/index.d.ts ===
1111

12-
1312
interface $ { x }
1413
>$ : Symbol($, Decl(index.d.ts, 0, 0))
15-
>x : Symbol($.x, Decl(index.d.ts, 2, 13))
14+
>x : Symbol($.x, Decl(index.d.ts, 1, 13))
1615

tests/baselines/reference/typeReferenceDirectives1.types

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ interface A {
99
}
1010
=== /types/lib/index.d.ts ===
1111

12-
1312
interface $ { x }
1413
>$ : $
1514
>x : any

tests/cases/compiler/typeReferenceDirectives1.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// @traceResolution: true
33
// @declaration: true
44
// @typeRoots: /types
5-
5+
// @currentDirectory: /
66

77
// @filename: /types/lib/index.d.ts
88
interface $ { x }

tests/cases/compiler/typeReferenceDirectives10.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// @declaration: true
33
// @typeRoots: /types
44
// @traceResolution: true
5+
// @currentDirectory: /
56

67
// @filename: /ref.d.ts
78
export interface $ { x }

tests/cases/compiler/typeReferenceDirectives11.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// @traceResolution: true
55
// @types: lib
66
// @out: output.js
7+
// @currentDirectory: /
78

89
// @filename: /types/lib/index.d.ts
910

tests/cases/compiler/typeReferenceDirectives12.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// @typeRoots: /types
44
// @traceResolution: true
55
// @out: output.js
6+
// @currentDirectory: /
67

78
// @filename: /types/lib/index.d.ts
89

tests/cases/compiler/typeReferenceDirectives13.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// @declaration: true
33
// @typeRoots: /types
44
// @traceResolution: true
5+
// @currentDirectory: /
56

67
// @filename: /ref.d.ts
78
export interface $ { x }

tests/cases/compiler/typeReferenceDirectives2.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// @declaration: true
44
// @typeRoots: /types
55
// @types: lib
6+
// @currentDirectory: /
67

78
// @filename: /types/lib/index.d.ts
89
interface $ { x }

tests/cases/compiler/typeReferenceDirectives3.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// @declaration: true
33
// @typeRoots: /types
44
// @traceResolution: true
5+
// @currentDirectory: /
56

67
// $ comes from d.ts file - no need to add type reference directive
78

tests/cases/compiler/typeReferenceDirectives4.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// @traceResolution: true
33
// @declaration: true
44
// @typeRoots: /types
5+
// @currentDirectory: /
56

67
// $ comes from d.ts file - no need to add type reference directive
78

tests/cases/compiler/typeReferenceDirectives5.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// @traceResolution: true
33
// @declaration: true
44
// @typeRoots: /types
5+
// @currentDirectory: /
56

67
// @filename: /ref.d.ts
78
export interface $ { x }

tests/cases/compiler/typeReferenceDirectives6.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// @traceResolution: true
33
// @declaration: true
44
// @typeRoots: /types
5+
// @currentDirectory: /
56

67
// $ comes from type declaration file - type reference directive should be added
78

tests/cases/compiler/typeReferenceDirectives7.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// @traceResolution: true
33
// @declaration: true
44
// @typeRoots: /types
5+
// @currentDirectory: /
56

67
// local value shadows global - no need to add type reference directive
78

tests/cases/compiler/typeReferenceDirectives8.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// @typeRoots: /types
44
// @traceResolution: true
55
// @types: lib
6+
// @currentDirectory: /
67

78
// @filename: /types/lib/index.d.ts
89

tests/cases/compiler/typeReferenceDirectives9.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// @declaration: true
33
// @typeRoots: /types
44
// @traceResolution: true
5+
// @currentDirectory: /
56

67
// @filename: /types/lib/index.d.ts
78

tests/cases/conformance/references/library-reference-13.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// @noImplicitReferences: true
22
// @traceResolution: true
3+
// @currentDirectory: /
34

45
// load type declarations from types section of tsconfig
56

tests/cases/conformance/typings/typingsLookup1.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// @traceResolution: true
22
// @noImplicitReferences: true
3+
// @currentDirectory: /
34

45
// @filename: /tsconfig.json
56
{ "files": "a.ts" }

0 commit comments

Comments
 (0)