refactor(compiler): renames

- `NgHost` to `CompilerHost`
- `AotCompilerHost.resolveFileToImport` to `AotCompilerHost.fileNameToModuleName`
- `AotCompilerHoset.resolveImportToFile` to `AotCompilerHost.moduleNameToFileName`
This commit is contained in:
Tobias Bosch
2016-11-17 12:24:33 -08:00
committed by Chuck Jazdzewski
parent dddbb1c1cb
commit adeea5d86a
15 changed files with 92 additions and 86 deletions

View File

@ -8,7 +8,7 @@
export {AotCompilerHost, AotCompilerHost as StaticReflectorHost, StaticReflector, StaticSymbol} from '@angular/compiler';
export {CodeGenerator} from './src/codegen';
export {CompilerHost, CompilerHostContext, NodeCompilerHostContext} from './src/compiler_host';
export {Extractor} from './src/extractor';
export {NgHost, NgHostContext, NodeNgHostContext} from './src/ng_host';
export * from '@angular/tsc-wrapped';

View File

@ -17,8 +17,8 @@ import {readFileSync} from 'fs';
import * as path from 'path';
import * as ts from 'typescript';
import {NgHost, NgHostContext} from './ng_host';
import {PathMappedNgHost} from './path_mapped_ng_host';
import {CompilerHost, CompilerHostContext} from './compiler_host';
import {PathMappedCompilerHost} from './path_mapped_compiler_host';
import {Console} from './private_import_core';
const GENERATED_FILES = /\.ngfactory\.ts$|\.css\.ts$|\.css\.shim\.ts$/;
@ -37,7 +37,7 @@ export class CodeGenerator {
constructor(
private options: AngularCompilerOptions, private program: ts.Program,
public host: ts.CompilerHost, private staticReflector: compiler.StaticReflector,
private compiler: compiler.AotCompiler, private ngHost: NgHost) {}
private compiler: compiler.AotCompiler, private ngCompilerHost: CompilerHost) {}
// Write codegen in a directory structure matching the sources.
private calculateEmitPath(filePath: string): string {
@ -64,8 +64,8 @@ export class CodeGenerator {
codegen(): Promise<any> {
return this.compiler
.compileAll(
this.program.getSourceFiles().map(sf => this.ngHost.getCanonicalFileName(sf.fileName)))
.compileAll(this.program.getSourceFiles().map(
sf => this.ngCompilerHost.getCanonicalFileName(sf.fileName)))
.then(generatedModules => {
generatedModules.forEach(generatedModule => {
const sourceFile = this.program.getSourceFile(generatedModule.fileUrl);
@ -78,13 +78,13 @@ export class CodeGenerator {
static create(
options: AngularCompilerOptions, cliOptions: NgcCliOptions, program: ts.Program,
compilerHost: ts.CompilerHost, ngHostContext?: NgHostContext,
ngHost?: NgHost): CodeGenerator {
if (!ngHost) {
tsCompilerHost: ts.CompilerHost, compilerHostContext?: CompilerHostContext,
ngCompilerHost?: CompilerHost): CodeGenerator {
if (!ngCompilerHost) {
const usePathMapping = !!options.rootDirs && options.rootDirs.length > 0;
ngHost = usePathMapping ?
new PathMappedNgHost(program, compilerHost, options, ngHostContext) :
new NgHost(program, compilerHost, options, ngHostContext);
ngCompilerHost = usePathMapping ?
new PathMappedCompilerHost(program, tsCompilerHost, options, compilerHostContext) :
new CompilerHost(program, tsCompilerHost, options, compilerHostContext);
}
const transFile = cliOptions.i18nFile;
const locale = cliOptions.locale;
@ -96,7 +96,7 @@ export class CodeGenerator {
}
transContent = readFileSync(transFile, 'utf8');
}
const {compiler: aotCompiler, reflector} = compiler.createAotCompiler(ngHost, {
const {compiler: aotCompiler, reflector} = compiler.createAotCompiler(ngCompilerHost, {
debug: options.debug === true,
translations: transContent,
i18nFormat: cliOptions.i18nFormat,
@ -104,16 +104,17 @@ export class CodeGenerator {
excludeFilePattern: options.generateCodeForLibraries === false ? GENERATED_OR_DTS_FILES :
GENERATED_FILES
});
return new CodeGenerator(options, program, compilerHost, reflector, aotCompiler, ngHost);
return new CodeGenerator(
options, program, tsCompilerHost, reflector, aotCompiler, ngCompilerHost);
}
}
export function extractProgramSymbols(
program: ts.Program, staticReflector: compiler.StaticReflector, ngHost: NgHost,
program: ts.Program, staticReflector: compiler.StaticReflector, compilerHost: CompilerHost,
options: AngularCompilerOptions): compiler.StaticSymbol[] {
return compiler.extractProgramSymbols(
staticReflector, program.getSourceFiles().map(sf => ngHost.getCanonicalFileName(sf.fileName)),
{
staticReflector,
program.getSourceFiles().map(sf => compilerHost.getCanonicalFileName(sf.fileName)), {
excludeFilePattern: options.generateCodeForLibraries === false ? GENERATED_OR_DTS_FILES :
GENERATED_FILES
});

View File

@ -17,7 +17,7 @@ const DTS = /\.d\.ts$/;
const NODE_MODULES = '/node_modules/';
const IS_GENERATED = /\.(ngfactory|css(\.shim)?)$/;
export interface NgHostContext {
export interface CompilerHostContext {
fileExists(fileName: string): boolean;
directoryExists(directoryName: string): boolean;
readFile(fileName: string): string;
@ -25,9 +25,9 @@ export interface NgHostContext {
assumeFileExists(fileName: string): void;
}
export class NgHost implements AotCompilerHost {
export class CompilerHost implements AotCompilerHost {
protected metadataCollector = new MetadataCollector();
protected context: NgHostContext;
protected context: CompilerHostContext;
private isGenDirChildOfRootDir: boolean;
protected basePath: string;
private genDir: string;
@ -35,12 +35,12 @@ export class NgHost implements AotCompilerHost {
constructor(
protected program: ts.Program, protected compilerHost: ts.CompilerHost,
protected options: AngularCompilerOptions, context?: NgHostContext) {
protected options: AngularCompilerOptions, context?: CompilerHostContext) {
// normalize the path so that it never ends with '/'.
this.basePath = path.normalize(path.join(this.options.basePath, '.')).replace(/\\/g, '/');
this.genDir = path.normalize(path.join(this.options.genDir, '.')).replace(/\\/g, '/');
this.context = context || new NodeNgHostContext(compilerHost);
this.context = context || new NodeCompilerHostContext(compilerHost);
const genPath: string = path.relative(this.basePath, this.genDir);
this.isGenDirChildOfRootDir = genPath === '' || !genPath.startsWith('..');
}
@ -48,7 +48,7 @@ export class NgHost implements AotCompilerHost {
// We use absolute paths on disk as canonical.
getCanonicalFileName(fileName: string): string { return fileName; }
resolveImportToFile(m: string, containingFile: string) {
moduleNameToFileName(m: string, containingFile: string) {
if (!containingFile || !containingFile.length) {
if (m.indexOf('.') === 0) {
throw new Error('Resolution of relative paths requires a containing file.');
@ -78,7 +78,7 @@ export class NgHost implements AotCompilerHost {
*
* NOTE: (*) the relative path is computed depending on `isGenDirChildOfRootDir`.
*/
resolveFileToImport(importedFile: string, containingFile: string): string {
fileNameToModuleName(importedFile: string, containingFile: string): string {
// If a file does not yet exist (because we compile it later), we still need to
// assume it exists it so that the `resolve` method works!
if (!this.compilerHost.fileExists(importedFile)) {
@ -149,6 +149,7 @@ export class NgHost implements AotCompilerHost {
}
throw new Error(`Source file ${filePath} not present in program.`);
}
return sf;
}
getMetadataFor(filePath: string): ModuleMetadata[] {
@ -215,7 +216,7 @@ export class NgHost implements AotCompilerHost {
loadResource(filePath: string): Promise<string> { return this.context.readResource(filePath); }
}
export class NodeNgHostContext implements NgHostContext {
export class NodeCompilerHostContext implements CompilerHostContext {
constructor(private host: ts.CompilerHost) {}
private assumedExists: {[fileName: string]: boolean} = {};

View File

@ -19,18 +19,18 @@ import * as tsc from '@angular/tsc-wrapped';
import * as ts from 'typescript';
import {extractProgramSymbols} from './codegen';
import {NgHost} from './ng_host';
import {CompilerHost} from './compiler_host';
export class Extractor {
constructor(
private options: tsc.AngularCompilerOptions, private program: ts.Program,
public host: ts.CompilerHost, private staticReflector: compiler.StaticReflector,
private messageBundle: compiler.MessageBundle, private ngHost: NgHost,
private messageBundle: compiler.MessageBundle, private compilerHost: CompilerHost,
private metadataResolver: compiler.CompileMetadataResolver) {}
extract(): Promise<compiler.MessageBundle> {
const programSymbols: compiler.StaticSymbol[] =
extractProgramSymbols(this.program, this.staticReflector, this.ngHost, this.options);
extractProgramSymbols(this.program, this.staticReflector, this.compilerHost, this.options);
const {ngModules, files} = compiler.analyzeAndValidateNgModules(
programSymbols, {transitiveModules: true}, this.metadataResolver);
@ -64,13 +64,13 @@ export class Extractor {
static create(
options: tsc.AngularCompilerOptions, translationsFormat: string, program: ts.Program,
compilerHost: ts.CompilerHost, resourceLoader: compiler.ResourceLoader,
ngHost?: NgHost): Extractor {
tsCompilerHost: ts.CompilerHost, resourceLoader: compiler.ResourceLoader,
ngCompilerHost?: CompilerHost): Extractor {
const htmlParser = new compiler.I18NHtmlParser(new compiler.HtmlParser());
const urlResolver: compiler.UrlResolver = compiler.createOfflineCompileUrlResolver();
if (!ngHost) ngHost = new NgHost(program, compilerHost, options);
const staticReflector = new compiler.StaticReflector(ngHost);
if (!ngCompilerHost) ngCompilerHost = new CompilerHost(program, tsCompilerHost, options);
const staticReflector = new compiler.StaticReflector(ngCompilerHost);
compiler.StaticAndDynamicReflectionCapabilities.install(staticReflector);
const config = new compiler.CompilerConfig({
@ -92,6 +92,6 @@ export class Extractor {
const messageBundle = new compiler.MessageBundle(htmlParser, [], {});
return new Extractor(
options, program, compilerHost, staticReflector, messageBundle, ngHost, resolver);
options, program, tsCompilerHost, staticReflector, messageBundle, ngCompilerHost, resolver);
}
}

View File

@ -12,7 +12,7 @@ import * as fs from 'fs';
import * as path from 'path';
import * as ts from 'typescript';
import {NgHost, NgHostContext} from './ng_host';
import {CompilerHost, CompilerHostContext} from './compiler_host';
const EXT = /(\.ts|\.d\.ts|\.js|\.jsx|\.tsx)$/;
const DTS = /\.d\.ts$/;
@ -24,10 +24,10 @@ const DTS = /\.d\.ts$/;
* import. This requires using TS `rootDirs` option and also teaching the module
* loader what to do.
*/
export class PathMappedNgHost extends NgHost {
export class PathMappedCompilerHost extends CompilerHost {
constructor(
program: ts.Program, compilerHost: ts.CompilerHost, options: AngularCompilerOptions,
context?: NgHostContext) {
context?: CompilerHostContext) {
super(program, compilerHost, options, context);
}
@ -42,7 +42,7 @@ export class PathMappedNgHost extends NgHost {
return fileName;
}
resolveImportToFile(m: string, containingFile: string) {
moduleNameToFileName(m: string, containingFile: string) {
if (!containingFile || !containingFile.length) {
if (m.indexOf('.') === 0) {
throw new Error('Resolution of relative paths requires a containing file.');
@ -69,7 +69,7 @@ export class PathMappedNgHost extends NgHost {
* Relativize the paths by checking candidate prefixes of the absolute path, to see if
* they are resolvable by the moduleResolution strategy from the CompilerHost.
*/
resolveFileToImport(importedFile: string, containingFile: string): string {
fileNameToModuleName(importedFile: string, containingFile: string): string {
if (this.options.traceResolution) {
console.log(
'getImportPath from containingFile', containingFile, 'to importedFile', importedFile);
@ -86,7 +86,8 @@ export class PathMappedNgHost extends NgHost {
}
const resolvable = (candidate: string) => {
const resolved = this.getCanonicalFileName(this.resolveImportToFile(candidate, importedFile));
const resolved =
this.getCanonicalFileName(this.moduleNameToFileName(candidate, importedFile));
return resolved && resolved.replace(EXT, '') === importedFile.replace(EXT, '');
};

View File

@ -8,19 +8,19 @@
import * as ts from 'typescript';
import {NgHost} from '../src/ng_host';
import {CompilerHost} from '../src/compiler_host';
import {Directory, Entry, MockCompilerHost, MockContext} from './mocks';
import {Directory, Entry, MockAotContext, MockCompilerHost} from './mocks';
describe('NgHost', () => {
let context: MockContext;
describe('CompilerHost', () => {
let context: MockAotContext;
let host: ts.CompilerHost;
let program: ts.Program;
let hostNestedGenDir: NgHost;
let hostSiblingGenDir: NgHost;
let hostNestedGenDir: CompilerHost;
let hostSiblingGenDir: CompilerHost;
beforeEach(() => {
context = new MockContext('/tmp/src', clone(FILES));
context = new MockAotContext('/tmp/src', clone(FILES));
host = new MockCompilerHost(context);
program = ts.createProgram(
['main.ts'], {
@ -32,7 +32,7 @@ describe('NgHost', () => {
if (errors && errors.length) {
throw new Error('Expected no errors');
}
hostNestedGenDir = new NgHost(
hostNestedGenDir = new CompilerHost(
program, host, {
genDir: '/tmp/project/src/gen/',
basePath: '/tmp/project/src',
@ -42,7 +42,7 @@ describe('NgHost', () => {
trace: false
},
context);
hostSiblingGenDir = new NgHost(
hostSiblingGenDir = new CompilerHost(
program, host, {
genDir: '/tmp/project/gen',
basePath: '/tmp/project/src/',
@ -56,32 +56,32 @@ describe('NgHost', () => {
describe('nestedGenDir', () => {
it('should import node_module from factory', () => {
expect(hostNestedGenDir.resolveFileToImport(
expect(hostNestedGenDir.fileNameToModuleName(
'/tmp/project/node_modules/@angular/core.d.ts',
'/tmp/project/src/gen/my.ngfactory.ts', ))
.toEqual('@angular/core');
});
it('should import factory from factory', () => {
expect(hostNestedGenDir.resolveFileToImport(
expect(hostNestedGenDir.fileNameToModuleName(
'/tmp/project/src/my.other.ngfactory.ts', '/tmp/project/src/my.ngfactory.ts'))
.toEqual('./my.other.ngfactory');
expect(hostNestedGenDir.resolveFileToImport(
expect(hostNestedGenDir.fileNameToModuleName(
'/tmp/project/src/my.other.css.ts', '/tmp/project/src/a/my.ngfactory.ts'))
.toEqual('../my.other.css');
expect(hostNestedGenDir.resolveFileToImport(
expect(hostNestedGenDir.fileNameToModuleName(
'/tmp/project/src/a/my.other.css.shim.ts', '/tmp/project/src/my.ngfactory.ts'))
.toEqual('./a/my.other.css.shim');
});
it('should import application from factory', () => {
expect(hostNestedGenDir.resolveFileToImport(
expect(hostNestedGenDir.fileNameToModuleName(
'/tmp/project/src/my.other.ts', '/tmp/project/src/my.ngfactory.ts'))
.toEqual('../my.other');
expect(hostNestedGenDir.resolveFileToImport(
expect(hostNestedGenDir.fileNameToModuleName(
'/tmp/project/src/my.other.ts', '/tmp/project/src/a/my.ngfactory.ts'))
.toEqual('../../my.other');
expect(hostNestedGenDir.resolveFileToImport(
expect(hostNestedGenDir.fileNameToModuleName(
'/tmp/project/src/a/my.other.ts', '/tmp/project/src/my.ngfactory.ts'))
.toEqual('../a/my.other');
});
@ -89,54 +89,54 @@ describe('NgHost', () => {
describe('siblingGenDir', () => {
it('should import node_module from factory', () => {
expect(hostSiblingGenDir.resolveFileToImport(
expect(hostSiblingGenDir.fileNameToModuleName(
'/tmp/project/node_modules/@angular/core.d.ts',
'/tmp/project/src/gen/my.ngfactory.ts'))
.toEqual('@angular/core');
});
it('should import factory from factory', () => {
expect(hostSiblingGenDir.resolveFileToImport(
expect(hostSiblingGenDir.fileNameToModuleName(
'/tmp/project/src/my.other.ngfactory.ts', '/tmp/project/src/my.ngfactory.ts'))
.toEqual('./my.other.ngfactory');
expect(hostSiblingGenDir.resolveFileToImport(
expect(hostSiblingGenDir.fileNameToModuleName(
'/tmp/project/src/my.other.css.ts', '/tmp/project/src/a/my.ngfactory.ts'))
.toEqual('../my.other.css');
expect(hostSiblingGenDir.resolveFileToImport(
expect(hostSiblingGenDir.fileNameToModuleName(
'/tmp/project/src/a/my.other.css.shim.ts', '/tmp/project/src/my.ngfactory.ts'))
.toEqual('./a/my.other.css.shim');
});
it('should import application from factory', () => {
expect(hostSiblingGenDir.resolveFileToImport(
expect(hostSiblingGenDir.fileNameToModuleName(
'/tmp/project/src/my.other.ts', '/tmp/project/src/my.ngfactory.ts'))
.toEqual('./my.other');
expect(hostSiblingGenDir.resolveFileToImport(
expect(hostSiblingGenDir.fileNameToModuleName(
'/tmp/project/src/my.other.ts', '/tmp/project/src/a/my.ngfactory.ts'))
.toEqual('../my.other');
expect(hostSiblingGenDir.resolveFileToImport(
expect(hostSiblingGenDir.fileNameToModuleName(
'/tmp/project/src/a/my.other.ts', '/tmp/project/src/my.ngfactory.ts'))
.toEqual('./a/my.other');
});
});
it('should be able to produce an import from main @angular/core', () => {
expect(hostNestedGenDir.resolveFileToImport(
expect(hostNestedGenDir.fileNameToModuleName(
'/tmp/project/node_modules/@angular/core.d.ts', '/tmp/project/src/main.ts'))
.toEqual('@angular/core');
});
it('should be able to produce an import from main to a sub-directory', () => {
expect(hostNestedGenDir.resolveFileToImport('lib/utils.ts', 'main.ts')).toEqual('./lib/utils');
expect(hostNestedGenDir.fileNameToModuleName('lib/utils.ts', 'main.ts')).toEqual('./lib/utils');
});
it('should be able to produce an import from to a peer file', () => {
expect(hostNestedGenDir.resolveFileToImport('lib/collections.ts', 'lib/utils.ts'))
expect(hostNestedGenDir.fileNameToModuleName('lib/collections.ts', 'lib/utils.ts'))
.toEqual('./collections');
});
it('should be able to produce an import from to a sibling directory', () => {
expect(hostNestedGenDir.resolveFileToImport('lib/utils.ts', 'lib2/utils2.ts'))
expect(hostNestedGenDir.fileNameToModuleName('lib/utils.ts', 'lib2/utils2.ts'))
.toEqual('../lib/utils');
});

View File

@ -6,14 +6,14 @@
* found in the LICENSE file at https://angular.io/license
*/
import {NgHostContext} from '@angular/compiler-cli/src/ng_host';
import {CompilerHostContext} from '@angular/compiler-cli/src/compiler_host';
import * as ts from 'typescript';
export type Entry = string | Directory;
export interface Directory { [name: string]: Entry; }
export class MockContext implements NgHostContext {
export class MockAotContext implements CompilerHostContext {
constructor(public currentDirectory: string, private files: Entry) {}
fileExists(fileName: string): boolean { return typeof this.getEntry(fileName) === 'string'; }
@ -97,7 +97,7 @@ function normalize(parts: string[]): string[] {
}
export class MockCompilerHost implements ts.CompilerHost {
constructor(private context: MockContext) {}
constructor(private context: MockAotContext) {}
fileExists(fileName: string): boolean { return this.context.fileExists(fileName); }