refactor: code cleanup (#9931)

This commit is contained in:
Victor Berchet 2016-07-09 10:12:39 -07:00 committed by GitHub
parent c3bdd504d0
commit 57473e72ec
5 changed files with 34 additions and 39 deletions

View File

@ -119,18 +119,23 @@ At a high level, this program
``` ```
# Build angular2 and the compiler # Build angular2 and the compiler
./build.sh ./build.sh
# Copy over the package so we can test the compiler tests # Copy over the package so we can test the compiler tests
$ cp tools/@angular/tsc-wrapped/package.json dist/tools/@angular/tsc-wrapped $ cp tools/@angular/tsc-wrapped/package.json dist/tools/@angular/tsc-wrapped
# Run the test once # Run the test once
# (First edit the LINKABLE_PKGS to use npm link instead of npm install) # (First edit the LINKABLE_PKGS to use npm link instead of npm install)
$ ./scripts/ci-lite/offline_compiler_test.sh $ ./scripts/ci-lite/offline_compiler_test.sh
# Keep a package fresh in watch mode # Keep a package fresh in watch mode
./node_modules/.bin/tsc -p modules/@angular/compiler/tsconfig-es5.json -w ./node_modules/.bin/tsc -p modules/@angular/compiler/tsconfig-es5.json -w
# Recompile @angular/core module (needs to use tsc-ext to keep the metadata) # Recompile @angular/core module (needs to use tsc-ext to keep the metadata)
export NODE_PATH=${NODE_PATH}:$(pwd)/dist/all:$(pwd)/dist/tools $ export NODE_PATH=${NODE_PATH}:$(pwd)/dist/all:$(pwd)/dist/tools
node dist/tools/@angular/tsc-wrapped/src/main -p modules/@angular/core/tsconfig-es5.json $ node dist/tools/@angular/tsc-wrapped/src/main -p modules/@angular/core/tsconfig-es5.json
# Iterate on the test # Iterate on the test
cd /tmp/wherever/e2e_test.1464388257/ $ cd /tmp/wherever/e2e_test.1464388257/
./node_modules/.bin/ngc $ ./node_modules/.bin/ngc
./node_modules/.bin/jasmine test/*_spec.js $ ./node_modules/.bin/jasmine test/*_spec.js
``` ```

View File

@ -35,8 +35,7 @@ export class CodeGenerator {
constructor( constructor(
private options: AngularCompilerOptions, private program: ts.Program, private options: AngularCompilerOptions, private program: ts.Program,
public host: ts.CompilerHost, private staticReflector: StaticReflector, public host: ts.CompilerHost, private staticReflector: StaticReflector,
private resolver: CompileMetadataResolver, private compiler: compiler.OfflineCompiler, private compiler: compiler.OfflineCompiler, private reflectorHost: ReflectorHost) {}
private reflectorHost: ReflectorHost) {}
private readFileMetadata(absSourcePath: string): FileMetadata { private readFileMetadata(absSourcePath: string): FileMetadata {
const moduleMetadata = this.staticReflector.getModuleMetadata(absSourcePath); const moduleMetadata = this.staticReflector.getModuleMetadata(absSourcePath);
@ -147,7 +146,7 @@ export class CodeGenerator {
new AppModuleCompiler(), new TypeScriptEmitter(reflectorHost)); new AppModuleCompiler(), new TypeScriptEmitter(reflectorHost));
return new CodeGenerator( return new CodeGenerator(
options, program, compilerHost, staticReflector, resolver, offlineCompiler, reflectorHost); options, program, compilerHost, staticReflector, offlineCompiler, reflectorHost);
} }
} }

View File

@ -99,26 +99,26 @@ export class DirectiveNormalizer {
normalizeLoadedTemplate( normalizeLoadedTemplate(
directiveType: CompileTypeMetadata, templateMeta: CompileTemplateMetadata, template: string, directiveType: CompileTypeMetadata, templateMeta: CompileTemplateMetadata, template: string,
templateAbsUrl: string): CompileTemplateMetadata { templateAbsUrl: string): CompileTemplateMetadata {
var rootNodesAndErrors = this._htmlParser.parse(template, directiveType.name); const rootNodesAndErrors = this._htmlParser.parse(template, directiveType.name);
if (rootNodesAndErrors.errors.length > 0) { if (rootNodesAndErrors.errors.length > 0) {
var errorString = rootNodesAndErrors.errors.join('\n'); const errorString = rootNodesAndErrors.errors.join('\n');
throw new BaseException(`Template parse errors:\n${errorString}`); throw new BaseException(`Template parse errors:\n${errorString}`);
} }
var templateMetadataStyles = this.normalizeStylesheet(new CompileStylesheetMetadata({ const templateMetadataStyles = this.normalizeStylesheet(new CompileStylesheetMetadata({
styles: templateMeta.styles, styles: templateMeta.styles,
styleUrls: templateMeta.styleUrls, styleUrls: templateMeta.styleUrls,
moduleUrl: directiveType.moduleUrl moduleUrl: directiveType.moduleUrl
})); }));
var visitor = new TemplatePreparseVisitor(); const visitor = new TemplatePreparseVisitor();
htmlVisitAll(visitor, rootNodesAndErrors.rootNodes); htmlVisitAll(visitor, rootNodesAndErrors.rootNodes);
var templateStyles = this.normalizeStylesheet(new CompileStylesheetMetadata( const templateStyles = this.normalizeStylesheet(new CompileStylesheetMetadata(
{styles: visitor.styles, styleUrls: visitor.styleUrls, moduleUrl: templateAbsUrl})); {styles: visitor.styles, styleUrls: visitor.styleUrls, moduleUrl: templateAbsUrl}));
var allStyles = templateMetadataStyles.styles.concat(templateStyles.styles); const allStyles = templateMetadataStyles.styles.concat(templateStyles.styles);
var allStyleUrls = templateMetadataStyles.styleUrls.concat(templateStyles.styleUrls); const allStyleUrls = templateMetadataStyles.styleUrls.concat(templateStyles.styleUrls);
var encapsulation = templateMeta.encapsulation; let encapsulation = templateMeta.encapsulation;
if (isBlank(encapsulation)) { if (isBlank(encapsulation)) {
encapsulation = this._config.defaultEncapsulation; encapsulation = this._config.defaultEncapsulation;
} }

View File

@ -6,8 +6,6 @@
* found in the LICENSE file at https://angular.io/license * found in the LICENSE file at https://angular.io/license
*/ */
import {AppModuleMetadata, ComponentMetadata} from '@angular/core';
import {AppModuleCompiler} from './app_module_compiler'; import {AppModuleCompiler} from './app_module_compiler';
import {CompileDirectiveMetadata, CompileIdentifierMetadata, CompilePipeMetadata, StaticSymbol, createHostComponentMeta} from './compile_metadata'; import {CompileDirectiveMetadata, CompileIdentifierMetadata, CompilePipeMetadata, StaticSymbol, createHostComponentMeta} from './compile_metadata';
import {DirectiveNormalizer} from './directive_normalizer'; import {DirectiveNormalizer} from './directive_normalizer';
@ -19,9 +17,7 @@ import {OutputEmitter} from './output/abstract_emitter';
import * as o from './output/output_ast'; import * as o from './output/output_ast';
import {CompiledStylesheet, StyleCompiler} from './style_compiler'; import {CompiledStylesheet, StyleCompiler} from './style_compiler';
import {TemplateParser} from './template_parser'; import {TemplateParser} from './template_parser';
import {assetUrl} from './util';
import {ComponentFactoryDependency, ViewCompileResult, ViewCompiler, ViewFactoryDependency} from './view_compiler/view_compiler'; import {ComponentFactoryDependency, ViewCompileResult, ViewCompiler, ViewFactoryDependency} from './view_compiler/view_compiler';
import {XHR} from './xhr';
export class SourceModule { export class SourceModule {
constructor(public moduleUrl: string, public source: string) {} constructor(public moduleUrl: string, public source: string) {}

View File

@ -7,13 +7,9 @@
*/ */
import {Injectable, ViewEncapsulation} from '@angular/core'; import {Injectable, ViewEncapsulation} from '@angular/core';
import {isPresent} from '../src/facade/lang';
import {CompileDirectiveMetadata, CompileIdentifierMetadata, CompileStylesheetMetadata} from './compile_metadata'; import {CompileDirectiveMetadata, CompileIdentifierMetadata, CompileStylesheetMetadata} from './compile_metadata';
import * as o from './output/output_ast'; import * as o from './output/output_ast';
import {ShadowCss} from './shadow_css'; import {ShadowCss} from './shadow_css';
import {extractStyleUrls} from './style_url_resolver';
import {UrlResolver} from './url_resolver'; import {UrlResolver} from './url_resolver';
const COMPONENT_VARIABLE = '%COMP%'; const COMPONENT_VARIABLE = '%COMP%';
@ -46,9 +42,8 @@ export class StyleCompiler {
constructor(private _urlResolver: UrlResolver) {} constructor(private _urlResolver: UrlResolver) {}
compileComponent(comp: CompileDirectiveMetadata): StylesCompileResult { compileComponent(comp: CompileDirectiveMetadata): StylesCompileResult {
var shim = comp.template.encapsulation === ViewEncapsulation.Emulated; const externalStylesheets: CompiledStylesheet[] = [];
var externalStylesheets: CompiledStylesheet[] = []; const componentStylesheet: CompiledStylesheet = this._compileStyles(
var componentStylesheet: CompiledStylesheet = this._compileStyles(
comp, new CompileStylesheetMetadata({ comp, new CompileStylesheetMetadata({
styles: comp.template.styles, styles: comp.template.styles,
styleUrls: comp.template.styleUrls, styleUrls: comp.template.styleUrls,
@ -56,7 +51,7 @@ export class StyleCompiler {
}), }),
true); true);
comp.template.externalStylesheets.forEach((stylesheetMeta) => { comp.template.externalStylesheets.forEach((stylesheetMeta) => {
var compiledStylesheet = this._compileStyles(comp, stylesheetMeta, false); const compiledStylesheet = this._compileStyles(comp, stylesheetMeta, false);
externalStylesheets.push(compiledStylesheet); externalStylesheets.push(compiledStylesheet);
}); });
return new StylesCompileResult(componentStylesheet, externalStylesheets); return new StylesCompileResult(componentStylesheet, externalStylesheets);
@ -65,22 +60,22 @@ export class StyleCompiler {
private _compileStyles( private _compileStyles(
comp: CompileDirectiveMetadata, stylesheet: CompileStylesheetMetadata, comp: CompileDirectiveMetadata, stylesheet: CompileStylesheetMetadata,
isComponentStylesheet: boolean): CompiledStylesheet { isComponentStylesheet: boolean): CompiledStylesheet {
var shim = comp.template.encapsulation === ViewEncapsulation.Emulated; const shim = comp.template.encapsulation === ViewEncapsulation.Emulated;
var styleExpressions = const styleExpressions =
stylesheet.styles.map(plainStyle => o.literal(this._shimIfNeeded(plainStyle, shim))); stylesheet.styles.map(plainStyle => o.literal(this._shimIfNeeded(plainStyle, shim)));
var dependencies: StylesCompileDependency[] = []; const dependencies: StylesCompileDependency[] = [];
for (var i = 0; i < stylesheet.styleUrls.length; i++) { for (let i = 0; i < stylesheet.styleUrls.length; i++) {
var identifier = new CompileIdentifierMetadata({name: getStylesVarName(null)}); const identifier = new CompileIdentifierMetadata({name: getStylesVarName(null)});
dependencies.push(new StylesCompileDependency(stylesheet.styleUrls[i], shim, identifier)); dependencies.push(new StylesCompileDependency(stylesheet.styleUrls[i], shim, identifier));
styleExpressions.push(new o.ExternalExpr(identifier)); styleExpressions.push(new o.ExternalExpr(identifier));
} }
// styles variable contains plain strings and arrays of other styles arrays (recursive), // styles variable contains plain strings and arrays of other styles arrays (recursive),
// so we set its type to dynamic. // so we set its type to dynamic.
var stylesVar = getStylesVarName(isComponentStylesheet ? comp : null); const stylesVar = getStylesVarName(isComponentStylesheet ? comp : null);
var stmt = o.variable(stylesVar) const stmt = o.variable(stylesVar)
.set(o.literalArr( .set(o.literalArr(
styleExpressions, new o.ArrayType(o.DYNAMIC_TYPE, [o.TypeModifier.Const]))) styleExpressions, new o.ArrayType(o.DYNAMIC_TYPE, [o.TypeModifier.Const])))
.toDeclStmt(null, [o.StmtModifier.Final]); .toDeclStmt(null, [o.StmtModifier.Final]);
return new CompiledStylesheet([stmt], stylesVar, dependencies, shim, stylesheet); return new CompiledStylesheet([stmt], stylesVar, dependencies, shim, stylesheet);
} }
@ -90,7 +85,7 @@ export class StyleCompiler {
} }
function getStylesVarName(component: CompileDirectiveMetadata): string { function getStylesVarName(component: CompileDirectiveMetadata): string {
var result = `styles`; let result = `styles`;
if (component) { if (component) {
result += `_${component.type.name}`; result += `_${component.type.name}`;
} }