Skip to content

Commit

Permalink
Add warnWhenCannotResolve option
Browse files Browse the repository at this point in the history
  • Loading branch information
jamiebuilds committed Jan 6, 2016
1 parent d8e2755 commit 6000b05
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
- Added: "warnWhenCannotResolve" option to warn when calc() are not reduced to a single value

# 5.0.0 - 2015-08-25

- Removed: compatibility with postcss v4.x
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,17 @@ var out = postcss()
.css
```

#### `warnWhenCannotResolve` (default: `false`)

Adds warnings when calc() are not reduced to a single value.

```js
var out = postcss()
.use(calc({warnWhenCannotResolve: true}))
.process(css)
.css
```

---

## Contributing
Expand Down
11 changes: 10 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@ var reduceCSSCalc = require("reduce-css-calc")
var helpers = require("postcss-message-helpers")
var postcss = require("postcss")

var CONTAINS_CALC = /calc\(.*\)/

/**
* PostCSS plugin to reduce calc() function calls.
*/
module.exports = postcss.plugin("postcss-calc", function(options) {
options = options || {}
var precision = options.precision
var preserve = options.preserve
var warnWhenCannotResolve = options.warnWhenCannotResolve

return function(style, result) {

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

if (warnWhenCannotResolve && CONTAINS_CALC.test(value)) {
result.warn("Could not reduce expression: " + decl.value,
{plugin: "postcss-calc", node: decl})
}

if (!preserve) {
decl.value = value
return
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/warnWhenCannotResolve.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
calc {
unresolved: calc(1.125rem - 1px);
}
3 changes: 3 additions & 0 deletions test/fixtures/warnWhenCannotResolve.expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
calc {
unresolved: calc(1.125rem - 1px);
}
19 changes: 16 additions & 3 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,24 @@ function fixture(name) {
}

function compareFixtures(t, name, options, message) {
var actual = postcss()
var result = postcss()
.use(customProperties())
.use(calc(options))
.process(fixture(name), {from: fixturePath(name)})
.css.trim()
var actual = result.css.trim()

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

return t.equal(
t.equal(
actual,
fixture(name + ".expected"),
message
? message
: "processed fixture '" + name + "' should be equal to expected output"
)

return result
}

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

var result = compareFixtures(
t,
"warnWhenCannotResolve",
{warnWhenCannotResolve: true}
)

t.ok(
result.messages[0].text.match(/^Could not reduce expression:/),
"should add a warning for unreduced calc() "
)

t.end()
})

0 comments on commit 6000b05

Please sign in to comment.