Skip to content

Commit

Permalink
improve error message on invalid CSS (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartveneman committed Mar 5, 2019
1 parent c351f74 commit 3d44318
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
10 changes: 6 additions & 4 deletions src/parser/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const postcss = require('postcss')
const {parse} = require('postcss')
const atRules = require('./atrules')
const rules = require('./rules')
const selectors = require('./selectors')
Expand All @@ -15,16 +15,18 @@ function processNodes(tree) {

module.exports = async css => {
try {
const result = await postcss.parse(css)
const result = await parse(css)
const rootNode = result.toResult().root

return Promise.resolve(processNodes(rootNode))
} catch (error) {
const {source, line, column, reason} = error
const {line, column, reason} = error

return Promise.reject(
new SyntaxError(
`${reason} at line ${line}, column ${column}. Source: ${source}`
`${reason} at line ${line}, column ${column}:\n\n${error.showSourceCode(
false
)}`
)
)
}
Expand Down
2 changes: 1 addition & 1 deletion test/parser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ test('parser throws a useful error on invalid CSS', async t => {

t.is(
error.message,
'Unknown word at line 1, column 5. Source: a { color red }'
'Unknown word at line 1, column 5:\n\n> 1 | a { color red }\n | ^'
)
})

0 comments on commit 3d44318

Please sign in to comment.