Skip to content

Commit f4db42e

Browse files
committed
Merge pull request #18 from suitcss/stylelint
Add stylelint to verify code style
2 parents a35a479 + a2218c3 commit f4db42e

File tree

8 files changed

+89
-12
lines changed

8 files changed

+89
-12
lines changed

.stylelintrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "stylelint-config-suitcss"
3+
}

README.md

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ Compiles CSS packages with:
1515
* [postcss-custom-media](https://github.com/postcss/postcss-custom-media)
1616
* [autoprefixer](https://github.com/postcss/autoprefixer)
1717

18-
Each imported file is linted with [postcss-bem-linter](https://github.com/postcss/postcss-bem-linter) and minification is provided by [cssnano](http://cssnano.co/). Additional plugins can be added via the configuration options.
18+
Each imported file is linted with [postcss-bem-linter](https://github.com/postcss/postcss-bem-linter) and minification is provided by [cssnano](http://cssnano.co/). Code style can also be checked with [stylelint](http://stylelint.io/)
19+
20+
Additional plugins can be added via the configuration options.
1921

2022
## Installation
2123

@@ -41,6 +43,7 @@ Options:
4143
-h, --help output usage information
4244
-V, --version output the version number
4345
-m, --minify minify output with cssnano
46+
-l, --lint ensure code adheres to the SUIT code style
4447
-i, --import-root [path] the root directory for imported css files
4548
-c, --config [path] a custom PostCSS config file
4649
-v, --verbose log verbose output for debugging
@@ -91,6 +94,24 @@ preprocessor(css, {
9194

9295
Where to resolve imports from. Passed to [`postcss-import`](https://github.com/postcss/postcss-import/blob/master/README.md#root).
9396

97+
##### `lint`
98+
99+
* Type: `Boolean`
100+
* Default: `false`
101+
102+
Ensure code conforms to the [SUIT code style](https://github.com/suitcss/suit/blob/master/doc/STYLE.md). Stylelint [configuration options](http://stylelint.io/?/docs/user-guide/configuration.md) can also be overridden:
103+
104+
```js
105+
{
106+
lint: true,
107+
stylelint: {
108+
rules: {
109+
indentation: [4, 'tab'],
110+
}
111+
}
112+
}
113+
```
114+
94115
##### `minify`
95116

96117
* Type: `Boolean`
@@ -125,7 +146,7 @@ A list of plugins that are passed to PostCSS. This can be used to add new plugin
125146

126147
```js
127148
{
128-
use: ['stylelint', 'postcss-property-lookup']
149+
use: ['postcss-at2x', 'postcss-property-lookup']
129150
}
130151
```
131152

@@ -218,7 +239,7 @@ var defaults = [
218239
// config
219240
module.exports = {
220241
use: [
221-
'stylelint',
242+
'postcss-at2x',
222243
'postcss-calc',
223244
'autoprefixer',
224245
'postcss-reporter'
@@ -229,7 +250,7 @@ var result = [
229250
'postcss-import',
230251
'postcss-custom-properties',
231252
'postcss-custom-media',
232-
'stylelint',
253+
'postcss-at2x',
233254
'postcss-calc',
234255
'autoprefixer',
235256
'postcss-reporter'

bin/suitcss

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ program
2020
.version(require('../package.json').version)
2121
.usage('[<input>] [<output>]')
2222
.option('-m, --minify', 'minify output with cssnano')
23-
.option('-i, --import-root [path]', 'the root directory for imported css files', '')
24-
.option('-c, --config [path]', 'a custom PostCSS config file', '')
23+
.option('-l, --lint', 'ensure code adheres to the SUIT code style')
24+
.option('-i, --import-root [path]', 'the root directory for imported css files')
25+
.option('-c, --config [path]', 'a custom PostCSS config file')
2526
.option('-v, --verbose', 'log verbose output for debugging')
2627
.option('-w, --watch', 'watch the input file and any imports for changes');
2728

@@ -106,7 +107,11 @@ function run () {
106107
read(input, function (err, buffer) {
107108
if (err) logger.throw(err);
108109
var css = buffer.toString();
109-
var opts = assign({}, {minify: program.minify, root: program.importRoot}, config);
110+
var opts = assign({}, {
111+
minify: program.minify,
112+
root: program.importRoot,
113+
lint: program.lint
114+
}, config);
110115

111116
suitcss(css, opts).then(function(result) {
112117
if (output) {

lib/index.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ var bemLinter = require('postcss-bem-linter');
99
var postcss = require('postcss');
1010
var cssnano = require('cssnano');
1111
var reporter = require('postcss-reporter');
12+
var stylelint = require('stylelint');
1213

1314
/**
1415
* Module export
@@ -22,6 +23,7 @@ module.exports = preprocessor;
2223

2324
var defaults = {
2425
minify: false,
26+
stylelint: false,
2527
use: [
2628
'postcss-import',
2729
'postcss-custom-properties',
@@ -120,10 +122,17 @@ function mergeOptions(options) {
120122
*/
121123
function lintImportedFiles(options) {
122124
return function(css, filename) {
123-
return postcss([
124-
bemLinter(options['postcss-bem-linter']),
125-
reporter(options['postcss-reporter'])
126-
]).process(css, {from: filename}).css;
125+
var processor = postcss();
126+
127+
if (options.lint) {
128+
processor.use(stylelint(options.stylelint));
129+
}
130+
131+
processor
132+
.use(bemLinter(options['postcss-bem-linter']))
133+
.use(reporter(options['postcss-reporter']));
134+
135+
return processor.process(css, {from: filename}).css;
127136
};
128137
}
129138

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
"postcss-custom-properties": "^5.0.0",
2929
"postcss-import": "^7.1.3",
3030
"postcss-reporter": "^1.3.0",
31-
"read-file-stdin": "0.2.0"
31+
"read-file-stdin": "0.2.0",
32+
"stylelint": "^2.3.4",
33+
"stylelint-config-suitcss": "^1.0.0"
3234
},
3335
"devDependencies": {
3436
"chai": "^3.4.1",

test/fixtures/stylelint-import.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@import "./stylelint.css";

test/fixtures/stylelint.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.Component {
2+
flex: 1;
3+
box-sizing: content-box;
4+
}

test/test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ describe('suitcss', function () {
3636
var keys = [
3737
'minify',
3838
'use',
39+
'stylelint',
3940
'postcss-import',
4041
'postcss-reporter',
4142
'cssnano'
@@ -51,6 +52,11 @@ describe('suitcss', function () {
5152
expect(opts['postcss-import'].root).to.equal('test/root');
5253
});
5354

55+
it('should allow stylelint to be enabled', function() {
56+
var opts = mergeOptions({lint: true});
57+
expect(opts.lint).to.be.true;
58+
});
59+
5460
it('should allow an minify option to be set', function() {
5561
var opts = mergeOptions({minify: true});
5662
expect(opts.minify).to.be.true;
@@ -110,6 +116,24 @@ describe('suitcss', function () {
110116
});
111117
});
112118

119+
describe('stylelint', function() {
120+
it('should check the input conforms to SUIT style rules', function(done) {
121+
suitcss('@import ./stylelint.css', {
122+
lint: true,
123+
root: 'test/fixtures',
124+
'postcss-reporter': {
125+
throwError: true
126+
}
127+
}
128+
).then(function(result) {
129+
done(new Error('stylelint should have failed conformance'));
130+
}).catch(function(err) {
131+
expect(err.message).to.contain('postcss-reporter: warnings or errors were found');
132+
done();
133+
});
134+
});
135+
});
136+
113137
describe('beforeLint option', function() {
114138
var lintImportedFilesStub, bemLintStub, beforeLintStub, revert;
115139

@@ -241,6 +265,14 @@ describe('cli', function () {
241265
});
242266
});
243267

268+
it('should output stylelint warnings', function (done) {
269+
exec('bin/suitcss -i test/fixtures test/fixtures/stylelint-import.css test/fixtures/cli/output.css -l', function (err, stdout) {
270+
if (err) return done(err);
271+
expect(stdout).to.contain('Expected property "box-sizing" to come before property "flex"');
272+
done();
273+
});
274+
});
275+
244276
it('should minify the output', function (done) {
245277
exec('bin/suitcss -i test/fixtures test/fixtures/import.css test/fixtures/cli/output.css -m', function (err, stdout) {
246278
if (err) return done(err);

0 commit comments

Comments
 (0)