Skip to content

Commit 8d6599c

Browse files
feat: make possible to define official formatter as string (#202)
1 parent 75a9ef4 commit 8d6599c

File tree

4 files changed

+85
-14
lines changed

4 files changed

+85
-14
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@ If `true`, `stylelint` will fix as many errors as possible. The fixes are made t
7777

7878
### `formatter`
7979

80-
- Type: `Function`
81-
- Default: `require('stylelint').formatters.string`
80+
- Type: `String|Function`
81+
- Default: `'string'`
8282

83-
Specify the formatter that you would like to use to format your results.
83+
Specify the formatter that you would like to use to format your results. See [formatter option](https://stylelint.io/user-guide/node-api#formatter).
8484

8585
### `lintDirtyModulesOnly`
8686

src/getOptions.js

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,39 @@ import validateOptions from 'schema-utils';
22

33
import schema from './options.json';
44

5-
export default function getOptions(options) {
5+
export default function getOptions(pluginOptions) {
6+
const options = {
7+
files: '**/*.s?(c|a)ss',
8+
formatter: 'string',
9+
stylelintPath: 'stylelint',
10+
...pluginOptions,
11+
};
12+
613
validateOptions(schema, options, {
714
name: 'Stylelint Webpack Plugin',
815
baseDataPath: 'options',
916
});
1017

11-
const stylelintPath = options.stylelintPath || 'stylelint';
12-
1318
// eslint-disable-next-line
14-
const { formatters } = require(stylelintPath);
19+
const stylelint = require(options.stylelintPath);
1520

16-
return {
17-
files: '**/*.s?(c|a)ss',
18-
formatter: formatters.string,
19-
stylelintPath,
20-
...options,
21-
};
21+
options.formatter = getFormatter(stylelint, options.formatter);
22+
23+
return options;
24+
}
25+
26+
function getFormatter({ formatters }, formatter) {
27+
if (typeof formatter === 'function') {
28+
return formatter;
29+
}
30+
31+
// Try to get oficial formatter
32+
if (
33+
typeof formatter === 'string' &&
34+
typeof formatters[formatter] === 'function'
35+
) {
36+
return formatters[formatter];
37+
}
38+
39+
return formatters.string;
2240
}

src/options.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
},
2929
"formatter": {
3030
"description": "Specify the formatter that you would like to use to format your results.",
31-
"instanceof": "Function"
31+
"anyOf": [{ "type": "string" }, { "instanceof": "Function" }]
3232
},
3333
"lintDirtyModulesOnly": {
3434
"description": "Lint only changed files, skip lint on start.",

test/formatter.test.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { formatters } from 'stylelint';
2+
3+
import pack from './utils/pack';
4+
5+
describe('formatter', () => {
6+
it('should use default formatter', (done) => {
7+
const compiler = pack('error');
8+
9+
compiler.run((err, stats) => {
10+
expect(err).toBeNull();
11+
expect(stats.hasWarnings()).toBe(false);
12+
expect(stats.hasErrors()).toBe(true);
13+
expect(stats.compilation.errors[0].message).toBeTruthy();
14+
done();
15+
});
16+
});
17+
18+
it('should use default formatter when invalid', (done) => {
19+
const compiler = pack('error', { formatter: 'invalid' });
20+
21+
compiler.run((err, stats) => {
22+
expect(err).toBeNull();
23+
expect(stats.hasWarnings()).toBe(false);
24+
expect(stats.hasErrors()).toBe(true);
25+
expect(stats.compilation.errors[0].message).toBeTruthy();
26+
done();
27+
});
28+
});
29+
30+
it('should use string formatter', (done) => {
31+
const compiler = pack('error', { formatter: 'json' });
32+
33+
compiler.run((err, stats) => {
34+
expect(err).toBeNull();
35+
expect(stats.hasWarnings()).toBe(false);
36+
expect(stats.hasErrors()).toBe(true);
37+
expect(stats.compilation.errors[0].message).toBeTruthy();
38+
done();
39+
});
40+
});
41+
42+
it('should use function formatter', (done) => {
43+
const compiler = pack('error', { formatter: formatters.verbose });
44+
45+
compiler.run((err, stats) => {
46+
expect(err).toBeNull();
47+
expect(stats.hasWarnings()).toBe(false);
48+
expect(stats.hasErrors()).toBe(true);
49+
expect(stats.compilation.errors[0].message).toBeTruthy();
50+
done();
51+
});
52+
});
53+
});

0 commit comments

Comments
 (0)