Skip to content

Add range ignore support for html languages.#6693

Open
voithos wants to merge 5 commits intoprettier:mainfrom
voithos:range-ignore-html
Open

Add range ignore support for html languages.#6693
voithos wants to merge 5 commits intoprettier:mainfrom
voithos:range-ignore-html

Conversation

@voithos
Copy link
Copy Markdown
Contributor

@voithos voithos commented Oct 22, 2019

This adds support for the prettier-ignore-start and prettier-ignore-end
comment markers in html based languages, allowing prettier to preserve
existing formatting of multi-element sections of code.

Fixes #5508.

  • I’ve added tests to confirm my change works.
  • (If changing the API or CLI) I’ve documented the changes I’ve made (in the docs/ directory)
  • (If the change is user-facing) I’ve added my changes to the CHANGELOG.unreleased.md file following the template.
  • I’ve read the contributing guidelines.

Try the playground for this PR

@voithos
Copy link
Copy Markdown
Contributor Author

voithos commented Oct 22, 2019

This PR has one problem that I can't seem to figure out: the <!-- prettier-ignore-start --> tag gets auto-indented properly (while its contents don't, of course) but the <!-- prettier-ignore-end --> tag seems to just take whatever indentation it had prior to formatting. Example.

This adds support for the prettier-ignore-start and prettier-ignore-end
comment markers in html based languages, allowing prettier to preserve
existing formatting of multi-element sections of code.

Fixes prettier#5508.
@voithos
Copy link
Copy Markdown
Contributor Author

voithos commented Oct 23, 2019

I played around with it a bit more, and made some changes that fix the issue I described earlier! Unfortunately, it feels somewhat hacky, although I can't think of any actual corner cases off the top of my head?

Any thoughts welcome.

@lydell
Copy link
Copy Markdown
Member

lydell commented Nov 2, 2019

@ikatyang Since you implemented the HTML printer, and the markdown printer which supports range ignore, could you review this please?

@voithos
Copy link
Copy Markdown
Contributor Author

voithos commented Nov 24, 2019

Friendly ping?

* the child should process normally.
* @return {'ignore' | 'continue' | Doc}
*/
function maybeIgnore(path, index, ignoreRanges) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's move this to util

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It's a bit difficult to move this as it uses variables from outer scopes and the doc builders. Other functions in utils don't use the doc builders. Let's keep it as is.

Copy link
Copy Markdown
Member

@alexander-akait alexander-akait left a comment

Choose a reason for hiding this comment

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

Can we add strange test like:

<div><!-- prettier-ignore-start --></div><!-- prettier-ignore-end -->
<div><!-- prettier-ignore-start --><span>test</span></div><!-- prettier-ignore-end -->

@alanorozco
Copy link
Copy Markdown

👋

Alan here from ampproject/amphtml.

We love prettier, and use it across the board except for HTML files.

Our blocker is that our documents hold something we call the "AMP boilerplate", which is a standard and specific subtree: a <style> element and (sometimes) a <noscript> element. See our playground for a base document.

In our ecosystem this subtree is usually written as one line, which is commonly pasted or automatically inserted in documents. <!-- prettier-ignore --> (for next line) won't work for the <noscript> element, since it's a sibling node.

This change would allow us to use prettier for all our HTML files, as we'd be able to hold the entire boilerplate in one explicit ignore section. This is useful for AMP authors who use prettier too.

Please let us know how we can help to get this merged!

@j-f1
Copy link
Copy Markdown
Member

j-f1 commented Mar 13, 2020

Could you include a <!-- prettier-ignore --> inside the boilerplate before the noscript and avoid the issue that way?

@alanorozco
Copy link
Copy Markdown

alanorozco commented Mar 13, 2020

@j-f1

Thanks for the suggestion. That can work, but we're avoiding it since we'd still like to keep the boilerplate's ignore section explicitly scoped.

<!-- prettier-ignore-start -->
<style amp-boilerplate>/* ... */</style><noscript><style amp-boilerplate>/* ... */</style></noscript>
<!-- prettier-ignore-end -->

vs

