2016-11-22 12:10:23 -05:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2017-10-20 12:46:41 -04:00
|
|
|
import {StaticSymbolResolverHost} from '@angular/compiler';
|
2019-08-05 20:13:20 -04:00
|
|
|
import {MetadataCollector, MetadataReaderHost, createMetadataReaderCache, readMetadata} from '@angular/compiler-cli/src/language_services';
|
2017-10-20 12:46:41 -04:00
|
|
|
import * as path from 'path';
|
2016-11-22 12:10:23 -05:00
|
|
|
import * as ts from 'typescript';
|
|
|
|
|
2017-10-20 12:46:41 -04:00
|
|
|
class ReflectorModuleModuleResolutionHost implements ts.ModuleResolutionHost, MetadataReaderHost {
|
2019-09-12 12:29:41 -04:00
|
|
|
private readonly metadataCollector = new MetadataCollector({
|
|
|
|
// Note: verboseInvalidExpressions is important so that
|
|
|
|
// the collector will collect errors instead of throwing
|
|
|
|
verboseInvalidExpression: true,
|
|
|
|
});
|
2017-10-20 12:46:41 -04:00
|
|
|
|
2019-09-12 12:29:41 -04:00
|
|
|
readonly directoryExists?: (directoryName: string) => boolean;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
private readonly tsLSHost: ts.LanguageServiceHost,
|
|
|
|
private readonly getProgram: () => ts.Program) {
|
|
|
|
if (tsLSHost.directoryExists) {
|
|
|
|
this.directoryExists = directoryName => tsLSHost.directoryExists !(directoryName);
|
|
|
|
}
|
2016-11-22 12:10:23 -05:00
|
|
|
}
|
|
|
|
|
2019-09-12 12:29:41 -04:00
|
|
|
fileExists(fileName: string): boolean {
|
|
|
|
// TypeScript resolution logic walks through the following sequence in order:
|
|
|
|
// package.json (read "types" field) -> .ts -> .tsx -> .d.ts
|
|
|
|
// For more info, see
|
|
|
|
// https://www.typescriptlang.org/docs/handbook/module-resolution.html
|
|
|
|
// For Angular specifically, we can skip .tsx lookup
|
|
|
|
if (fileName.endsWith('.tsx')) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (this.tsLSHost.fileExists) {
|
|
|
|
return this.tsLSHost.fileExists(fileName);
|
|
|
|
}
|
|
|
|
return !!this.tsLSHost.getScriptSnapshot(fileName);
|
|
|
|
}
|
2016-11-22 12:10:23 -05:00
|
|
|
|
|
|
|
readFile(fileName: string): string {
|
2019-09-12 12:29:41 -04:00
|
|
|
// readFile() is used by TypeScript to read package.json during module
|
|
|
|
// resolution, and it's used by Angular to read metadata.json during
|
|
|
|
// metadata resolution.
|
|
|
|
if (this.tsLSHost.readFile) {
|
|
|
|
return this.tsLSHost.readFile(fileName) !;
|
2016-11-22 12:10:23 -05:00
|
|
|
}
|
2019-09-12 12:29:41 -04:00
|
|
|
// As a fallback, read the JSON files from the editor snapshot.
|
|
|
|
const snapshot = this.tsLSHost.getScriptSnapshot(fileName);
|
|
|
|
if (!snapshot) {
|
|
|
|
// MetadataReaderHost readFile() declaration should be
|
|
|
|
// `readFile(fileName: string): string | undefined`
|
|
|
|
return undefined !;
|
|
|
|
}
|
|
|
|
return snapshot.getText(0, snapshot.getLength());
|
2016-11-22 12:10:23 -05:00
|
|
|
}
|
|
|
|
|
2017-10-20 12:46:41 -04:00
|
|
|
getSourceFileMetadata(fileName: string) {
|
|
|
|
const sf = this.getProgram().getSourceFile(fileName);
|
|
|
|
return sf ? this.metadataCollector.getMetadata(sf) : undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
cacheMetadata(fileName: string) {
|
|
|
|
// Don't cache the metadata for .ts files as they might change in the editor!
|
|
|
|
return fileName.endsWith('.d.ts');
|
|
|
|
}
|
2016-11-22 12:10:23 -05:00
|
|
|
}
|
|
|
|
|
2017-10-20 12:46:41 -04:00
|
|
|
export class ReflectorHost implements StaticSymbolResolverHost {
|
2019-09-04 15:45:48 -04:00
|
|
|
private readonly hostAdapter: ReflectorModuleModuleResolutionHost;
|
|
|
|
private readonly metadataReaderCache = createMetadataReaderCache();
|
|
|
|
private readonly moduleResolutionCache: ts.ModuleResolutionCache;
|
|
|
|
private readonly fakeContainingPath: string;
|
2017-10-20 12:46:41 -04:00
|
|
|
|
2019-09-04 15:45:48 -04:00
|
|
|
constructor(getProgram: () => ts.Program, private readonly tsLSHost: ts.LanguageServiceHost) {
|
|
|
|
// tsLSHost.getCurrentDirectory() returns the directory where tsconfig.json
|
|
|
|
// is located. This is not the same as process.cwd() because the language
|
|
|
|
// service host sets the "project root path" as its current directory.
|
|
|
|
const currentDir = tsLSHost.getCurrentDirectory();
|
|
|
|
this.fakeContainingPath = currentDir ? path.join(currentDir, 'fakeContainingFile.ts') : '';
|
|
|
|
this.hostAdapter = new ReflectorModuleModuleResolutionHost(tsLSHost, getProgram);
|
|
|
|
this.moduleResolutionCache = ts.createModuleResolutionCache(
|
|
|
|
currentDir,
|
|
|
|
s => s, // getCanonicalFileName
|
|
|
|
tsLSHost.getCompilationSettings());
|
2017-10-20 12:46:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
getMetadataFor(modulePath: string): {[key: string]: any}[]|undefined {
|
|
|
|
return readMetadata(modulePath, this.hostAdapter, this.metadataReaderCache);
|
2016-11-22 12:10:23 -05:00
|
|
|
}
|
|
|
|
|
2017-10-20 12:46:41 -04:00
|
|
|
moduleNameToFileName(moduleName: string, containingFile?: string): string|null {
|
|
|
|
if (!containingFile) {
|
2019-08-05 20:13:20 -04:00
|
|
|
if (moduleName.startsWith('.')) {
|
2017-10-20 12:46:41 -04:00
|
|
|
throw new Error('Resolution of relative paths requires a containing file.');
|
|
|
|
}
|
2019-09-04 15:45:48 -04:00
|
|
|
if (!this.fakeContainingPath) {
|
2019-08-05 20:13:20 -04:00
|
|
|
// If current directory is empty then the file must belong to an inferred
|
|
|
|
// project (no tsconfig.json), in which case it's not possible to resolve
|
|
|
|
// the module without the caller explicitly providing a containing file.
|
|
|
|
throw new Error(`Could not resolve '${moduleName}' without a containing file.`);
|
|
|
|
}
|
2019-09-04 15:45:48 -04:00
|
|
|
containingFile = this.fakeContainingPath;
|
2017-10-20 12:46:41 -04:00
|
|
|
}
|
2019-09-04 15:45:48 -04:00
|
|
|
const compilerOptions = this.tsLSHost.getCompilationSettings();
|
|
|
|
const resolved = ts.resolveModuleName(
|
|
|
|
moduleName, containingFile, compilerOptions, this.hostAdapter,
|
|
|
|
this.moduleResolutionCache)
|
|
|
|
.resolvedModule;
|
2017-10-20 12:46:41 -04:00
|
|
|
return resolved ? resolved.resolvedFileName : null;
|
2016-11-22 12:10:23 -05:00
|
|
|
}
|
2018-01-10 12:55:03 -05:00
|
|
|
|
|
|
|
getOutputName(filePath: string) { return filePath; }
|
2016-11-22 12:10:23 -05:00
|
|
|
}
|