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:
parent
59078c444c
commit
bef14cf424
|
@ -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 {
|
||||
|
|
|
@ -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;
|
||||
|
@ -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)}`);
|
||||
}
|
||||
|
||||
|
@ -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}'`);
|
||||
|
@ -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;
|
||||
|
@ -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`);
|
||||
|
@ -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--;
|
||||
|
@ -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--;
|
||||
|
@ -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--;
|
||||
|
@ -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--;
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -683,7 +683,7 @@ 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();
|
||||
|
@ -691,7 +691,7 @@ export class _ParseAST {
|
|||
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);
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -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[];
|
||||
}
|
||||
|
||||
|
@ -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(
|
||||
|
@ -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) {
|
||||
|
|
|
@ -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(
|
||||
|
@ -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(
|
||||
|
|
|
@ -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}`);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -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];
|
||||
});
|
||||
|
@ -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', () => {
|
||||
|
@ -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);
|
||||
});
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue