Skip to content

Commit

Permalink
Add possibility to ignore parts of the style files from processing
Browse files Browse the repository at this point in the history
  • Loading branch information
Hannu Pelkonen committed Apr 10, 2015
1 parent 7a03917 commit d442841
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
19 changes: 19 additions & 0 deletions lib/modules/variable-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,26 @@ function astToSrc(ast, syntax) {
});
}

function removeIgnoredBlocks(string) {
var lines = string.split('\n'),
results = [],
ignoreEnabled = false;
lines.forEach(function(line) {
if (line.indexOf('styleguide:ignore:start') !== -1) {
ignoreEnabled = true;
} else if (line.indexOf('styleguide:ignore:end') !== -1) {
ignoreEnabled = false;
} else if (!ignoreEnabled) {
results.push(line);
}
});
return results.join('\n');
}

// Parse Style variables to object
function parseVariableDeclarations(string, syntax) {
syntax = syntax || 'scss';
string = removeIgnoredBlocks(string);

var out = [],
ast = gonzales.srcToAST({
Expand Down Expand Up @@ -79,6 +96,7 @@ function parseVariableDeclarations(string, syntax) {
// Parse Style variables to object
function findVariables(string, syntax) {
syntax = syntax || 'scss';
string = removeIgnoredBlocks(string);

var out = [],
ast = gonzales.srcToAST({
Expand Down Expand Up @@ -144,6 +162,7 @@ function findModifierVariables(modifiers) {
}

module.exports = {
removeIgnoredBlocks: removeIgnoredBlocks,
parseVariableDeclarations: parseVariableDeclarations,
parseVariableDeclarationsFromFiles: parseVariableDeclarationsFromFiles,
findVariables: findVariables,
Expand Down
60 changes: 60 additions & 0 deletions test/unit/modules/variable-parser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,66 @@ var requireModule = require('requirefrom')('lib/modules'),
multiline = require('multiline'),
parser = requireModule('variable-parser');

describe('ignore block removal', function() {
it('should remove definitions between tags and leave other lines intact', function() {
var str = multiline(function() {
/*
First line
styleguide:ignore:start
Ignore 1
Ignore 2
styleguide:ignore:end
Last line
*/
});
expect(parser.removeIgnoredBlocks(str)).to.eql('First line\nLast line')
});

it('should remove everything on the same line as tags', function() {
var str = multiline(function() {
/*
First line
// styleguide:ignore:start something
Ignore 1
Ignore 2
// styleguide:ignore:end something
Last line
*/
});
expect(parser.removeIgnoredBlocks(str)).to.eql('First line\nLast line')
});

it('should support multiple blocks', function() {
var str = multiline(function() {
/*
First line
styleguide:ignore:start
Ignore 1
styleguide:ignore:end
Middle line
styleguide:ignore:start
Ignore 1
styleguide:ignore:end
Last line
*/
});
expect(parser.removeIgnoredBlocks(str)).to.eql('First line\nMiddle line\nLast line')
});

it('should remove everything after start tag even if it is not closed', function() {
var str = multiline(function() {
/*
First line
styleguide:ignore:start
Ignore 1
Ignore 2
Ignore 3
*/
});
expect(parser.removeIgnoredBlocks(str)).to.eql('First line')
});
});

describe('Variable Parser', function() {

it('should handle plain CSS files', function(done) {
Expand Down

0 comments on commit d442841

Please sign in to comment.