Skip to content

Commit ba1399e

Browse files
committed
Remove beforeLint feature
This was an underused feature and adds complexity to the preprocessor. This could be solved more elegantly by allowing the user to pass their own `transform` function to postcss-import. Fixes #35 Improve variable naming This makes it a little clearer what `merged` is Remove superfluous comments These are too basic to need comments
1 parent 7b8526d commit ba1399e

File tree

5 files changed

+12
-124
lines changed

5 files changed

+12
-124
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
### HEAD
22

3+
* Remove `beforeLint` feature
34
* Add default browsers list for autoprefixer
45
* Use `stylelint-config-suitcss` from within the preprocessor. No longer needs
56
to be installed locally by the consumer.

README.md

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -126,24 +126,6 @@ locally in your package.
126126

127127
If set to `true` then the output is minified by [`cssnano`](http://cssnano.co/).
128128

129-
##### `beforeLint`
130-
131-
* Type: `Function`
132-
* Default: `undefined`
133-
134-
Called with the imported CSS before it's passed to `postcss-bem-linter`. This allows you to transform the CSS first and it must return the css string.
135-
136-
Third paramater is the options object containing any PostCSS configuration you may need.
137-
138-
```js
139-
{
140-
beforeLint(css, filename, options) {
141-
// Do something to the imported css
142-
return css;
143-
}
144-
}
145-
```
146-
147129
##### `postcss`
148130

149131
* Type: `Object`
@@ -157,7 +139,6 @@ Options that are passed directly to `postcss`, as per [the documentation](https:
157139
}
158140
```
159141

160-
161142
##### `use`
162143

163144
* Type: `Array`

lib/index.js

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
/**
2-
* Module dependencies
3-
*/
41
var assign = require('object-assign-deep');
52
var isEmpty = require('lodash.isempty');
63
var difference = require('lodash.difference');
@@ -11,10 +8,6 @@ var reporter = require('postcss-reporter');
118
var stylelint = require('stylelint');
129
var stylelintConfigSuit = require('stylelint-config-suitcss');
1310

14-
/**
15-
* Module export
16-
*/
17-
1811
module.exports = preprocessor;
1912

2013
/**
@@ -92,34 +85,27 @@ function preprocessor(css, options) {
9285

9386
function mergeOptions(options) {
9487
options = options || {};
95-
96-
var merged = assign({}, defaults, options);
88+
var mergedOpts = assign({}, defaults, options);
9789

9890
// Set some core options
99-
if (merged.root) {
100-
merged['postcss-easy-import'].root = merged.root;
91+
if (mergedOpts.root) {
92+
mergedOpts['postcss-easy-import'].root = mergedOpts.root;
10193
}
10294

103-
// Call beforeLint function and pass processed css to bem-linter
104-
var beforeLint = merged.beforeLint;
105-
merged['postcss-easy-import'].transform = function(css, filename) {
106-
if (typeof beforeLint === 'function') {
107-
css = beforeLint(css, filename, merged);
108-
}
109-
110-
return lintImportedFiles(merged, css, filename).then(function(result) {
95+
mergedOpts['postcss-easy-import'].transform = function(css, filename) {
96+
return lintImportedFiles(mergedOpts, css, filename).then(function(result) {
11197
return result.css;
11298
});
11399
};
114100

115101
// Allow additional plugins to be merged with the defaults
116102
// but remove any duplicates so that it respects the new order
117103
if (!isEmpty(options.use)) {
118-
var plugins = difference(merged.use, options.use);
119-
merged.use = plugins.concat(options.use);
104+
var plugins = difference(mergedOpts.use, options.use);
105+
mergedOpts.use = plugins.concat(options.use);
120106
}
121107

122-
return merged;
108+
return mergedOpts;
123109
}
124110

125111
/**

test/test.config.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
module.exports = {
2-
beforeLint: function (css) {
3-
console.log('beforeLint ran');
4-
return css;
5-
},
62
use: [
73
"postcss-property-lookup"
84
],

test/test.js

Lines changed: 3 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -197,77 +197,6 @@ describe('suitcss', function() {
197197
).to.be.rejectedWith(Error, 'postcss-reporter: warnings or errors were found');
198198
});
199199
});
200-
201-
describe('transforming css before linting', function() {
202-
describe('ensuring functions are called correctly', function() {
203-
var lintImportedFilesStub, beforeLintStub, revert;
204-
205-
beforeEach(function() {
206-
var postcssPromise = sinon.stub().resolves('/*linting done*/')();
207-
lintImportedFilesStub = sinon.stub().returns(postcssPromise);
208-
beforeLintStub = sinon.stub().returns('/*before lint*/');
209-
revert = suitcss.__set__('lintImportedFiles', lintImportedFilesStub);
210-
});
211-
212-
afterEach(function() {
213-
revert();
214-
});
215-
216-
it('should call `beforeLint` function before linting', function(done) {
217-
suitcss(read('fixtures/component'), {
218-
root: 'test/fixtures',
219-
beforeLint: beforeLintStub
220-
})
221-
.then(function() {
222-
expect(beforeLintStub).to.be.calledOnce;
223-
expect(lintImportedFilesStub).to.be.calledOnce;
224-
expect(beforeLintStub).to.have.been.calledBefore(lintImportedFilesStub);
225-
done();
226-
})
227-
.catch(done);
228-
});
229-
230-
it('should pass the result of `beforeLint` to `lintImportedFiles`', function(done) {
231-
suitcss(read('fixtures/component'), {
232-
root: 'test/fixtures',
233-
beforeLint: beforeLintStub
234-
})
235-
.then(function() {
236-
expect(lintImportedFilesStub.args[0][1]).to.equal('/*before lint*/');
237-
done();
238-
})
239-
.catch(done);
240-
});
241-
242-
it('should pass the options object to the beforeLint function as the third parameter', function(done) {
243-
suitcss(read('fixtures/component'), {
244-
root: 'test/fixtures',
245-
beforeLint: beforeLintStub
246-
})
247-
.then(function() {
248-
expect(beforeLintStub.args[0][2]).to.contain({root: 'test/fixtures'});
249-
done();
250-
})
251-
.catch(done);
252-
});
253-
});
254-
255-
describe('outputting transformed css', function() {
256-
it('should use the CSS returned from beforeLint', function(done) {
257-
suitcss(read('fixtures/import'), {
258-
root: 'test/fixtures',
259-
beforeLint: function() {
260-
return 'body {}';
261-
}
262-
})
263-
.then(function(result) {
264-
expect(result.css).to.equal('body {}\n');
265-
done();
266-
})
267-
.catch(done);
268-
});
269-
});
270-
});
271200
});
272201
});
273202

@@ -338,7 +267,6 @@ describe('cli', function() {
338267
var testChild = exec('node bin/suitcss -c test/noautoprefixer.config.js', function(err, stdout) {
339268
if (err) return done(err);
340269
expect(stdout).to.equal(output);
341-
expect(stdout).to.not.contain('beforeLint ran');
342270
done();
343271
});
344272

@@ -350,18 +278,16 @@ describe('cli', function() {
350278
exec('node bin/suitcss -v -c test/noautoprefixer.config.js test/fixtures/cli/input.css test/fixtures/cli/output.css', function(err, stdout) {
351279
if (err) return done(err);
352280
expect(stdout).to.contain('write');
353-
expect(stdout).to.not.contain('beforeLint ran');
354281
done();
355282
});
356283
});
357284

358285
it('should allow configurable import root', function(done) {
359-
exec('node bin/suitcss -i test/fixtures -c test/noautoprefixer.config.js test/fixtures/import.css test/fixtures/cli/output.css', function(err, stdout) {
286+
exec('node bin/suitcss -i test/fixtures -c test/noautoprefixer.config.js test/fixtures/import.css test/fixtures/cli/output.css', function(err) {
360287
if (err) return done(err);
361288
var res = read('fixtures/cli/output');
362289
var expected = read('fixtures/component.out');
363290
expect(res).to.equal(expected);
364-
expect(stdout).to.not.contain('beforeLint ran');
365291
done();
366292
});
367293
});
@@ -375,22 +301,20 @@ describe('cli', function() {
375301
});
376302

377303
it('should minify the output', function(done) {
378-
exec('node bin/suitcss -i test/fixtures -c test/noautoprefixer.config.js test/fixtures/import.css test/fixtures/cli/output.css -m', function(err, stdout) {
304+
exec('node bin/suitcss -i test/fixtures -c test/noautoprefixer.config.js test/fixtures/import.css test/fixtures/cli/output.css -m', function(err) {
379305
if (err) return done(err);
380306
var res = read('fixtures/cli/output');
381307
var expected = read('fixtures/minify.out');
382-
expect(stdout).to.not.contain('beforeLint ran');
383308
expect(res).to.equal(expected);
384309
done();
385310
});
386311
});
387312

388313
it('should allow a config file to be passed', function(done) {
389-
exec('node bin/suitcss -i test/fixtures -c test/test.config.js test/fixtures/config.css test/fixtures/cli/output.css', function(err, stdout) {
314+
exec('node bin/suitcss -i test/fixtures -c test/test.config.js test/fixtures/config.css test/fixtures/cli/output.css', function(err) {
390315
if (err) return done(err);
391316
var res = read('fixtures/cli/output');
392317
var expected = read('fixtures/config.out');
393-
expect(stdout).to.contain('beforeLint ran');
394318
expect(res).to.equal(expected);
395319
done();
396320
});

0 commit comments

Comments
 (0)