2016-06-23 12:47:54 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2016-07-08 19:46:49 -04:00
|
|
|
import {ASTWithSource, BindingPipe, Interpolation, ParserError, TemplateBinding} from '@angular/compiler/src/expression_parser/ast';
|
2016-06-08 19:38:52 -04:00
|
|
|
import {Lexer} from '@angular/compiler/src/expression_parser/lexer';
|
2016-10-06 18:22:10 -04:00
|
|
|
import {Parser, SplitInterpolation, TemplateBindingParseResult} from '@angular/compiler/src/expression_parser/parser';
|
2017-03-02 15:12:46 -05:00
|
|
|
import {expect} from '@angular/platform-browser/testing/src/matchers';
|
2016-06-08 19:38:52 -04:00
|
|
|
|
|
|
|
|
2016-07-06 17:06:47 -04:00
|
|
|
import {unparse} from './unparser';
|
|
|
|
import {validate} from './validator';
|
2014-10-28 12:22:38 -04:00
|
|
|
|
|
|
|
export function main() {
|
2016-06-01 20:31:35 -04:00
|
|
|
function createParser() { return new Parser(new Lexer()); }
|
2014-11-04 12:06:46 -05:00
|
|
|
|
2016-07-06 17:06:47 -04:00
|
|
|
function parseAction(text: string, location: any = null): ASTWithSource {
|
2014-12-10 22:21:15 -05:00
|
|
|
return createParser().parseAction(text, location);
|
2014-11-04 18:51:56 -05:00
|
|
|
}
|
|
|
|
|
2016-07-06 17:06:47 -04:00
|
|
|
function parseBinding(text: string, location: any = null): ASTWithSource {
|
2014-12-10 22:21:15 -05:00
|
|
|
return createParser().parseBinding(text, location);
|
2014-11-18 19:38:36 -05:00
|
|
|
}
|
|
|
|
|
2016-07-06 17:06:47 -04:00
|
|
|
function parseTemplateBindingsResult(
|
2016-11-10 16:15:09 -05:00
|
|
|
text: string, location: any = null, prefix?: string): TemplateBindingParseResult {
|
|
|
|
return createParser().parseTemplateBindings(prefix, text, location);
|
2016-07-06 17:06:47 -04:00
|
|
|
}
|
2016-11-10 16:15:09 -05:00
|
|
|
function parseTemplateBindings(
|
|
|
|
text: string, location: any = null, prefix?: string): TemplateBinding[] {
|
|
|
|
return parseTemplateBindingsResult(text, location, prefix).templateBindings;
|
2014-11-03 20:25:16 -05:00
|
|
|
}
|
|
|
|
|
2016-07-06 17:06:47 -04:00
|
|
|
function parseInterpolation(text: string, location: any = null): ASTWithSource {
|
2015-01-08 19:17:56 -05:00
|
|
|
return createParser().parseInterpolation(text, location);
|
|
|
|
}
|
|
|
|
|
2016-10-06 18:22:10 -04:00
|
|
|
function splitInterpolation(text: string, location: any = null): SplitInterpolation {
|
|
|
|
return createParser().splitInterpolation(text, location);
|
|
|
|
}
|
|
|
|
|
2016-07-06 17:06:47 -04:00
|
|
|
function parseSimpleBinding(text: string, location: any = null): ASTWithSource {
|
2015-06-22 11:21:03 -04:00
|
|
|
return createParser().parseSimpleBinding(text, location);
|
|
|
|
}
|
|
|
|
|
2015-12-06 01:06:24 -05:00
|
|
|
function checkInterpolation(exp: string, expected?: string) {
|
2016-11-12 08:08:58 -05:00
|
|
|
const ast = parseInterpolation(exp);
|
2017-03-02 12:37:01 -05:00
|
|
|
if (expected == null) expected = exp;
|
2015-12-06 01:06:24 -05:00
|
|
|
expect(unparse(ast)).toEqual(expected);
|
2016-07-06 17:06:47 -04:00
|
|
|
validate(ast);
|
2015-12-06 01:06:24 -05:00
|
|
|
}
|
|
|
|
|
2015-08-12 19:26:21 -04:00
|
|
|
function checkBinding(exp: string, expected?: string) {
|
2016-11-12 08:08:58 -05:00
|
|
|
const ast = parseBinding(exp);
|
2017-03-02 12:37:01 -05:00
|
|
|
if (expected == null) expected = exp;
|
2015-08-12 19:26:21 -04:00
|
|
|
expect(unparse(ast)).toEqual(expected);
|
2016-07-06 17:06:47 -04:00
|
|
|
validate(ast);
|
2015-06-10 05:11:01 -04:00
|
|
|
}
|
|
|
|
|
2015-08-12 19:26:21 -04:00
|
|
|
function checkAction(exp: string, expected?: string) {
|
2016-11-12 08:08:58 -05:00
|
|
|
const ast = parseAction(exp);
|
2017-03-02 12:37:01 -05:00
|
|
|
if (expected == null) expected = exp;
|
2015-08-12 19:26:21 -04:00
|
|
|
expect(unparse(ast)).toEqual(expected);
|
2016-07-06 17:06:47 -04:00
|
|
|
validate(ast);
|
|
|
|
}
|
|
|
|
|
|
|
|
function expectError(ast: {errors: ParserError[]}, message: string) {
|
2016-11-12 08:08:58 -05:00
|
|
|
for (const error of ast.errors) {
|
2016-07-06 17:06:47 -04:00
|
|
|
if (error.message.indexOf(message) >= 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2016-10-20 18:24:58 -04:00
|
|
|
const errMsgs = ast.errors.map(err => err.message).join('\n');
|
|
|
|
throw Error(
|
|
|
|
`Expected an error containing "${message}" to be reported, but got the errors:\n` +
|
|
|
|
errMsgs);
|
2014-11-03 20:25:16 -05:00
|
|
|
}
|
|
|
|
|
2016-07-06 17:06:47 -04:00
|
|
|
function expectActionError(text: string, message: string) {
|
|
|
|
expectError(validate(parseAction(text)), message);
|
2016-06-08 19:38:52 -04:00
|
|
|
}
|
2014-10-28 12:22:38 -04:00
|
|
|
|
2016-07-06 17:06:47 -04:00
|
|
|
function expectBindingError(text: string, message: string) {
|
|
|
|
expectError(validate(parseBinding(text)), message);
|
2016-06-08 19:38:52 -04:00
|
|
|
}
|
2014-11-18 19:38:36 -05:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
describe('parser', () => {
|
|
|
|
describe('parseAction', () => {
|
|
|
|
it('should parse numbers', () => { checkAction('1'); });
|
2014-10-31 02:47:22 -04:00
|
|
|
|
2015-08-12 19:26:21 -04:00
|
|
|
it('should parse strings', () => {
|
2016-06-08 19:38:52 -04:00
|
|
|
checkAction('\'1\'', '"1"');
|
2015-08-12 19:26:21 -04:00
|
|
|
checkAction('"1"');
|
|
|
|
});
|
2014-10-31 02:47:22 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
it('should parse null', () => { checkAction('null'); });
|
2014-10-31 02:47:22 -04:00
|
|
|
|
2016-08-04 20:04:30 -04:00
|
|
|
it('should parse undefined', () => { checkAction('undefined'); });
|
|
|
|
|
2015-08-12 19:26:21 -04:00
|
|
|
it('should parse unary - expressions', () => {
|
2016-06-08 19:38:52 -04:00
|
|
|
checkAction('-1', '0 - 1');
|
|
|
|
checkAction('+1', '1');
|
2015-08-12 19:26:21 -04:00
|
|
|
});
|
2014-10-31 02:47:22 -04:00
|
|
|
|
2015-08-12 19:26:21 -04:00
|
|
|
it('should parse unary ! expressions', () => {
|
2016-06-08 19:38:52 -04:00
|
|
|
checkAction('!true');
|
|
|
|
checkAction('!!true');
|
|
|
|
checkAction('!!!true');
|
2015-08-12 19:26:21 -04:00
|
|
|
});
|
2014-10-31 02:47:22 -04:00
|
|
|
|
2015-08-12 19:26:21 -04:00
|
|
|
it('should parse multiplicative expressions',
|
2016-06-08 19:38:52 -04:00
|
|
|
() => { checkAction('3*4/2%5', '3 * 4 / 2 % 5'); });
|
2015-04-22 05:45:33 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
it('should parse additive expressions', () => { checkAction('3 + 6 - 2'); });
|
2014-10-31 02:47:22 -04:00
|
|
|
|
2015-08-12 19:26:21 -04:00
|
|
|
it('should parse relational expressions', () => {
|
2016-06-08 19:38:52 -04:00
|
|
|
checkAction('2 < 3');
|
|
|
|
checkAction('2 > 3');
|
|
|
|
checkAction('2 <= 2');
|
|
|
|
checkAction('2 >= 2');
|
2015-08-12 19:26:21 -04:00
|
|
|
});
|
2014-10-31 02:47:22 -04:00
|
|
|
|
2015-08-12 19:26:21 -04:00
|
|
|
it('should parse equality expressions', () => {
|
2016-06-08 19:38:52 -04:00
|
|
|
checkAction('2 == 3');
|
|
|
|
checkAction('2 != 3');
|
2015-08-12 19:26:21 -04:00
|
|
|
});
|
2014-11-03 20:25:16 -05:00
|
|
|
|
2015-08-12 19:26:21 -04:00
|
|
|
it('should parse strict equality expressions', () => {
|
2016-06-08 19:38:52 -04:00
|
|
|
checkAction('2 === 3');
|
|
|
|
checkAction('2 !== 3');
|
2015-08-12 19:26:21 -04:00
|
|
|
});
|
2014-11-06 15:00:09 -05:00
|
|
|
|
2015-08-12 19:26:21 -04:00
|
|
|
it('should parse expressions', () => {
|
2016-06-08 19:38:52 -04:00
|
|
|
checkAction('true && true');
|
|
|
|
checkAction('true || false');
|
2015-08-12 19:26:21 -04:00
|
|
|
});
|
2014-11-06 15:00:09 -05:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
it('should parse grouped expressions', () => { checkAction('(1 + 2) * 3', '1 + 2 * 3'); });
|
2014-11-06 15:00:09 -05:00
|
|
|
|
2016-04-14 19:16:22 -04:00
|
|
|
it('should ignore comments in expressions', () => { checkAction('a //comment', 'a'); });
|
|
|
|
|
|
|
|
it('should retain // in string literals',
|
|
|
|
() => { checkAction(`"http://www.google.com"`, `"http://www.google.com"`); });
|
|
|
|
|
2015-08-12 19:26:21 -04:00
|
|
|
it('should parse an empty string', () => { checkAction(''); });
|
2014-11-04 19:08:01 -05:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
describe('literals', () => {
|
2015-08-12 19:26:21 -04:00
|
|
|
it('should parse array', () => {
|
2016-06-08 19:38:52 -04:00
|
|
|
checkAction('[1][0]');
|
|
|
|
checkAction('[[1]][0][0]');
|
|
|
|
checkAction('[]');
|
|
|
|
checkAction('[].length');
|
|
|
|
checkAction('[1, 2].length');
|
2014-11-06 15:00:09 -05:00
|
|
|
});
|
|
|
|
|
2015-08-12 19:26:21 -04:00
|
|
|
it('should parse map', () => {
|
2016-06-08 19:38:52 -04:00
|
|
|
checkAction('{}');
|
|
|
|
checkAction('{a: 1}[2]');
|
|
|
|
checkAction('{}["a"]');
|
2014-11-06 15:00:09 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should only allow identifier, string, or keyword as map key', () => {
|
2016-07-06 17:06:47 -04:00
|
|
|
expectActionError('{(:0}', 'expected identifier, keyword, or string');
|
|
|
|
expectActionError('{1234:0}', 'expected identifier, keyword, or string');
|
2014-11-06 15:00:09 -05:00
|
|
|
});
|
2014-11-03 20:25:16 -05:00
|
|
|
});
|
2014-11-04 12:06:46 -05:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
describe('member access', () => {
|
|
|
|
it('should parse field access', () => {
|
|
|
|
checkAction('a');
|
2016-08-04 13:14:44 -04:00
|
|
|
checkAction('this.a', 'a');
|
2016-06-08 19:38:52 -04:00
|
|
|
checkAction('a.a');
|
2014-11-06 15:00:09 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should only allow identifier or keyword as member names', () => {
|
2016-07-06 17:06:47 -04:00
|
|
|
expectActionError('x.(', 'identifier or keyword');
|
|
|
|
expectActionError('x. 1234', 'identifier or keyword');
|
|
|
|
expectActionError('x."foo"', 'identifier or keyword');
|
2015-05-26 04:19:47 -04:00
|
|
|
});
|
|
|
|
|
2015-08-12 19:26:21 -04:00
|
|
|
it('should parse safe field access', () => {
|
|
|
|
checkAction('a?.a');
|
|
|
|
checkAction('a.a?.a');
|
2015-05-26 04:19:47 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
describe('method calls', () => {
|
|
|
|
it('should parse method calls', () => {
|
|
|
|
checkAction('fn()');
|
|
|
|
checkAction('add(1, 2)');
|
|
|
|
checkAction('a.add(1, 2)');
|
|
|
|
checkAction('fn().add(1, 2)');
|
2015-01-19 06:58:44 -05:00
|
|
|
});
|
2014-11-04 19:08:01 -05:00
|
|
|
});
|
2014-11-04 13:19:37 -05:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
describe('functional calls', () => {
|
|
|
|
it('should parse function calls', () => { checkAction('fn()(1, 2)'); });
|
|
|
|
});
|
2014-11-04 13:19:37 -05:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
describe('conditional', () => {
|
2014-11-06 15:00:09 -05:00
|
|
|
it('should parse ternary/conditional expressions', () => {
|
2016-06-08 19:38:52 -04:00
|
|
|
checkAction('7 == 3 + 4 ? 10 : 20');
|
|
|
|
checkAction('false ? 10 : 20');
|
2014-11-06 15:00:09 -05:00
|
|
|
});
|
|
|
|
|
2016-07-06 17:06:47 -04:00
|
|
|
it('should report incorrect ternary operator syntax', () => {
|
|
|
|
expectActionError('true?1', 'Conditional expression true?1 requires all 3 expressions');
|
2014-11-06 15:00:09 -05:00
|
|
|
});
|
2014-11-04 13:19:37 -05:00
|
|
|
});
|
2014-11-04 18:51:56 -05:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
describe('assignment', () => {
|
|
|
|
it('should support field assignments', () => {
|
|
|
|
checkAction('a = 12');
|
|
|
|
checkAction('a.a.a = 123');
|
|
|
|
checkAction('a = 123; b = 234;');
|
2014-11-06 15:00:09 -05:00
|
|
|
});
|
2014-11-04 12:21:28 -05:00
|
|
|
|
2016-07-06 17:06:47 -04:00
|
|
|
it('should report on safe field assignments',
|
|
|
|
() => { expectActionError('a?.a = 123', 'cannot be used in the assignment'); });
|
2014-11-06 15:00:09 -05:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
it('should support array updates', () => { checkAction('a[0] = 200'); });
|
2014-11-05 18:38:44 -05:00
|
|
|
});
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
it('should error when using pipes',
|
2016-07-06 17:06:47 -04:00
|
|
|
() => { expectActionError('x|blah', 'Cannot have a pipe'); });
|
2014-11-18 19:38:36 -05:00
|
|
|
|
2015-05-26 20:12:38 -04:00
|
|
|
it('should store the source in the result',
|
2016-07-06 17:06:47 -04:00
|
|
|
() => { expect(parseAction('someExpr', 'someExpr')); });
|
2014-12-10 22:21:15 -05:00
|
|
|
|
2015-05-26 20:12:38 -04:00
|
|
|
it('should store the passed-in location',
|
|
|
|
() => { expect(parseAction('someExpr', 'location').location).toBe('location'); });
|
2015-08-20 10:34:47 -04:00
|
|
|
|
2016-07-06 17:06:47 -04:00
|
|
|
it('should report when encountering interpolation', () => {
|
|
|
|
expectActionError('{{a()}}', 'Got interpolation ({{}}) where expression was expected');
|
2015-08-20 10:34:47 -04:00
|
|
|
});
|
2014-11-06 15:00:09 -05:00
|
|
|
});
|
2014-11-04 12:21:28 -05:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
describe('general error handling', () => {
|
2016-07-06 17:06:47 -04:00
|
|
|
it('should report an unexpected token',
|
|
|
|
() => { expectActionError('[1,2] trac', 'Unexpected token \'trac\''); });
|
2015-08-12 19:26:21 -04:00
|
|
|
|
2016-07-06 17:06:47 -04:00
|
|
|
it('should report reasonable error for unconsumed tokens',
|
|
|
|
() => { expectActionError(')', 'Unexpected token ) at column 1 in [)]'); });
|
2015-08-12 19:26:21 -04:00
|
|
|
|
2016-07-06 17:06:47 -04:00
|
|
|
it('should report a missing expected token', () => {
|
|
|
|
expectActionError('a(b', 'Missing expected ) at the end of the expression [a(b]');
|
2015-08-12 19:26:21 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
describe('parseBinding', () => {
|
|
|
|
describe('pipes', () => {
|
|
|
|
it('should parse pipes', () => {
|
2015-08-12 19:26:21 -04:00
|
|
|
checkBinding('a(b | c)', 'a((b | c))');
|
|
|
|
checkBinding('a.b(c.d(e) | f)', 'a.b((c.d(e) | f))');
|
|
|
|
checkBinding('[1, 2, 3] | a', '([1, 2, 3] | a)');
|
|
|
|
checkBinding('{a: 1} | b', '({a: 1} | b)');
|
|
|
|
checkBinding('a[b] | c', '(a[b] | c)');
|
|
|
|
checkBinding('a?.b | c', '(a?.b | c)');
|
|
|
|
checkBinding('true | a', '(true | a)');
|
2015-10-13 21:55:46 -04:00
|
|
|
checkBinding('a | b:c | d', '((a | b:c) | d)');
|
|
|
|
checkBinding('a | b:(c | d)', '(a | b:(c | d))');
|
2014-11-05 13:00:19 -05:00
|
|
|
});
|
|
|
|
|
2014-11-06 15:00:09 -05:00
|
|
|
it('should only allow identifier or keyword as formatter names', () => {
|
2016-07-06 17:06:47 -04:00
|
|
|
expectBindingError('"Foo"|(', 'identifier or keyword');
|
|
|
|
expectBindingError('"Foo"|1234', 'identifier or keyword');
|
|
|
|
expectBindingError('"Foo"|"uppercase"', 'identifier or keyword');
|
2015-06-04 13:06:09 -04:00
|
|
|
});
|
2015-11-23 20:58:12 -05:00
|
|
|
|
|
|
|
it('should parse quoted expressions', () => { checkBinding('a:b', 'a:b'); });
|
|
|
|
|
2015-11-25 18:28:42 -05:00
|
|
|
it('should not crash when prefix part is not tokenizable',
|
|
|
|
() => { checkBinding('"a:b"', '"a:b"'); });
|
|
|
|
|
2015-11-23 20:58:12 -05:00
|
|
|
it('should ignore whitespace around quote prefix', () => { checkBinding(' a :b', 'a:b'); });
|
|
|
|
|
|
|
|
it('should refuse prefixes that are not single identifiers', () => {
|
2016-07-06 17:06:47 -04:00
|
|
|
expectBindingError('a + b:c', '');
|
|
|
|
expectBindingError('1:c', '');
|
2015-11-23 20:58:12 -05:00
|
|
|
});
|
2014-11-18 19:38:36 -05:00
|
|
|
});
|
|
|
|
|
2015-05-26 20:12:38 -04:00
|
|
|
it('should store the source in the result',
|
|
|
|
() => { expect(parseBinding('someExpr').source).toBe('someExpr'); });
|
2014-12-10 22:21:15 -05:00
|
|
|
|
2015-05-26 20:12:38 -04:00
|
|
|
it('should store the passed-in location',
|
|
|
|
() => { expect(parseBinding('someExpr', 'location').location).toBe('location'); });
|
2014-11-06 15:00:09 -05:00
|
|
|
|
2016-07-06 17:06:47 -04:00
|
|
|
it('should report chain expressions',
|
|
|
|
() => { expectError(parseBinding('1;2'), 'contain chained expression'); });
|
2014-11-06 15:00:09 -05:00
|
|
|
|
2016-07-06 17:06:47 -04:00
|
|
|
it('should report assignment',
|
|
|
|
() => { expectError(parseBinding('a=2'), 'contain assignments'); });
|
2015-08-20 10:34:47 -04:00
|
|
|
|
2016-07-06 17:06:47 -04:00
|
|
|
it('should report when encountering interpolation', () => {
|
|
|
|
expectBindingError('{{a.b}}', 'Got interpolation ({{}}) where expression was expected');
|
2015-08-20 10:34:47 -04:00
|
|
|
});
|
2015-12-06 01:06:24 -05:00
|
|
|
|
|
|
|
it('should parse conditional expression', () => { checkBinding('a < b ? a : b'); });
|
2016-04-14 19:16:22 -04:00
|
|
|
|
|
|
|
it('should ignore comments in bindings', () => { checkBinding('a //comment', 'a'); });
|
|
|
|
|
|
|
|
it('should retain // in string literals',
|
|
|
|
() => { checkBinding(`"http://www.google.com"`, `"http://www.google.com"`); });
|
|
|
|
|
|
|
|
it('should retain // in : microsyntax', () => { checkBinding('one:a//b', 'one:a//b'); });
|
|
|
|
|
2014-10-28 12:22:38 -04:00
|
|
|
});
|
2014-11-18 19:38:36 -05:00
|
|
|
|
|
|
|
describe('parseTemplateBindings', () => {
|
|
|
|
|
2015-10-06 22:05:09 -04:00
|
|
|
function keys(templateBindings: any[]) {
|
|
|
|
return templateBindings.map(binding => binding.key);
|
|
|
|
}
|
2014-11-18 19:38:36 -05:00
|
|
|
|
2015-10-06 22:05:09 -04:00
|
|
|
function keyValues(templateBindings: any[]) {
|
2015-10-06 21:00:42 -04:00
|
|
|
return templateBindings.map(binding => {
|
2015-01-28 01:34:25 -05:00
|
|
|
if (binding.keyIsVar) {
|
2017-03-02 12:37:01 -05:00
|
|
|
return 'let ' + binding.key + (binding.name == null ? '=null' : '=' + binding.name);
|
2015-01-28 01:34:25 -05:00
|
|
|
} else {
|
2017-03-02 12:37:01 -05:00
|
|
|
return binding.key + (binding.expression == null ? '' : `=${binding.expression}`);
|
2015-01-28 01:34:25 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-11-10 16:15:09 -05:00
|
|
|
function keySpans(source: string, templateBindings: TemplateBinding[]) {
|
|
|
|
return templateBindings.map(
|
|
|
|
binding => source.substring(binding.span.start, binding.span.end));
|
|
|
|
}
|
|
|
|
|
2015-10-06 22:05:09 -04:00
|
|
|
function exprSources(templateBindings: any[]) {
|
2015-10-06 21:00:42 -04:00
|
|
|
return templateBindings.map(
|
2017-03-02 12:37:01 -05:00
|
|
|
binding => binding.expression != null ? binding.expression.source : null);
|
2014-11-18 19:38:36 -05:00
|
|
|
}
|
|
|
|
|
2015-08-12 19:26:21 -04:00
|
|
|
it('should parse an empty string', () => { expect(parseTemplateBindings('')).toEqual([]); });
|
2014-11-18 19:38:36 -05:00
|
|
|
|
2015-08-12 19:26:21 -04:00
|
|
|
it('should parse a string without a value',
|
|
|
|
() => { expect(keys(parseTemplateBindings('a'))).toEqual(['a']); });
|
2014-12-01 19:05:44 -05:00
|
|
|
|
|
|
|
it('should only allow identifier, string, or keyword including dashes as keys', () => {
|
2016-11-12 08:08:58 -05:00
|
|
|
let bindings = parseTemplateBindings('a:\'b\'');
|
2014-11-18 19:38:36 -05:00
|
|
|
expect(keys(bindings)).toEqual(['a']);
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
bindings = parseTemplateBindings('\'a\':\'b\'');
|
2014-11-18 19:38:36 -05:00
|
|
|
expect(keys(bindings)).toEqual(['a']);
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
bindings = parseTemplateBindings('"a":\'b\'');
|
2014-11-18 19:38:36 -05:00
|
|
|
expect(keys(bindings)).toEqual(['a']);
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
bindings = parseTemplateBindings('a-b:\'c\'');
|
2014-12-01 19:05:44 -05:00
|
|
|
expect(keys(bindings)).toEqual(['a-b']);
|
|
|
|
|
2016-07-06 17:06:47 -04:00
|
|
|
expectError(parseTemplateBindingsResult('(:0'), 'expected identifier, keyword, or string');
|
2014-11-18 19:38:36 -05:00
|
|
|
|
2016-07-06 17:06:47 -04:00
|
|
|
expectError(
|
|
|
|
parseTemplateBindingsResult('1234:0'), 'expected identifier, keyword, or string');
|
2014-11-18 19:38:36 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should detect expressions as value', () => {
|
2016-11-12 08:08:58 -05:00
|
|
|
let bindings = parseTemplateBindings('a:b');
|
2014-11-18 19:38:36 -05:00
|
|
|
expect(exprSources(bindings)).toEqual(['b']);
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
bindings = parseTemplateBindings('a:1+1');
|
2014-11-18 19:38:36 -05:00
|
|
|
expect(exprSources(bindings)).toEqual(['1+1']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should detect names as value', () => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const bindings = parseTemplateBindings('a:let b');
|
2016-04-25 22:52:24 -04:00
|
|
|
expect(keyValues(bindings)).toEqual(['a', 'let b=\$implicit']);
|
2014-11-18 19:38:36 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should allow space and colon as separators', () => {
|
2016-11-12 08:08:58 -05:00
|
|
|
let bindings = parseTemplateBindings('a:b');
|
2014-11-18 19:38:36 -05:00
|
|
|
expect(keys(bindings)).toEqual(['a']);
|
|
|
|
expect(exprSources(bindings)).toEqual(['b']);
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
bindings = parseTemplateBindings('a b');
|
2014-11-18 19:38:36 -05:00
|
|
|
expect(keys(bindings)).toEqual(['a']);
|
|
|
|
expect(exprSources(bindings)).toEqual(['b']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should allow multiple pairs', () => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const bindings = parseTemplateBindings('a 1 b 2');
|
2015-11-23 19:02:19 -05:00
|
|
|
expect(keys(bindings)).toEqual(['a', 'aB']);
|
2014-11-18 19:38:36 -05:00
|
|
|
expect(exprSources(bindings)).toEqual(['1 ', '2']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should store the sources in the result', () => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const bindings = parseTemplateBindings('a 1,b 2');
|
2014-11-18 19:38:36 -05:00
|
|
|
expect(bindings[0].expression.source).toEqual('1');
|
|
|
|
expect(bindings[1].expression.source).toEqual('2');
|
|
|
|
});
|
2014-12-10 22:21:15 -05:00
|
|
|
|
|
|
|
it('should store the passed-in location', () => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const bindings = parseTemplateBindings('a 1,b 2', 'location');
|
2014-12-10 22:21:15 -05:00
|
|
|
expect(bindings[0].expression.location).toEqual('location');
|
|
|
|
});
|
2015-01-28 01:34:25 -05:00
|
|
|
|
2016-04-25 22:52:24 -04:00
|
|
|
it('should support let notation', () => {
|
2016-11-12 08:08:58 -05:00
|
|
|
let bindings = parseTemplateBindings('let i');
|
2016-04-25 22:52:24 -04:00
|
|
|
expect(keyValues(bindings)).toEqual(['let i=\$implicit']);
|
2015-01-28 01:34:25 -05:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
bindings = parseTemplateBindings('let i');
|
2016-04-25 22:52:24 -04:00
|
|
|
expect(keyValues(bindings)).toEqual(['let i=\$implicit']);
|
2015-05-21 15:17:46 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
bindings = parseTemplateBindings('let a; let b');
|
2016-04-25 22:52:24 -04:00
|
|
|
expect(keyValues(bindings)).toEqual(['let a=\$implicit', 'let b=\$implicit']);
|
2015-05-21 15:17:46 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
bindings = parseTemplateBindings('let a; let b;');
|
2016-04-25 22:52:24 -04:00
|
|
|
expect(keyValues(bindings)).toEqual(['let a=\$implicit', 'let b=\$implicit']);
|
2015-01-28 01:34:25 -05:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
bindings = parseTemplateBindings('let i-a = k-a');
|
2016-04-25 22:52:24 -04:00
|
|
|
expect(keyValues(bindings)).toEqual(['let i-a=k-a']);
|
2015-01-28 01:34:25 -05:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
bindings = parseTemplateBindings('keyword let item; let i = k');
|
2016-04-25 22:52:24 -04:00
|
|
|
expect(keyValues(bindings)).toEqual(['keyword', 'let item=\$implicit', 'let i=k']);
|
2015-01-28 01:34:25 -05:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
bindings = parseTemplateBindings('keyword: let item; let i = k');
|
2016-04-25 22:52:24 -04:00
|
|
|
expect(keyValues(bindings)).toEqual(['keyword', 'let item=\$implicit', 'let i=k']);
|
2015-01-28 01:34:25 -05:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
bindings = parseTemplateBindings('directive: let item in expr; let a = b', 'location');
|
|
|
|
expect(keyValues(bindings)).toEqual([
|
|
|
|
'directive', 'let item=\$implicit', 'directiveIn=expr in location', 'let a=b'
|
|
|
|
]);
|
2015-01-28 01:34:25 -05:00
|
|
|
});
|
2015-02-23 16:12:49 -05:00
|
|
|
|
2017-03-14 23:46:29 -04:00
|
|
|
it('should support as notation', () => {
|
|
|
|
let bindings = parseTemplateBindings('ngIf exp as local', 'location');
|
|
|
|
expect(keyValues(bindings)).toEqual(['ngIf=exp in location', 'let local=ngIf']);
|
|
|
|
|
|
|
|
bindings = parseTemplateBindings('ngFor let item of items as iter; index as i', 'L');
|
|
|
|
expect(keyValues(bindings)).toEqual([
|
|
|
|
'ngFor', 'let item=$implicit', 'ngForOf=items in L', 'let iter=ngForOf', 'let i=index'
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2015-02-23 16:12:49 -05:00
|
|
|
it('should parse pipes', () => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const bindings = parseTemplateBindings('key value|pipe');
|
|
|
|
const ast = bindings[0].expression.ast;
|
2015-06-18 18:40:12 -04:00
|
|
|
expect(ast).toBeAnInstanceOf(BindingPipe);
|
2015-02-23 16:12:49 -05:00
|
|
|
});
|
2016-11-10 16:15:09 -05:00
|
|
|
|
|
|
|
describe('spans', () => {
|
|
|
|
it('should should support let', () => {
|
|
|
|
const source = 'let i';
|
|
|
|
expect(keySpans(source, parseTemplateBindings(source))).toEqual(['let i']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should support multiple lets', () => {
|
|
|
|
const source = 'let item; let i=index; let e=even;';
|
|
|
|
expect(keySpans(source, parseTemplateBindings(source))).toEqual([
|
|
|
|
'let item', 'let i=index', 'let e=even'
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should support a prefix', () => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const source = 'let person of people';
|
|
|
|
const prefix = 'ngFor';
|
|
|
|
const bindings = parseTemplateBindings(source, null, prefix);
|
2016-11-10 16:15:09 -05:00
|
|
|
expect(keyValues(bindings)).toEqual([
|
|
|
|
'ngFor', 'let person=$implicit', 'ngForOf=people in null'
|
|
|
|
]);
|
|
|
|
expect(keySpans(source, bindings)).toEqual(['', 'let person ', 'of people']);
|
|
|
|
});
|
|
|
|
});
|
2014-11-18 19:38:36 -05:00
|
|
|
});
|
2015-01-08 19:17:56 -05:00
|
|
|
|
|
|
|
describe('parseInterpolation', () => {
|
2015-05-26 20:12:38 -04:00
|
|
|
it('should return null if no interpolation',
|
|
|
|
() => { expect(parseInterpolation('nothing')).toBe(null); });
|
2015-01-08 19:17:56 -05:00
|
|
|
|
|
|
|
it('should parse no prefix/suffix interpolation', () => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const ast = parseInterpolation('{{a}}').ast as Interpolation;
|
2015-01-08 19:17:56 -05:00
|
|
|
expect(ast.strings).toEqual(['', '']);
|
|
|
|
expect(ast.expressions.length).toEqual(1);
|
|
|
|
expect(ast.expressions[0].name).toEqual('a');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should parse prefix/suffix with multiple interpolation', () => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const originalExp = 'before {{ a }} middle {{ b }} after';
|
|
|
|
const ast = parseInterpolation(originalExp).ast;
|
2016-07-06 17:06:47 -04:00
|
|
|
expect(unparse(ast)).toEqual(originalExp);
|
|
|
|
validate(ast);
|
2015-01-08 19:17:56 -05:00
|
|
|
});
|
2015-08-03 06:05:45 -04:00
|
|
|
|
2016-07-06 17:06:47 -04:00
|
|
|
it('should report empty interpolation expressions', () => {
|
|
|
|
expectError(
|
|
|
|
parseInterpolation('{{}}'),
|
|
|
|
'Blank expressions are not allowed in interpolated strings');
|
2015-08-03 06:05:45 -04:00
|
|
|
|
2016-07-06 17:06:47 -04:00
|
|
|
expectError(
|
|
|
|
parseInterpolation('foo {{ }}'),
|
|
|
|
'Parser Error: Blank expressions are not allowed in interpolated strings');
|
2015-08-03 06:05:45 -04:00
|
|
|
});
|
2015-12-06 01:06:24 -05:00
|
|
|
|
|
|
|
it('should parse conditional expression',
|
|
|
|
() => { checkInterpolation('{{ a < b ? a : b }}'); });
|
2015-12-21 11:32:23 -05:00
|
|
|
|
|
|
|
it('should parse expression with newline characters', () => {
|
|
|
|
checkInterpolation(`{{ 'foo' +\n 'bar' +\r 'baz' }}`, `{{ "foo" + "bar" + "baz" }}`);
|
|
|
|
});
|
2016-04-14 19:16:22 -04:00
|
|
|
|
2016-06-20 12:52:41 -04:00
|
|
|
it('should support custom interpolation', () => {
|
|
|
|
const parser = new Parser(new Lexer());
|
|
|
|
const ast = parser.parseInterpolation('{% a %}', null, {start: '{%', end: '%}'}).ast as any;
|
|
|
|
expect(ast.strings).toEqual(['', '']);
|
|
|
|
expect(ast.expressions.length).toEqual(1);
|
|
|
|
expect(ast.expressions[0].name).toEqual('a');
|
|
|
|
});
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
describe('comments', () => {
|
2016-04-14 19:16:22 -04:00
|
|
|
it('should ignore comments in interpolation expressions',
|
|
|
|
() => { checkInterpolation('{{a //comment}}', '{{ a }}'); });
|
|
|
|
|
|
|
|
it('should retain // in single quote strings', () => {
|
|
|
|
checkInterpolation(`{{ 'http://www.google.com' }}`, `{{ "http://www.google.com" }}`);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should retain // in double quote strings', () => {
|
|
|
|
checkInterpolation(`{{ "http://www.google.com" }}`, `{{ "http://www.google.com" }}`);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should ignore comments after string literals',
|
|
|
|
() => { checkInterpolation(`{{ "a//b" //comment }}`, `{{ "a//b" }}`); });
|
|
|
|
|
|
|
|
it('should retain // in complex strings', () => {
|
2016-06-08 19:38:52 -04:00
|
|
|
checkInterpolation(
|
|
|
|
`{{"//a\'//b\`//c\`//d\'//e" //comment}}`, `{{ "//a\'//b\`//c\`//d\'//e" }}`);
|
2016-04-14 19:16:22 -04:00
|
|
|
});
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
it('should retain // in nested, unterminated strings',
|
|
|
|
() => { checkInterpolation(`{{ "a\'b\`" //comment}}`, `{{ "a\'b\`" }}`); });
|
2016-04-14 19:16:22 -04:00
|
|
|
});
|
|
|
|
|
2015-01-08 19:17:56 -05:00
|
|
|
});
|
2015-02-05 14:13:32 -05:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
describe('parseSimpleBinding', () => {
|
|
|
|
it('should parse a field access', () => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const p = parseSimpleBinding('name');
|
2016-06-08 19:38:52 -04:00
|
|
|
expect(unparse(p)).toEqual('name');
|
2016-07-06 17:06:47 -04:00
|
|
|
validate(p);
|
2015-06-22 11:21:03 -04:00
|
|
|
});
|
|
|
|
|
2016-10-20 18:24:58 -04:00
|
|
|
it('should report when encountering pipes', () => {
|
2016-07-06 17:06:47 -04:00
|
|
|
expectError(
|
2016-10-20 18:24:58 -04:00
|
|
|
validate(parseSimpleBinding('a | somePipe')),
|
|
|
|
'Host binding expression cannot contain pipes');
|
2015-08-20 10:34:47 -04:00
|
|
|
});
|
|
|
|
|
2016-07-06 17:06:47 -04:00
|
|
|
it('should report when encountering interpolation', () => {
|
|
|
|
expectError(
|
|
|
|
validate(parseSimpleBinding('{{exp}}')),
|
|
|
|
'Got interpolation ({{}}) where expression was expected');
|
2015-06-22 11:21:03 -04:00
|
|
|
});
|
2016-10-20 18:24:58 -04:00
|
|
|
|
|
|
|
it('should report when encountering field write', () => {
|
|
|
|
expectError(validate(parseSimpleBinding('a = b')), 'Bindings cannot contain assignments');
|
|
|
|
});
|
2015-06-22 11:21:03 -04:00
|
|
|
});
|
|
|
|
|
2015-02-05 14:13:32 -05:00
|
|
|
describe('wrapLiteralPrimitive', () => {
|
|
|
|
it('should wrap a literal primitive', () => {
|
2016-07-06 17:06:47 -04:00
|
|
|
expect(unparse(validate(createParser().wrapLiteralPrimitive('foo', null))))
|
|
|
|
.toEqual('"foo"');
|
2015-02-05 14:13:32 -05:00
|
|
|
});
|
|
|
|
});
|
2016-07-06 17:06:47 -04:00
|
|
|
|
|
|
|
describe('error recovery', () => {
|
|
|
|
function recover(text: string, expected?: string) {
|
2016-11-12 08:08:58 -05:00
|
|
|
const expr = validate(parseAction(text));
|
2016-07-06 17:06:47 -04:00
|
|
|
expect(unparse(expr)).toEqual(expected || text);
|
|
|
|
}
|
|
|
|
it('should be able to recover from an extra paren', () => recover('((a)))', 'a'));
|
|
|
|
it('should be able to recover from an extra bracket', () => recover('[[a]]]', '[[a]]'));
|
|
|
|
it('should be able to recover from a missing )', () => recover('(a;b', 'a; b;'));
|
|
|
|
it('should be able to recover from a missing ]', () => recover('[a,b', '[a, b]'));
|
|
|
|
it('should be able to recover from a missing selector', () => recover('a.'));
|
|
|
|
it('should be able to recover from a missing selector in a array literal',
|
2016-07-21 20:12:00 -04:00
|
|
|
() => recover('[[a.], b, c]'));
|
2016-07-06 17:06:47 -04:00
|
|
|
});
|
2016-10-06 18:22:10 -04:00
|
|
|
|
|
|
|
describe('offsets', () => {
|
|
|
|
it('should retain the offsets of an interpolation', () => {
|
|
|
|
const interpolations = splitInterpolation('{{a}} {{b}} {{c}}');
|
|
|
|
expect(interpolations.offsets).toEqual([2, 9, 16]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should retain the offsets into the expression AST of interpolations', () => {
|
|
|
|
const source = parseInterpolation('{{a}} {{b}} {{c}}');
|
|
|
|
const interpolation = source.ast as Interpolation;
|
|
|
|
expect(interpolation.expressions.map(e => e.span.start)).toEqual([2, 9, 16]);
|
|
|
|
});
|
|
|
|
});
|
2014-10-28 12:22:38 -04:00
|
|
|
});
|
2015-04-22 05:45:33 -04:00
|
|
|
}
|