X Tutup
Skip to content

Commit 1ad2a02

Browse files
fix(core): properly evaluate expressions with conditional and boolean operators
Fixes angular#8235 Fixes angular#8244 Closes angular#8282
1 parent 1e8864c commit 1ad2a02

File tree

5 files changed

+34
-4
lines changed

5 files changed

+34
-4
lines changed

modules/angular2/src/compiler/output/abstract_emitter.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,11 +264,13 @@ export abstract class AbstractEmitterVisitor implements o.StatementVisitor, o.Ex
264264
abstract visitExternalExpr(ast: o.ExternalExpr, ctx: EmitterVisitorContext): any;
265265

266266
visitConditionalExpr(ast: o.ConditionalExpr, ctx: EmitterVisitorContext): any {
267+
ctx.print(`(`);
267268
ast.condition.visitExpression(this, ctx);
268269
ctx.print('? ');
269270
ast.trueCase.visitExpression(this, ctx);
270271
ctx.print(': ');
271272
ast.falseCase.visitExpression(this, ctx);
273+
ctx.print(`)`);
272274
return null;
273275
}
274276
visitNotExpr(ast: o.NotExpr, ctx: EmitterVisitorContext): any {
@@ -421,4 +423,4 @@ function _createIndent(count: number): string {
421423
res += ' ';
422424
}
423425
return res;
424-
}
426+
}

modules/angular2/test/compiler/output/dart_emitter_spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ export function main() {
136136
expect(emitStmt(o.not(someVar).toStmt())).toEqual('!someVar;');
137137
expect(
138138
emitStmt(someVar.conditional(o.variable('trueCase'), o.variable('falseCase')).toStmt()))
139-
.toEqual('someVar? trueCase: falseCase;');
139+
.toEqual('(someVar? trueCase: falseCase);');
140140

141141
expect(emitStmt(lhs.equals(rhs).toStmt())).toEqual('(lhs == rhs);');
142142
expect(emitStmt(lhs.notEquals(rhs).toStmt())).toEqual('(lhs != rhs);');

modules/angular2/test/compiler/output/js_emitter_spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ export function main() {
125125
expect(emitStmt(o.not(someVar).toStmt())).toEqual('!someVar;');
126126
expect(
127127
emitStmt(someVar.conditional(o.variable('trueCase'), o.variable('falseCase')).toStmt()))
128-
.toEqual('someVar? trueCase: falseCase;');
128+
.toEqual('(someVar? trueCase: falseCase);');
129129

130130
expect(emitStmt(lhs.equals(rhs).toStmt())).toEqual('(lhs == rhs);');
131131
expect(emitStmt(lhs.notEquals(rhs).toStmt())).toEqual('(lhs != rhs);');

modules/angular2/test/compiler/output/ts_emitter_spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ export function main() {
126126
expect(emitStmt(o.not(someVar).toStmt())).toEqual('!someVar;');
127127
expect(
128128
emitStmt(someVar.conditional(o.variable('trueCase'), o.variable('falseCase')).toStmt()))
129-
.toEqual('someVar? trueCase: falseCase;');
129+
.toEqual('(someVar? trueCase: falseCase);');
130130

131131
expect(emitStmt(lhs.equals(rhs).toStmt())).toEqual('(lhs == rhs);');
132132
expect(emitStmt(lhs.notEquals(rhs).toStmt())).toEqual('(lhs != rhs);');

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,35 @@ function declareTests(isJit: boolean) {
7171
async.done();
7272
});
7373
}));
74+
});
75+
76+
describe('expressions', () => {
7477

78+
it('should evaluate conditional and boolean operators with right precedence - #8244',
79+
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
80+
tcb.overrideView(MyComp,
81+
new ViewMetadata({template: `{{'red' + (true ? ' border' : '')}}`}))
82+
.createAsync(MyComp)
83+
.then((fixture) => {
84+
fixture.detectChanges();
85+
expect(fixture.nativeElement).toHaveText('red border');
86+
async.done();
87+
});
88+
}));
89+
90+
if (!IS_DART) {
91+
it('should evaluate conditional and unary operators with right precedence - #8235',
92+
inject([TestComponentBuilder, AsyncTestCompleter],
93+
(tcb: TestComponentBuilder, async) => {
94+
tcb.overrideView(MyComp, new ViewMetadata({template: `{{!null?.length}}`}))
95+
.createAsync(MyComp)
96+
.then((fixture) => {
97+
fixture.detectChanges();
98+
expect(fixture.nativeElement).toHaveText('true');
99+
async.done();
100+
});
101+
}));
102+
}
75103
});
76104

77105
describe('providers', () => {

0 commit comments

Comments
 (0)
X Tutup