Skip to content

fix(compiler-cli): handle nullable expressions correctly in the nullish coalescing extended template diagnostic - #43572

Closed
dgp1130 wants to merge 1 commit into
angular:masterfrom
dgp1130:nullish-coalescing-symbol
Closed

dgp1130 wants to merge 1 commit into
angular:masterfrom
dgp1130:nullish-coalescing-symbol

Conversation

@dgp1130

@dgp1130 dgp1130 commented Sep 24, 2021 •

Copy link
Copy Markdown
Contributor

Checking a template with the syntax:

<div>{{ foo() ?? 'test' }}</div>

Where foo() returns a nullable value:

@component(/* ... */)
class TestCmp {
  foo: (): string | null => null;
}

Will always log a nullish coalescing not nullable warning. This is because getSymbolOfNode(node.left) would return the symbol of the function (foo) rather than the symbol of its returned value (foo()). Fixed this getting the symbol for the whole expression's span, rather than just the function receiver.

This broke one existing test case of exactly this behavior which was easily updated. However, there's no explicit justification for why this test case was important. Nothing else seems to be obviously broken by this change.

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix

What is the current behavior?

Issue Number: b/199443160

Getting the symbol of a Call expression returns the receiver's symbol rather than the value returned by the function call.

What is the new behavior?

Getting the symbol of a Call expression now returns the symbol of the value returned by the function call. In particular, this fixes the nullish coalescing not nullable check to work correctly with Call expression inputs.

Does this PR introduce a breaking change?

  • No

@google-cla google-cla Bot added the cla: yes label Sep 24, 2021
@dgp1130
dgp1130 force-pushed the nullish-coalescing-symbol branch from 387275a to b9bd2f9 Compare September 24, 2021 18:10
@dgp1130

dgp1130 commented Sep 24, 2021

Copy link
Copy Markdown
Contributor Author

^ @alxhub @atscott, not sure what the right function to fix this is. Need some way of converting an expression node to its type.

@dgp1130
dgp1130 force-pushed the nullish-coalescing-symbol branch 3 times, most recently from b687ada to f13cd2d Compare September 24, 2021 23:23
@dgp1130 dgp1130 changed the title Nullish coalescing symbol fix(compiler-cli): handle nullable expressions correctly in the nullish coalescing extended template diagnostic Sep 24, 2021
@dgp1130 dgp1130 added the target: patch This PR is targeted for the next patch release label Sep 24, 2021

@dgp1130 dgp1130 left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After some discussion with @alxhub, it seems that getSymbolForNode() is the right function here, it's just that it shouldn't special-case Call expressions. Updated the PR to fix this behavior.

@dgp1130
dgp1130 marked this pull request as ready for review September 24, 2021 23:30
@dgp1130
dgp1130 force-pushed the nullish-coalescing-symbol branch from f13cd2d to 89035bf Compare September 24, 2021 23:31
@dgp1130
dgp1130 force-pushed the nullish-coalescing-symbol branch from 89035bf to 689ad58 Compare September 27, 2021 17:30
…sh coalescing extended template diagnostic

Refs angular#42966.

Previously, checking a template with the syntax:

```html
<div>{{ foo() ?? 'test' }}</div>
```

Where `foo()` returns a nullable value:

```typescript
@component(/* ... */)
class TestCmp {
  foo: (): string | null => null;
}
```

Would always log a nullish coalescing not nullable warning. This is because [`getSymbolOfNode(node.left)`](https://github.com/angular/angular/blob/fe691935091aaf7090864c8111a15f7cc7e53b6c/packages/compiler-cli/src/ngtsc/typecheck/extended/checks/nullish_coalescing_not_nullable/index.ts#L30) would return the [symbol of the function (`foo`)](https://github.com/angular/angular/blob/fe691935091aaf7090864c8111a15f7cc7e53b6c/packages/compiler-cli/src/ngtsc/typecheck/src/template_symbol_builder.ts#L536-L538) rather than the symbol of its returned value (`foo()`). Fixed this by getting the symbol for the whole expression's span, rather than just the function receiver.

Also made some minor refactorings to `template_symbol_builder` to make a similar change to safe method calls. This behavior was originally for the language service in order to handle quick info, as the user highlighting a function name would actually apply to the entire expression. This is no longer true as the language service will correctly request the type from the function rather than the `Call` expression, so these hacks are not necessary anymore. This broke two existing test cases of exactly this behavior which were easily updated. Also added a test to the language service to confirm that it is not broken by this change.
@dgp1130
dgp1130 force-pushed the nullish-coalescing-symbol branch from 689ad58 to 7006856 Compare September 28, 2021 22:20
@dgp1130
dgp1130 requested a review from atscott September 28, 2021 22:22
@alxhub alxhub added area: compiler Issues related to `ngc`, Angular's template compiler compiler: template type-checking labels Sep 30, 2021
@ngbot ngbot Bot modified the milestone: Backlog Sep 30, 2021

@atscott atscott left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👌

@dgp1130
dgp1130 removed request for alxhub and crisbeto October 1, 2021 17:18
@dgp1130 dgp1130 added the action: merge The PR is ready for merge by the caretaker label Oct 1, 2021
@dylhunn

dylhunn commented Oct 4, 2021

Copy link
Copy Markdown
Contributor

This PR doesn't merge cleanly into 12.2.x. Mind rebasing, or targeting major?

@dylhunn dylhunn added action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews and removed action: merge The PR is ready for merge by the caretaker labels Oct 4, 2021
@dgp1130

dgp1130 commented Oct 5, 2021

Copy link
Copy Markdown
Contributor Author

@dylhunn, sorry about that. It looks like the previous PR (#38844) which introduced some of this code was not merged to patch, so it causes conflicts here. I could manually back port this PR, but extended templates diagnostics won't be shipped to patch anyways, so I'm not sure it's worth the effort. I was intending to keep patch synchronized to reduce differences with major, but since there will be differences anyways due to #38844 and patch will be falling into LTS soon anyways, I'm not too concerned about it.

I think we can just merge this to major and go with that.

@dgp1130 dgp1130 added target: major This PR is targeted for the next major release action: merge The PR is ready for merge by the caretaker and removed target: patch This PR is targeted for the next patch release action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews labels Oct 5, 2021
@dylhunn

dylhunn commented Oct 5, 2021

Copy link
Copy Markdown
Contributor

This PR was merged into the repository by commit 263feba.

@dylhunn dylhunn closed this in 263feba Oct 5, 2021
@dgp1130
dgp1130 deleted the nullish-coalescing-symbol branch October 5, 2021 00:59
@angular-automatic-lock-bot

Copy link
Copy Markdown

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot Bot locked and limited conversation to collaborators Nov 5, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

action: merge The PR is ready for merge by the caretaker area: compiler Issues related to `ngc`, Angular's template compiler cla: yes compiler: template type-checking target: major This PR is targeted for the next major release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants