refactor(compiler-cli): Add nameSpan to SafePropertyRead TCB (#39768)

In order to map the a safe property read's method access in the type check block
directly back to the property in the template source, we need to
include the `SafePropertyRead`'s `nameSpan` with the `ts.propertyAccess` for
the pipe's transform method.

Note that this is specifically relevant to the Language Service's "find
references" feature. As an example, with something like `{{a?.value}}`,
when calling "find references" on the 'value' we want the text
span of the reference to just be `value` rather than the entire source
`a?.value`.

PR Close #39768
This commit is contained in:
Andrew Scott 2020-11-20 12:07:36 -08:00 committed by Misko Hevery
parent 1a5e5f86a3
commit 786295dfbd
2 changed files with 4 additions and 1 deletions

View File

@ -309,6 +309,7 @@ class AstTranslator implements AstVisitor {
// "a?.b" becomes (null as any ? a!.b : undefined)
// The type of this expression is (typeof a!.b) | undefined, which is exactly as desired.
const expr = ts.createPropertyAccess(ts.createNonNullExpression(receiver), ast.name);
addParseSpanInfo(expr, ast.nameSpan);
node = ts.createParen(ts.createConditional(NULL_AS_ANY, expr, UNDEFINED));
} else if (VeSafeLhsInferenceBugDetector.veWillInferAnyFor(ast)) {
// Emulate a View Engine bug where 'any' is inferred for the left-hand side of the safe
@ -322,6 +323,7 @@ class AstTranslator implements AstVisitor {
// result is still inferred as `any`.
// "a?.b" becomes (a!.b as any)
const expr = ts.createPropertyAccess(ts.createNonNullExpression(receiver), ast.name);
addParseSpanInfo(expr, ast.nameSpan);
node = tsCastToAny(expr);
}
addParseSpanInfo(node, ast.sourceSpan);

View File

@ -116,7 +116,8 @@ describe('type check blocks diagnostics', () => {
it('should annotate safe property access', () => {
const TEMPLATE = `{{ a?.b }}`;
expect(tcbWithSpans(TEMPLATE))
.toContain('((null as any) ? (((ctx).a /*3,4*/) /*3,4*/)!.b : undefined) /*3,7*/');
.toContain(
'((null as any) ? (((ctx).a /*3,4*/) /*3,4*/)!.b /*6,7*/ : undefined) /*3,7*/');
});
it('should annotate safe method calls', () => {