Skip to content

Commit 1ead4c5

Browse files
committed
Fix syntax detecting (close #99)
Get file's extension and check if it's present in a list of supported syntaxes. If not, ignore the file.
1 parent d9e6bd6 commit 1ead4c5

2 files changed

Lines changed: 71 additions & 14 deletions

File tree

lib/csscomb.js

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ var doNothing = function() {};
1111
* @name Comb
1212
*/
1313
var Comb = function() {
14+
this.SUPPORTED_SYNTAXES = ['css', 'scss', 'less'];
1415
this._options = [
1516
'remove-empty-rulesets',
1617
'always-semicolon',
@@ -130,8 +131,7 @@ Comb.prototype = {
130131
*/
131132
processFile: function(path) {
132133
var _this = this;
133-
// TODO: Move extension check into `_shouldProcess` method
134-
if (this._shouldProcess(path) && path.match(/\.[css, scss]$/)) {
134+
if (this._shouldProcessFile(path)) {
135135
return vfs.read(path, 'utf8').then(function(data) {
136136
var syntax = path.split('.').pop();
137137
var processedData = _this.processString(data, syntax, path);
@@ -168,17 +168,15 @@ Comb.prototype = {
168168
return vfs.listDir(path).then(function(filenames) {
169169
return vow.all(filenames.map(function(filename) {
170170
var fullname = path + '/' + filename;
171-
if (_this._shouldProcess(fullname)) {
172-
return vfs.stat(fullname).then(function(stat) {
173-
if (stat.isDirectory()) {
174-
return _this.processDirectory(fullname);
175-
} else if (fullname.match(/\.css$/)) {
176-
return vow.when(_this.processFile(fullname)).then(function(errors) {
177-
if (errors) return errors;
178-
});
179-
}
180-
});
181-
}
171+
return vfs.stat(fullname).then(function(stat) {
172+
if (stat.isDirectory()) {
173+
return _this._shouldProcess(fullname) && _this.processDirectory(fullname);
174+
} else {
175+
return vow.when(_this.processFile(fullname)).then(function(errors) {
176+
if (errors) return errors;
177+
});
178+
}
179+
});
182180
})).then(function(results) {
183181
return [].concat.apply([], results);
184182
});
@@ -223,8 +221,24 @@ Comb.prototype = {
223221
if (exclude[i].match(path)) return false;
224222
}
225223
return true;
226-
}
224+
},
225+
226+
/**
227+
* Returns true if specified path is not in excluded list and has one of
228+
* acceptable extensions.
229+
*
230+
* @returns {Boolean}
231+
*/
232+
_shouldProcessFile: function(path) {
233+
// Get file's extension:
234+
var syntax = path.split('.').pop();
235+
// Check if syntax is supported. If not, ignore the file:
236+
if (this.SUPPORTED_SYNTAXES.indexOf(syntax) < 0) {
237+
return false;
238+
}
227239

240+
return this._shouldProcess(path);
241+
}
228242
};
229243

230244
module.exports = Comb;

test/csscomb.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
var Comb = require('../lib/csscomb');
2+
var assert = require('assert');
3+
4+
describe('csscomb methods', function() {
5+
var comb;
6+
7+
beforeEach(function() {
8+
comb = new Comb();
9+
comb.configure({ exclude: ['nani/*', 'foo', 'b.css'] });
10+
});
11+
12+
it('shouldProcess(path)', function() {
13+
assert.equal(true, comb._shouldProcess('styles'));
14+
});
15+
16+
it('shouldProcess(excluded path)', function() {
17+
assert.equal(false, comb._shouldProcess('foo'));
18+
});
19+
20+
it('shouldProcessFile(css)', function() {
21+
assert.equal(true, comb._shouldProcessFile('a.css'));
22+
});
23+
24+
it('shouldProcessFile(scss)', function() {
25+
assert.equal(true, comb._shouldProcessFile('a.scss'));
26+
});
27+
28+
it('shouldProcessFile(less)', function() {
29+
assert.equal(true, comb._shouldProcessFile('a.less'));
30+
});
31+
32+
it('shouldProcessFile(txt)', function() {
33+
assert.equal(false, comb._shouldProcessFile('a.txt'));
34+
});
35+
36+
it('shouldProcessFile(css, excluded directory)', function() {
37+
assert.equal(false, comb._shouldProcessFile('nani/a.css'));
38+
});
39+
40+
it('shouldProcessFile(css, excluded file)', function() {
41+
assert.equal(false, comb._shouldProcessFile('b.css'));
42+
});
43+
});

0 commit comments

Comments
 (0)