Skip to content

Commit

Permalink
Merge pull request #983 from rwjblue/codemod-ignores-whitespace
Browse files Browse the repository at this point in the history
Ensure codemod mode ignores whitespace control.
  • Loading branch information
rwjblue committed Oct 28, 2019
2 parents c996080 + f1b9619 commit 8cc7175
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 23 deletions.
3 changes: 1 addition & 2 deletions packages/@glimmer/syntax/handlebars-shim.js
@@ -1,2 +1 @@
import { parse } from './handlebars/compiler/base';
export { parse };
export { parse, parser as Parser } from './handlebars/compiler/base';
20 changes: 13 additions & 7 deletions packages/@glimmer/syntax/lib/parser/tokenizer-event-handlers.ts
Expand Up @@ -379,20 +379,26 @@ const syntax: Syntax = {
Walker,
};

// emulate parseWithoutProcessing (from https://github.com/wycats/handlebars.js/pull/1584)
// can be removed when we update to Handlebars 4.5.0
function codemodParse(html: string, parseOptions?: HandlebarsParseOptions) {
// this sets up parser.yy and parser.yy.locInfo
handlebars.parse(html, parseOptions);

// casting to any here, because Parser is not listed in the types
return (handlebars as any).Parser.parse(html);
}

export function preprocess(html: string, options: PreprocessOptions = {}): AST.Template {
let mode = options.mode || 'precompile';

let ast: HBS.Program;
if (typeof html === 'object') {
ast = html;
} else if (mode === 'codemod') {
ast = codemodParse(html, options.parseOptions);
} else {
let parseOptions = options.parseOptions || {};

if (mode === 'codemod') {
parseOptions.ignoreStandalone = true;
}

ast = handlebars.parse(html, parseOptions) as HBS.Program;
ast = handlebars.parse(html, options.parseOptions) as HBS.Program;
}

let entityParser = undefined;
Expand Down
22 changes: 8 additions & 14 deletions packages/@glimmer/syntax/test/generation/print-test.ts
Expand Up @@ -97,25 +97,19 @@ QUnit.module('[glimmer-syntax] Code generation - source -> source', function() {
templates.forEach(buildTest);

[
// custom HTML Entities
'< &   > ©2018',

// whitespace control
'\n{{~var~}} ',
'\n{{~#foo-bar~}} {{~else if x~}} {{~else~}} {{~/foo-bar~}} ',

// newlines after opening block
'{{#each}}\n <li> foo </li>\n{{/each}}',
].forEach(buildTest);

test('whitespace control is preserved', function(assert) {
let before = '\n{{~var~}} ';
let after = '{{~var~}}';

assert.equal(printTransform(before), after);
});

test('block whitespace control is preserved', function(assert) {
let before = '\n{{~#foo-bar~}} {{~else if x~}} {{~else~}} {{~/foo-bar~}} ';
let after = '{{~#foo-bar~}}{{~else if x~}}{{~else~}}{{~/foo-bar~}}';

assert.equal(printTransform(before), after);
});
// "stand alone"
' {{#foo}}\n {{bar}}\n {{/foo}}',
].forEach(buildTest);
});

QUnit.module('[glimmer-syntax] Code generation - override', function() {
Expand Down

0 comments on commit 8cc7175

Please sign in to comment.