X Tutup
Skip to content

Commit e667ad3

Browse files
committed
refactor(compiler): make all commands const
Closes angular#5135
1 parent fb8b815 commit e667ad3

35 files changed

+997
-863
lines changed

modules/angular2/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ dependencies:
2323
source_span: '^1.0.0'
2424
stack_trace: '^1.1.1'
2525
dev_dependencies:
26-
guinness: '^0.1.17'
26+
guinness: '^0.1.18'
2727
transformers:
2828
- angular2
2929
- $dart2js:

modules/angular2/src/compiler/change_detector_compiler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export class ChangeDetectionCompiler {
6363
var index = 0;
6464
var sourceParts = changeDetectorDefinitions.map(definition => {
6565
var codegen: any;
66-
var sourcePart;
66+
var sourcePart: string;
6767
// TODO(tbosch): move the 2 code generators to the same place, one with .dart and one with .ts
6868
// suffix
6969
// and have the same API for calling them!
@@ -74,7 +74,7 @@ export class ChangeDetectionCompiler {
7474
'dynamic' :
7575
`${moduleRef(componentType.moduleUrl)}${componentType.name}`;
7676
codegen.generate(typeRef, className, definition);
77-
factories.push(`(dispatcher) => new ${className}(dispatcher)`);
77+
factories.push(`${className}.newChangeDetector`);
7878
sourcePart = codegen.toString();
7979
} else {
8080
codegen = new ChangeDetectorJITGenerator(

modules/angular2/src/compiler/command_compiler.ts

Lines changed: 85 additions & 103 deletions
Large diffs are not rendered by default.

modules/angular2/src/compiler/style_compiler.ts

Lines changed: 19 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,12 @@ import {UrlResolver} from 'angular2/src/compiler/url_resolver';
99
import {extractStyleUrls} from './style_url_resolver';
1010
import {
1111
escapeSingleQuoteString,
12-
codeGenConcatArray,
13-
codeGenMapArray,
14-
codeGenReplaceAll,
1512
codeGenExportVariable,
1613
codeGenToString,
1714
MODULE_SUFFIX
1815
} from './util';
1916
import {Injectable} from 'angular2/src/core/di';
20-
21-
const COMPONENT_VARIABLE = '%COMP%';
22-
var COMPONENT_REGEX = /%COMP%/g;
23-
const HOST_ATTR = `_nghost-${COMPONENT_VARIABLE}`;
24-
const HOST_ATTR_EXPR = `'_nghost-'+${COMPONENT_VARIABLE}`;
25-
const CONTENT_ATTR = `_ngcontent-${COMPONENT_VARIABLE}`;
26-
const CONTENT_ATTR_EXPR = `'_ngcontent-'+${COMPONENT_VARIABLE}`;
17+
import {COMPONENT_VARIABLE, HOST_ATTR, CONTENT_ATTR} from 'angular2/src/core/render/view_factory';
2718

2819
@Injectable()
2920
export class StyleCompiler {
@@ -32,46 +23,33 @@ export class StyleCompiler {
3223

3324
constructor(private _xhr: XHR, private _urlResolver: UrlResolver) {}
3425

35-
compileComponentRuntime(appId: string, templateId: number,
36-
template: CompileTemplateMetadata): Promise<string[]> {
26+
compileComponentRuntime(template: CompileTemplateMetadata): Promise<Array<string | any[]>> {
3727
var styles = template.styles;
3828
var styleAbsUrls = template.styleUrls;
3929
return this._loadStyles(styles, styleAbsUrls,
40-
template.encapsulation === ViewEncapsulation.Emulated)
41-
.then(styles => styles.map(style => StringWrapper.replaceAll(
42-
style, COMPONENT_REGEX, componentId(appId, templateId))));
30+
template.encapsulation === ViewEncapsulation.Emulated);
4331
}
4432

45-
compileComponentCodeGen(appIdExpression: string, templateIdExpression: string,
46-
template: CompileTemplateMetadata): SourceExpression {
33+
compileComponentCodeGen(template: CompileTemplateMetadata): SourceExpression {
4734
var shim = template.encapsulation === ViewEncapsulation.Emulated;
48-
var suffix;
49-
if (shim) {
50-
suffix = codeGenMapArray(
51-
['style'],
52-
`style${codeGenReplaceAll(COMPONENT_VARIABLE, componentIdExpression(appIdExpression, templateIdExpression))}`);
53-
} else {
54-
suffix = '';
55-
}
56-
return this._styleCodeGen(template.styles, template.styleUrls, shim, suffix);
35+
return this._styleCodeGen(template.styles, template.styleUrls, shim);
5736
}
5837

5938
compileStylesheetCodeGen(stylesheetUrl: string, cssText: string): SourceModule[] {
6039
var styleWithImports = extractStyleUrls(this._urlResolver, stylesheetUrl, cssText);
6140
return [
6241
this._styleModule(
6342
stylesheetUrl, false,
64-
this._styleCodeGen([styleWithImports.style], styleWithImports.styleUrls, false, '')),
65-
this._styleModule(
66-
stylesheetUrl, true,
67-
this._styleCodeGen([styleWithImports.style], styleWithImports.styleUrls, true, ''))
43+
this._styleCodeGen([styleWithImports.style], styleWithImports.styleUrls, false)),
44+
this._styleModule(stylesheetUrl, true, this._styleCodeGen([styleWithImports.style],
45+
styleWithImports.styleUrls, true))
6846
];
6947
}
7048

7149
clearCache() { this._styleCache.clear(); }
7250

7351
private _loadStyles(plainStyles: string[], absUrls: string[],
74-
encapsulate: boolean): Promise<string[]> {
52+
encapsulate: boolean): Promise<Array<string | any[]>> {
7553
var promises = absUrls.map((absUrl) => {
7654
var cacheKey = `${absUrl}${encapsulate ? '.shim' : ''}`;
7755
var result = this._styleCache.get(cacheKey);
@@ -86,22 +64,23 @@ export class StyleCompiler {
8664
return result;
8765
});
8866
return PromiseWrapper.all(promises).then((nestedStyles: string[][]) => {
89-
var result = plainStyles.map(plainStyle => this._shimIfNeeded(plainStyle, encapsulate));
90-
nestedStyles.forEach(styles => styles.forEach(style => result.push(style)));
67+
var result: Array<string | any[]> =
68+
plainStyles.map(plainStyle => this._shimIfNeeded(plainStyle, encapsulate));
69+
nestedStyles.forEach(styles => result.push(styles));
9170
return result;
9271
});
9372
}
9473

95-
private _styleCodeGen(plainStyles: string[], absUrls: string[], shim: boolean,
96-
suffix: string): SourceExpression {
97-
var expressionSource = `(`;
98-
expressionSource +=
99-
`[${plainStyles.map( plainStyle => escapeSingleQuoteString(this._shimIfNeeded(plainStyle, shim)) ).join(',')}]`;
74+
private _styleCodeGen(plainStyles: string[], absUrls: string[], shim: boolean): SourceExpression {
75+
var arrayPrefix = IS_DART ? `const` : '';
76+
var styleExpressions = plainStyles.map(
77+
plainStyle => escapeSingleQuoteString(this._shimIfNeeded(plainStyle, shim)));
78+
10079
for (var i = 0; i < absUrls.length; i++) {
10180
var moduleUrl = this._createModuleUrl(absUrls[i], shim);
102-
expressionSource += codeGenConcatArray(`${moduleRef(moduleUrl)}STYLES`);
81+
styleExpressions.push(`${moduleRef(moduleUrl)}STYLES`);
10382
}
104-
expressionSource += `)${suffix}`;
83+
var expressionSource = `${arrayPrefix} [${styleExpressions.join(',')}]`;
10584
return new SourceExpression([], expressionSource);
10685
}
10786

@@ -122,29 +101,3 @@ export class StyleCompiler {
122101
return shim ? `${stylesheetUrl}.shim${MODULE_SUFFIX}` : `${stylesheetUrl}${MODULE_SUFFIX}`;
123102
}
124103
}
125-
126-
export function shimContentAttribute(appId: string, templateId: number): string {
127-
return StringWrapper.replaceAll(CONTENT_ATTR, COMPONENT_REGEX, componentId(appId, templateId));
128-
}
129-
130-
export function shimContentAttributeExpr(appIdExpr: string, templateIdExpr: string): string {
131-
return StringWrapper.replaceAll(CONTENT_ATTR_EXPR, COMPONENT_REGEX,
132-
componentIdExpression(appIdExpr, templateIdExpr));
133-
}
134-
135-
export function shimHostAttribute(appId: string, templateId: number): string {
136-
return StringWrapper.replaceAll(HOST_ATTR, COMPONENT_REGEX, componentId(appId, templateId));
137-
}
138-
139-
export function shimHostAttributeExpr(appIdExpr: string, templateIdExpr: string): string {
140-
return StringWrapper.replaceAll(HOST_ATTR_EXPR, COMPONENT_REGEX,
141-
componentIdExpression(appIdExpr, templateIdExpr));
142-
}
143-
144-
function componentId(appId: string, templateId: number): string {
145-
return `${appId}-${templateId}`;
146-
}
147-
148-
function componentIdExpression(appIdExpression: string, templateIdExpression: string): string {
149-
return `${appIdExpression}+'-'+${codeGenToString(templateIdExpression)}`;
150-
}

0 commit comments

Comments
 (0)
X Tutup