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

handlebars: Support escaping a mustache with a backslash #8634

Merged
merged 4 commits into from Jun 24, 2020

Conversation

dcyriller
Copy link
Collaborator

@dcyriller dcyriller commented Jun 23, 2020

Description

In handlebars, it is possible to escape a mustache with a backslash. See handelbars documentation here.

This PR propose to add the support for this feature.

This would close #8633

Initial report: jgwhite#1 (comment)

Details

This PR would introduce a new file: src/language-handlebars/preprocess.js. It is present to make an adjustement to the AST produced by Glimmer parser.

Indeed, given that template:

a non-escaped mustache: \\\{{helper}}

Glimmer parser will produce two nodes for the template body:

  • a TextNode where node.chars === "a non-escaped mustache: \\\\" (note the backslash escaping here, so it is actually"a non-escaped mustache: \\", which means that a backslash has been swallowed)
  • a MustacheStatement for {{helper}}

In order to correctly print the input, we need to adjust the TextNode to node.chars === "a non-escaped mustache: \\\\\\" (or "a non-escaped mustache: \\\" without the backslash escaping), namely we have to add a backslash that is removed by Glimmer parser. This is the third commit purpose. If you'd like you could checkout the second commit's CI report. It highlights the need for the third commit (the preprocess file).

Checklist

  • 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 changelog_unreleased/*/pr-XXXX.md file following changelog_unreleased/TEMPLATE.md.
  • I’ve read the contributing guidelines.

Try the playground for this PR

Given that template:
```hbs
a non-escaped mustache: \\\{{helper}}
```

Glimmer parser will produce two body nodes:
- a TextNode where `node.chars === "a non-escaped mustache: \\\\"`
- a MustacheStatement for `{{helper}}`

In order to correctly print the input, we need to adjust the TextNode to
`node.chars === "a non-escaped mustache: \\\\\\"`, namely we have to add
a backslash that is removed by Glimmer parser. This is this commit
purpose.
Copy link
Member

@fisker fisker 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.

@thorn0 thorn0 merged commit bff049f into prettier:master Jun 24, 2020
@dcyriller dcyriller deleted the hbs-escape-mustache branch June 25, 2020 06:15
@fisker
Copy link
Member

fisker commented Jun 26, 2020

Just noticed thee package size increased ~500k, is there better way?

@fisker
Copy link
Member

fisker commented Jun 26, 2020

require("@glimmer/syntax/dist/commonjs/es5/lib/traversal/traverse.js") ?

@fisker
Copy link
Member

fisker commented Jun 26, 2020

Or maybe let it be parser postprocess ?

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 27, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support escaping a MustacheStatement ({{statement}}) with a backslash
4 participants