Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(compiler-cli): catch function instance properties in interpolated signal diagnostic #54325

Conversation

cexbrayat
Copy link
Member

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • angular.io application / infrastructure changes
  • Other... Please describe:

What is the current behavior?

Currently, the following template compiles without error, even if the signal is not properly called:

<div>{{ mySignal.name }}</div>

This is because name is one of the instance properties of Function (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function#instance_properties).

What is the new behavior?

The interpolated signal diagnostic is now extended to catch such issues.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

@pullapprove pullapprove bot requested a review from dylhunn February 7, 2024 21:50
@jessicajaniuk jessicajaniuk added the area: compiler Issues related to `ngc`, Angular's template compiler label Feb 7, 2024
@ngbot ngbot bot added this to the Backlog milestone Feb 7, 2024
// check for `{{ mySignal.name }}` or `{{ mySignal.length }}` or `{{ mySignal.prototype }}`
// as these are the name of instance properties of Function, the compiler does _not_ throw an error
const symbolOfReceiver = ctx.templateTypeChecker.getSymbolOfNode(node.receiver, component);
if (isFunctionInstanceProperty(node.name) && symbolOfReceiver?.kind === SymbolKind.Expression && (isSignal(symbolOfReceiver.tsType.symbol) || isSignal(symbolOfReceiver.tsType.aliasSymbol))) {
Copy link
Member

Choose a reason for hiding this comment

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

Would it make sense to always report a diagnostic if the receiver is a signal and not invoked? e.g.

  • {{mySignal.set()}} should not be supported anyway
  • `x="{{mySignal.set()}} neither
  • etc.

or do I miss something? Just a high-level comment from a quick glance

Copy link
Member Author

Choose a reason for hiding this comment

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

Most cases should already be caught by the compiler (for example {{ mySignal.version }}) but I suppose {{ mySignal.set }} or {{ mySignal.update }} aren't and we probably should. It may never make sense to have a signal receiver that is not invoked, you're right.

Let me know if the team thinks we should report this, and I'll update the PR.

Copy link
Member

Choose a reason for hiding this comment

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

How is .version caught btw? In either case, I think we can forbid set and .update as well. I'd propose doing that change

Copy link
Member Author

Choose a reason for hiding this comment

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

Something like version is caught because it does not exist on WritableSignal so the compiler throws.
I updated the PR to catch set, update, and asReadonly 👍

Copy link
Member

Choose a reason for hiding this comment

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

I wonder if asReadonly might not be useful to allow, in property bindings, but we can always change

@cexbrayat cexbrayat force-pushed the fix/function-property-in-signal-interpolation branch 5 times, most recently from 57fa4aa to f3e6910 Compare February 19, 2024 16:31
@devversion devversion added action: merge The PR is ready for merge by the caretaker target: patch This PR is targeted for the next patch release labels Feb 23, 2024
@devversion devversion removed the request for review from dylhunn February 23, 2024 17:32
@devversion
Copy link
Member

@cexbrayat can you please rebase?

@cexbrayat cexbrayat force-pushed the fix/function-property-in-signal-interpolation branch from f3e6910 to 19f6fbd Compare February 26, 2024 15:16
… signal diagnostic

Currently, the following template compiles without error, even if the signal is not properly called:

```
<div>{{ mySignal.name }}</div>
```

This is because `name` is one of the instance properties of Function (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function#instance_properties).

The interpolated signal diagnostic is now extended to catch such issues.
@cexbrayat cexbrayat force-pushed the fix/function-property-in-signal-interpolation branch from 19f6fbd to 4ed94ba Compare February 26, 2024 15:21
@cexbrayat
Copy link
Member Author

@devversion Done

@dylhunn
Copy link
Contributor

dylhunn commented Feb 27, 2024

This PR was merged into the repository by commit f578889.

@dylhunn dylhunn closed this in f578889 Feb 27, 2024
dylhunn pushed a commit that referenced this pull request Feb 27, 2024
… signal diagnostic (#54325)

Currently, the following template compiles without error, even if the signal is not properly called:

```
<div>{{ mySignal.name }}</div>
```

This is because `name` is one of the instance properties of Function (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function#instance_properties).

The interpolated signal diagnostic is now extended to catch such issues.

PR Close #54325
@angular-automatic-lock-bot
Copy link

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 Mar 29, 2024
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 target: patch This PR is targeted for the next patch release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants