Skip to content

Commit

Permalink
refactor: code cleanup (angular#9931)
Browse files Browse the repository at this point in the history
  • Loading branch information
vicb authored Jul 9, 2016
1 parent c3bdd50 commit 57473e7
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 39 deletions.
15 changes: 10 additions & 5 deletions modules/@angular/compiler-cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,18 +119,23 @@ At a high level, this program
```
# Build angular2 and the compiler
./build.sh
# Copy over the package so we can test the compiler tests
$ cp tools/@angular/tsc-wrapped/package.json dist/tools/@angular/tsc-wrapped
# Run the test once
# (First edit the LINKABLE_PKGS to use npm link instead of npm install)
$ ./scripts/ci-lite/offline_compiler_test.sh
# Keep a package fresh in watch mode
./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)
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
$ 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
# Iterate on the test
cd /tmp/wherever/e2e_test.1464388257/
./node_modules/.bin/ngc
./node_modules/.bin/jasmine test/*_spec.js
$ cd /tmp/wherever/e2e_test.1464388257/
$ ./node_modules/.bin/ngc
$ ./node_modules/.bin/jasmine test/*_spec.js
```
5 changes: 2 additions & 3 deletions modules/@angular/compiler-cli/src/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ export class CodeGenerator {
constructor(
private options: AngularCompilerOptions, private program: ts.Program,
public host: ts.CompilerHost, private staticReflector: StaticReflector,
private resolver: CompileMetadataResolver, private compiler: compiler.OfflineCompiler,
private reflectorHost: ReflectorHost) {}
private compiler: compiler.OfflineCompiler, private reflectorHost: ReflectorHost) {}

private readFileMetadata(absSourcePath: string): FileMetadata {
const moduleMetadata = this.staticReflector.getModuleMetadata(absSourcePath);
Expand Down Expand Up @@ -147,7 +146,7 @@ export class CodeGenerator {
new AppModuleCompiler(), new TypeScriptEmitter(reflectorHost));

return new CodeGenerator(
options, program, compilerHost, staticReflector, resolver, offlineCompiler, reflectorHost);
options, program, compilerHost, staticReflector, offlineCompiler, reflectorHost);
}
}

