refactor(compiler-cli): avoid superfluous parenthesis around statements (#33514)

Previously, due to a bug a `Context` with `isStatement: false` could be
returned in places where a `Context` with `isStatement: true` was
requested. As a result, some statements would be unnecessarily wrapped
in parenthesis.

This commit fixes the bug in `Context#withStatementMode` to always
return a `Context` with the correct `isStatement` value. Note that this
does not have any impact on the generated code other than avoiding some
superfluous parenthesis on certain statements.

PR Close #33514
This commit is contained in:
George Kalpakas 2019-10-31 01:09:38 +02:00 committed by Kara Erickson
parent 624425a288
commit c79d50f38f
4 changed files with 30 additions and 30 deletions

View File

@ -18,7 +18,7 @@ export class Context {
get withExpressionMode(): Context { return this.isStatement ? new Context(false) : this; } get withExpressionMode(): Context { return this.isStatement ? new Context(false) : this; }
get withStatementMode(): Context { return this.isStatement ? new Context(true) : this; } get withStatementMode(): Context { return !this.isStatement ? new Context(true) : this; }
} }
const BINARY_OPERATORS = new Map<BinaryOperator, ts.BinaryOperator>([ const BINARY_OPERATORS = new Map<BinaryOperator, ts.BinaryOperator>([

View File

@ -1532,8 +1532,8 @@ describe('compiler compliance', () => {
} }
if (rf & 2) { if (rf & 2) {
var $tmp$; var $tmp$;
$r3$.ɵɵqueryRefresh(($tmp$ = $r3$.ɵɵloadQuery())) && (ctx.someDir = $tmp$.first); $r3$.ɵɵqueryRefresh($tmp$ = $r3$.ɵɵloadQuery()) && (ctx.someDir = $tmp$.first);
$r3$.ɵɵqueryRefresh(($tmp$ = $r3$.ɵɵloadQuery())) && (ctx.someDirs = $tmp$); $r3$.ɵɵqueryRefresh($tmp$ = $r3$.ɵɵloadQuery()) && (ctx.someDirs = $tmp$);
} }
}, },
decls: 1, decls: 1,
@ -1591,8 +1591,8 @@ describe('compiler compliance', () => {
} }
if (rf & 2) { if (rf & 2) {
var $tmp$; var $tmp$;
$r3$.ɵɵqueryRefresh(($tmp$ = $r3$.ɵɵloadQuery())) && (ctx.myRef = $tmp$.first); $r3$.ɵɵqueryRefresh($tmp$ = $r3$.ɵɵloadQuery()) && (ctx.myRef = $tmp$.first);
$r3$.ɵɵqueryRefresh(($tmp$ = $r3$.ɵɵloadQuery())) && (ctx.myRefs = $tmp$); $r3$.ɵɵqueryRefresh($tmp$ = $r3$.ɵɵloadQuery()) && (ctx.myRefs = $tmp$);
} }
}, },
@ -1642,8 +1642,8 @@ describe('compiler compliance', () => {
} }
if (rf & 2) { if (rf & 2) {
var $tmp$; var $tmp$;
$r3$.ɵɵqueryRefresh(($tmp$ = $r3$.ɵɵloadQuery())) && (ctx.someDir = $tmp$.first); $r3$.ɵɵqueryRefresh($tmp$ = $r3$.ɵɵloadQuery()) && (ctx.someDir = $tmp$.first);
$r3$.ɵɵqueryRefresh(($tmp$ = $r3$.ɵɵloadQuery())) && (ctx.foo = $tmp$.first); $r3$.ɵɵqueryRefresh($tmp$ = $r3$.ɵɵloadQuery()) && (ctx.foo = $tmp$.first);
} }
}, },
decls: 1, decls: 1,
@ -1708,10 +1708,10 @@ describe('compiler compliance', () => {
} }
if (rf & 2) { if (rf & 2) {
var $tmp$; var $tmp$;
$r3$.ɵɵqueryRefresh(($tmp$ = $r3$.ɵɵloadQuery())) && (ctx.myRef = $tmp$.first); $r3$.ɵɵqueryRefresh($tmp$ = $r3$.ɵɵloadQuery()) && (ctx.myRef = $tmp$.first);
$r3$.ɵɵqueryRefresh(($tmp$ = $r3$.ɵɵloadQuery())) && (ctx.someDir = $tmp$.first); $r3$.ɵɵqueryRefresh($tmp$ = $r3$.ɵɵloadQuery()) && (ctx.someDir = $tmp$.first);
$r3$.ɵɵqueryRefresh(($tmp$ = $r3$.ɵɵloadQuery())) && (ctx.myRefs = $tmp$); $r3$.ɵɵqueryRefresh($tmp$ = $r3$.ɵɵloadQuery()) && (ctx.myRefs = $tmp$);
$r3$.ɵɵqueryRefresh(($tmp$ = $r3$.ɵɵloadQuery())) && (ctx.someDirs = $tmp$); $r3$.ɵɵqueryRefresh($tmp$ = $r3$.ɵɵloadQuery()) && (ctx.someDirs = $tmp$);
} }
}, },
@ -1769,8 +1769,8 @@ describe('compiler compliance', () => {
} }
if (rf & 2) { if (rf & 2) {
var $tmp$; var $tmp$;
$r3$.ɵɵqueryRefresh(($tmp$ = $r3$.ɵɵloadQuery())) && (ctx.someDir = $tmp$.first); $r3$.ɵɵqueryRefresh($tmp$ = $r3$.ɵɵloadQuery()) && (ctx.someDir = $tmp$.first);
$r3$.ɵɵqueryRefresh(($tmp$ = $r3$.ɵɵloadQuery())) && (ctx.someDirList = $tmp$); $r3$.ɵɵqueryRefresh($tmp$ = $r3$.ɵɵloadQuery()) && (ctx.someDirList = $tmp$);
} }
}, },
ngContentSelectors: _c0, ngContentSelectors: _c0,
@ -1829,8 +1829,8 @@ describe('compiler compliance', () => {
} }
if (rf & 2) { if (rf & 2) {
var $tmp$; var $tmp$;
$r3$.ɵɵqueryRefresh(($tmp$ = $r3$.ɵɵloadQuery())) && (ctx.myRef = $tmp$.first); $r3$.ɵɵqueryRefresh($tmp$ = $r3$.ɵɵloadQuery()) && (ctx.myRef = $tmp$.first);
$r3$.ɵɵqueryRefresh(($tmp$ = $r3$.ɵɵloadQuery())) && (ctx.myRefs = $tmp$); $r3$.ɵɵqueryRefresh($tmp$ = $r3$.ɵɵloadQuery()) && (ctx.myRefs = $tmp$);
} }
}, },
@ -1888,8 +1888,8 @@ describe('compiler compliance', () => {
} }
if (rf & 2) { if (rf & 2) {
var $tmp$; var $tmp$;
$r3$.ɵɵqueryRefresh(($tmp$ = $r3$.ɵɵloadQuery())) && (ctx.someDir = $tmp$.first); $r3$.ɵɵqueryRefresh($tmp$ = $r3$.ɵɵloadQuery()) && (ctx.someDir = $tmp$.first);
$r3$.ɵɵqueryRefresh(($tmp$ = $r3$.ɵɵloadQuery())) && (ctx.foo = $tmp$.first); $r3$.ɵɵqueryRefresh($tmp$ = $r3$.ɵɵloadQuery()) && (ctx.foo = $tmp$.first);
} }
}, },
ngContentSelectors: $_c1$, ngContentSelectors: $_c1$,
@ -1955,10 +1955,10 @@ describe('compiler compliance', () => {
} }
if (rf & 2) { if (rf & 2) {
var $tmp$; var $tmp$;
$r3$.ɵɵqueryRefresh(($tmp$ = $r3$.ɵɵloadQuery())) && (ctx.myRef = $tmp$.first); $r3$.ɵɵqueryRefresh($tmp$ = $r3$.ɵɵloadQuery()) && (ctx.myRef = $tmp$.first);
$r3$.ɵɵqueryRefresh(($tmp$ = $r3$.ɵɵloadQuery())) && (ctx.someDir = $tmp$.first); $r3$.ɵɵqueryRefresh($tmp$ = $r3$.ɵɵloadQuery()) && (ctx.someDir = $tmp$.first);
$r3$.ɵɵqueryRefresh(($tmp$ = $r3$.ɵɵloadQuery())) && (ctx.myRefs = $tmp$); $r3$.ɵɵqueryRefresh($tmp$ = $r3$.ɵɵloadQuery()) && (ctx.myRefs = $tmp$);
$r3$.ɵɵqueryRefresh(($tmp$ = $r3$.ɵɵloadQuery())) && (ctx.someDirs = $tmp$); $r3$.ɵɵqueryRefresh($tmp$ = $r3$.ɵɵloadQuery()) && (ctx.someDirs = $tmp$);
} }
}, },
@ -3251,7 +3251,7 @@ describe('compiler compliance', () => {
} }
if (rf & 2) { if (rf & 2) {
var $tmp$; var $tmp$;
$r3$.ɵɵqueryRefresh(($tmp$ = $r3$.ɵɵloadQuery())) && (ctx.something = $tmp$.first); $r3$.ɵɵqueryRefresh($tmp$ = $r3$.ɵɵloadQuery()) && (ctx.something = $tmp$.first);
} }
} }
}); });
@ -3297,7 +3297,7 @@ describe('compiler compliance', () => {
} }
if (rf & 2) { if (rf & 2) {
var $tmp$; var $tmp$;
$r3$.ɵɵqueryRefresh(($tmp$ = $r3$.ɵɵloadQuery())) && (ctx.something = $tmp$); $r3$.ɵɵqueryRefresh($tmp$ = $r3$.ɵɵloadQuery()) && (ctx.something = $tmp$);
} }
} }
}); });
@ -3341,7 +3341,7 @@ describe('compiler compliance', () => {
} }
if (rf & 2) { if (rf & 2) {
var $tmp$; var $tmp$;
$r3$.ɵɵqueryRefresh(($tmp$ = $r3$.ɵɵloadQuery())) && (ctx.something = $tmp$.first); $r3$.ɵɵqueryRefresh($tmp$ = $r3$.ɵɵloadQuery()) && (ctx.something = $tmp$.first);
} }
} }
}); });
@ -3387,7 +3387,7 @@ describe('compiler compliance', () => {
} }
if (rf & 2) { if (rf & 2) {
var $tmp$; var $tmp$;
$r3$.ɵɵqueryRefresh(($tmp$ = $r3$.ɵɵloadQuery())) && (ctx.something = $tmp$); $r3$.ɵɵqueryRefresh($tmp$ = $r3$.ɵɵloadQuery()) && (ctx.something = $tmp$);
} }
} }
}); });

