Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add postcss warning when color function cannot be parsed #35

Merged
merged 1 commit into from
Feb 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,22 @@ var helpers = require("postcss-message-helpers")
* PostCSS plugin to transform color()
*/
module.exports = postcss.plugin("postcss-color-function", function() {
return function(style) {
return function(style, result) {
style.walkDecls(function transformDecl(decl) {
if (!decl.value || decl.value.indexOf("color(") === -1) {
return
}

decl.value = helpers.try(function transformColorValue() {
return transformColor(decl.value)
}, decl.source)
try {
decl.value = helpers.try(function transformColorValue() {
return transformColor(decl.value)
}, decl.source)
} catch (error) {
decl.warn(result, error.message, {
word: decl.value,
index: decl.index,
})
}
})
}
})
Expand Down
33 changes: 26 additions & 7 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,31 @@ test("color()", function(t) {
t.end()
})

test("throw errors", function(t) {
t.throws(function() {
return postcss(plugin()).process(read(filename("fixtures/error"))).css
},
/Unable to parse color from string/,
"throws a readable error when a color can't be parsed")
test("logs warning when color() value cannot be parsed", function(t) {
postcss(plugin()).process(read(filename("fixtures/error")))
.then(function(result) {
var warnings = result.warnings();
t.equals(warnings.length, 1, "expected only 1 warning");

t.end()
var warning = warnings[0]
t.equals(
warning.plugin,
"postcss-color-function",
"expected `warning.plugin` to match this plugin's name"
)

t.equals(
warning.word,
"color(blurp a(+10%))",
"expected `warning.word` to match color() declaration"
)

t.equals(
warning.text,
"<css input>:2:3: Unable to parse color from string \"blurp\"",
"expected `warning.text` to contain a readable error when a color can't be parsed"
)

t.end()
})
})