Skip to content

Commit 453acee

Browse files
drewbourneMoOx
authored andcommitted
Changed: postcss warning when color function cannot be parsed (#35)
When processing an invalid value `css-color-function` will throw errors. This change catches those errors and turns them into postcss warnings.
1 parent ad1e58c commit 453acee

File tree

2 files changed

+37
-11
lines changed

2 files changed

+37
-11
lines changed

index.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,22 @@ var helpers = require("postcss-message-helpers")
1010
* PostCSS plugin to transform color()
1111
*/
1212
module.exports = postcss.plugin("postcss-color-function", function() {
13-
return function(style) {
13+
return function(style, result) {
1414
style.walkDecls(function transformDecl(decl) {
1515
if (!decl.value || decl.value.indexOf("color(") === -1) {
1616
return
1717
}
1818

19-
decl.value = helpers.try(function transformColorValue() {
20-
return transformColor(decl.value)
21-
}, decl.source)
19+
try {
20+
decl.value = helpers.try(function transformColorValue() {
21+
return transformColor(decl.value)
22+
}, decl.source)
23+
} catch (error) {
24+
decl.warn(result, error.message, {
25+
word: decl.value,
26+
index: decl.index,
27+
})
28+
}
2229
})
2330
}
2431
})

test/index.js

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,31 @@ test("color()", function(t) {
2323
t.end()
2424
})
2525

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

33-
t.end()
32+
var warning = warnings[0]
33+
t.equals(
34+
warning.plugin,
35+
"postcss-color-function",
36+
"expected `warning.plugin` to match this plugin's name"
37+
)
38+
39+
t.equals(
40+
warning.word,
41+
"color(blurp a(+10%))",
42+
"expected `warning.word` to match color() declaration"
43+
)
44+
45+
t.equals(
46+
warning.text,
47+
"<css input>:2:3: Unable to parse color from string \"blurp\"",
48+
"expected `warning.text` to contain a readable error when a color can't be parsed"
49+
)
50+
51+
t.end()
52+
})
3453
})

0 commit comments

Comments
 (0)