<!-- prettier-ignore -->
<style amp-boilerplate>/* ... */</style>
<!-- prettier-ignore -->
<noscript><style amp-boilerplate>/* ... */</style></noscript>

The boilerplate is often pasted, or new authors might see it and be unsure what it is. I worry that splitting it might create confusion, especially since we "validate" for its presence on every doc.

@thorn0 thorn0 changed the base branch from master to next March 17, 2020 01:07
@thorn0 thorn0 requested review from alexander-akait and j-f1 March 17, 2020 01:25
@thorn0 thorn0 changed the base branch from next to master March 21, 2020 21:02
@thorn0 thorn0 added this to the 2.1 milestone Mar 23, 2020
@voithos
Copy link
Copy Markdown
Contributor Author

voithos commented Mar 23, 2020

Hey, sorry for the radio silence on this, but thanks for making the fixes to this! Let me know if there's anything I can do to help this get merged. 👍

@thorn0
Copy link
Copy Markdown
Member

thorn0 commented Mar 23, 2020

This PR needs 2 more reviews from the collaborators.

Besides, this is a feature, and there are fixes in master that we might want to publish as a patch release 2.0.2 before merging new features.

@thorn0 thorn0 requested a review from sosukesuzuki March 23, 2020 10:08
Copy link
Copy Markdown
Member

@alexander-akait alexander-akait left a comment

Choose a reason for hiding this comment

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

Looks good

/cc @prettier/core

Copy link
Copy Markdown
Member

@j-f1 j-f1 left a comment

Choose a reason for hiding this comment

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

Bug:

Prettier pr-6693
Playground link

--parser html

Input:

<div>
  <!-- prettier-ignore-start -->
    <span>

      Test</span><!-- prettier-ignore-end -->


  <!-- prettier-ignore-start -->
    <span>

      Test</span>
  <!-- prettier-ignore-end -->
</div>

Output:

<div>
  <!-- prettier-ignore-start -->
    <span>

      Test</span>><!-- prettier-ignore-end -->

  <!-- prettier-ignore-start -->
    <span>

      Test</span><!-- prettier-ignore-end -->
</div>

Copy link
Copy Markdown
Member

@thorn0 thorn0 left a comment

Choose a reason for hiding this comment

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

Bug # 2 (whitespace is lost between the spans):

Prettier pr-6693
Playground link

--parser html

Input:

<div>
<!-- prettier-ignore-start -->
    <span>Test</span>
  <!-- prettier-ignore-end --><span>2</span>
</div>

Output:

<div>
  <!-- prettier-ignore-start -->
    <span>Test</span><!-- prettier-ignore-end --><span
    >2</span
  >
</div>

@thorn0
Copy link
Copy Markdown
Member

thorn0 commented May 6, 2020

@voithos Do you plan to fix the bugs found above? If not, we can probably ask @alanorozco to look into them.

@voithos
Copy link
Copy Markdown
Contributor Author

voithos commented May 8, 2020

Hello! Yep, plan on taking a look soon.

@voithos
Copy link
Copy Markdown
Contributor Author

voithos commented Jul 17, 2020

Welp, super late response to this, but I still haven't managed to find the time/motivation to pick this up again. :\ Really sorry about that. That being said, feel free to have someone else take a look at fixing the remaining bugs and getting this merged, if the desire is still there. 👍

@sosukesuzuki
Copy link
Copy Markdown
Contributor

@voithos Okay, thank you. Let's keep this PR open, remove from 2.1 milestone. It may be good to put help wanted label.
/cc @thorn0 @fisker @evilebottnawi

@sosukesuzuki sosukesuzuki removed this from the 2.1 milestone Jul 18, 2020
@fisker fisker added this to the 2.2 milestone Aug 4, 2020
Base automatically changed from master to main January 23, 2021 17:13
@thorn0 thorn0 removed this from the 2.3 milestone Feb 11, 2021
@thorn0 thorn0 added the help wanted We're a small group who can't get to every issue promptly. We’d appreciate help fixing this issue! label Mar 12, 2021
@camslice
Copy link
Copy Markdown

bump

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

Labels

help wanted We're a small group who can't get to every issue promptly. We’d appreciate help fixing this issue!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Range Ignore Support in HTML

9 participants