From 786295dfbdbf37c452da152936b173c62c0239c9 Mon Sep 17 00:00:00 2001 From: Andrew Scott Date: Fri, 20 Nov 2020 12:07:36 -0800 Subject: [PATCH] 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 --- packages/compiler-cli/src/ngtsc/typecheck/src/expression.ts | 2 ++ .../src/ngtsc/typecheck/test/span_comments_spec.ts | 3 ++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/compiler-cli/src/ngtsc/typecheck/src/expression.ts b/packages/compiler-cli/src/ngtsc/typecheck/src/expression.ts index f4360a183c..51469e76b1 100644 --- a/packages/compiler-cli/src/ngtsc/typecheck/src/expression.ts +++ b/packages/compiler-cli/src/ngtsc/typecheck/src/expression.ts @@ -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); diff --git a/packages/compiler-cli/src/ngtsc/typecheck/test/span_comments_spec.ts b/packages/compiler-cli/src/ngtsc/typecheck/test/span_comments_spec.ts index 0f8267085d..47e1183dbb 100644 --- a/packages/compiler-cli/src/ngtsc/typecheck/test/span_comments_spec.ts +++ b/packages/compiler-cli/src/ngtsc/typecheck/test/span_comments_spec.ts @@ -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', () => {