62ed3c25c7
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
😉).
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.
43 lines
1.0 KiB
JavaScript
43 lines
1.0 KiB
JavaScript
import runtime from './handlebars.runtime';
|
|
|
|
// Compiler imports
|
|
import AST from './handlebars/compiler/ast';
|
|
import { parser as Parser, parse, parseWithoutProcessing } from './handlebars/compiler/base';
|
|
import { Compiler, compile, precompile } from './handlebars/compiler/compiler';
|
|
import JavaScriptCompiler from './handlebars/compiler/javascript-compiler';
|
|
import Visitor from './handlebars/compiler/visitor';
|
|
|
|
import noConflict from './handlebars/no-conflict';
|
|
|
|
let _create = runtime.create;
|
|
function create() {
|
|
let hb = _create();
|
|
|
|
hb.compile = function(input, options) {
|
|
return compile(input, options, hb);
|
|
};
|
|
hb.precompile = function(input, options) {
|
|
return precompile(input, options, hb);
|
|
};
|
|
|
|
hb.AST = AST;
|
|
hb.Compiler = Compiler;
|
|
hb.JavaScriptCompiler = JavaScriptCompiler;
|
|
hb.Parser = Parser;
|
|
hb.parse = parse;
|
|
hb.parseWithoutProcessing = parseWithoutProcessing;
|
|
|
|
return hb;
|
|
}
|
|
|
|
let inst = create();
|
|
inst.create = create;
|
|
|
|
noConflict(inst);
|
|
|
|
inst.Visitor = Visitor;
|
|
|
|
inst['default'] = inst;
|
|
|
|
export default inst;
|