View File

@ -183,9 +183,9 @@ describe('compiler compliance: dependency injection', () => {
factory: function MyService_Factory(t) { factory: function MyService_Factory(t) {
var r = null; var r = null;
if (t) { if (t) {
(r = new t()); r = new t();
} else { } else {
(r = (() => new MyAlternateFactory())($r3$.ɵɵinject(SomeDep))); r = (() => new MyAlternateFactory())($r3$.ɵɵinject(SomeDep));
} }
return r; return r;
}, },
@ -258,9 +258,9 @@ describe('compiler compliance: dependency injection', () => {
factory: function MyService_Factory(t) { factory: function MyService_Factory(t) {
var r = null; var r = null;
if (t) { if (t) {
(r = new t()); r = new t();
} else { } else {
(r = new MyAlternateService($r3$.ɵɵinject(SomeDep))); r = new MyAlternateService($r3$.ɵɵinject(SomeDep));
} }
return r; return r;
}, },

View File

@ -167,7 +167,7 @@ runInEachFileSystem(os => {
expect(jsContents).toContain('factory: function Service_Factory(t) { var r = null; if (t) {'); expect(jsContents).toContain('factory: function Service_Factory(t) { var r = null; if (t) {');
expect(jsContents).toContain('return new (t || Service)(i0.ɵɵinject(Dep));'); expect(jsContents).toContain('return new (t || Service)(i0.ɵɵinject(Dep));');
expect(jsContents) expect(jsContents)
.toContain('(r = (function (dep) { return new Service(dep); })(i0.ɵɵinject(Dep)));'); .toContain('r = (function (dep) { return new Service(dep); })(i0.ɵɵinject(Dep));');
expect(jsContents).toContain('return r; }, providedIn: \'root\' });'); expect(jsContents).toContain('return r; }, providedIn: \'root\' });');
expect(jsContents).not.toContain('__decorate'); expect(jsContents).not.toContain('__decorate');
const dtsContents = env.getContents('test.d.ts'); const dtsContents = env.getContents('test.d.ts');