Add ignoreStandalone compiler option

Fixes #1072
This commit is contained in:
kpdecker
2015-08-13 01:50:51 -05:00
parent 269dd492bb
commit ea3a5a1eb6
3 changed files with 20 additions and 6 deletions
+1 -1
View File
@@ -20,6 +20,6 @@ export function parse(input, options) {
return new yy.SourceLocation(options && options.srcName, locInfo);
};
let strip = new WhitespaceControl();
let strip = new WhitespaceControl(options);
return strip.accept(parser.parse(input));
}
@@ -1,10 +1,13 @@
import Visitor from './visitor';
function WhitespaceControl() {
function WhitespaceControl(options = {}) {
this.options = options;
}
WhitespaceControl.prototype = new Visitor();
WhitespaceControl.prototype.Program = function(program) {
const doStandalone = !this.options.ignoreStandalone;
let isRoot = !this.isRootSeen;
this.isRootSeen = true;
@@ -31,7 +34,7 @@ WhitespaceControl.prototype.Program = function(program) {
omitLeft(body, i, true);
}
if (inlineStandalone) {
if (doStandalone && inlineStandalone) {
omitRight(body, i);
if (omitLeft(body, i)) {
@@ -42,13 +45,13 @@ WhitespaceControl.prototype.Program = function(program) {
}
}
}
if (openStandalone) {
if (doStandalone && openStandalone) {
omitRight((current.program || current.inverse).body);
// Strip out the previous content node if it's whitespace only
omitLeft(body, i);
}
if (closeStandalone) {
if (doStandalone && closeStandalone) {
// Always strip the next node
omitRight(body, i);
@@ -106,7 +109,8 @@ WhitespaceControl.prototype.BlockStatement = function(block) {
}
// Find standalone else statments
if (isPrevWhitespace(program.body)
if (!this.options.ignoreStandalone
&& isPrevWhitespace(program.body)
&& isNextWhitespace(firstInverse.body)) {
omitLeft(program.body);
omitRight(firstInverse.body);
+10
View File
@@ -125,6 +125,16 @@ describe('blocks', function() {
shouldCompileTo('{{#people}}\n{{name}}\n{{^}}\n{{none}}\n{{/people}}\n', {none: 'No people'},
'No people\n');
});
it('block standalone else sections can be disabled', function() {
shouldCompileTo(
'{{#people}}\n{{name}}\n{{^}}\n{{none}}\n{{/people}}\n',
[{none: 'No people'}, {}, {}, {ignoreStandalone: true}],
'\nNo people\n\n');
shouldCompileTo(
'{{#none}}\n{{.}}\n{{^}}\nFail\n{{/none}}\n',
[{none: 'No people'}, {}, {}, {ignoreStandalone: true}],
'\nNo people\n\n');
});
it('block standalone chained else sections', function() {
shouldCompileTo('{{#people}}\n{{name}}\n{{else if none}}\n{{none}}\n{{/people}}\n', {none: 'No people'},
'No people\n');