feat(compiler): support a non-null postfix assert (#16672)

Template expressions can now use a post-fix `!` operator
that asserts the target of the operator is not null. This is
similar to the TypeScript non-null assert operator. Expressions
generated in factories will be generated with the non-null assert
operator.

Closes: #10855
This commit is contained in:
Chuck Jazdzewski
2017-05-11 10:15:54 -07:00
committed by Jason Aden
parent 2eca6e67e1
commit b9521b568f
16 changed files with 122 additions and 5 deletions

View File

@ -112,6 +112,12 @@ export function main() {
checkAction('!!!true');
});
it('should parse postfix ! expression', () => {
checkAction('true!');
checkAction('a!.b');
checkAction('a!!!!.b');
});
it('should parse multiplicative expressions',
() => { checkAction('3*4/2%5', '3 * 4 / 2 % 5'); });

View File

@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/
import {AST, AstVisitor, Binary, BindingPipe, Chain, Conditional, FunctionCall, ImplicitReceiver, Interpolation, KeyedRead, KeyedWrite, LiteralArray, LiteralMap, LiteralPrimitive, MethodCall, PrefixNot, PropertyRead, PropertyWrite, Quote, SafeMethodCall, SafePropertyRead} from '../../src/expression_parser/ast';
import {AST, AstVisitor, Binary, BindingPipe, Chain, Conditional, FunctionCall, ImplicitReceiver, Interpolation, KeyedRead, KeyedWrite, LiteralArray, LiteralMap, LiteralPrimitive, MethodCall, NonNullAssert, PrefixNot, PropertyRead, PropertyWrite, Quote, SafeMethodCall, SafePropertyRead} from '../../src/expression_parser/ast';
import {DEFAULT_INTERPOLATION_CONFIG, InterpolationConfig} from '../../src/ml_parser/interpolation_config';
class Unparser implements AstVisitor {
@ -156,6 +156,11 @@ class Unparser implements AstVisitor {
this._visit(ast.expression);
}
visitNonNullAssert(ast: NonNullAssert, context: any) {
this._visit(ast.expression);
this._expression += '!';
}
visitSafePropertyRead(ast: SafePropertyRead, context: any) {
this._visit(ast.receiver);
this._expression += `?.${ast.name}`;