Skip to content

Commit 11955f9

Browse files
committed
fix(compiler): support empty array and map literals.
This was broken after 152a117 Fixes angular#8336
1 parent 5ff31f0 commit 11955f9

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed

modules/angular2/src/compiler/identifiers.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import {
77
interpolate,
88
checkBinding,
99
castByValue,
10+
EMPTY_ARRAY,
11+
EMPTY_MAP,
1012
pureProxy1,
1113
pureProxy2,
1214
pureProxy3,
@@ -72,6 +74,8 @@ var impDevModeEqual = devModeEqual;
7274
var impInterpolate = interpolate;
7375
var impCheckBinding = checkBinding;
7476
var impCastByValue = castByValue;
77+
var impEMPTY_ARRAY = EMPTY_ARRAY;
78+
var impEMPTY_MAP = EMPTY_MAP;
7579

7680
export class Identifiers {
7781
static ViewUtils = new CompileIdentifierMetadata({
@@ -179,6 +183,11 @@ export class Identifiers {
179183
{name: 'interpolate', moduleUrl: VIEW_UTILS_MODULE_URL, runtime: impInterpolate});
180184
static castByValue = new CompileIdentifierMetadata(
181185
{name: 'castByValue', moduleUrl: VIEW_UTILS_MODULE_URL, runtime: impCastByValue});
186+
static EMPTY_ARRAY = new CompileIdentifierMetadata(
187+
{name: 'EMPTY_ARRAY', moduleUrl: VIEW_UTILS_MODULE_URL, runtime: impEMPTY_ARRAY});
188+
static EMPTY_MAP = new CompileIdentifierMetadata(
189+
{name: 'EMPTY_MAP', moduleUrl: VIEW_UTILS_MODULE_URL, runtime: impEMPTY_MAP});
190+
182191
static pureProxies = [
183192
null,
184193
new CompileIdentifierMetadata(

modules/angular2/src/compiler/view_compiler/compile_view.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
} from './util';
2525
import {CompilerConfig} from '../config';
2626
import {CompileBinding} from './compile_binding';
27+
import {Identifiers} from '../identifiers';
2728

2829
export class CompileView implements NameResolver {
2930
public viewType: ViewType;
@@ -155,6 +156,9 @@ export class CompileView implements NameResolver {
155156
}
156157

157158
createLiteralArray(values: o.Expression[]): o.Expression {
159+
if (values.length === 0) {
160+
return o.importExpr(Identifiers.EMPTY_ARRAY);
161+
}
158162
var proxyExpr = o.THIS_EXPR.prop(`_arr_${this.literalArrayCount++}`);
159163
var proxyParams: o.FnParam[] = [];
160164
var proxyReturnEntries: o.Expression[] = [];
@@ -169,6 +173,9 @@ export class CompileView implements NameResolver {
169173
}
170174

171175
createLiteralMap(entries: Array<Array<string | o.Expression>>): o.Expression {
176+
if (entries.length === 0) {
177+
return o.importExpr(Identifiers.EMPTY_MAP);
178+
}
172179
var proxyExpr = o.THIS_EXPR.prop(`_map_${this.literalMapCount++}`);
173180
var proxyParams: o.FnParam[] = [];
174181
var proxyReturnEntries: Array<Array<string | o.Expression>> = [];

modules/angular2/src/core/linker/view_utils.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ export function castByValue<T>(input: any, value: T): T {
156156
return <T>input;
157157
}
158158

159+
export const EMPTY_ARRAY = CONST_EXPR([]);
160+
export const EMPTY_MAP = CONST_EXPR({});
161+
159162
export function pureProxy1<P0, R>(fn: (p0: P0) => R): (p0: P0) => R {
160163
var result: R;
161164
var v0;

modules/angular2/test/core/linker/change_detection_integration_spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,12 @@ export function main() {
371371
expect(renderLog.loggedValues).toEqual([[1, 2]]);
372372
}));
373373

374+
it('should support empty literal array', fakeAsync(() => {
375+
var ctx = _bindSimpleValue('[]');
376+
ctx.detectChanges(false);
377+
expect(renderLog.loggedValues).toEqual([[]]);
378+
}));
379+
374380
it('should support literal array made of expressions', fakeAsync(() => {
375381
var ctx = _bindSimpleValue('[1, a]', TestData);
376382
ctx.componentInstance.a = 2;
@@ -395,6 +401,12 @@ export function main() {
395401
expect(renderLog.loggedValues[0]['z']).toEqual(1);
396402
}));
397403

404+
it('should support empty literal map', fakeAsync(() => {
405+
var ctx = _bindSimpleValue('{}');
406+
ctx.detectChanges(false);
407+
expect(renderLog.loggedValues).toEqual([{}]);
408+
}));
409+
398410
it('should support literal maps made of expressions', fakeAsync(() => {
399411
var ctx = _bindSimpleValue('{z: a}');
400412
ctx.componentInstance.a = 1;

0 commit comments

Comments
 (0)