Skip to content

Commit fe299f4

Browse files
tboschvsavkin
authored andcommitted
refactor(compiler): minor cleanups
1 parent 4cac650 commit fe299f4

11 files changed

Lines changed: 184 additions & 118 deletions

File tree

‎modules/@angular/compiler/src/compiler_util/identifier_util.ts‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@ export function createDiTokenExpression(token: CompileTokenMetadata): o.Expressi
2222
}
2323
}
2424

25-
export function createFastArray(values: o.Expression[]): o.Expression {
25+
export function createInlineArray(values: o.Expression[]): o.Expression {
2626
if (values.length === 0) {
27-
return o.importExpr(resolveIdentifier(Identifiers.EMPTY_FAST_ARRAY));
27+
return o.importExpr(resolveIdentifier(Identifiers.EMPTY_INLINE_ARRAY));
2828
}
29-
const index = Math.ceil(values.length / 2) - 1;
30-
const identifierSpec = index < Identifiers.fastArrays.length ? Identifiers.fastArrays[index] :
31-
Identifiers.FastArrayDynamic;
29+
const log2 = Math.log(values.length) / Math.log(2);
30+
const index = Math.ceil(log2);
31+
const identifierSpec = index < Identifiers.inlineArrays.length ? Identifiers.inlineArrays[index] :
32+
Identifiers.InlineArrayDynamic;
3233
const identifier = resolveIdentifier(identifierSpec);
3334
return o.importExpr(identifier).instantiate([
3435
<o.Expression>o.literal(values.length)

‎modules/@angular/compiler/src/directive_wrapper_compiler.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ export class DirectiveWrapperCompiler {
4848
compile(dirMeta: CompileDirectiveMetadata): DirectiveWrapperCompileResult {
4949
const builder = new DirectiveWrapperBuilder(this.compilerConfig, dirMeta);
5050
Object.keys(dirMeta.inputs).forEach((inputFieldName) => {
51-
createCheckInputMethod(inputFieldName, builder);
51+
addCheckInputMethod(inputFieldName, builder);
5252
});
53-
createDetectChangesInternalMethod(builder);
53+
addDetectChangesInternalMethod(builder);
5454
const classStmt = builder.build();
5555
return new DirectiveWrapperCompileResult([classStmt], classStmt.name);
5656
}
@@ -102,12 +102,12 @@ class DirectiveWrapperBuilder implements ClassBuilder {
102102
return createClassStmt({
103103
name: DirectiveWrapperCompiler.dirWrapperClassName(this.dirMeta.type),
104104
ctorParams: dirDepParamNames.map((paramName) => new o.FnParam(paramName, o.DYNAMIC_TYPE)),
105-
builders: [{fields: fields, ctorStmts: ctorStmts}, this]
106-
})
105+
builders: [{fields, ctorStmts}, this]
106+
});
107107
}
108108
}
109109

110-
function createDetectChangesInternalMethod(builder: DirectiveWrapperBuilder) {
110+
function addDetectChangesInternalMethod(builder: DirectiveWrapperBuilder) {
111111
const changedVar = o.variable('changed');
112112
const stmts: o.Statement[] = [
113113
changedVar.set(o.THIS_EXPR.prop(CHANGED_FIELD_NAME)).toDeclStmt(),
@@ -157,7 +157,7 @@ function createDetectChangesInternalMethod(builder: DirectiveWrapperBuilder) {
157157
stmts, o.BOOL_TYPE));
158158
}
159159

160-
function createCheckInputMethod(input: string, builder: DirectiveWrapperBuilder) {
160+
function addCheckInputMethod(input: string, builder: DirectiveWrapperBuilder) {
161161
const fieldName = `_${input}`;
162162
const fieldExpr = o.THIS_EXPR.prop(fieldName);
163163
// private is fine here as no child view will reference the cached value...

‎modules/@angular/compiler/src/identifiers.ts‎

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -294,23 +294,24 @@ export class Identifiers {
294294
};
295295

296296
// This is just the interface!
297-
static FastArray:
298-
IdentifierSpec = {name: 'FastArray', moduleUrl: VIEW_UTILS_MODULE_URL, runtime: null};
299-
static fastArrays: IdentifierSpec[] = [
300-
{name: 'FastArray2', moduleUrl: VIEW_UTILS_MODULE_URL, runtime: view_utils.FastArray2},
301-
{name: 'FastArray4', moduleUrl: VIEW_UTILS_MODULE_URL, runtime: view_utils.FastArray4},
302-
{name: 'FastArray8', moduleUrl: VIEW_UTILS_MODULE_URL, runtime: view_utils.FastArray8},
303-
{name: 'FastArray16', moduleUrl: VIEW_UTILS_MODULE_URL, runtime: view_utils.FastArray16},
297+
static InlineArray:
298+
IdentifierSpec = {name: 'InlineArray', moduleUrl: VIEW_UTILS_MODULE_URL, runtime: null};
299+
static inlineArrays: IdentifierSpec[] = [
300+
{name: 'InlineArray2', moduleUrl: VIEW_UTILS_MODULE_URL, runtime: view_utils.InlineArray2},
301+
{name: 'InlineArray2', moduleUrl: VIEW_UTILS_MODULE_URL, runtime: view_utils.InlineArray2},
302+
{name: 'InlineArray4', moduleUrl: VIEW_UTILS_MODULE_URL, runtime: view_utils.InlineArray4},
303+
{name: 'InlineArray8', moduleUrl: VIEW_UTILS_MODULE_URL, runtime: view_utils.InlineArray8},
304+
{name: 'InlineArray16', moduleUrl: VIEW_UTILS_MODULE_URL, runtime: view_utils.InlineArray16},
304305
];
305-
static EMPTY_FAST_ARRAY: IdentifierSpec = {
306-
name: 'EMPTY_FAST_ARRAY',
306+
static EMPTY_INLINE_ARRAY: IdentifierSpec = {
307+
name: 'EMPTY_INLINE_ARRAY',
307308
moduleUrl: VIEW_UTILS_MODULE_URL,
308-
runtime: view_utils.EMPTY_FAST_ARRAY
309+
runtime: view_utils.EMPTY_INLINE_ARRAY
309310
};
310-
static FastArrayDynamic: IdentifierSpec = {
311-
name: 'FastArrayDynamic',
311+
static InlineArrayDynamic: IdentifierSpec = {
312+
name: 'InlineArrayDynamic',
312313
moduleUrl: VIEW_UTILS_MODULE_URL,
313-
runtime: view_utils.FastArrayDynamic
314+
runtime: view_utils.InlineArrayDynamic
314315
};
315316
}
316317

‎modules/@angular/compiler/src/ng_module_compiler.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,14 @@ export class NgModuleCompiler {
8484
}
8585

8686
class _InjectorBuilder implements ClassBuilder {
87-
private _tokens: CompileTokenMetadata[] = [];
88-
private _instances = new Map<any, o.Expression>();
8987
fields: o.ClassField[] = [];
90-
private _createStmts: o.Statement[] = [];
91-
private _destroyStmts: o.Statement[] = [];
9288
getters: o.ClassGetter[] = [];
9389
methods: o.ClassMethod[] = [];
9490
ctorStmts: o.Statement[] = [];
91+
private _tokens: CompileTokenMetadata[] = [];
92+
private _instances = new Map<any, o.Expression>();
93+
private _createStmts: o.Statement[] = [];
94+
private _destroyStmts: o.Statement[] = [];
9595

9696
constructor(
9797
private _ngModuleMeta: CompileNgModuleMetadata,
@@ -154,7 +154,7 @@ class _InjectorBuilder implements ClassBuilder {
154154
parent: o.importExpr(
155155
resolveIdentifier(Identifiers.NgModuleInjector), [o.importType(this._ngModuleMeta.type)]),
156156
parentArgs: parentArgs,
157-
builders: [{methods: methods}, this]
157+
builders: [{methods}, this]
158158
});
159159
}
160160

‎modules/@angular/compiler/src/template_parser/binding_parser.ts‎

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ const STYLE_PREFIX = 'style';
2828

2929
const ANIMATE_PROP_PREFIX = 'animate-';
3030

31-
/**
32-
* Type of a parsed property
33-
*/
3431
export enum BoundPropertyType {
3532
DEFAULT,
3633
LITERAL_ATTR,
@@ -55,49 +52,48 @@ export class BoundProperty {
5552
*/
5653
export class BindingParser {
5754
pipesByName: Map<string, CompilePipeMetadata> = new Map();
58-
errors: ParseError[] = [];
5955

6056
constructor(
6157
private _exprParser: Parser, private _interpolationConfig: InterpolationConfig,
62-
protected _schemaRegistry: ElementSchemaRegistry, protected _schemas: SchemaMetadata[],
63-
pipes: CompilePipeMetadata[]) {
58+
private _schemaRegistry: ElementSchemaRegistry, private _schemas: SchemaMetadata[],
59+
pipes: CompilePipeMetadata[], private _targetErrors: ParseError[]) {
6460
pipes.forEach(pipe => this.pipesByName.set(pipe.name, pipe));
6561
}
6662

6763
createDirectiveHostPropertyAsts(
68-
elementName: string, hostProps: {[key: string]: string}, sourceSpan: ParseSourceSpan,
69-
targetPropertyAsts: BoundElementPropertyAst[]) {
64+
elementName: string, hostProps: {[key: string]: string},
65+
sourceSpan: ParseSourceSpan): BoundElementPropertyAst[] {
7066
if (hostProps) {
7167
const boundProps: BoundProperty[] = [];
7268
Object.keys(hostProps).forEach(propName => {
7369
const expression = hostProps[propName];
7470
if (typeof expression === 'string') {
7571
this.parsePropertyBinding(propName, expression, true, sourceSpan, [], boundProps);
7672
} else {
77-
this.reportError(
73+
this._reportError(
7874
`Value of the host property binding "${propName}" needs to be a string representing an expression but got "${expression}" (${typeof expression})`,
7975
sourceSpan);
8076
}
8177
});
82-
boundProps.forEach(
83-
(prop) => { targetPropertyAsts.push(this.createElementPropertyAst(elementName, prop)); });
78+
return boundProps.map((prop) => this.createElementPropertyAst(elementName, prop));
8479
}
8580
}
8681

87-
createDirectiveHostEventAsts(
88-
hostListeners: {[key: string]: string}, sourceSpan: ParseSourceSpan,
89-
targetEventAsts: BoundEventAst[]) {
82+
createDirectiveHostEventAsts(hostListeners: {[key: string]: string}, sourceSpan: ParseSourceSpan):
83+
BoundEventAst[] {
9084
if (hostListeners) {
85+
const targetEventAsts: BoundEventAst[] = [];
9186
Object.keys(hostListeners).forEach(propName => {
9287
const expression = hostListeners[propName];
9388
if (typeof expression === 'string') {
9489
this.parseEvent(propName, expression, sourceSpan, [], targetEventAsts);
9590
} else {
96-
this.reportError(
91+
this._reportError(
9792
`Value of the host listener "${propName}" needs to be a string representing an expression but got "${expression}" (${typeof expression})`,
9893
sourceSpan);
9994
}
10095
});
96+
return targetEventAsts;
10197
}
10298
}
10399

@@ -115,7 +111,7 @@ export class BindingParser {
115111
}
116112
return ast;
117113
} catch (e) {
118-
this.reportError(`${e}`, sourceSpan);
114+
this._reportError(`${e}`, sourceSpan);
119115
return this._exprParser.wrapLiteralPrimitive('ERROR', sourceInfo);
120116
}
121117
}
@@ -150,10 +146,10 @@ export class BindingParser {
150146
}
151147
});
152148
bindingsResult.warnings.forEach(
153-
(warning) => { this.reportError(warning, sourceSpan, ParseErrorLevel.WARNING); });
149+
(warning) => { this._reportError(warning, sourceSpan, ParseErrorLevel.WARNING); });
154150
return bindingsResult.templateBindings;
155151
} catch (e) {
156-
this.reportError(`${e}`, sourceSpan);
152+
this._reportError(`${e}`, sourceSpan);
157153
return [];
158154
}
159155
}
@@ -163,8 +159,8 @@ export class BindingParser {
163159
targetProps: BoundProperty[]) {
164160
if (_isAnimationLabel(name)) {
165161
name = name.substring(1);
166-
if (isPresent(value) && value.length > 0) {
167-
this.reportError(
162+
if (value) {
163+
this._reportError(
168164
`Assigning animation triggers via @prop="exp" attributes with an expression is invalid.` +
169165
` Use property bindings (e.g. [@prop]="exp") or use an attribute without a value (e.g. @prop) instead.`,
170166
sourceSpan, ParseErrorLevel.FATAL);
@@ -239,7 +235,7 @@ export class BindingParser {
239235
this._checkPipes(ast, sourceSpan);
240236
return ast;
241237
} catch (e) {
242-
this.reportError(`${e}`, sourceSpan);
238+
this._reportError(`${e}`, sourceSpan);
243239
return this._exprParser.wrapLiteralPrimitive('ERROR', sourceInfo);
244240
}
245241
}
@@ -271,7 +267,7 @@ export class BindingParser {
271267
`\n1. If '${elementName}' is an Angular component and it has '${boundPropertyName}' input, then verify that it is part of this module.` +
272268
`\n2. If '${elementName}' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schemas' of this component to suppress this message.\n`;
273269
}
274-
this.reportError(errorMsg, boundProp.sourceSpan);
270+
this._reportError(errorMsg, boundProp.sourceSpan);
275271
}
276272
} else {
277273
if (parts[0] == ATTRIBUTE_PREFIX) {
@@ -299,7 +295,7 @@ export class BindingParser {
299295
bindingType = PropertyBindingType.Style;
300296
securityContext = SecurityContext.STYLE;
301297
} else {
302-
this.reportError(`Invalid property name '${boundProp.name}'`, boundProp.sourceSpan);
298+
this._reportError(`Invalid property name '${boundProp.name}'`, boundProp.sourceSpan);
303299
bindingType = null;
304300
securityContext = null;
305301
}
@@ -336,13 +332,13 @@ export class BindingParser {
336332
break;
337333

338334
default:
339-
this.reportError(
335+
this._reportError(
340336
`The provided animation output phase value "${phase}" for "@${eventName}" is not supported (use start or done)`,
341337
sourceSpan);
342338
break;
343339
}
344340
} else {
345-
this.reportError(
341+
this._reportError(
346342
`The animation trigger output event (@${eventName}) is missing its phase value name (start or done are currently supported)`,
347343
sourceSpan);
348344
}
@@ -369,26 +365,26 @@ export class BindingParser {
369365
this._reportExpressionParserErrors(ast.errors, sourceSpan);
370366
}
371367
if (!ast || ast.ast instanceof EmptyExpr) {
372-
this.reportError(`Empty expressions are not allowed`, sourceSpan);
368+
this._reportError(`Empty expressions are not allowed`, sourceSpan);
373369
return this._exprParser.wrapLiteralPrimitive('ERROR', sourceInfo);
374370
}
375371
this._checkPipes(ast, sourceSpan);
376372
return ast;
377373
} catch (e) {
378-
this.reportError(`${e}`, sourceSpan);
374+
this._reportError(`${e}`, sourceSpan);
379375
return this._exprParser.wrapLiteralPrimitive('ERROR', sourceInfo);
380376
}
381377
}
382378

383-
reportError(
379+
private _reportError(
384380
message: string, sourceSpan: ParseSourceSpan,
385381
level: ParseErrorLevel = ParseErrorLevel.FATAL) {
386-
this.errors.push(new ParseError(sourceSpan, message, level));
382+
this._targetErrors.push(new ParseError(sourceSpan, message, level));
387383
}
388384

389385
private _reportExpressionParserErrors(errors: ParserError[], sourceSpan: ParseSourceSpan) {
390386
for (const error of errors) {
391-
this.reportError(error.message, sourceSpan);
387+
this._reportError(error.message, sourceSpan);
392388
}
393389
}
394390

@@ -398,7 +394,7 @@ export class BindingParser {
398394
ast.visit(collector);
399395
collector.pipes.forEach((pipeName) => {
400396
if (!this.pipesByName.has(pipeName)) {
401-
this.reportError(`The pipe '${pipeName}' could not be found`, sourceSpan);
397+
this._reportError(`The pipe '${pipeName}' could not be found`, sourceSpan);
402398
}
403399
});
404400
}
@@ -415,13 +411,13 @@ export class BindingParser {
415411
const report = isAttr ? this._schemaRegistry.validateAttribute(propName) :
416412
this._schemaRegistry.validateProperty(propName);
417413
if (report.error) {
418-
this.reportError(report.msg, sourceSpan, ParseErrorLevel.FATAL);
414+
this._reportError(report.msg, sourceSpan, ParseErrorLevel.FATAL);
419415
}
420416
}
421417
}
422418

423419
export class PipeCollector extends RecursiveAstVisitor {
424-
pipes: Set<string> = new Set<string>();
420+
pipes = new Set<string>();
425421
visitPipe(ast: BindingPipe, context: any): any {
426422
this.pipes.add(ast.name);
427423
ast.exp.visit(this);

0 commit comments

Comments
 (0)