Skip to content

Add throwWhenCannotReduce option #20

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

Merged
merged 1 commit into from
Jan 7, 2016
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
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()
})