Skip to content

Commit 4dcfeea

Browse files
committed
Merge pull request #20 from thejameskyle/throwWhenCannotReduce
Add throwWhenCannotReduce option
2 parents d8e2755 + 6000b05 commit 4dcfeea

File tree

6 files changed

+45
-4
lines changed

6 files changed

+45
-4
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
- Added: "warnWhenCannotResolve" option to warn when calc() are not reduced to a single value
2+
13
# 5.0.0 - 2015-08-25
24

35
- Removed: compatibility with postcss v4.x

README.md

+11
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,17 @@ var out = postcss()
107107
.css
108108
```
109109

110+
#### `warnWhenCannotResolve` (default: `false`)
111+
112+
Adds warnings when calc() are not reduced to a single value.
113+
114+
```js
115+
var out = postcss()
116+
.use(calc({warnWhenCannotResolve: true}))
117+
.process(css)
118+
.css
119+
```
120+
110121
---
111122

112123
## Contributing

index.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,19 @@ var reduceCSSCalc = require("reduce-css-calc")
55
var helpers = require("postcss-message-helpers")
66
var postcss = require("postcss")
77

8+
var CONTAINS_CALC = /calc\(.*\)/
9+
810
/**
911
* PostCSS plugin to reduce calc() function calls.
1012
*/
1113
module.exports = postcss.plugin("postcss-calc", function(options) {
1214
options = options || {}
1315
var precision = options.precision
1416
var preserve = options.preserve
17+
var warnWhenCannotResolve = options.warnWhenCannotResolve
18+
19+
return function(style, result) {
1520

16-
return function(style) {
1721
style.walkDecls(function transformDecl(decl) {
1822
if (!decl.value || decl.value.indexOf("calc(") === -1) {
1923
return
@@ -22,6 +26,11 @@ module.exports = postcss.plugin("postcss-calc", function(options) {
2226
helpers.try(function transformCSSCalc() {
2327
var value = reduceCSSCalc(decl.value, precision)
2428

29+
if (warnWhenCannotResolve && CONTAINS_CALC.test(value)) {
30+
result.warn("Could not reduce expression: " + decl.value,
31+
{plugin: "postcss-calc", node: decl})
32+
}
33+
2534
if (!preserve) {
2635
decl.value = value
2736
return
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
calc {
2+
unresolved: calc(1.125rem - 1px);
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
calc {
2+
unresolved: calc(1.125rem - 1px);
3+
}

test/index.js

+16-3
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,24 @@ function fixture(name) {
1414
}
1515

1616
function compareFixtures(t, name, options, message) {
17-
var actual = postcss()
17+
var result = postcss()
1818
.use(customProperties())
1919
.use(calc(options))
2020
.process(fixture(name), {from: fixturePath(name)})
21-
.css.trim()
21+
var actual = result.css.trim()
2222

2323
// handy thing: checkout actual in the *.actual.css file
2424
fs.writeFile(fixturePath(name + ".actual"), actual)
2525

26-
return t.equal(
26+
t.equal(
2727
actual,
2828
fixture(name + ".expected"),
2929
message
3030
? message
3131
: "processed fixture '" + name + "' should be equal to expected output"
3232
)
33+
34+
return result
3335
}
3436

3537
test("calc", function(t) {
@@ -55,5 +57,16 @@ test("calc", function(t) {
5557
"should have a preserve option that allow to keep original calc() usage"
5658
)
5759

60+
var result = compareFixtures(
61+
t,
62+
"warnWhenCannotResolve",
63+
{warnWhenCannotResolve: true}
64+
)
65+
66+
t.ok(
67+
result.messages[0].text.match(/^Could not reduce expression:/),
68+
"should add a warning for unreduced calc() "
69+
)
70+
5871
t.end()
5972
})

0 commit comments

Comments
 (0)