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

Support for the new Angular Control Flow syntax #2219

Closed
thaoula opened this issue Nov 7, 2023 · 11 comments
Closed

Support for the new Angular Control Flow syntax #2219

thaoula opened this issue Nov 7, 2023 · 11 comments

Comments

@thaoula
Copy link

thaoula commented Nov 7, 2023

Description

Hi Team,

The Angular team recently introduced a new Control Flow syntax for templates - https://angular.dev/guide/templates/control-flow

The new control flow syntax provides the following elements - If, else, switch and for loops. These are alternatives to *ngIf, *ngFor and *ngSwitch.

Below is some examples of the control flow syntax and expected output-

Input

With this new feature, when I give like this input:

@if (a > b) {
{{a}} is greater than {{b}}
}

@if (a > b) {
{{a}} is greater than {{b}}
} @else if (b > a) {
{{a}} is less than {{b}}
} @else {
{{a}} is equal to {{b}}
}

@for (item of items; track item.name) {
<li> {{ item.name }} </li>
} @empty {
<li> There are no items. </li>
}

@switch (condition) {
@case (caseA) { 
Case A.
}
@case (caseB) {
Case B.
}
@default {
Default case.
}
}

Expected Output

I'd like to see this output:

@if (a > b) {
  {{a}} is greater than {{b}}
}

@if (a > b) {
  {{a}} is greater than {{b}}
} @else if (b > a) {
  {{a}} is less than {{b}}
} @else {
  {{a}} is equal to {{b}}
}

@for (item of items; track item.name) {
  <li> {{ item.name }} </li>
} @empty {
  <li> There are no items. </li>
}

@switch (condition) {
  @case (caseA) {
    Case A.
  }
  @case (caseB) {
    Case B.
  }
  @default {
    Default case.
  }
}

Environment

OS: Mac

@liesahead
Copy link

This issue deserves more attention, imo, because currently it's a pain to read such code while also no desire to return to the old syntax.

@for (item of items; track item.id) {
<p>{{ item.name }}
} @empty {
<p>[No items.]</p>
}

@gergely-gyorgy-both
Copy link
Contributor

This issue deserves more attention, imo, because currently it's a pain to read such code while also no desire to return to the old syntax.

@for (item of items; track item.id) {
<p>{{ item.name }}
} @empty {
<p>[No items.]</p>
}

There is an ongoing PR regarding this issue, so it's definitely work in progress.

@liesahead
Copy link

@gergely-gyorgy-both , great, thanks 😄 just wanted to bump the issue up a bit by commenting here.

@schulzi66
Copy link

@gergely-gyorgy-both Thank you! Really looking forward to it, can't wait 🙏

@tsenguunchik
Copy link

Excited to see this!

@meowzhin
Copy link

meowzhin commented Feb 4, 2024

Any updates on this? The alternative is use Prettier which in my opinion is terrible and I tend to avoid it at all costs.

bitwiseman added a commit that referenced this issue Feb 15, 2024
* Format index.html

* Fix package.json files config

* Ignore Pythong .eggs directory

* update strings for move from beautify-web to beautifier org

* Bump actions/setup-python from 4 to 5

Bumps [actions/setup-python](https://github.com/actions/setup-python) from 4 to 5.
- [Release notes](https://github.com/actions/setup-python/releases)
- [Commits](actions/setup-python@v4...v5)

---
updated-dependencies:
- dependency-name: actions/setup-python
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <[email protected]>

* Bump github/codeql-action from 2 to 3

Bumps [github/codeql-action](https://github.com/github/codeql-action) from 2 to 3.
- [Release notes](https://github.com/github/codeql-action/releases)
- [Changelog](https://github.com/github/codeql-action/blob/main/CHANGELOG.md)
- [Commits](github/codeql-action@v2...v3)

---
updated-dependencies:
- dependency-name: github/codeql-action
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <[email protected]>

* Update codemirror and other web page depenedencies (#2234)

* Fixed #2219 - formatting of new Angular control flow syntax (#2221)

* Fixed #2219 - formatting of new Angular control flow syntax

* Add 'angular' templating option; use it for html beautifier control flow syntax

* Add more precise selection for angular control flow close tag

* Print angular control flow tokens with basic formatting

* Add tests for fixing issue #2219

* Change angular control flow selection to do via pattern

* Fix selecting control flow closing brace if it is not preceded by whitespace

* Fix regex for control flow start pattern; only select control flow open if indent_handlebars is true

* Changing angular at-string detection regex

Limiting this to a smaller set.

---------

Co-authored-by: Liam Newman <[email protected]>

* Update Changelog

* Bump version numbers for 1.15.0

* Release: 1.15.0

---------

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: Liam Newman <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: gergely-gyorgy-both <[email protected]>
Co-authored-by: GitHub Action <[email protected]>
@gndk
Copy link

gndk commented Feb 15, 2024

@thaoula @bitwiseman FYI this change seems to have possibly unintended consequences.

I use js-beautify to beautify MJML output. With the new release the output changed, see here:

Before:

    <style type="text/css">
      @media only screen and (min-width:480px) {
        .mj-column-per-100 { width:100% !important; max-width: 100%; }
      }
    </style>

After:

   <style type="text/css">
-    @media only screen and (min-width:480px) {
-      .mj-column-per-100 {
-        width: 100% !important;
-        max-width: 100%;
-      }
+  @media only screen and (min-width:480px) {
+      .mj-column-per-100
+      {
+      width:100%
+      !important;
+      max-width:
+      100%;
+  }
     }
   </style>

Settings used with htmlBeautify:

indent_size: 2,
preserve_newlines: false,
css: {
  end_with_newline: false,
},
js: {
  end_with_newline: false,
},
html: {
  end_with_newline: true,
},

@bitwiseman
Copy link
Member

bitwiseman commented Feb 16, 2024

Thanks for the heads up!

Hm, strange. I would expect the css beautifier to handle the style tag contents.

If I can resolve it today, I'll roll back.

@gergely-gyorgy-both
Copy link
Contributor

@thaoula @bitwiseman FYI this change seems to have possibly unintended consequences.

I use js-beautify to beautify MJML output. With the new release the output changed, see here:

Before:

    <style type="text/css">
      @media only screen and (min-width:480px) {
        .mj-column-per-100 { width:100% !important; max-width: 100%; }
      }
    </style>

After:

   <style type="text/css">
-    @media only screen and (min-width:480px) {
-      .mj-column-per-100 {
-        width: 100% !important;
-        max-width: 100%;
-      }
+  @media only screen and (min-width:480px) {
+      .mj-column-per-100
+      {
+      width:100%
+      !important;
+      max-width:
+      100%;
+  }
     }
   </style>

Settings used with htmlBeautify:

indent_size: 2,
preserve_newlines: false,
css: {
  end_with_newline: false,
},
js: {
  end_with_newline: false,
},
html: {
  end_with_newline: true,
},

This is quite strange. Sorry for the inconvenience, @bitwiseman please notify me if you need any help fixing this.

@bitwiseman
Copy link
Member

@gergely-gyorgy-both
See also #2246 . I've switching this feature to off by default (#2247).
Don't feel bad. This is a hard problem space and these issues are why I lean towards "off by default" changes. In this, I took the change with the feature "off by default in most cases". Lesson learned for me. 😄

@bitwiseman
Copy link
Member

microsoft/vscode-html-languageservice#177 is the last step needed in getting this made available in VS Code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants