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

Support for if-else-endif. #121

Merged
merged 1 commit into from
Apr 10, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
});
});
});