From 6000b056b0ece4df4ee10df1b6706ed36f5eb52c Mon Sep 17 00:00:00 2001 From: James Kyle Date: Tue, 5 Jan 2016 23:24:41 -0800 Subject: [PATCH] Add warnWhenCannotResolve option --- CHANGELOG.md | 2 ++ README.md | 11 +++++++++++ index.js | 11 ++++++++++- test/fixtures/warnWhenCannotResolve.css | 3 +++ .../warnWhenCannotResolve.expected.css | 3 +++ test/index.js | 19 ++++++++++++++++--- 6 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 test/fixtures/warnWhenCannotResolve.css create mode 100644 test/fixtures/warnWhenCannotResolve.expected.css diff --git a/CHANGELOG.md b/CHANGELOG.md index f7878e7..77a43a9 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index 694ab3e..4382803 100755 --- a/README.md +++ b/README.md @@ -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 diff --git a/index.js b/index.js index 61a11cf..29c60a1 100755 --- a/index.js +++ b/index.js @@ -5,6 +5,8 @@ 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. */ @@ -12,8 +14,10 @@ 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 @@ -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 diff --git a/test/fixtures/warnWhenCannotResolve.css b/test/fixtures/warnWhenCannotResolve.css new file mode 100644 index 0000000..f570cdf --- /dev/null +++ b/test/fixtures/warnWhenCannotResolve.css @@ -0,0 +1,3 @@ +calc { + unresolved: calc(1.125rem - 1px); +} diff --git a/test/fixtures/warnWhenCannotResolve.expected.css b/test/fixtures/warnWhenCannotResolve.expected.css new file mode 100644 index 0000000..f570cdf --- /dev/null +++ b/test/fixtures/warnWhenCannotResolve.expected.css @@ -0,0 +1,3 @@ +calc { + unresolved: calc(1.125rem - 1px); +} diff --git a/test/index.js b/test/index.js index c9ef218..20d8756 100755 --- a/test/index.js +++ b/test/index.js @@ -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) { @@ -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() })