Skip to content

Commit 30a8a2f

Browse files
committed
Merge pull request #64 from gmetais/listErrors
Add a listError option for parsing
2 parents 4640aaa + 0b4b2c3 commit 30a8a2f

File tree

3 files changed

+51
-11
lines changed

3 files changed

+51
-11
lines changed

Readme.md

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,23 @@ result.map // source map object
5757

5858
### Errors
5959

60-
Errors will have `error.position`, just like [`node.position`](#position). The
61-
error contains the source position in the message. To get the error message
62-
without the position use `error.reason`.
60+
Errors thrown during parsing have the following properties:
61+
62+
- message: `String`. The full error message with the source position.
63+
- reason: `String`. The error message without position.
64+
- filename: `String` or `undefined`. The value of `options.source` if
65+
passed to `css.parse`. Otherwise `undefined`.
66+
- line: `Integer`.
67+
- column: `Integer`.
68+
- source: `String`. The portion of code that couldn't be parsed.
69+
70+
When parsing with the `silent` option, errors are listed in the
71+
`parsingErrors` property of the [`stylesheet`](#stylesheet) node instead
72+
of being thrown.
6373

6474
If you create any errors in plugins such as in
65-
[rework](https://github.com/reworkcss/rework), you __must__ set the `position`
66-
as well for consistency.
75+
[rework](https://github.com/reworkcss/rework), you __must__ set the same
76+
properties for consistency.
6777

6878
## AST
6979

@@ -115,6 +125,8 @@ The root node returned by `css.parse`.
115125
- stylesheet: `Object`:
116126
- rules: `Array` of nodes with the types `rule`, `comment` and any of the
117127
at-rule types.
128+
- parsingErrors: `Array` of `Error`s. Errors collected during parsing when
129+
option `silent` is true.
118130

119131
#### rule
120132

lib/parse/index.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,29 +56,35 @@ module.exports = function(css, options){
5656
* Error `msg`.
5757
*/
5858

59-
function error(msg) {
60-
if (options.silent === true) {
61-
return false;
62-
}
59+
var errorsList = [];
6360

61+
function error(msg) {
6462
var err = new Error(options.source + ':' + lineno + ':' + column + ': ' + msg);
6563
err.reason = msg;
6664
err.filename = options.source;
6765
err.line = lineno;
6866
err.column = column;
6967
err.source = css;
70-
throw err;
68+
69+
if (options.silent) {
70+
errorsList.push(err);
71+
} else {
72+
throw err;
73+
}
7174
}
7275

7376
/**
7477
* Parse stylesheet.
7578
*/
7679

7780
function stylesheet() {
81+
var rulesList = rules();
82+
7883
return {
7984
type: 'stylesheet',
8085
stylesheet: {
81-
rules: rules()
86+
rules: rulesList,
87+
parsingErrors: errorsList
8288
}
8389
};
8490
}

test/parse.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,28 @@ describe('parse(str)', function() {
5252
}).not.throw();
5353
});
5454

55+
it('should list the parsing errors and continue parsing', function() {
56+
var result = parse('foo { color= red; } bar { color: blue; } baz {}} boo { display: none}', {
57+
silent: true,
58+
source: 'foo.css'
59+
});
60+
61+
var rules = result.stylesheet.rules;
62+
rules.length.should.be.above(2);
63+
64+
var errors = result.stylesheet.parsingErrors;
65+
errors.length.should.equal(2);
66+
67+
errors[0].should.have.a.property('message');
68+
errors[0].should.have.a.property('reason');
69+
errors[0].should.have.a.property('filename');
70+
errors[0].filename.should.equal('foo.css');
71+
errors[0].should.have.a.property('line');
72+
errors[0].should.have.a.property('column');
73+
errors[0].should.have.a.property('source');
74+
75+
});
76+
5577
it('should set parent property', function() {
5678
var result = parse(
5779
'thing { test: value; }\n' +

0 commit comments

Comments
 (0)