Skip to content

Feature Request: Control flow schematic formatter option to preserve newlines #53494

Closed

Description

@TimUnderhay

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

core

Description

The new control flow migration is a marvellous bit of engineering, however the formatter has a troublesome side-effect of removing all newlines from the templates. It would be preferable were the migration formatter to take a more minimalistic approach to formatting, only adding newlines / whitespace where the control flow code is added to the html templates.

Proposed solution

Add a property to the control migration schema to preserve existing newlines when formatting. And naturally, the formatter should thus obey this.

Something like:

"preserveNewLines": {
      "type": "boolean",
      "description": "Preserve newlines when formatting",
      "x-prompt": "Should existing newlines be preserved when formatting?",
      "default": "false"
    }

Alternatives considered

There are other formatters such as prettier, which is too opinionated, or beautify-js does not yet support the new control flow syntax.

Activity

  1. jessicajaniuk commented on Dec 11, 2023

    @jessicajaniuk
    Contributor

    I can understand the desire for this. There's a tradeoff to it though. When the control flow migration runs and replaces some content, some of the items, like ng-templates and ng-containers, get removed. That ends up leaving a blank line behind. The removal of newlines was a compromise to not leave awkward empty lines where there were none before. So preserving newlines would essentially leave all the prior existing newlines, but would also leave in the awkwardly added newlines from the migration, as well.

  2. TimUnderhay commented on Dec 11, 2023

    @TimUnderhay
    Author

    Thanks @jessicajaniuk for the quick reply! The thought process makes sense. Speaking for myself, I wonder if it be bad to simply remove only those lines where the ng-templates and ng-containers were previously, as they no longer have meaning?

    The current behaviour presents a conundrum for some users: use the formatter and have all newlines wiped out, and have to go line by line through every file to undo it, or not use the formatter and have to go through every template and reposition all of the new control statements. Either of those involves a not inconsiderable amount of tedium and headache. IMHO, this method might be a nice compromise between the two.

  3. jessicajaniuk commented on Dec 11, 2023

    @jessicajaniuk
    Contributor

    The challenge here is that during the formatting process, there's no context into what was there before vs what's changed. It happens after the actual migration of code finishes and is just looking at the new template line by line. I can see if there's a way I can mark lines that were changed and only remove those newlines. If so, I can safely only remove newlines in the migrated areas.

  4. added a commit that references this issue on Dec 11, 2023
    34d9b17
  5. added
    area: migrations
    core: control flow
    on Dec 11, 2023
  6. modified the milestone: needsTriage on Dec 11, 2023
  7. jessicajaniuk commented on Dec 11, 2023

    @jessicajaniuk
    Contributor

    I put a fix up that should resolve this without needing an optional flag. It's in #53508.

  8. TimUnderhay commented on Dec 12, 2023

    @TimUnderhay
    Author

    @jessicajaniuk you are a legend. Thank you!

  9. added a commit that references this issue on Dec 12, 2023
    b75aca1
  10. angular-automatic-lock-bot commented on Jan 12, 2024

    @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.

  11. locked and limited conversation to collaborators on Jan 12, 2024
  12. added a commit that references this issue on Jan 23, 2024
    b7ff47f
  13. added a commit that references this issue on Jan 26, 2024
    62c6ca8
  14. added a commit that references this issue on Jan 26, 2024
    1192324
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area: migrationscore: control flow

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions