mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-12 21:37:41 -06:00
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
This commit is contained in:
parent
d173bca569
commit
8830d7691e
@ -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;
|
||||
@ -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)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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]);
|
||||
|
||||
@ -2872,6 +2872,7 @@ namespace ts {
|
||||
directoryExists?(directoryName: string): boolean;
|
||||
realpath?(path: string): string;
|
||||
getCurrentDirectory?(): string;
|
||||
getDirectories?(path: string): string[];
|
||||
}
|
||||
|
||||
export interface ResolvedModule {
|
||||
|
||||
@ -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 {
|
||||
|
||||
@ -72,8 +72,13 @@ 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[]
|
||||
*
|
||||
@ -81,9 +86,14 @@ namespace ts {
|
||||
* 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;
|
||||
}
|
||||
|
||||
///
|
||||
@ -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;
|
||||
@ -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 {
|
||||
@ -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);
|
||||
@ -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;
|
||||
|
||||
@ -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.",
|
||||
|
||||
@ -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.",
|
||||
|
||||
@ -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.",
|
||||
|
||||
@ -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'.",
|
||||
|
||||
@ -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.",
|
||||
|
||||
@ -2,7 +2,6 @@
|
||||
|
||||
//// [index.d.ts]
|
||||
|
||||
|
||||
interface $ { x }
|
||||
|
||||
//// [app.ts]
|
||||
|
||||
@ -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))
|
||||
|
||||
|
||||
@ -9,7 +9,6 @@ interface A {
|
||||
}
|
||||
=== /types/lib/index.d.ts ===
|
||||
|
||||
|
||||
interface $ { x }
|
||||
>$ : $
|
||||
>x : any
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
// @traceResolution: true
|
||||
// @declaration: true
|
||||
// @typeRoots: /types
|
||||
|
||||
// @currentDirectory: /
|
||||
|
||||
// @filename: /types/lib/index.d.ts
|
||||
interface $ { x }
|
||||
|
||||
@ -2,6 +2,7 @@
|
||||
// @declaration: true
|
||||
// @typeRoots: /types
|
||||
// @traceResolution: true
|
||||
// @currentDirectory: /
|
||||
|
||||
// @filename: /ref.d.ts
|
||||
export interface $ { x }
|
||||
|
||||
@ -4,6 +4,7 @@
|
||||
// @traceResolution: true
|
||||
// @types: lib
|
||||
// @out: output.js
|
||||
// @currentDirectory: /
|
||||
|
||||
// @filename: /types/lib/index.d.ts
|
||||
|
||||
|
||||
@ -3,6 +3,7 @@
|
||||
// @typeRoots: /types
|
||||
// @traceResolution: true
|
||||
// @out: output.js
|
||||
// @currentDirectory: /
|
||||
|
||||
// @filename: /types/lib/index.d.ts
|
||||
|
||||
|
||||
@ -2,6 +2,7 @@
|
||||
// @declaration: true
|
||||
// @typeRoots: /types
|
||||
// @traceResolution: true
|
||||
// @currentDirectory: /
|
||||
|
||||
// @filename: /ref.d.ts
|
||||
export interface $ { x }
|
||||
|
||||
@ -3,6 +3,7 @@
|
||||
// @declaration: true
|
||||
// @typeRoots: /types
|
||||
// @types: lib
|
||||
// @currentDirectory: /
|
||||
|
||||
// @filename: /types/lib/index.d.ts
|
||||
interface $ { x }
|
||||
|
||||
@ -2,6 +2,7 @@
|
||||
// @declaration: true
|
||||
// @typeRoots: /types
|
||||
// @traceResolution: true
|
||||
// @currentDirectory: /
|
||||
|
||||
// $ comes from d.ts file - no need to add type reference directive
|
||||
|
||||
|
||||
@ -2,6 +2,7 @@
|
||||
// @traceResolution: true
|
||||
// @declaration: true
|
||||
// @typeRoots: /types
|
||||
// @currentDirectory: /
|
||||
|
||||
// $ comes from d.ts file - no need to add type reference directive
|
||||
|
||||
|
||||
@ -2,6 +2,7 @@
|
||||
// @traceResolution: true
|
||||
// @declaration: true
|
||||
// @typeRoots: /types
|
||||
// @currentDirectory: /
|
||||
|
||||
// @filename: /ref.d.ts
|
||||
export interface $ { x }
|
||||
|
||||
@ -2,6 +2,7 @@
|
||||
// @traceResolution: true
|
||||
// @declaration: true
|
||||
// @typeRoots: /types
|
||||
// @currentDirectory: /
|
||||
|
||||
// $ comes from type declaration file - type reference directive should be added
|
||||
|
||||
|
||||
@ -2,6 +2,7 @@
|
||||
// @traceResolution: true
|
||||
// @declaration: true
|
||||
// @typeRoots: /types
|
||||
// @currentDirectory: /
|
||||
|
||||
// local value shadows global - no need to add type reference directive
|
||||
|
||||
|
||||
@ -3,6 +3,7 @@
|
||||
// @typeRoots: /types
|
||||
// @traceResolution: true
|
||||
// @types: lib
|
||||
// @currentDirectory: /
|
||||
|
||||
// @filename: /types/lib/index.d.ts
|
||||
|
||||
|
||||
@ -2,6 +2,7 @@
|
||||
// @declaration: true
|
||||
// @typeRoots: /types
|
||||
// @traceResolution: true
|
||||
// @currentDirectory: /
|
||||
|
||||
// @filename: /types/lib/index.d.ts
|
||||
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
// @noImplicitReferences: true
|
||||
// @traceResolution: true
|
||||
// @currentDirectory: /
|
||||
|
||||
// load type declarations from types section of tsconfig
|
||||
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
// @traceResolution: true
|
||||
// @noImplicitReferences: true
|
||||
// @currentDirectory: /
|
||||
|
||||
// @filename: /tsconfig.json
|
||||
{ "files": "a.ts" }
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user