Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/compiler/src/expression_parser/ast.ts
Expand Up @@ -286,7 +286,7 @@ export class ASTWithSource extends AST {
export class TemplateBinding {
constructor(
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 {
Expand Down
36 changes: 18 additions & 18 deletions packages/compiler/src/expression_parser/parser.ts
Expand Up @@ -314,7 +314,7 @@ export class _ParseAST {

advance() { this.index++; }

optionalCharacter(code: number): boolean {
consumeOptionalCharacter(code: number): boolean {
if (this.next.isCharacter(code)) {
this.advance();
return true;
Expand All @@ -327,7 +327,7 @@ export class _ParseAST {
peekKeywordAs(): boolean { return this.next.isKeywordAs(); }

expectCharacter(code: number) {
if (this.optionalCharacter(code)) return;
if (this.consumeOptionalCharacter(code)) return;
this.error(`Missing expected ${String.fromCharCode(code)}`);
}

Expand Down Expand Up @@ -372,11 +372,11 @@ export class _ParseAST {
const expr = this.parsePipe();
exprs.push(expr);

if (this.optionalCharacter(chars.$SEMICOLON)) {
if (this.consumeOptionalCharacter(chars.$SEMICOLON)) {
if (!this.parseAction) {
this.error('Binding expression cannot contain chained expression');
}
while (this.optionalCharacter(chars.$SEMICOLON)) {
while (this.consumeOptionalCharacter(chars.$SEMICOLON)) {
} // read all semicolons
} else if (this.index < this.tokens.length) {
this.error(`Unexpected token '${this.next}'`);
Expand All @@ -399,7 +399,7 @@ export class _ParseAST {
const name = this.expectIdentifierOrKeyword();
const nameSpan = this.sourceSpan(nameStart);
const args: AST[] = [];
while (this.optionalCharacter(chars.$COLON)) {
while (this.consumeOptionalCharacter(chars.$COLON)) {
args.push(this.parseExpression());
}
const {start} = result.span;
Expand All @@ -420,7 +420,7 @@ export class _ParseAST {
if (this.optionalOperator('?')) {
const yes = this.parsePipe();
let no: AST;
if (!this.optionalCharacter(chars.$COLON)) {
if (!this.consumeOptionalCharacter(chars.$COLON)) {
const end = this.inputIndex;
const expression = this.input.substring(start, end);
this.error(`Conditional expression ${expression} requires all 3 expressions`);
Expand Down Expand Up @@ -570,13 +570,13 @@ export class _ParseAST {
let result = this.parsePrimary();
const resultStart = result.span.start;
while (true) {
if (this.optionalCharacter(chars.$PERIOD)) {
if (this.consumeOptionalCharacter(chars.$PERIOD)) {
result = this.parseAccessMemberOrMethodCall(result, false);

} else if (this.optionalOperator('?.')) {
result = this.parseAccessMemberOrMethodCall(result, true);

} else if (this.optionalCharacter(chars.$LBRACKET)) {
} else if (this.consumeOptionalCharacter(chars.$LBRACKET)) {
this.rbracketsExpected++;
const key = this.parsePipe();
this.rbracketsExpected--;
Expand All @@ -589,7 +589,7 @@ export class _ParseAST {
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++;
const args = this.parseCallArguments();
this.rparensExpected--;
Expand All @@ -608,7 +608,7 @@ export class _ParseAST {

parsePrimary(): AST {
const start = this.inputIndex;
if (this.optionalCharacter(chars.$LPAREN)) {
if (this.consumeOptionalCharacter(chars.$LPAREN)) {
this.rparensExpected++;
const result = this.parsePipe();
this.rparensExpected--;
Expand All @@ -635,7 +635,7 @@ export class _ParseAST {
this.advance();
return new ImplicitReceiver(this.span(start), this.sourceSpan(start));

} else if (this.optionalCharacter(chars.$LBRACKET)) {
} else if (this.consumeOptionalCharacter(chars.$LBRACKET)) {
this.rbracketsExpected++;
const elements = this.parseExpressionList(chars.$RBRACKET);
this.rbracketsExpected--;
Expand Down Expand Up @@ -673,7 +673,7 @@ export class _ParseAST {
if (!this.next.isCharacter(terminator)) {
do {
result.push(this.parsePipe());
} while (this.optionalCharacter(chars.$COMMA));
} while (this.consumeOptionalCharacter(chars.$COMMA));
}
return result;
}
Expand All @@ -683,15 +683,15 @@ export class _ParseAST {
const values: AST[] = [];
const start = this.inputIndex;
this.expectCharacter(chars.$LBRACE);
if (!this.optionalCharacter(chars.$RBRACE)) {
if (!this.consumeOptionalCharacter(chars.$RBRACE)) {
this.rbracesExpected++;
do {
const quoted = this.next.isString();
const key = this.expectIdentifierOrKeywordOrString();
keys.push({key, quoted});
this.expectCharacter(chars.$COLON);
values.push(this.parsePipe());
} while (this.optionalCharacter(chars.$COMMA));
} while (this.consumeOptionalCharacter(chars.$COMMA));
this.rbracesExpected--;
this.expectCharacter(chars.$RBRACE);
}
Expand All @@ -702,7 +702,7 @@ export class _ParseAST {
const start = receiver.span.start;
const id = this.expectIdentifierOrKeyword();

if (this.optionalCharacter(chars.$LPAREN)) {
if (this.consumeOptionalCharacter(chars.$LPAREN)) {
this.rparensExpected++;
const args = this.parseCallArguments();
this.expectCharacter(chars.$RPAREN);
Expand Down Expand Up @@ -742,7 +742,7 @@ export class _ParseAST {
const positionals: AST[] = [];
do {
positionals.push(this.parsePipe());
} while (this.optionalCharacter(chars.$COMMA));
} while (this.consumeOptionalCharacter(chars.$COMMA));
return positionals as BindingPipe[];
}

Expand Down Expand Up @@ -845,7 +845,7 @@ export class _ParseAST {
private parseDirectiveKeywordBindings(key: string, keySpan: ParseSpan, absoluteOffset: number):
TemplateBinding[] {
const bindings: TemplateBinding[] = [];
this.optionalCharacter(chars.$COLON); // trackBy: trackByFunction
this.consumeOptionalCharacter(chars.$COLON); // trackBy: trackByFunction
const valueExpr = this.getDirectiveBoundTarget();
const span = new ParseSpan(keySpan.start, this.inputIndex);
bindings.push(new TemplateBinding(
Expand Down Expand Up @@ -943,7 +943,7 @@ export class _ParseAST {
* Consume the optional statement terminator: semicolon or comma.
*/
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) {
Expand Down
9 changes: 4 additions & 5 deletions packages/compiler/src/template_parser/binding_parser.ts
Expand Up @@ -134,10 +134,9 @@ export class BindingParser {
const binding = bindings[i];
if (binding.keyIsVar) {
targetVars.push(new ParsedVariable(binding.key, binding.name, sourceSpan));
} else if (binding.expression) {
} else if (binding.value) {
this._parsePropertyAst(
binding.key, binding.expression, sourceSpan, undefined, targetMatchableAttrs,
targetProps);
binding.key, binding.value, sourceSpan, undefined, targetMatchableAttrs, targetProps);
} else {
targetMatchableAttrs.push([binding.key, '']);
this.parseLiteralAttr(
Expand Down Expand Up @@ -165,8 +164,8 @@ export class BindingParser {
this._exprParser.parseTemplateBindings(tplKey, tplValue, sourceInfo, absoluteValueOffset);
this._reportExpressionParserErrors(bindingsResult.errors, sourceSpan);
bindingsResult.templateBindings.forEach((binding) => {
if (binding.expression) {
this._checkPipes(binding.expression, sourceSpan);
if (binding.value) {
this._checkPipes(binding.value, sourceSpan);
}
});
bindingsResult.warnings.forEach(
Expand Down
23 changes: 12 additions & 11 deletions packages/compiler/test/expression_parser/parser_spec.ts
Expand Up @@ -248,14 +248,16 @@ describe('parser', () => {

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 => {
if (binding.keyIsVar) {
return 'let ' + binding.key + (binding.name == null ? '=null' : '=' + binding.name);
} else {
return binding.key + (binding.expression == null ? '' : `=${binding.expression}`);
return binding.key + (binding.value == null ? '' : `=${binding.value}`);
}
});
}
Expand All @@ -265,14 +267,13 @@ describe('parser', () => {
binding => source.substring(binding.span.start, binding.span.end));
}

function exprSources(templateBindings: any[]) {
return templateBindings.map(
binding => binding.expression != null ? binding.expression.source : null);
function exprSources(templateBindings: TemplateBinding[]) {
return templateBindings.map(binding => binding.value != null ? binding.value.source : null);
}

function humanize(bindings: TemplateBinding[]): Array<[string, string | null, boolean]> {
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);
return [key, value, keyIsVar];
});
Expand Down Expand Up @@ -316,13 +317,13 @@ describe('parser', () => {

it('should store the sources in the result', () => {
const bindings = parseTemplateBindings('a', '1,b 2');
expect(bindings[0].expression !.source).toEqual('1');
expect(bindings[1].expression !.source).toEqual('2');
expect(bindings[0].value !.source).toEqual('1');
expect(bindings[1].value !.source).toEqual('2');
});

it('should store the passed-in 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', () => {
Expand Down Expand Up @@ -415,7 +416,7 @@ describe('parser', () => {

it('should parse pipes', () => {
const bindings = parseTemplateBindings('key', 'value|pipe');
const ast = bindings[0].expression !.ast;
const ast = bindings[0].value !.ast;
expect(ast).toBeAnInstanceOf(BindingPipe);
});

Expand Down
4 changes: 2 additions & 2 deletions packages/language-service/src/completions.ts
Expand Up @@ -567,8 +567,8 @@ class ExpressionVisitor extends NullTemplateVisitor {
}
}

if (binding.expression && inSpan(valueRelativePosition, binding.expression.ast.span)) {
this.processExpressionCompletions(binding.expression.ast);
if (binding.value && inSpan(valueRelativePosition, binding.value.ast.span)) {
this.processExpressionCompletions(binding.value.ast);
return;
}

Expand Down
4 changes: 2 additions & 2 deletions packages/language-service/src/locate_symbol.ts
Expand Up @@ -209,10 +209,10 @@ function getSymbolInMicrosyntax(info: AstResult, path: TemplateAstPath, attribut

// Find the symbol that contains the position.
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 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)) {
const template = path.first(EmbeddedTemplateAst);
if (template) {
Expand Down