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

Ensure codemod mode ignores whitespace control. #983

Merged
merged 1 commit into from Oct 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
Copy link
Member Author

Choose a reason for hiding this comment

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

FYI - ignoreStandalone is not used in parseWithoutProcessing mode so no need to preserve passing it here.

}

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