Expand Down
16 changes: 8 additions & 8 deletions modules/@angular/compiler/src/directive_normalizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,26 +99,26 @@ export class DirectiveNormalizer {
normalizeLoadedTemplate(
directiveType: CompileTypeMetadata, templateMeta: CompileTemplateMetadata, template: string,
templateAbsUrl: string): CompileTemplateMetadata {
var rootNodesAndErrors = this._htmlParser.parse(template, directiveType.name);
const rootNodesAndErrors = this._htmlParser.parse(template, directiveType.name);
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}`);
}
var templateMetadataStyles = this.normalizeStylesheet(new CompileStylesheetMetadata({
const templateMetadataStyles = this.normalizeStylesheet(new CompileStylesheetMetadata({
styles: templateMeta.styles,
styleUrls: templateMeta.styleUrls,
moduleUrl: directiveType.moduleUrl
}));

var visitor = new TemplatePreparseVisitor();
const visitor = new TemplatePreparseVisitor();
htmlVisitAll(visitor, rootNodesAndErrors.rootNodes);
var templateStyles = this.normalizeStylesheet(new CompileStylesheetMetadata(
const templateStyles = this.normalizeStylesheet(new CompileStylesheetMetadata(
{styles: visitor.styles, styleUrls: visitor.styleUrls, moduleUrl: templateAbsUrl}));

var allStyles = templateMetadataStyles.styles.concat(templateStyles.styles);
var allStyleUrls = templateMetadataStyles.styleUrls.concat(templateStyles.styleUrls);
const allStyles = templateMetadataStyles.styles.concat(templateStyles.styles);
const allStyleUrls = templateMetadataStyles.styleUrls.concat(templateStyles.styleUrls);

var encapsulation = templateMeta.encapsulation;
let encapsulation = templateMeta.encapsulation;
if (isBlank(encapsulation)) {
encapsulation = this._config.defaultEncapsulation;
}
Expand Down
4 changes: 0 additions & 4 deletions modules/@angular/compiler/src/offline_compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
* found in the LICENSE file at https://angular.io/license
*/

import {AppModuleMetadata, ComponentMetadata} from '@angular/core';

import {AppModuleCompiler} from './app_module_compiler';
import {CompileDirectiveMetadata, CompileIdentifierMetadata, CompilePipeMetadata, StaticSymbol, createHostComponentMeta} from './compile_metadata';
import {DirectiveNormalizer} from './directive_normalizer';
Expand All @@ -19,9 +17,7 @@ import {OutputEmitter} from './output/abstract_emitter';
import * as o from './output/output_ast';
import {CompiledStylesheet, StyleCompiler} from './style_compiler';
import {TemplateParser} from './template_parser';
import {assetUrl} from './util';
import {ComponentFactoryDependency, ViewCompileResult, ViewCompiler, ViewFactoryDependency} from './view_compiler/view_compiler';
import {XHR} from './xhr';

export class SourceModule {
constructor(public moduleUrl: string, public source: string) {}
Expand Down
33 changes: 14 additions & 19 deletions modules/@angular/compiler/src/style_compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,9 @@
*/

import {Injectable, ViewEncapsulation} from '@angular/core';

import {isPresent} from '../src/facade/lang';

import {CompileDirectiveMetadata, CompileIdentifierMetadata, CompileStylesheetMetadata} from './compile_metadata';
import * as o from './output/output_ast';
import {ShadowCss} from './shadow_css';
import {extractStyleUrls} from './style_url_resolver';
import {UrlResolver} from './url_resolver';

const COMPONENT_VARIABLE = '%COMP%';
Expand Down Expand Up @@ -46,17 +42,16 @@ export class StyleCompiler {
constructor(private _urlResolver: UrlResolver) {}

compileComponent(comp: CompileDirectiveMetadata): StylesCompileResult {
var shim = comp.template.encapsulation === ViewEncapsulation.Emulated;
var externalStylesheets: CompiledStylesheet[] = [];
var componentStylesheet: CompiledStylesheet = this._compileStyles(
const externalStylesheets: CompiledStylesheet[] = [];
const componentStylesheet: CompiledStylesheet = this._compileStyles(
comp, new CompileStylesheetMetadata({
styles: comp.template.styles,
styleUrls: comp.template.styleUrls,
moduleUrl: comp.type.moduleUrl
}),
true);
comp.template.externalStylesheets.forEach((stylesheetMeta) => {
var compiledStylesheet = this._compileStyles(comp, stylesheetMeta, false);
const compiledStylesheet = this._compileStyles(comp, stylesheetMeta, false);
externalStylesheets.push(compiledStylesheet);
});
return new StylesCompileResult(componentStylesheet, externalStylesheets);
Expand All @@ -65,22 +60,22 @@ export class StyleCompiler {
private _compileStyles(
comp: CompileDirectiveMetadata, stylesheet: CompileStylesheetMetadata,
isComponentStylesheet: boolean): CompiledStylesheet {
var shim = comp.template.encapsulation === ViewEncapsulation.Emulated;
var styleExpressions =
const shim = comp.template.encapsulation === ViewEncapsulation.Emulated;
const styleExpressions =
stylesheet.styles.map(plainStyle => o.literal(this._shimIfNeeded(plainStyle, shim)));
var dependencies: StylesCompileDependency[] = [];
for (var i = 0; i < stylesheet.styleUrls.length; i++) {
var identifier = new CompileIdentifierMetadata({name: getStylesVarName(null)});
const dependencies: StylesCompileDependency[] = [];
for (let i = 0; i < stylesheet.styleUrls.length; i++) {
const identifier = new CompileIdentifierMetadata({name: getStylesVarName(null)});
dependencies.push(new StylesCompileDependency(stylesheet.styleUrls[i], shim, identifier));
styleExpressions.push(new o.ExternalExpr(identifier));
}
// styles variable contains plain strings and arrays of other styles arrays (recursive),
// so we set its type to dynamic.
var stylesVar = getStylesVarName(isComponentStylesheet ? comp : null);
var stmt = o.variable(stylesVar)
.set(o.literalArr(
styleExpressions, new o.ArrayType(o.DYNAMIC_TYPE, [o.TypeModifier.Const])))
.toDeclStmt(null, [o.StmtModifier.Final]);
const stylesVar = getStylesVarName(isComponentStylesheet ? comp : null);
const stmt = o.variable(stylesVar)
.set(o.literalArr(
styleExpressions, new o.ArrayType(o.DYNAMIC_TYPE, [o.TypeModifier.Const])))
.toDeclStmt(null, [o.StmtModifier.Final]);
return new CompiledStylesheet([stmt], stylesVar, dependencies, shim, stylesheet);
}

Expand All @@ -90,7 +85,7 @@ export class StyleCompiler {
}

function getStylesVarName(component: CompileDirectiveMetadata): string {
var result = `styles`;
let result = `styles`;
if (component) {
result += `_${component.type.name}`;
}
Expand Down

0 comments on commit 57473e7

Please sign in to comment.