refactor(compiler): Break up parseTemplateBindings() for microsyntax (#35812)

This commit is purely a refactoring of the logic in
`parseTemplateBindings` method for parsing the microsyntax expression.
This is done to enable the introduction of `keySpan` and `valueSpan` in
subsequent PR.

For a detailed explanation of this work and the subsequent work items,
please see https://docs.google.com/document/d/1mEVF2pSSMSnOloqOPQTYNiAJO0XQxA1H0BZyESASOrE/edit?usp=sharing

PR Close #35812
This commit is contained in:
Keen Yee Liau
2020-03-02 16:38:55 -08:00
committed by Matias Niemelä
parent 876aa5a78a
commit 716d50aa21
2 changed files with 256 additions and 60 deletions

View File

@ -270,6 +270,14 @@ describe('parser', () => {
binding => binding.expression != null ? binding.expression.source : null);
}
function humanize(bindings: TemplateBinding[]): Array<[string, string | null, boolean]> {
return bindings.map(binding => {
const {key, expression, name, keyIsVar} = binding;
const value = keyIsVar ? name : (expression ? expression.source : expression);
return [key, value, keyIsVar];
});
}
it('should parse a key without a value',
() => { expect(keys(parseTemplateBindings('a', ''))).toEqual(['a']); });
@ -317,6 +325,44 @@ describe('parser', () => {
expect(bindings[0].expression !.location).toEqual('location');
});
it('should support common usage of ngIf', () => {
const bindings = parseTemplateBindings('ngIf', 'cond | pipe as foo, let x; ngIf as y');
expect(humanize(bindings)).toEqual([
// [ key, value, keyIsVar ]
['ngIf', 'cond | pipe ', false],
['foo', 'ngIf', true],
['x', '$implicit', true],
['y', 'ngIf', true],
]);
});
it('should support common usage of ngFor', () => {
let bindings: TemplateBinding[];
bindings = parseTemplateBindings(
'ngFor', 'let item; of items | slice:0:1 as collection, trackBy: func; index as i');
expect(humanize(bindings)).toEqual([
// [ key, value, keyIsVar ]
['ngFor', null, false],
['item', '$implicit', true],
['ngForOf', 'items | slice:0:1 ', false],
['collection', 'ngForOf', true],
['ngForTrackBy', 'func', false],
['i', 'index', true],
]);
bindings = parseTemplateBindings(
'ngFor', 'let item, of: [1,2,3] | pipe as items; let i=index, count as len');
expect(humanize(bindings)).toEqual([
// [ key, value, keyIsVar ]
['ngFor', null, false],
['item', '$implicit', true],
['ngForOf', '[1,2,3] | pipe ', false],
['items', 'ngForOf', true],
['i', 'index', true],
['len', 'count', true],
]);
});
it('should support let notation', () => {
let bindings = parseTemplateBindings('key', 'let i');
expect(keyValues(bindings)).toEqual(['key', 'let i=$implicit']);