Skip to content

Commit 0ec22e2

Browse files
committed
Add beforeLint option
This allows a user to perform operations on the imported CSS before passing it along to be linted. An example might be to run `postcss-nested` first.
1 parent 5c7dca7 commit 0ec22e2

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

lib/index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,15 @@ function mergeOptions(options) {
9898
// Set some core options
9999
merged.minify = options.minify;
100100
merged['postcss-import'].root = options.root;
101-
merged['postcss-import'].transform = lintImportedFiles(merged);
101+
102+
// Call beforeLint function and pass processed css to bem-linter
103+
var beforeLint = options.beforeLint;
104+
merged['postcss-import'].transform = function(css, filename) {
105+
if (typeof beforeLint === 'function') {
106+
css = beforeLint(css, filename, merged);
107+
}
108+
return lintImportedFiles(merged)(css, filename);
109+
};
102110

103111
// Allow additional plugins to be merged with the defaults
104112
// but remove any duplicates so that it respects the new order

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
"chai": "^3.4.1",
3535
"mocha": "^2.3.4",
3636
"postcss-property-lookup": "^1.1.4",
37-
"rewire": "^2.5.0"
37+
"rewire": "^2.5.0",
38+
"sinon": "^1.17.2"
3839
},
3940
"scripts": {
4041
"test": "mocha --reporter spec --slow 400",

test/test.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var expect = require('chai').expect;
2+
var sinon = require('sinon');
23
var child = require('child_process');
34
var exec = child.exec;
45
var spawn = child.spawn;
@@ -114,6 +115,47 @@ describe('suitcss', function () {
114115
]);
115116
});
116117
});
118+
119+
describe('beforeLint option', function() {
120+
var lintImportedFilesStub, bemLintStub, beforeLintStub, revert;
121+
122+
beforeEach(function() {
123+
lintImportedFilesStub = sinon.stub();
124+
bemLintStub = sinon.stub().returns('/* lint */');
125+
beforeLintStub = sinon.stub().returns('/* before lint */');
126+
127+
lintImportedFilesStub.onFirstCall().returns(bemLintStub);
128+
revert = suitcss.__set__('lintImportedFiles', lintImportedFilesStub);
129+
});
130+
131+
afterEach(function() {
132+
revert();
133+
});
134+
135+
it('should call user supplied function before linting', function (done) {
136+
suitcss(read('fixtures/component'), {
137+
root: 'test/fixtures',
138+
beforeLint: beforeLintStub
139+
}).catch(done);
140+
141+
expect(bemLintStub.calledOnce).to.be.ok;
142+
expect(beforeLintStub.calledOnce).to.be.ok;
143+
expect(beforeLintStub.calledBefore(bemLintStub)).to.be.ok;
144+
145+
done();
146+
});
147+
148+
it('should pass processed CSS to the linting transform function', function (done) {
149+
suitcss(read('fixtures/component'), {
150+
root: 'test/fixtures',
151+
beforeLint: beforeLintStub
152+
}).catch(done);
153+
154+
expect(bemLintStub.getCall(0).args[0]).to.equal('/* before lint */');
155+
156+
done();
157+
});
158+
});
117159
});
118160
});
119161

0 commit comments

Comments
 (0)