Skip to content

Commit 5ebd026

Browse files
committed
Add throw-validate-errors option #71
1 parent cfb60a8 commit 5ebd026

File tree

3 files changed

+40
-7
lines changed

3 files changed

+40
-7
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ The plugin has no default options. Everything is disabled by default.
2727
- [`order`](./lib/order/README.md): Specify the order of content within declaration blocks.
2828
- [`properties-order`](./lib/properties-order/README.md): Specify the order of properties within declaration blocks.
2929
- [`unspecified-properties-position`](./lib/properties-order/unspecified-properties-position.md): Specify position for properties not specified in `properties-order`.
30+
- `throw-validate-errors`: Throw config validation errors instead of just showing and ignoring them.
3031

3132
## Handling comments
3233

index.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,22 @@ function plugin(css, opts) {
1717
const validatedOptions = validateOptions(opts);
1818

1919
if (validatedOptions !== true) {
20-
// eslint-disable-next-line no-console
21-
if (console && console.warn && _.isString(validatedOptions)) {
22-
console.warn(validatedOptions); // eslint-disable-line no-console
23-
}
20+
const throwValidateErrors = _.get(opts, 'throw-validate-errors', false);
21+
22+
if (throwValidateErrors) {
23+
if (_.isString(validatedOptions)) {
24+
throw new Error(validatedOptions);
25+
}
2426

25-
return;
27+
throw new Error(`postcss-sorting: Invalid config.`);
28+
} else {
29+
// eslint-disable-next-line no-console
30+
if (console && console.warn && _.isString(validatedOptions)) {
31+
console.warn(validatedOptions); // eslint-disable-line no-console
32+
}
33+
34+
return;
35+
}
2636
}
2737

2838
if (opts.order) {

lib/__tests__/test.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,26 @@
11
'use strict';
22

3-
test(`Should do nothing if config is undefined`, () =>
4-
runTest('empty-lines-preserve', undefined, __dirname));
3+
const plugin = require('../../');
4+
const postcss = require('postcss');
5+
6+
test(`Should do nothing if config is undefined`, () => {
7+
runTest('empty-lines-preserve', undefined, __dirname);
8+
});
9+
10+
test(`Should throw an error if config has error`, () => {
11+
const opts = {
12+
'throw-validate-errors': true,
13+
order: 'Justice Rains From Above',
14+
};
15+
16+
const pluginRun = postcss([plugin(opts)])
17+
.process('')
18+
.then(() => {
19+
expect('Plugin should throw an error').toBeFalst();
20+
})
21+
.catch(err => {
22+
throw err;
23+
});
24+
25+
expect(pluginRun).rejects.toBeTruthy();
26+
});

0 commit comments

Comments
 (0)