Skip to content

Better error message for nullish coalescing a pipe argument #44871

Closed as not planned

Description

@dgp1130

Which @angular/* package(s) are relevant/releated to the feature request?

compiler-cli

Description

Operator precedence between nullish coalescing an a pipe argument can be confusing. Often they are used together in this way:

<span>{{ birthday | date: 'YYYY-MM-dd' ?? 'Unknown' }}</span>

In this case the developer probably intended for the birthday to be formatted like "YYYY-MM-dd", and if that format returned null or undefined for some reason (such as birthday being null or undefined), then it should fall back to "Unknown". However in practice, the ?? has higher precedence than the |, so it actually does 'YYYY-MM-dd' ?? 'Unknown', which is useless.

<!-- How the developer wanted it to work. -->
<span>{{ (birthday | date: 'YYYY-MM-dd') ?? 'Unknown' }}</span>

<!-- How it actually works. -->
<span>{{ birthday | date: ('YYYY-MM-dd' ?? 'Unknown') }}</span>

This mistake should be highlighted to users to bring awareness about the issue and how to properly parenthesize the expression to do what they want.

Proposed solution

We already have an extended diagnostic for a useless nullish coalescing operator, which is triggered by 'YYYY-MM-dd' ?? 'Unknown' and correctly identifies this mistake. Unfortunately the error message is a bit unclear. We should either adjust the diagnostic or add a new one to specifically call out this pipe use case and suggest adding parentheses in the right manner.

Alternatives considered

Alternatively we could just leave the existing diagnostic as is and accept a less-accurate error message. It does correctly identify the mistake, just with a less-than-ideal error message.

Activity

  1. added
    feature
    area: compiler
    on Jan 28, 2022
  2. modified the milestone: Backlog on Jan 28, 2022
  3. angular-robot commented on Jan 28, 2022

    @angular-robot
    Contributor

    This feature request is now candidate for our backlog! In the next phase, the community has 60 days to upvote. If the request receives more than 20 upvotes, we'll move it to our consideration list.

    You can find more details about the feature request process in our documentation.

  4. angular-robot commented on Mar 8, 2022

    @angular-robot
    Contributor

    Just a heads up that we kicked off a community voting process for your feature request. There are 20 days until the voting process ends.

    Find more details about Angular's feature request process in our documentation.

  5. angular-robot commented on Mar 28, 2022

    @angular-robot
    Contributor

    Thank you for submitting your feature request! Looks like during the polling process it didn't collect a sufficient number of votes to move to the next stage.

    We want to keep Angular rich and ergonomic and at the same time be mindful about its scope and learning journey. If you think your request could live outside Angular's scope, we'd encourage you to collaborate with the community on publishing it as an open source package.

    You can find more details about the feature request process in our documentation.

  6. atscott commented on Jun 16, 2022

    @atscott
    Contributor

    Closing as won't fix for the time-being / insufficient votes. As mentioned, the existing diagnostic does warn about the mistake and does have the correct error. It's just less descriptive than it could be. The template AST does not maintain a reference to parent nodes so modifying the existing diagnostic to have more contextual knowledge would be difficult/impossible.

  7. angular-automatic-lock-bot commented on Jul 17, 2022

    @angular-automatic-lock-bot

    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.

  8. locked and limited conversation to collaborators on Jul 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area: compilercompiler: extended diagnosticsfeature

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions