Skip to content

Commit

Permalink
Merge pull request #121 from hu9o/else-support
Browse files Browse the repository at this point in the history
Support for if-else-endif.
  • Loading branch information
pioug committed Apr 10, 2020
2 parents 80407ec + 07a1696 commit 4b4e7b3
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
18 changes: 15 additions & 3 deletions lib/preprocess.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,15 +162,27 @@ function preprocessor(src, context, opts, noRestoreEol) {

if (opts.type.if) {
rv = replaceRecursive(rv, opts.type.if, function (startMatches, endMatches, include, recurse) {
// I need to recurse first, so I don't catch "inner" else-directives
var recursed = recurse(include);

// look for the first else-directive
var matches = opts.type.else && recursed.match(new RegExp(opts.type.else));
var match = (matches || [""])[0];
var index = match ? recursed.indexOf(match) : recursed.length;

var ifBlock = recursed.substring(0, index);
var elseBlock = recursed.substring(index + match.length); // empty string if no else-directive

var variant = startMatches[1];
var test = (startMatches[2] || '').trim();

switch(variant) {
case 'if':
return testPasses(test,context) ? recurse(include) : '';
return testPasses(test,context) ? ifBlock : elseBlock;
case 'ifdef':
return typeof getDeepPropFromObj(context, test) !== 'undefined' ? recurse(include) : '';
return typeof getDeepPropFromObj(context, test) !== 'undefined' ? ifBlock : elseBlock;
case 'ifndef':
return typeof getDeepPropFromObj(context, test) === 'undefined' ? recurse(include) : '';
return typeof getDeepPropFromObj(context, test) === 'undefined' ? ifBlock : elseBlock;
default:
throw new Error('Unknown if variant ' + variant + '.');
}
Expand Down
3 changes: 3 additions & 0 deletions lib/regexrules.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module.exports = {
start : "[ \t]*<!--[ \t]*@(ifndef|ifdef|if)[ \t]+(.*?)[ \t]*(?:-->|!>)(?:[ \t]*\n+)?",
end : "[ \t]*<!(?:--)?[ \t]*@endif[ \t]*(?:-->|!>)(?:[ \t]*\n)?"
},
else : "[ \t]*<!(?:--)?[ \t]*@else[ \t]*(?:-->|!>)(?:[ \t]*\n)?",
foreach : {
start : "[ \t]*<!--[ \t]*@foreach[ \t]+(.*?)[ \t]*(?:-->|!>)(?:[ \t]*\n+)?",
end : "[ \t]*<!(?:--)?[ \t]*@endfor[ \t]*(?:-->|!>)(?:[ \t]*\n)?"
Expand Down Expand Up @@ -56,6 +57,7 @@ module.exports = {
start : "[ \t]*(?://|/\\*)[ \t]*@(ifndef|ifdef|if)[ \t]+([^\n*]*)(?:\\*(?:\\*|/))?(?:[ \t]*\n+)?",
end : "[ \t]*(?://|/\\*)[ \t]*@endif[ \t]*(?:\\*(?:\\*|/))?(?:[ \t]*\n)?"
},
else : "[ \t]*(?://|/\\*)[ \t]*@else[ \t]*(?:\\*(?:\\*|/))?(?:[ \t]*\n)?",
foreach : {
start : "[ \t]*(?://|/\\*)[ \t]*@foreach[ \t]+([^\n*]*)(?:\\*(?:\\*|/))?(?:[ \t]*\n+)?",
end : "[ \t]*(?://|/\\*)[ \t]*@endfor[ \t]*(?:\\*(?:\\*|/))?(?:[ \t]*\n)?"
Expand All @@ -79,6 +81,7 @@ module.exports = {
start : "^[ \t]*#+[ \t]*@(ifndef|ifdef|if)[ \t]+(.*?)[ \t]*\n+",
end : "^[ \t]*#+[ \t]*@endif[ \t]*\n?"
},
else : "^[ \t]*#+[ \t]*@else[ \t]*\n?",
foreach : {
start : "^[ \t]*#+[ \t]*@foreach[ \t]+(.*?)[ \t]*\n+",
end : "^[ \t]*#+[ \t]*@endfor[ \t]*\n?"
Expand Down
52 changes: 52 additions & 0 deletions test/if-else.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
'use strict';

var chai = require('chai'),
pp = require('../lib/preprocess');

chai.should();

describe('@if/@else directive shall be preprocessed', function () {
describe('in html', function () {

['=', '==', '==='].forEach(function (equalsOp) {
describe('and should work with unequality operator `' + equalsOp + '`', function () {

describe('with common comment syntax', function () {
var input = "a\n" +
"<!-- @if NODE_ENV" + equalsOp + "'production' -->\n" +
"b\n" +
"<!-- @else -->\n" +
"c\n" +
"<!-- @endif -->\n" +
"d";

it('and exclude second block if condition evals to true', function () {
pp.preprocess(input, {NODE_ENV: 'production'}).should.equal("a\nb\nd");
});

it('and exclude first block if condition evals to false', function () {
pp.preprocess(input, {NODE_ENV: 'dev'}).should.equal("a\nc\nd");
});
});

});
});

it('should support nesting', function () {
var input = "a\n" +
"// @ifdef FLAG\n" +
"b\n" +
"// @ifdef FLAG2\n" +
"bad\n" +
"// @else\n" +
"c\n" +
"// @endif\n" +
"d\n" +
"// @else\n" +
"equally bad\n" +
"// @endif\n" +
"e";
pp.preprocess(input, {FLAG: 1}, 'js').should.equal("a\nb\nc\nd\ne");
});
});
});

0 comments on commit 4b4e7b3

Please sign in to comment.