refactor(compiler-cli): wrap RHS of property write in parens (#39143)

The right needs to be wrapped in parens or we cannot accurately match its
span to just the RHS. For example, the span in `e = $event /*0,10*/` is ambiguous.
It could refer to either the whole binary expression or just the RHS.
We should instead generate `e = ($event /*0,10*/)` so we know the span 0,10 matches RHS.
This is specifically needed for the TemplateTypeChecker/Language Service
when mapping template positions to items in the TCB.

PR Close #39143
This commit is contained in:
Andrew Scott 2020-10-06 15:02:19 -07:00 committed by atscott
parent d4a51ae594
commit 4ede190571
2 changed files with 14 additions and 1 deletions

View File

@ -251,7 +251,14 @@ class AstTranslator implements AstVisitor {
// ^ nameSpan
const leftWithPath = wrapForDiagnostics(left);
addParseSpanInfo(leftWithPath, ast.sourceSpan);
const right = this.translate(ast.value);
let right = this.translate(ast.value);
// The right needs to be wrapped in parens as well or we cannot accurately match its
// span to just the RHS. For example, the span in `e = $event /*0,10*/` is ambiguous.
// It could refer to either the whole binary expression or just the RHS.
// We should instead generate `e = ($event /*0,10*/)` so we know the span 0,10 matches RHS.
if (!ts.isParenthesizedExpression(right)) {
right = wrapForTypeChecker(right);
}
const node =
wrapForDiagnostics(ts.createBinary(leftWithPath, ts.SyntaxKind.EqualsToken, right));
addParseSpanInfo(node, ast.sourceSpan);

View File

@ -94,6 +94,12 @@ describe('type check blocks diagnostics', () => {
'(((((((ctx).a /*14,15*/) /*14,15*/).b /*16,17*/) /*14,17*/).c /*18,19*/) /*14,23*/ = ((ctx).d /*22,23*/) /*22,23*/) /*14,23*/');
});
it('should $event property writes', () => {
const TEMPLATE = `<div (click)='a = $event'></div>`;
expect(tcbWithSpans(TEMPLATE))
.toContain('(((ctx).a /*14,15*/) /*14,24*/ = ($event /*18,24*/)) /*14,24*/;');
});
it('should annotate keyed property access', () => {
const TEMPLATE = `{{ a[b] }}`;
expect(tcbWithSpans(TEMPLATE))