Skip to content

Port PR#9867 to Release-2.0 #10147

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Aug 5, 2016
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
19 changes: 9 additions & 10 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1066,19 +1066,15 @@ namespace ts {
return resolutions;
}

function getInferredTypesRoot(options: CompilerOptions, rootFiles: string[], host: CompilerHost) {
return computeCommonSourceDirectoryOfFilenames(rootFiles, host.getCurrentDirectory(), f => host.getCanonicalFileName(f));
}

/**
* Given a set of options and a set of root files, returns the set of type directive names
* Given a set of options, returns the set of type directive names
* that should be included for this program automatically.
* This list could either come from the config file,
* or from enumerating the types root + initial secondary types lookup location.
* More type directives might appear in the program later as a result of loading actual source files;
* this list is only the set of defaults that are implicitly included.
*/
export function getAutomaticTypeDirectiveNames(options: CompilerOptions, rootFiles: string[], host: CompilerHost): string[] {
export function getAutomaticTypeDirectiveNames(options: CompilerOptions, host: ModuleResolutionHost): string[] {
// Use explicit type list from tsconfig.json
if (options.types) {
return options.types;
Expand All @@ -1091,7 +1087,10 @@ namespace ts {
if (typeRoots) {
for (const root of typeRoots) {
if (host.directoryExists(root)) {
result = result.concat(host.getDirectories(root));
for (const typeDirectivePath of host.getDirectories(root)) {
// Return just the type directive names
result = result.concat(getBaseFileName(normalizePath(typeDirectivePath)));
}
}
}
}
Expand Down Expand Up @@ -1166,11 +1165,11 @@ namespace ts {
forEach(rootNames, name => processRootFile(name, /*isDefaultLib*/ false));

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

if (typeReferences) {
const inferredRoot = getInferredTypesRoot(options, rootNames, host);
const containingFilename = combinePaths(inferredRoot, "__inferred type names__.ts");
// This containingFilename needs to match with the one used in managed-side
const containingFilename = combinePaths(host.getCurrentDirectory(), "__inferred type names__.ts");
const resolutions = resolveTypeReferenceDirectiveNamesWorker(typeReferences, containingFilename);
for (let i = 0; i < typeReferences.length; i++) {
processTypeReferenceDirective(typeReferences[i], resolutions[i]);
Expand Down
1 change: 1 addition & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2872,6 +2872,7 @@ namespace ts {
directoryExists?(directoryName: string): boolean;
realpath?(path: string): string;
getCurrentDirectory?(): string;
getDirectories?(path: string): string[];
}

export interface ResolvedModule {
Expand Down
2 changes: 1 addition & 1 deletion src/services/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1794,7 +1794,7 @@ namespace ts {
};
}

// Cache host information about scrip Should be refreshed
// Cache host information about script should be refreshed
// at each language service public entry point, since we don't know when
// set of scripts handled by the host changes.
class HostCache {
Expand Down
35 changes: 30 additions & 5 deletions src/services/shims.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,28 @@ namespace ts {
directoryExists(directoryName: string): boolean;
}

/** Public interface of the the of a config service shim instance.*/
export interface CoreServicesShimHost extends Logger, ModuleResolutionHost {
/** Public interface of the core-services host instance used in managed side */
export interface CoreServicesShimHost extends Logger {
directoryExists(directoryName: string): boolean;
fileExists(fileName: string): boolean;
getCurrentDirectory(): string;
getDirectories(path: string): string;

/**
* Returns a JSON-encoded value of the type: string[]
*
* @param exclude A JSON encoded string[] containing the paths to exclude
* when enumerating the directory.
*/
readDirectory(rootDir: string, extension: string, basePaths?: string, excludeEx?: string, includeFileEx?: string, includeDirEx?: string, depth?: number): string;
useCaseSensitiveFileNames?(): boolean;
getCurrentDirectory(): string;

/**
* 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
*/
readFile(fileName: string): string;
realpath?(path: string): string;
trace(s: string): void;
useCaseSensitiveFileNames?(): boolean;
}

///
Expand Down Expand Up @@ -240,6 +250,7 @@ namespace ts {
}

export interface CoreServicesShim extends Shim {
getAutomaticTypeDirectiveNames(compilerOptionsJson: string): string;
getPreProcessedFileInfo(fileName: string, sourceText: IScriptSnapshot): string;
getTSConfigFileInfo(fileName: string, sourceText: IScriptSnapshot): string;
getDefaultCompilationSettings(): string;
Expand Down Expand Up @@ -492,6 +503,10 @@ namespace ts {
private readDirectoryFallback(rootDir: string, extension: string, exclude: string[]) {
return JSON.parse(this.shimHost.readDirectory(rootDir, extension, JSON.stringify(exclude)));
}

public getDirectories(path: string): string[] {
return JSON.parse(this.shimHost.getDirectories(path));
}
}

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

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

public getAutomaticTypeDirectiveNames(compilerOptionsJson: string): string {
return this.forwardJSONCall(
`getAutomaticTypeDirectiveNames('${compilerOptionsJson}')`,
() => {
const compilerOptions = <CompilerOptions>JSON.parse(compilerOptionsJson);
return getAutomaticTypeDirectiveNames(compilerOptions, this.host);
}
);
}

private convertFileReferences(refs: FileReference[]): IFileReference[] {
if (!refs) {
return undefined;
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/library-reference-13.trace.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[
"======== Resolving type reference directive 'jquery', containing file '/a/b/__inferred type names__.ts', root directory '/a/types'. ========",
"======== Resolving type reference directive 'jquery', containing file '/__inferred type names__.ts', root directory '/a/types'. ========",
"Resolving with primary search path '/a/types'",
"File '/a/types/jquery/package.json' does not exist.",
"File '/a/types/jquery/index.d.ts' exist - use it as a name resolution result.",
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/library-reference-14.trace.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[
"======== Resolving type reference directive 'jquery', containing file '/a/b/__inferred type names__.ts', root directory '/a/types'. ========",
"======== Resolving type reference directive 'jquery', containing file '/a/__inferred type names__.ts', root directory '/a/types'. ========",
"Resolving with primary search path '/a/types'",
"File '/a/types/jquery/package.json' does not exist.",
"File '/a/types/jquery/index.d.ts' exist - use it as a name resolution result.",
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/library-reference-15.trace.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[
"======== Resolving type reference directive 'jquery', containing file '/a/b/__inferred type names__.ts', root directory 'types'. ========",
"======== Resolving type reference directive 'jquery', containing file '/a/__inferred type names__.ts', root directory 'types'. ========",
"Resolving with primary search path 'types'",
"File 'types/jquery/package.json' does not exist.",
"File 'types/jquery/index.d.ts' exist - use it as a name resolution result.",
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/library-reference-2.trace.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"'package.json' has 'types' field 'jquery.d.ts' that references '/types/jquery/jquery.d.ts'.",
"File '/types/jquery/jquery.d.ts' exist - use it as a name resolution result.",
"======== Type reference directive 'jquery' was successfully resolved to '/types/jquery/jquery.d.ts', primary: true. ========",
"======== Resolving type reference directive 'jquery', containing file '/__inferred type names__.ts', root directory '/types'. ========",
"======== Resolving type reference directive 'jquery', containing file 'test/__inferred type names__.ts', root directory '/types'. ========",
"Resolving with primary search path '/types'",
"Found 'package.json' at '/types/jquery/package.json'.",
"'package.json' has 'types' field 'jquery.d.ts' that references '/types/jquery/jquery.d.ts'.",
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/library-reference-6.trace.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"File '/node_modules/@types/alpha/package.json' does not exist.",
"File '/node_modules/@types/alpha/index.d.ts' exist - use it as a name resolution result.",
"======== Type reference directive 'alpha' was successfully resolved to '/node_modules/@types/alpha/index.d.ts', primary: true. ========",
"======== Resolving type reference directive 'alpha', containing file '/src/__inferred type names__.ts', root directory '/node_modules/@types'. ========",
"======== Resolving type reference directive 'alpha', containing file '/__inferred type names__.ts', root directory '/node_modules/@types'. ========",
"Resolving with primary search path '/node_modules/@types'",
"File '/node_modules/@types/alpha/package.json' does not exist.",
"File '/node_modules/@types/alpha/index.d.ts' exist - use it as a name resolution result.",
Expand Down
1 change: 0 additions & 1 deletion tests/baselines/reference/typeReferenceDirectives1.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

//// [index.d.ts]


interface $ { x }

//// [app.ts]
Expand Down
3 changes: 1 addition & 2 deletions tests/baselines/reference/typeReferenceDirectives1.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ interface A {
}
=== /types/lib/index.d.ts ===


interface $ { x }
>$ : Symbol($, Decl(index.d.ts, 0, 0))
>x : Symbol($.x, Decl(index.d.ts, 2, 13))
>x : Symbol($.x, Decl(index.d.ts, 1, 13))

1 change: 0 additions & 1 deletion tests/baselines/reference/typeReferenceDirectives1.types
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ interface A {
}
=== /types/lib/index.d.ts ===


interface $ { x }
>$ : $
>x : any
Expand Down
2 changes: 1 addition & 1 deletion tests/cases/compiler/typeReferenceDirectives1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// @traceResolution: true
// @declaration: true
// @typeRoots: /types

// @currentDirectory: /

// @filename: /types/lib/index.d.ts
interface $ { x }
Expand Down
1 change: 1 addition & 0 deletions tests/cases/compiler/typeReferenceDirectives10.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// @declaration: true
// @typeRoots: /types
// @traceResolution: true
// @currentDirectory: /

// @filename: /ref.d.ts
export interface $ { x }
Expand Down
1 change: 1 addition & 0 deletions tests/cases/compiler/typeReferenceDirectives11.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// @traceResolution: true
// @types: lib
// @out: output.js
// @currentDirectory: /

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

Expand Down
1 change: 1 addition & 0 deletions tests/cases/compiler/typeReferenceDirectives12.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// @typeRoots: /types
// @traceResolution: true
// @out: output.js
// @currentDirectory: /

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

Expand Down
1 change: 1 addition & 0 deletions tests/cases/compiler/typeReferenceDirectives13.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// @declaration: true
// @typeRoots: /types
// @traceResolution: true
// @currentDirectory: /

// @filename: /ref.d.ts
export interface $ { x }
Expand Down
1 change: 1 addition & 0 deletions tests/cases/compiler/typeReferenceDirectives2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// @declaration: true
// @typeRoots: /types
// @types: lib
// @currentDirectory: /

// @filename: /types/lib/index.d.ts
interface $ { x }
Expand Down
1 change: 1 addition & 0 deletions tests/cases/compiler/typeReferenceDirectives3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// @declaration: true
// @typeRoots: /types
// @traceResolution: true
// @currentDirectory: /

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

Expand Down
1 change: 1 addition & 0 deletions tests/cases/compiler/typeReferenceDirectives4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// @traceResolution: true
// @declaration: true
// @typeRoots: /types
// @currentDirectory: /

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

Expand Down
1 change: 1 addition & 0 deletions tests/cases/compiler/typeReferenceDirectives5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// @traceResolution: true
// @declaration: true
// @typeRoots: /types
// @currentDirectory: /

// @filename: /ref.d.ts
export interface $ { x }
Expand Down
1 change: 1 addition & 0 deletions tests/cases/compiler/typeReferenceDirectives6.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// @traceResolution: true
// @declaration: true
// @typeRoots: /types
// @currentDirectory: /

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

Expand Down
1 change: 1 addition & 0 deletions tests/cases/compiler/typeReferenceDirectives7.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// @traceResolution: true
// @declaration: true
// @typeRoots: /types
// @currentDirectory: /

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

Expand Down
1 change: 1 addition & 0 deletions tests/cases/compiler/typeReferenceDirectives8.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// @typeRoots: /types
// @traceResolution: true
// @types: lib
// @currentDirectory: /

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

Expand Down
1 change: 1 addition & 0 deletions tests/cases/compiler/typeReferenceDirectives9.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// @declaration: true
// @typeRoots: /types
// @traceResolution: true
// @currentDirectory: /

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

Expand Down
1 change: 1 addition & 0 deletions tests/cases/conformance/references/library-reference-13.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @noImplicitReferences: true
// @traceResolution: true
// @currentDirectory: /

// load type declarations from types section of tsconfig

Expand Down
1 change: 1 addition & 0 deletions tests/cases/conformance/typings/typingsLookup1.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @traceResolution: true
// @noImplicitReferences: true
// @currentDirectory: /

// @filename: /tsconfig.json
{ "files": "a.ts" }
Expand Down