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

[WIP] Initial Handlebars/Glimmer support #1951

Closed
wants to merge 12 commits into from
Closed

Conversation

azz
Copy link
Member

@azz azz commented Jun 4, 2017

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:

  • Some handlebars features ({{> 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.

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)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

.moustache as well

Copy link
Member Author

Choose a reason for hiding this comment

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

Or .mustache?

Copy link
Member Author

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

Copy link
Contributor

Choose a reason for hiding this comment

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

hah, indeed!

@azz
Copy link
Member Author

azz commented Jun 4, 2017

Not sure what's going on with the build on Node.js v4.

FAIL  tests/html_glimmer/jsfmt.spec.js
  ● Test suite failed to run
    SyntaxError: Unexpected token =
      
      at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/ScriptTransformer.js:289:17)
      at Object.<anonymous> (node_modules/@glimmer/syntax/dist/commonjs/index.js:4:16)
      at parse (src/parser-glimmer.js:6:25)

src/parser-glimmer.js:6:25 is:

  const glimmerSyntax = require("@glimmer/syntax");

@azz azz force-pushed the feat/html branch 2 times, most recently from 2d4af09 to ecd2027 Compare June 4, 2017 13:40
@azz
Copy link
Member Author

azz commented Jun 4, 2017

Misleading error. Tracked it down to glimmer's source, turns out they're building JS without node v4 support:

node_modules/@glimmer/syntax/dist/commonjs/lib/parser.js:33:

     constructor(source, options = {}) {

@vjeux
Copy link
Contributor

vjeux commented Jun 4, 2017

Omg, this is so exciting!

@vjeux
Copy link
Contributor

vjeux commented Jun 4, 2017

Cc @mixonic, @wycats

@azz azz force-pushed the feat/html branch 5 times, most recently from e301279 to ecd2027 Compare June 4, 2017 15:06
@azz
Copy link
Member Author

azz commented Jun 4, 2017

Not having much luck with patching @glimmer/syntax, will need to clone it and do a custom build tomorrow.

@azz
Copy link
Member Author

azz commented Jun 4, 2017

@vjeux I disabled the new tests in case you want to merge this.

@azz azz changed the title [WIP] Initial HTML/Handlebars/Glimmer support Initial HTML/Handlebars/Glimmer support Jun 4, 2017
@vjeux
Copy link
Contributor

vjeux commented Jun 4, 2017

Is glimmer parser a fully compliant html5 parser?

@vjeux
Copy link
Contributor

vjeux commented Jun 4, 2017

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.

@wycats
Copy link

wycats commented Jun 4, 2017

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.

@wycats
Copy link

wycats commented Jun 4, 2017

Is your concern about applying prettier to Handlebars templates that are emitting HTML but less strict than the Glimmer parser?

@vjeux
Copy link
Contributor

vjeux commented Jun 4, 2017

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.

@vjeux
Copy link
Contributor

vjeux commented Jun 4, 2017

It looks like glimmer parser is simple-html-tokenizer for the html parsing and then uses handlebars itself to compile the templates: import { parse } from "./handlebars/compiler/base";.

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?

@vjeux
Copy link
Contributor

vjeux commented Jun 4, 2017

Vue uses a small handrolled html parser: https://github.com/vuejs/vue/blob/dev/src/compiler/parser/html-parser.js

@azz
Copy link
Member Author

azz commented Jun 4, 2017

Looking into parse5, it supports two AST formats:

default: TreeAdapter
Default tree format for parse5.

htmlparser2: TreeAdapter
Quite popular htmlparser2 tree format (e.g. used by cheerio and jsdom).

Any preferences?

@azz
Copy link
Member Author

azz commented Jun 4, 2017

Angular also appears to use a handmade parser.

https://github.com/angular/angular/blob/master/packages/compiler/src/ml_parser/html_parser.ts

@wycats
Copy link

wycats commented Jun 5, 2017

@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 t) is closing the title attribute. What we do instead is parse as handlebars first, then tokenize the leftover content using the HTML tokenizer library above.

@vjeux
Copy link
Contributor

vjeux commented Jun 5, 2017

Thanks for the insight! @wycats do you know if glimmer parser is able to parse all the angular 2 and vue files as well?

@aboyton
Copy link
Contributor

aboyton commented Jun 5, 2017

I know it's a lot to ask, but any idea if glimmer can also parse underscore/lodash templates?

@vjeux
Copy link
Contributor

vjeux commented Jun 5, 2017

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

@azz
Copy link
Member Author

azz commented Jun 5, 2017

Done. #1969

@azz azz changed the title Initial HTML/Handlebars/Glimmer support [WIP] Initial HTML/Handlebars/Glimmer support Jun 10, 2017
@vjeux
Copy link
Contributor

vjeux commented Jun 13, 2017

Are you going to keep working on this PR or should you open a new one?

@azz
Copy link
Member Author

azz commented Jun 13, 2017

I'll look at this today.

@azz azz changed the title [WIP] Initial HTML/Handlebars/Glimmer support [WIP] Initial Handlebars/Glimmer support Jun 14, 2017
@azz
Copy link
Member Author

azz commented Jun 14, 2017

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.

@vjeux
Copy link
Contributor

vjeux commented Jun 21, 2017

I'm going to close it. Feel free to re-open it when you'll be working on it again.

@vjeux vjeux closed this Jun 21, 2017
@azz azz mentioned this pull request Sep 9, 2017
@lock lock bot added the locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. label Jan 20, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Jan 20, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants