feat(compiler): Add keySpan to Variable Node (#38965)

Now that we have `keySpan` for `BoundAttribute` (implemented in
https://github.com/angular/angular/pull/38898) we could do the same
for `Variable`.

This would allow us to distinguish the LHS and RHS from the whole source
span.

PR Close #38965
This commit is contained in:
Keen Yee Liau
2020-09-23 16:28:36 -07:00
committed by Alex Rickabaugh
parent cf9176eb46
commit 239968d2f1
3 changed files with 18 additions and 13 deletions

View File

@ -118,7 +118,7 @@ export class Content implements Node {
export class Variable implements Node {
constructor(
public name: string, public value: string, public sourceSpan: ParseSourceSpan,
public valueSpan?: ParseSourceSpan) {}
readonly keySpan: ParseSourceSpan, public valueSpan?: ParseSourceSpan) {}
visit<Result>(visitor: Visitor<Result>): Result {
return visitor.visitVariable(this);
}

View File

@ -159,7 +159,7 @@ class HtmlAstToIvyAst implements html.Visitor {
templateKey, templateValue, attribute.sourceSpan, absoluteValueOffset, [],
templateParsedProperties, parsedVariables);
templateVariables.push(...parsedVariables.map(
v => new t.Variable(v.name, v.value, v.sourceSpan, v.valueSpan)));
v => new t.Variable(v.name, v.value, v.sourceSpan, v.keySpan, v.valueSpan)));
} else {
// Check for variables, events, property bindings, interpolation
hasBinding = this.parseAttribute(
@ -356,7 +356,8 @@ class HtmlAstToIvyAst implements html.Visitor {
} else if (bindParts[KW_LET_IDX]) {
if (isTemplateElement) {
const identifier = bindParts[IDENT_KW_IDX];
this.parseVariable(identifier, value, srcSpan, attribute.valueSpan, variables);
const keySpan = createKeySpan(srcSpan, bindParts[KW_LET_IDX], identifier);
this.parseVariable(identifier, value, srcSpan, keySpan, attribute.valueSpan, variables);
} else {
this.reportError(`"let-" is only supported on ng-template elements.`, srcSpan);
}
@ -425,7 +426,7 @@ class HtmlAstToIvyAst implements html.Visitor {
}
private parseVariable(
identifier: string, value: string, sourceSpan: ParseSourceSpan,
identifier: string, value: string, sourceSpan: ParseSourceSpan, keySpan: ParseSourceSpan,
valueSpan: ParseSourceSpan|undefined, variables: t.Variable[]) {
if (identifier.indexOf('-') > -1) {
this.reportError(`"-" is not allowed in variable names`, sourceSpan);
@ -433,7 +434,7 @@ class HtmlAstToIvyAst implements html.Visitor {
this.reportError(`Variable does not have a name`, sourceSpan);
}
variables.push(new t.Variable(identifier, value, sourceSpan, valueSpan));
variables.push(new t.Variable(identifier, value, sourceSpan, keySpan, valueSpan));
}
private parseReference(