Skip to content

Commit

Permalink
fix(compiler): fix two existing expression transformer issues (#28523)
Browse files Browse the repository at this point in the history
Testing of Ivy revealed two bugs in the AstMemoryEfficientTransformer
class, a part of existing View Engine compiler infrastructure that's
reused in Ivy. These bugs cause AST expressions not to be transformed
under certain circumstances.

The fix is simple, and tests are added to ensure the specific expression
forms that trigger the issue compile properly under Ivy.

PR Close #28523
  • Loading branch information
alxhub authored and mhevery committed Feb 14, 2019
1 parent dba2a40 commit 09af7ea
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
43 changes: 43 additions & 0 deletions packages/compiler-cli/test/ngtsc/ngtsc_spec.ts
Expand Up @@ -624,6 +624,49 @@ describe('ngtsc behavioral tests', () => {
'i0.ɵNgModuleDefWithMeta<TestModule, [typeof TestPipe, typeof TestCmp], never, never>');
});

describe('former View Engine AST transform bugs', () => {
it('should compile array literals behind conditionals', () => {
env.tsconfig();
env.write('test.ts', `
import {Component} from '@angular/core';
@Component({
selector: 'test',
template: '{{value ? "yes" : [no]}}',
})
class TestCmp {
value = true;
no = 'no';
}
`);

env.driveMain();
expect(env.getContents('test.js')).toContain('i0.ɵpureFunction1');
});

it('should compile array literals inside function arguments', () => {
env.tsconfig();
env.write('test.ts', `
import {Component} from '@angular/core';
@Component({
selector: 'test',
template: '{{fn([test])}}',
})
class TestCmp {
fn(arg: any): string {
return 'test';
}
test = 'test';
}
`);

env.driveMain();
expect(env.getContents('test.js')).toContain('i0.ɵpureFunction1');
});
});

describe('unwrapping ModuleWithProviders functions', () => {
it('should extract the generic type and include it in the module\'s declaration', () => {
env.tsconfig();
Expand Down
5 changes: 3 additions & 2 deletions packages/compiler/src/expression_parser/ast.ts
Expand Up @@ -477,8 +477,9 @@ export class AstMemoryEfficientTransformer implements AstVisitor {

visitMethodCall(ast: MethodCall, context: any): AST {
const receiver = ast.receiver.visit(this);
if (receiver !== ast.receiver) {
return new MethodCall(ast.span, receiver, ast.name, this.visitAll(ast.args));
const args = this.visitAll(ast.args);
if (receiver !== ast.receiver || args !== ast.args) {
return new MethodCall(ast.span, receiver, ast.name, args);
}
return ast;
}
Expand Down

0 comments on commit 09af7ea

Please sign in to comment.