-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
[WIP] Initial Handlebars/Glimmer support #1951
Conversation
src/options.js
Outdated
@@ -33,6 +33,8 @@ function normalize(options) { | |||
normalized.parser = "postcss"; | |||
} else if (/\.(ts|tsx)$/.test(filepath)) { | |||
normalized.parser = "typescript"; | |||
} else if (/\.(html|hbs|vue)$/.test(filepath)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.moustache
as well
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or .mustache
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll go with /mo?ustache/
:p
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hah, indeed!
Not sure what's going on with the build on Node.js v4.
const glimmerSyntax = require("@glimmer/syntax"); |
2d4af09
to
ecd2027
Compare
Misleading error. Tracked it down to glimmer's source, turns out they're building JS without node v4 support:
|
Omg, this is so exciting! |
e301279
to
ecd2027
Compare
Not having much luck with patching |
@vjeux I disabled the new tests in case you want to merge this. |
Is glimmer parser a fully compliant html5 parser? |
Looks like it's not using an html5 compliant parser and doesn't intend to be: https://github.com/tildeio/simple-html-tokenizer That makes me uneasy to include inside of prettier as people are likely to throw all sorts of crazy code at it. |
simple-html-tokenizer is intended to be a compliant tokenizer for HTML that could be found inside of an HTML body, not including scripts. It doesn't always error-correct (in cases where the HTML spec specifies that a particular character is an error, we sometimes choose to throw an exception rather than follow the error correction behavior). We think this is a good trade-off of size and simplicity to compliance for the use case of HTML templates. We also have found that Glimmer and Ember users really appreciate early errors for invalid HTML, which can be confusing when read later on. |
Is your concern about applying prettier to Handlebars templates that are emitting HTML but less strict than the Glimmer parser? |
I'd love for prettier to support regular html, vue files, angular ones, glimmer templates, mustache, handlebar... I don't have a lot of context around the different ways each of them parse code, but if we can find a single parser that is able to parse them in a way that if we print them, they have the same semantics, that would be awesome. I'm not sure if one exists though. |
It looks like glimmer parser is simple-html-tokenizer for the html parsing and then uses handlebars itself to compile the templates: Would it be possible to use a real html5 parser like https://github.com/inikulin/parse5 which is used by angular2 and polymer to parse the html and then use handlebars parser for the templates part? |
Vue uses a small handrolled html parser: https://github.com/vuejs/vue/blob/dev/src/compiler/parser/html-parser.js |
Looking into parse5, it supports two AST formats:
Any preferences? |
Angular also appears to use a handmade parser. https://github.com/angular/angular/blob/master/packages/compiler/src/ml_parser/html_parser.ts |
@vjeux not quite. Stuff like this needs to be parsed with an integrated parser: <div title="{{t "frontpage.title"}}"> Otherwise, the HTML parser will think that the second quotation mark (after |
Thanks for the insight! @wycats do you know if glimmer parser is able to parse all the angular 2 and vue files as well? |
I know it's a lot to ask, but any idea if glimmer can also parse underscore/lodash templates? |
While we figure out which parser to use, would you mind shipping the part that splits css in its own file? This is good and should help the folks trying to integrate graphql: https://twitter.com/stubailo/status/871584723524505600 |
Done. #1969 |
Are you going to keep working on this PR or should you open a new one? |
I'll look at this today. |
Gah, didn't get a chance to look further into this today. Got #2108 merged though. Since we've recently merged a separate HTML parser, I'm thinking it might be wise to stabilise that before merging in another parser. |
I'm going to close it. Feel free to re-open it when you'll be working on it again. |
Experimenting with HTML/Handlebars/Mustache/Glimmer support for #1882
Picked the glimmer parser as I thought it would have the widest range of support.
I've ran into a few issues so far:
{{> foo}}
,{{../foo}}
) are not supported. source<!doctype>
doesn't parse.The formatting is also super ugly at the moment because I figured the best way to go would be re-use some of the JSX formatting logic.
I also moved the postcss printer to a separate file.