Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(animations): always quote string map key values in AOT code #13602

Merged
merged 2 commits into from
Dec 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions modules/@angular/compiler/src/animation/animation_compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class _AnimationBuilder implements AnimationAstVisitor {
ast.styles.forEach(entry => {
const entries =
Object.keys(entry).map((key): [string, o.Expression] => [key, o.literal(entry[key])]);
stylesArr.push(o.literalMap(entries));
stylesArr.push(o.literalMap(entries, null, true));
});

return o.importExpr(createIdentifier(Identifiers.AnimationStyles)).instantiate([
Expand Down Expand Up @@ -322,12 +322,13 @@ class _AnimationBuilder implements AnimationAstVisitor {
if (isPresent(value)) {
const styleMap: any[] = [];
Object.keys(value).forEach(key => { styleMap.push([key, o.literal(value[key])]); });
variableValue = o.literalMap(styleMap);
variableValue = o.literalMap(styleMap, null, true);
}
lookupMap.push([stateName, variableValue]);
});

const compiledStatesMapStmt = this._statesMapVar.set(o.literalMap(lookupMap)).toDeclStmt();
const compiledStatesMapStmt =
this._statesMapVar.set(o.literalMap(lookupMap, null, true)).toDeclStmt();
const statements: o.Statement[] = [compiledStatesMapStmt, fnStatement];

return new AnimationEntryCompileResult(this.animationName, statements, fnVariable);
Expand Down
6 changes: 4 additions & 2 deletions modules/@angular/compiler/src/output/output_ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -894,8 +894,10 @@ export function literalArr(values: Expression[], type: Type = null): LiteralArra
return new LiteralArrayExpr(values, type);
}

export function literalMap(values: [string, Expression][], type: MapType = null): LiteralMapExpr {
return new LiteralMapExpr(values.map(entry => new LiteralMapEntry(entry[0], entry[1])), type);
export function literalMap(
values: [string, Expression][], type: MapType = null, quoted: boolean = false): LiteralMapExpr {
return new LiteralMapExpr(
values.map(entry => new LiteralMapEntry(entry[0], entry[1], quoted)), type);
}

export function not(expr: Expression): NotExpr {
Expand Down
8 changes: 8 additions & 0 deletions modules/@angular/compiler/test/output/ts_emitter_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ export function main() {
expect(emitStmt(o.literalMap([['someKey', o.literal(1)]]).toStmt())).toEqual(`{someKey: 1};`);
});

it('should apply quotes to each entry within a map produced with literalMap when true', () => {
expect(
emitStmt(
o.literalMap([['a', o.literal('a')], ['*', o.literal('star')]], null, true).toStmt())
.replace(/\s+/gm, ''))
.toEqual(`{'a':'a','*':'star'};`);
});

it('should support blank literals', () => {
expect(emitStmt(o.literal(null).toStmt())).toEqual('(null as any);');
expect(emitStmt(o.literal(undefined).toStmt())).toEqual('(undefined as any);');
Expand Down