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

feat: support HTML/Vue/Angular #5259

Merged
merged 182 commits into from Nov 4, 2018
Merged

Conversation

ikatyang
Copy link
Member

@ikatyang ikatyang commented Oct 13, 2018

Fixes #1882
Fixes #2097
Fixes #3767
Fixes #4095
Fixes #5098
Fixes #5263


Try out this PR:

yarn add ikatyang/prettier#feat/html/todos

# *.html defaults to `--parser html`
yarn prettier filename.html

# *.component.html defaults to `--parser angular`
yarn prettier filename.component.html

# *.vue defaults to `--parser vue`
yarn prettier filename.vue

# NOTE: framework-specific formatting won't be triggered in `--parser html`

or preview playground (hard reload to ensure it's the latest one).

Please note that syntax errors produced by child language in the playground is a known issue, it won't happen if you use it locally (i.e., prettier something.html).


Already supported on master:

  • HTML: support front matter
  • HTML: whitespace-sensitive formatting
    • support magic comment (<!-- display: block -->)
    • --html-whitespace-sensitivity <css|strict|ignore>
      • magic comments take precedence
      • (default) css: respect default css style (safe in the most cases)
      • strict: every element is considered whitespace sensitive (the safest)
      • ignore: every element is considered whitespace insensitive (dangerous)
  • HTML: support IE conditional comment (<!--[if IE]><![endif]-->)

Added in this PR:

  • HTML: support pragma (<!-- @prettier -->)
  • JS: support lit-html (html`<div>Hello ${name}!</div>`)
  • HTML: support CDATA (<![CDATA[ content ]]>)
  • HTML: support interpolation ({{jsExpression}})
  • HTML: format vue binding syntax as js expression (:class, v-if, v-bind:id, @click)
  • HTML: format angular binding syntax as js expression (*ngIf, [target], (target), [(target)])
  • JS: support angular inline template (@Component({ template: `<div>Hello World</div>` }))
  • HTML: disable custom self-closing tag recognition
  • Vue: replace with HTML parser + enable custom self-closing tag recognition

TODOs:


  • 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)
  • I’ve read the contributing guidelines.

@ikatyang ikatyang mentioned this pull request Oct 13, 2018
26 tasks
This was referenced Oct 13, 2018
@Dassderdie

This comment has been minimized.

@ikatyang

This comment has been minimized.

@michaeljota
Copy link

Thanks @ikatyang!

@ruchernchong
Copy link

ruchernchong commented Nov 5, 2018

Thanks, @ikatyang. I am glad to be using Vue with the awesome people behind the team.

@kissu
Copy link

kissu commented Nov 7, 2018

Was waiting so long for that, thanks @ikatyang ! 💪🏻 🎉

@homerjam

This comment has been minimized.

@alexander-akait

This comment has been minimized.

@kissu

This comment has been minimized.

@michaeljota

This comment has been minimized.

@homerjam

This comment has been minimized.

@alexander-akait

This comment has been minimized.

@michaeljota
Copy link

@ikatyang Sorry for bother you, but I would like to confirm this behavior before I open an issue for this. I'm formatting my teams project, and came with some awkward behavior.

With --html-whitespace-sensitivity=ignore this output is produced.
Link

With --html-whitespace-sensitivity=css this output is produced.
Link

Notice that when is set to ignore, the colon is moved to a new line, while when set to css, is not. Does this is an actual issue or this is working as you would expect? I would certainly not expect for ignore to add a new line after this.

@suchipi
Copy link
Member

suchipi commented Nov 8, 2018

@michaeljota that looks like a bug to me, could you open an issue for it?

@ikatyang
Copy link
Member Author

ikatyang commented Nov 8, 2018

Opened #5396 to address the sensitivity issue between interpolation and text.

@michaeljota
Copy link

Thanks @ikatyang!

@jordan-erisman
Copy link

This is awesome work!

One issue I am having is when I use arrow functions for handling events in Vue.

<custom-component @click="() => { console.log('clicked') }" />

Prettier adds a semi colon after the curly brace which throws an unexpected token error when building.

@michaeljota
Copy link

@erisman20 Can you provide a link to Prettier playground reproducing the issue?

@kissu
Copy link

kissu commented Nov 8, 2018

@erisman20 can't you use <custom-component @click="console.log('clicked')" /> in that case ?
I don't see the point of an arrow function here.

For more complicated/longer functions, write down the method inside of the @click listener.

@michaeljota
Copy link

@kissu I don't much know about vue, but I'll guess this is about the this context. Although seems an unusual behavior, Prettier should not break the code. And I can't reproduce the issue, Prettier indeed adds a semi colon, but after the function, so it shouldn't break anything.

@jordan-erisman
Copy link

@kissu That was just a basic example to showcase what was happening.

Here is a Playground.

I have never used that before, but the "second format" is how prettier formats my code.

@michaeljota In terms of Vue, you cant have a trailing semi colon like that. It will throw an error. You can even try it in the provided playground and you can see the error

@lydell
Copy link
Member

lydell commented Nov 8, 2018

@erisman20 I opened an issue for you: #5406

@alfaproject
Copy link

Why add a semicolon in the first place? That adds an extra character for no reason o.o

@michaeljota
Copy link

@alfaproject That's configurable, the default behavior however is to add a semicolon at the end of a line.

@gogakoreli
Copy link

Can I find out why html wrap attributes are now enforced? I am using prettier for my html files inside Angular project. This happened after I updated prettier

@duailibe
Copy link
Member

@gogakoreli Please open an issue filling the template

@prettier prettier locked as resolved and limited conversation to collaborators Nov 12, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet