refactor(compiler): rename _ParseAST.optionalCharacter TemplateBinding.expression (#35886)

This commit renames
1. _ParseAST.optionalCharacter -> consumeOptionalCharacter
2. TemplateBinding.expression -> value

PR Close #35886
This commit is contained in:
Keen Yee Liau 2020-03-05 15:05:30 -08:00 committed by Matias Niemelä
parent 59078c444c
commit bef14cf424
6 changed files with 39 additions and 39 deletions

View File

@ -286,7 +286,7 @@ export class ASTWithSource extends AST {
export class TemplateBinding { export class TemplateBinding {
constructor( constructor(
public span: ParseSpan, sourceSpan: AbsoluteSourceSpan, public key: string, public span: ParseSpan, sourceSpan: AbsoluteSourceSpan, public key: string,
public keyIsVar: boolean, public name: string, public expression: ASTWithSource|null) {} public keyIsVar: boolean, public name: string, public value: ASTWithSource|null) {}
} }
export interface AstVisitor { export interface AstVisitor {

View File

@ -314,7 +314,7 @@ export class _ParseAST {
advance() { this.index++; } advance() { this.index++; }
optionalCharacter(code: number): boolean { consumeOptionalCharacter(code: number): boolean {
if (this.next.isCharacter(code)) { if (this.next.isCharacter(code)) {
this.advance(); this.advance();
return true; return true;
@ -327,7 +327,7 @@ export class _ParseAST {
peekKeywordAs(): boolean { return this.next.isKeywordAs(); } peekKeywordAs(): boolean { return this.next.isKeywordAs(); }
expectCharacter(code: number) { expectCharacter(code: number) {
if (this.optionalCharacter(code)) return; if (this.consumeOptionalCharacter(code)) return;
this.error(`Missing expected ${String.fromCharCode(code)}`); this.error(`Missing expected ${String.fromCharCode(code)}`);
} }
@ -372,11 +372,11 @@ export class _ParseAST {
const expr = this.parsePipe(); const expr = this.parsePipe();
exprs.push(expr); exprs.push(expr);
if (this.optionalCharacter(chars.$SEMICOLON)) { if (this.consumeOptionalCharacter(chars.$SEMICOLON)) {
if (!this.parseAction) { if (!this.parseAction) {
this.error('Binding expression cannot contain chained expression'); this.error('Binding expression cannot contain chained expression');
} }
while (this.optionalCharacter(chars.$SEMICOLON)) { while (this.consumeOptionalCharacter(chars.$SEMICOLON)) {
} // read all semicolons } // read all semicolons
} else if (this.index < this.tokens.length) { } else if (this.index < this.tokens.length) {
this.error(`Unexpected token '${this.next}'`); this.error(`Unexpected token '${this.next}'`);
@ -399,7 +399,7 @@ export class _ParseAST {
const name = this.expectIdentifierOrKeyword(); const name = this.expectIdentifierOrKeyword();
const nameSpan = this.sourceSpan(nameStart); const nameSpan = this.sourceSpan(nameStart);
const args: AST[] = []; const args: AST[] = [];
while (this.optionalCharacter(chars.$COLON)) { while (this.consumeOptionalCharacter(chars.$COLON)) {
args.push(this.parseExpression()); args.push(this.parseExpression());
} }
const {start} = result.span; const {start} = result.span;
@ -420,7 +420,7 @@ export class _ParseAST {
if (this.optionalOperator('?')) { if (this.optionalOperator('?')) {
const yes = this.parsePipe(); const yes = this.parsePipe();
let no: AST; let no: AST;
if (!this.optionalCharacter(chars.$COLON)) { if (!this.consumeOptionalCharacter(chars.$COLON)) {
const end = this.inputIndex; const end = this.inputIndex;
const expression = this.input.substring(start, end); const expression = this.input.substring(start, end);
this.error(`Conditional expression ${expression} requires all 3 expressions`); this.error(`Conditional expression ${expression} requires all 3 expressions`);
@ -570,13 +570,13 @@ export class _ParseAST {
let result = this.parsePrimary(); let result = this.parsePrimary();
const resultStart = result.span.start; const resultStart = result.span.start;
while (true) { while (true) {
if (this.optionalCharacter(chars.$PERIOD)) { if (this.consumeOptionalCharacter(chars.$PERIOD)) {
result = this.parseAccessMemberOrMethodCall(result, false); result = this.parseAccessMemberOrMethodCall(result, false);
} else if (this.optionalOperator('?.')) { } else if (this.optionalOperator('?.')) {
result = this.parseAccessMemberOrMethodCall(result, true); result = this.parseAccessMemberOrMethodCall(result, true);
} else if (this.optionalCharacter(chars.$LBRACKET)) { } else if (this.consumeOptionalCharacter(chars.$LBRACKET)) {
this.rbracketsExpected++; this.rbracketsExpected++;
const key = this.parsePipe(); const key = this.parsePipe();
this.rbracketsExpected--; this.rbracketsExpected--;
@ -589,7 +589,7 @@ export class _ParseAST {
result = new KeyedRead(this.span(resultStart), this.sourceSpan(resultStart), result, key); result = new KeyedRead(this.span(resultStart), this.sourceSpan(resultStart), result, key);
} }
} else if (this.optionalCharacter(chars.$LPAREN)) { } else if (this.consumeOptionalCharacter(chars.$LPAREN)) {
this.rparensExpected++; this.rparensExpected++;
const args = this.parseCallArguments(); const args = this.parseCallArguments();
this.rparensExpected--; this.rparensExpected--;
@ -608,7 +608,7 @@ export class _ParseAST {
parsePrimary(): AST { parsePrimary(): AST {
const start = this.inputIndex; const start = this.inputIndex;
if (this.optionalCharacter(chars.$LPAREN)) { if (this.consumeOptionalCharacter(chars.$LPAREN)) {
this.rparensExpected++; this.rparensExpected++;
const result = this.parsePipe(); const result = this.parsePipe();
this.rparensExpected--; this.rparensExpected--;
@ -635,7 +635,7 @@ export class _ParseAST {
this.advance(); this.advance();
return new ImplicitReceiver(this.span(start), this.sourceSpan(start)); return new ImplicitReceiver(this.span(start), this.sourceSpan(start));
} else if (this.optionalCharacter(chars.$LBRACKET)) { } else if (this.consumeOptionalCharacter(chars.$LBRACKET)) {
this.rbracketsExpected++; this.rbracketsExpected++;
const elements = this.parseExpressionList(chars.$RBRACKET); const elements = this.parseExpressionList(chars.$RBRACKET);
this.rbracketsExpected--; this.rbracketsExpected--;
@ -673,7 +673,7 @@ export class _ParseAST {
if (!this.next.isCharacter(terminator)) { if (!this.next.isCharacter(terminator)) {
do { do {
result.push(this.parsePipe()); result.push(this.parsePipe());
} while (this.optionalCharacter(chars.$COMMA)); } while (this.consumeOptionalCharacter(chars.$COMMA));
} }
return result; return result;
} }
@ -683,7 +683,7 @@ export class _ParseAST {
const values: AST[] = []; const values: AST[] = [];
const start = this.inputIndex; const start = this.inputIndex;
this.expectCharacter(chars.$LBRACE); this.expectCharacter(chars.$LBRACE);
if (!this.optionalCharacter(chars.$RBRACE)) { if (!this.consumeOptionalCharacter(chars.$RBRACE)) {
this.rbracesExpected++; this.rbracesExpected++;
do { do {
const quoted = this.next.isString(); const quoted = this.next.isString();
@ -691,7 +691,7 @@ export class _ParseAST {
keys.push({key, quoted}); keys.push({key, quoted});
this.expectCharacter(chars.$COLON); this.expectCharacter(chars.$COLON);
values.push(this.parsePipe()); values.push(this.parsePipe());
} while (this.optionalCharacter(chars.$COMMA)); } while (this.consumeOptionalCharacter(chars.$COMMA));
this.rbracesExpected--; this.rbracesExpected--;
this.expectCharacter(chars.$RBRACE); this.expectCharacter(chars.$RBRACE);
} }
@ -702,7 +702,7 @@ export class _ParseAST {
const start = receiver.span.start; const start = receiver.span.start;
const id = this.expectIdentifierOrKeyword(); const id = this.expectIdentifierOrKeyword();
if (this.optionalCharacter(chars.$LPAREN)) { if (this.consumeOptionalCharacter(chars.$LPAREN)) {
this.rparensExpected++; this.rparensExpected++;
const args = this.parseCallArguments(); const args = this.parseCallArguments();
this.expectCharacter(chars.$RPAREN); this.expectCharacter(chars.$RPAREN);
@ -742,7 +742,7 @@ export class _ParseAST {
const positionals: AST[] = []; const positionals: AST[] = [];
do { do {
positionals.push(this.parsePipe()); positionals.push(this.parsePipe());
} while (this.optionalCharacter(chars.$COMMA)); } while (this.consumeOptionalCharacter(chars.$COMMA));
return positionals as BindingPipe[]; return positionals as BindingPipe[];
} }
@ -845,7 +845,7 @@ export class _ParseAST {
private parseDirectiveKeywordBindings(key: string, keySpan: ParseSpan, absoluteOffset: number): private parseDirectiveKeywordBindings(key: string, keySpan: ParseSpan, absoluteOffset: number):
TemplateBinding[] { TemplateBinding[] {
const bindings: TemplateBinding[] = []; const bindings: TemplateBinding[] = [];
this.optionalCharacter(chars.$COLON); // trackBy: trackByFunction this.consumeOptionalCharacter(chars.$COLON); // trackBy: trackByFunction
const valueExpr = this.getDirectiveBoundTarget(); const valueExpr = this.getDirectiveBoundTarget();
const span = new ParseSpan(keySpan.start, this.inputIndex); const span = new ParseSpan(keySpan.start, this.inputIndex);
bindings.push(new TemplateBinding( bindings.push(new TemplateBinding(
@ -943,7 +943,7 @@ export class _ParseAST {
* Consume the optional statement terminator: semicolon or comma. * Consume the optional statement terminator: semicolon or comma.
*/ */
private consumeStatementTerminator() { private consumeStatementTerminator() {
this.optionalCharacter(chars.$SEMICOLON) || this.optionalCharacter(chars.$COMMA); this.consumeOptionalCharacter(chars.$SEMICOLON) || this.consumeOptionalCharacter(chars.$COMMA);
} }
error(message: string, index: number|null = null) { error(message: string, index: number|null = null) {

View File

@ -134,10 +134,9 @@ export class BindingParser {
const binding = bindings[i]; const binding = bindings[i];
if (binding.keyIsVar) { if (binding.keyIsVar) {
targetVars.push(new ParsedVariable(binding.key, binding.name, sourceSpan)); targetVars.push(new ParsedVariable(binding.key, binding.name, sourceSpan));
} else if (binding.expression) { } else if (binding.value) {
this._parsePropertyAst( this._parsePropertyAst(
binding.key, binding.expression, sourceSpan, undefined, targetMatchableAttrs, binding.key, binding.value, sourceSpan, undefined, targetMatchableAttrs, targetProps);
targetProps);
} else { } else {
targetMatchableAttrs.push([binding.key, '']); targetMatchableAttrs.push([binding.key, '']);
this.parseLiteralAttr( this.parseLiteralAttr(
@ -165,8 +164,8 @@ export class BindingParser {
this._exprParser.parseTemplateBindings(tplKey, tplValue, sourceInfo, absoluteValueOffset); this._exprParser.parseTemplateBindings(tplKey, tplValue, sourceInfo, absoluteValueOffset);
this._reportExpressionParserErrors(bindingsResult.errors, sourceSpan); this._reportExpressionParserErrors(bindingsResult.errors, sourceSpan);
bindingsResult.templateBindings.forEach((binding) => { bindingsResult.templateBindings.forEach((binding) => {
if (binding.expression) { if (binding.value) {
this._checkPipes(binding.expression, sourceSpan); this._checkPipes(binding.value, sourceSpan);
} }
}); });
bindingsResult.warnings.forEach( bindingsResult.warnings.forEach(

View File

@ -248,14 +248,16 @@ describe('parser', () => {
describe('parseTemplateBindings', () => { describe('parseTemplateBindings', () => {
function keys(templateBindings: any[]) { return templateBindings.map(binding => binding.key); } function keys(templateBindings: TemplateBinding[]) {
return templateBindings.map(binding => binding.key);
}
function keyValues(templateBindings: any[]) { function keyValues(templateBindings: TemplateBinding[]) {
return templateBindings.map(binding => { return templateBindings.map(binding => {
if (binding.keyIsVar) { if (binding.keyIsVar) {
return 'let ' + binding.key + (binding.name == null ? '=null' : '=' + binding.name); return 'let ' + binding.key + (binding.name == null ? '=null' : '=' + binding.name);
} else { } else {
return binding.key + (binding.expression == null ? '' : `=${binding.expression}`); return binding.key + (binding.value == null ? '' : `=${binding.value}`);
} }
}); });
} }
@ -265,14 +267,13 @@ describe('parser', () => {
binding => source.substring(binding.span.start, binding.span.end)); binding => source.substring(binding.span.start, binding.span.end));
} }
function exprSources(templateBindings: any[]) { function exprSources(templateBindings: TemplateBinding[]) {
return templateBindings.map( return templateBindings.map(binding => binding.value != null ? binding.value.source : null);
binding => binding.expression != null ? binding.expression.source : null);
} }
function humanize(bindings: TemplateBinding[]): Array<[string, string | null, boolean]> { function humanize(bindings: TemplateBinding[]): Array<[string, string | null, boolean]> {
return bindings.map(binding => { return bindings.map(binding => {
const {key, expression, name, keyIsVar} = binding; const {key, value: expression, name, keyIsVar} = binding;
const value = keyIsVar ? name : (expression ? expression.source : expression); const value = keyIsVar ? name : (expression ? expression.source : expression);
return [key, value, keyIsVar]; return [key, value, keyIsVar];
}); });
@ -316,13 +317,13 @@ describe('parser', () => {
it('should store the sources in the result', () => { it('should store the sources in the result', () => {
const bindings = parseTemplateBindings('a', '1,b 2'); const bindings = parseTemplateBindings('a', '1,b 2');
expect(bindings[0].expression !.source).toEqual('1'); expect(bindings[0].value !.source).toEqual('1');
expect(bindings[1].expression !.source).toEqual('2'); expect(bindings[1].value !.source).toEqual('2');
}); });
it('should store the passed-in location', () => { it('should store the passed-in location', () => {
const bindings = parseTemplateBindings('a', '1,b 2', 'location'); const bindings = parseTemplateBindings('a', '1,b 2', 'location');
expect(bindings[0].expression !.location).toEqual('location'); expect(bindings[0].value !.location).toEqual('location');
}); });
it('should support common usage of ngIf', () => { it('should support common usage of ngIf', () => {
@ -415,7 +416,7 @@ describe('parser', () => {
it('should parse pipes', () => { it('should parse pipes', () => {
const bindings = parseTemplateBindings('key', 'value|pipe'); const bindings = parseTemplateBindings('key', 'value|pipe');
const ast = bindings[0].expression !.ast; const ast = bindings[0].value !.ast;
expect(ast).toBeAnInstanceOf(BindingPipe); expect(ast).toBeAnInstanceOf(BindingPipe);
}); });

View File

@ -567,8 +567,8 @@ class ExpressionVisitor extends NullTemplateVisitor {
} }
} }
if (binding.expression && inSpan(valueRelativePosition, binding.expression.ast.span)) { if (binding.value && inSpan(valueRelativePosition, binding.value.ast.span)) {
this.processExpressionCompletions(binding.expression.ast); this.processExpressionCompletions(binding.value.ast);
return; return;
} }

View File

@ -209,10 +209,10 @@ function getSymbolInMicrosyntax(info: AstResult, path: TemplateAstPath, attribut
// Find the symbol that contains the position. // Find the symbol that contains the position.
templateBindings.filter(tb => !tb.keyIsVar).forEach(tb => { templateBindings.filter(tb => !tb.keyIsVar).forEach(tb => {
if (inSpan(valueRelativePosition, tb.expression?.ast.span)) { if (inSpan(valueRelativePosition, tb.value?.ast.span)) {
const dinfo = diagnosticInfoFromTemplateInfo(info); const dinfo = diagnosticInfoFromTemplateInfo(info);
const scope = getExpressionScope(dinfo, path); const scope = getExpressionScope(dinfo, path);
result = getExpressionSymbol(scope, tb.expression !, path.position, info.template.query); result = getExpressionSymbol(scope, tb.value !, path.position, info.template.query);
} else if (inSpan(valueRelativePosition, tb.span)) { } else if (inSpan(valueRelativePosition, tb.span)) {
const template = path.first(EmbeddedTemplateAst); const template = path.first(EmbeddedTemplateAst);
if (template) { if (template) {