refactor: format & lint

This commit is contained in:
Victor Berchet
2016-12-13 17:44:52 -08:00
parent 7256d0ede5
commit a0a05041ac
11 changed files with 274 additions and 238 deletions

View File

@ -1,3 +1,11 @@
/**
* @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
*/
/**
* This is a private API for the ngtools toolkit.
*
@ -5,9 +13,9 @@
* something else.
*/
import * as ts from 'typescript';
import {StaticReflector, AotCompilerHost} from '@angular/compiler';
import {AotCompilerHost, StaticReflector} from '@angular/compiler';
import {AngularCompilerOptions, NgcCliOptions} from '@angular/tsc-wrapped';
import * as ts from 'typescript';
import {CodeGenerator} from './codegen';
import {CompilerHost, CompilerHostContext, ModuleResolutionHostAdapter} from './compiler_host';
@ -43,9 +51,7 @@ export interface NgTools_InternalApi_NG2_ListLazyRoutes_Options {
}
export interface NgTools_InternalApi_NG_2_LazyRouteMap {
[route: string]: string;
}
export interface NgTools_InternalApi_NG_2_LazyRouteMap { [route: string]: string; }
/**
@ -53,14 +59,12 @@ export interface NgTools_InternalApi_NG_2_LazyRouteMap {
* passed in the interface.
*/
class CustomLoaderModuleResolutionHostAdapter extends ModuleResolutionHostAdapter {
constructor(private _readResource: (path: string) => Promise<string>,
host: ts.ModuleResolutionHost) {
constructor(
private _readResource: (path: string) => Promise<string>, host: ts.ModuleResolutionHost) {
super(host);
}
readResource(path: string) {
return this._readResource(path);
}
readResource(path: string) { return this._readResource(path); }
}
@ -74,9 +78,8 @@ export class NgTools_InternalApi_NG_2 {
* @private
*/
static codeGen(options: NgTools_InternalApi_NG2_CodeGen_Options): Promise<void> {
const hostContext: CompilerHostContext = new CustomLoaderModuleResolutionHostAdapter(
options.readResource, options.host
);
const hostContext: CompilerHostContext =
new CustomLoaderModuleResolutionHostAdapter(options.readResource, options.host);
const cliOptions: NgcCliOptions = {
i18nFormat: options.i18nFormat,
i18nFile: options.i18nFile,
@ -86,12 +89,7 @@ export class NgTools_InternalApi_NG_2 {
// Create the Code Generator.
const codeGenerator = CodeGenerator.create(
options.angularCompilerOptions,
cliOptions,
options.program,
options.host,
hostContext
);
options.angularCompilerOptions, cliOptions, options.program, options.host, hostContext);
return codeGenerator.codegen();
}
@ -101,24 +99,26 @@ export class NgTools_InternalApi_NG_2 {
* @internal
* @private
*/
static listLazyRoutes(options: NgTools_InternalApi_NG2_ListLazyRoutes_Options)
: NgTools_InternalApi_NG_2_LazyRouteMap {
static listLazyRoutes(options: NgTools_InternalApi_NG2_ListLazyRoutes_Options):
NgTools_InternalApi_NG_2_LazyRouteMap {
const angularCompilerOptions = options.angularCompilerOptions;
const program = options.program;
const moduleResolutionHost = new ModuleResolutionHostAdapter(options.host);
const usePathMapping = !!angularCompilerOptions.rootDirs && angularCompilerOptions.rootDirs.length > 0;
const ngCompilerHost: AotCompilerHost = usePathMapping
? new PathMappedCompilerHost(program, angularCompilerOptions, moduleResolutionHost)
: new CompilerHost(program, angularCompilerOptions, moduleResolutionHost);
const usePathMapping =
!!angularCompilerOptions.rootDirs && angularCompilerOptions.rootDirs.length > 0;
const ngCompilerHost: AotCompilerHost = usePathMapping ?
new PathMappedCompilerHost(program, angularCompilerOptions, moduleResolutionHost) :
new CompilerHost(program, angularCompilerOptions, moduleResolutionHost);
const staticReflector = new StaticReflector(ngCompilerHost);
const routeMap = listLazyRoutesOfModule(options.entryModule, ngCompilerHost, staticReflector);
return Object.keys(routeMap)
.reduce((acc: NgTools_InternalApi_NG_2_LazyRouteMap, route: string) => {
acc[route] = routeMap[route].absoluteFilePath;
return acc;
}, {});
return Object.keys(routeMap).reduce(
(acc: NgTools_InternalApi_NG_2_LazyRouteMap, route: string) => {
acc[route] = routeMap[route].absoluteFilePath;
return acc;
},
{});
}
}

View File

@ -1,11 +1,20 @@
/**
* @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
*/
/**
* This is a private API for the ngtools toolkit.
*
* This API should be stable for NG 2. It can be removed in NG 4..., but should be replaced by
* something else.
*/
import {AotCompilerHost, StaticReflector, StaticSymbol} from '@angular/compiler';
import {NgModule} from '@angular/core';
import {StaticSymbol, StaticReflector, AotCompilerHost} from '@angular/compiler';
// We cannot depend directly to @angular/router.
@ -19,7 +28,9 @@ export interface LazyRoute {
routeDef: RouteDef;
absoluteFilePath: string;
}
export type LazyRouteMap = {[route: string]: LazyRoute};
export type LazyRouteMap = {
[route: string]: LazyRoute
};
// A route definition. Normally the short form 'path/to/module#ModuleClassName' is used by
// the user, and this is a helper class to extract information from it.
@ -27,9 +38,9 @@ export class RouteDef {
private constructor(public readonly path: string, public readonly className: string = null) {}
toString() {
return (this.className === null || this.className == 'default')
? this.path
: `${this.path}#${this.className}`;
return (this.className === null || this.className == 'default') ?
this.path :
`${this.path}#${this.className}`;
}
static fromString(entry: string): RouteDef {
@ -44,9 +55,8 @@ export class RouteDef {
* @returns {LazyRouteMap}
* @private
*/
export function listLazyRoutesOfModule(entryModule: string,
host: AotCompilerHost,
reflector: StaticReflector): LazyRouteMap {
export function listLazyRoutesOfModule(
entryModule: string, host: AotCompilerHost, reflector: StaticReflector): LazyRouteMap {
const entryRouteDef = RouteDef.fromString(entryModule);
const containingFile = _resolveModule(entryRouteDef.path, entryRouteDef.path, host);
const modulePath = `./${containingFile.replace(/^(.*)\//, '')}`;
@ -55,27 +65,25 @@ export function listLazyRoutesOfModule(entryModule: string,
// List loadChildren of this single module.
const staticSymbol = reflector.findDeclaration(modulePath, className, containingFile);
const ROUTES = reflector.findDeclaration(ROUTER_MODULE_PATH, ROUTER_ROUTES_SYMBOL_NAME);
const lazyRoutes: LazyRoute[] = _extractLazyRoutesFromStaticModule(
staticSymbol, reflector, host, ROUTES);
const lazyRoutes: LazyRoute[] =
_extractLazyRoutesFromStaticModule(staticSymbol, reflector, host, ROUTES);
const routes: LazyRouteMap = {};
lazyRoutes
.forEach((lazyRoute: LazyRoute) => {
const route: string = lazyRoute.routeDef.toString();
_assertRoute(routes, lazyRoute);
routes[route] = lazyRoute;
lazyRoutes.forEach((lazyRoute: LazyRoute) => {
const route: string = lazyRoute.routeDef.toString();
_assertRoute(routes, lazyRoute);
routes[route] = lazyRoute;
const lazyModuleSymbol = reflector.findDeclaration(
const lazyModuleSymbol = reflector.findDeclaration(
lazyRoute.absoluteFilePath, lazyRoute.routeDef.className || 'default');
const subRoutes = _extractLazyRoutesFromStaticModule(
lazyModuleSymbol, reflector, host, ROUTES);
const subRoutes = _extractLazyRoutesFromStaticModule(lazyModuleSymbol, reflector, host, ROUTES);
// Populate the map using the routes we just found.
subRoutes.forEach(subRoute => {
_assertRoute(routes, subRoute);
routes[subRoute.routeDef.toString()] = subRoute;
});
// Populate the map using the routes we just found.
subRoutes.forEach(subRoute => {
_assertRoute(routes, subRoute);
routes[subRoute.routeDef.toString()] = subRoute;
});
});
return routes;
}
@ -101,10 +109,11 @@ function _resolveModule(modulePath: string, containingFile: string, host: AotCom
function _assertRoute(map: LazyRouteMap, route: LazyRoute) {
const r = route.routeDef.toString();
if (map[r] && map[r].absoluteFilePath != route.absoluteFilePath) {
throw new Error(`Duplicated path in loadChildren detected: "${r}" is used in 2 loadChildren, `
+ `but they point to different modules "(${map[r].absoluteFilePath} and `
+ `"${route.absoluteFilePath}"). Webpack cannot distinguish on context and would fail to `
+ 'load the proper one.');
throw new Error(
`Duplicated path in loadChildren detected: "${r}" is used in 2 loadChildren, ` +
`but they point to different modules "(${map[r].absoluteFilePath} and ` +
`"${route.absoluteFilePath}"). Webpack cannot distinguish on context and would fail to ` +
'load the proper one.');
}
}
@ -114,34 +123,35 @@ function _assertRoute(map: LazyRouteMap, route: LazyRoute) {
* module and all statically referred modules.
* @private
*/
function _extractLazyRoutesFromStaticModule(staticSymbol: StaticSymbol,
reflector: StaticReflector,
host: AotCompilerHost,
ROUTES: StaticSymbol): LazyRoute[] {
function _extractLazyRoutesFromStaticModule(
staticSymbol: StaticSymbol, reflector: StaticReflector, host: AotCompilerHost,
ROUTES: StaticSymbol): LazyRoute[] {
const moduleMetadata = _getNgModuleMetadata(staticSymbol, reflector);
const allRoutes: any = (moduleMetadata.imports || [])
.filter(i => 'providers' in i)
.reduce((mem: Route[], m: any) => {
return mem.concat(_collectRoutes(m.providers || [], reflector, ROUTES));
}, _collectRoutes(moduleMetadata.providers || [], reflector, ROUTES));
const allRoutes: any =
(moduleMetadata.imports || [])
.filter(i => 'providers' in i)
.reduce((mem: Route[], m: any) => {
return mem.concat(_collectRoutes(m.providers || [], reflector, ROUTES));
}, _collectRoutes(moduleMetadata.providers || [], reflector, ROUTES));
const lazyRoutes: LazyRoute[] = _collectLoadChildren(allRoutes)
.reduce((acc: LazyRoute[], route: string) => {
const routeDef = RouteDef.fromString(route);
const absoluteFilePath = _resolveModule(routeDef.path, staticSymbol.filePath, host);
acc.push({ routeDef, absoluteFilePath });
return acc;
}, []);
const lazyRoutes: LazyRoute[] =
_collectLoadChildren(allRoutes).reduce((acc: LazyRoute[], route: string) => {
const routeDef = RouteDef.fromString(route);
const absoluteFilePath = _resolveModule(routeDef.path, staticSymbol.filePath, host);
acc.push({routeDef, absoluteFilePath});
return acc;
}, []);
const importedSymbols = ((moduleMetadata.imports || []) as any[])
.filter(i => i instanceof StaticSymbol) as StaticSymbol[];
.filter(i => i instanceof StaticSymbol) as StaticSymbol[];
return importedSymbols
.reduce((acc: LazyRoute[], i: StaticSymbol) => {
return acc.concat(_extractLazyRoutesFromStaticModule(
i, reflector, host, ROUTES));
}, [])
.concat(lazyRoutes);
.reduce(
(acc: LazyRoute[], i: StaticSymbol) => {
return acc.concat(_extractLazyRoutesFromStaticModule(i, reflector, host, ROUTES));
},
[])
.concat(lazyRoutes);
}
@ -150,7 +160,7 @@ function _extractLazyRoutesFromStaticModule(staticSymbol: StaticSymbol,
* @private
*/
function _getNgModuleMetadata(staticSymbol: StaticSymbol, reflector: StaticReflector): NgModule {
const ngModules = reflector.annotations(staticSymbol).filter((s: any) => s instanceof NgModule);
const ngModules = reflector.annotations(staticSymbol).filter((s: any) => s instanceof NgModule);
if (ngModules.length === 0) {
throw new Error(`${staticSymbol.name} is not an NgModule`);
}
@ -162,8 +172,8 @@ function _getNgModuleMetadata(staticSymbol: StaticSymbol, reflector: StaticRefle
* Return the routes from the provider list.
* @private
*/
function _collectRoutes(providers: any[], reflector: StaticReflector,
ROUTES: StaticSymbol): Route[] {
function _collectRoutes(
providers: any[], reflector: StaticReflector, ROUTES: StaticSymbol): Route[] {
return providers.reduce((routeList: Route[], p: any) => {
if (p.provide === ROUTES) {
return routeList.concat(p.useValue);