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

Add ability to disable whitespace control: Handlebars.parseWithoutProcessing #1584

Merged

Commits on Oct 27, 2019

  1. Add Handlebars.parseWithoutProcessing

    When authoring tooling that parses Handlebars files and emits Handlebars
    files, you often want to preserve the **exact** formatting of the input.
    The changes in this commit add a new method to the `Handlebars`
    namespace: `parseWithoutProcessing`. Unlike, `Handlebars.parse` (which
    will mutate the parsed AST to apply whitespace control) this method will
    parse the template and return it directly (**without** processing
    :wink:).
    
    For example, parsing the following template:
    
    ```hbs
     {{#foo}}
       {{~bar~}} {{baz~}}
     {{/foo}}
    ```
    
    Using `Handlebars.parse`, the AST returned would have truncated the
    following whitespace:
    
    * The whitespace prior to the `{{#foo}}`
    * The newline following `{{#foo}}`
    * The leading whitespace before `{{~bar~}}`
    * The whitespace between `{{~bar~}}` and `{{baz~}}`
    * The newline after `{{baz~}}`
    * The whitespace prior to the `{{/foo}}`
    
    When `Handlebars.parse` is used from  `Handlebars.precompile` or
    `Handlebars.compile`, this whitespace stripping is **very** important
    (these behaviors are intentional, and generally lead to better rendered
    output).
    
    When the same template is parsed with
    `Handlebars.parseWithoutProcessing` none of those modifications to the
    AST are made. This enables "codemod tooling" (e.g. `prettier` and
    `ember-template-recast`) to preserve the **exact** initial formatting.
    Prior to these changes, those tools would have to _manually_ reconstruct
    the whitespace that is lost prior to emitting source.
    rwjblue committed Oct 27, 2019
    Configuration menu
    Copy the full SHA
    b58a228 View commit details
    Browse the repository at this point in the history