Skip to content

Commit 49571b0

Browse files
author
Hannu Pelkonen
committed
Add possibility to ignore parts of the style files from processing
1 parent 7a03917 commit 49571b0

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

lib/modules/variable-parser.js

+19
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,26 @@ function astToSrc(ast, syntax) {
1313
});
1414
}
1515

16+
function removeIgnoredBlocks(string) {
17+
var lines = string.split('\n'),
18+
results = [],
19+
ignoreEnabled = false;
20+
lines.forEach(function(line) {
21+
if (line.indexOf('styleguide:ignore:start') !== -1) {
22+
ignoreEnabled = true;
23+
} else if (line.indexOf('styleguide:ignore:end') !== -1) {
24+
ignoreEnabled = false;
25+
} else if (!ignoreEnabled) {
26+
results.push(line);
27+
}
28+
});
29+
return results.join('\n');
30+
}
31+
1632
// Parse Style variables to object
1733
function parseVariableDeclarations(string, syntax) {
1834
syntax = syntax || 'scss';
35+
string = removeIgnoredBlocks(string);
1936

2037
var out = [],
2138
ast = gonzales.srcToAST({
@@ -79,6 +96,7 @@ function parseVariableDeclarations(string, syntax) {
7996
// Parse Style variables to object
8097
function findVariables(string, syntax) {
8198
syntax = syntax || 'scss';
99+
string = removeIgnoredBlocks(string);
82100

83101
var out = [],
84102
ast = gonzales.srcToAST({
@@ -144,6 +162,7 @@ function findModifierVariables(modifiers) {
144162
}
145163

146164
module.exports = {
165+
removeIgnoredBlocks: removeIgnoredBlocks,
147166
parseVariableDeclarations: parseVariableDeclarations,
148167
parseVariableDeclarationsFromFiles: parseVariableDeclarationsFromFiles,
149168
findVariables: findVariables,

test/unit/modules/variable-parser.test.js

+60
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,66 @@ var requireModule = require('requirefrom')('lib/modules'),
44
multiline = require('multiline'),
55
parser = requireModule('variable-parser');
66

7+
describe('ignore block removal', function() {
8+
it('should remove definitions between tags and leave other lines intact', function() {
9+
var str = multiline(function() {
10+
/*
11+
First line
12+
styleguide:ignore:start
13+
Ignore 1
14+
Ignore 2
15+
styleguide:ignore:end
16+
Last line
17+
*/
18+
});
19+
expect(parser.removeIgnoredBlocks(str)).to.eql('First line\nLast line');
20+
});
21+
22+
it('should remove everything on the same line as tags', function() {
23+
var str = multiline(function() {
24+
/*
25+
First line
26+
// styleguide:ignore:start something
27+
Ignore 1
28+
Ignore 2
29+
// styleguide:ignore:end something
30+
Last line
31+
*/
32+
});
33+
expect(parser.removeIgnoredBlocks(str)).to.eql('First line\nLast line');
34+
});
35+
36+
it('should support multiple blocks', function() {
37+
var str = multiline(function() {
38+
/*
39+
First line
40+
styleguide:ignore:start
41+
Ignore 1
42+
styleguide:ignore:end
43+
Middle line
44+
styleguide:ignore:start
45+
Ignore 1
46+
styleguide:ignore:end
47+
Last line
48+
*/
49+
});
50+
expect(parser.removeIgnoredBlocks(str)).to.eql('First line\nMiddle line\nLast line');
51+
});
52+
53+
it('should remove everything after start tag even if it is not closed', function() {
54+
var str = multiline(function() {
55+
/*
56+
First line
57+
styleguide:ignore:start
58+
Ignore 1
59+
Ignore 2
60+
Ignore 3
61+
*/
62+
});
63+
expect(parser.removeIgnoredBlocks(str)).to.eql('First line');
64+
});
65+
});
66+
767
describe('Variable Parser', function() {
868

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

0 commit comments

Comments
 (0)