fix(compiler): support empty array and map literals.

This was broken after 152a117d5c

Fixes #8336
This commit is contained in:
Tobias Bosch 2016-04-29 10:43:26 -07:00
parent 5ff31f0713
commit 11955f9b13
4 changed files with 31 additions and 0 deletions

View File

@ -7,6 +7,8 @@ import {
interpolate,
checkBinding,
castByValue,
EMPTY_ARRAY,
EMPTY_MAP,
pureProxy1,
pureProxy2,
pureProxy3,
@ -72,6 +74,8 @@ var impDevModeEqual = devModeEqual;
var impInterpolate = interpolate;
var impCheckBinding = checkBinding;
var impCastByValue = castByValue;
var impEMPTY_ARRAY = EMPTY_ARRAY;
var impEMPTY_MAP = EMPTY_MAP;
export class Identifiers {
static ViewUtils = new CompileIdentifierMetadata({
@ -179,6 +183,11 @@ export class Identifiers {
{name: 'interpolate', moduleUrl: VIEW_UTILS_MODULE_URL, runtime: impInterpolate});
static castByValue = new CompileIdentifierMetadata(
{name: 'castByValue', moduleUrl: VIEW_UTILS_MODULE_URL, runtime: impCastByValue});
static EMPTY_ARRAY = new CompileIdentifierMetadata(
{name: 'EMPTY_ARRAY', moduleUrl: VIEW_UTILS_MODULE_URL, runtime: impEMPTY_ARRAY});
static EMPTY_MAP = new CompileIdentifierMetadata(
{name: 'EMPTY_MAP', moduleUrl: VIEW_UTILS_MODULE_URL, runtime: impEMPTY_MAP});
static pureProxies = [
null,
new CompileIdentifierMetadata(

View File

@ -24,6 +24,7 @@ import {
} from './util';
import {CompilerConfig} from '../config';
import {CompileBinding} from './compile_binding';
import {Identifiers} from '../identifiers';
export class CompileView implements NameResolver {
public viewType: ViewType;
@ -155,6 +156,9 @@ export class CompileView implements NameResolver {
}
createLiteralArray(values: o.Expression[]): o.Expression {
if (values.length === 0) {
return o.importExpr(Identifiers.EMPTY_ARRAY);
}
var proxyExpr = o.THIS_EXPR.prop(`_arr_${this.literalArrayCount++}`);
var proxyParams: o.FnParam[] = [];
var proxyReturnEntries: o.Expression[] = [];
@ -169,6 +173,9 @@ export class CompileView implements NameResolver {
}
createLiteralMap(entries: Array<Array<string | o.Expression>>): o.Expression {
if (entries.length === 0) {
return o.importExpr(Identifiers.EMPTY_MAP);
}
var proxyExpr = o.THIS_EXPR.prop(`_map_${this.literalMapCount++}`);
var proxyParams: o.FnParam[] = [];
var proxyReturnEntries: Array<Array<string | o.Expression>> = [];

View File

@ -156,6 +156,9 @@ export function castByValue<T>(input: any, value: T): T {
return <T>input;
}
export const EMPTY_ARRAY = CONST_EXPR([]);
export const EMPTY_MAP = CONST_EXPR({});
export function pureProxy1<P0, R>(fn: (p0: P0) => R): (p0: P0) => R {
var result: R;
var v0;

View File

@ -371,6 +371,12 @@ export function main() {
expect(renderLog.loggedValues).toEqual([[1, 2]]);
}));
it('should support empty literal array', fakeAsync(() => {
var ctx = _bindSimpleValue('[]');
ctx.detectChanges(false);
expect(renderLog.loggedValues).toEqual([[]]);
}));
it('should support literal array made of expressions', fakeAsync(() => {
var ctx = _bindSimpleValue('[1, a]', TestData);
ctx.componentInstance.a = 2;
@ -395,6 +401,12 @@ export function main() {
expect(renderLog.loggedValues[0]['z']).toEqual(1);
}));
it('should support empty literal map', fakeAsync(() => {
var ctx = _bindSimpleValue('{}');
ctx.detectChanges(false);
expect(renderLog.loggedValues).toEqual([{}]);
}));
it('should support literal maps made of expressions', fakeAsync(() => {
var ctx = _bindSimpleValue('{z: a}');
ctx.componentInstance.a = 1;