Skip to content

Commit 7a070c2

Browse files
MoOxromainmenke
authored andcommitted
Added: warnings option allows you to disable warnings.
Close MoOx/postcss-cssnext#186
1 parent 7805ec7 commit 7a070c2

File tree

5 files changed

+61
-17
lines changed

5 files changed

+61
-17
lines changed

plugins/postcss-custom-properties/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# 4.2.0 - 2015-07-21
2+
3+
- Added: `warnings` option allows you to disable warnings.
4+
([cssnext#186](https://github.com/cssnext/cssnext/issues/186))
5+
16
# 4.1.0 - 2015-07-14
27

38
- Added: plugin now returns itself in order to expose a `setVariables` function

plugins/postcss-custom-properties/README.md

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,17 @@ Checkout [tests](test) for more.
7979

8080
### Options
8181

82-
#### `strict` (default: `true`)
82+
#### `strict`
83+
84+
Default: `true`
8385

8486
Per specifications, all fallbacks should be added since we can't verify if a
8587
computed value is valid or not.
8688
This option allows you to avoid adding too many fallback values in your CSS.
8789

88-
#### `preserve` (default: `false`)
90+
#### `preserve`
91+
92+
Default: `false`
8993

9094
Allows you to preserve custom properties & var() usage in output.
9195

@@ -96,27 +100,43 @@ var out = postcss()
96100
.css
97101
```
98102

99-
You can also set `preserve: "computed"` to get computed resolved custom properties in the final output.
103+
You can also set `preserve: "computed"` to get computed resolved custom
104+
properties in the final output.
100105
Handy to make them available to your JavaScript.
101106

102-
#### `variables` (default: `{}`)
107+
#### `variables`
103108

104-
Allows you to pass an object of variables for `:root`. These definitions will override any that exist in the CSS.
109+
Default: `{}`
110+
111+
Allows you to pass an object of variables for `:root`. These definitions will
112+
override any that exist in the CSS.
105113
The keys are automatically prefixed with the CSS `--` to make it easier to share
106114
variables in your codebase.
107115

108-
#### `appendVariables` (default: `false`)
116+
#### `appendVariables`
117+
118+
Default: `false`
119+
120+
If `preserve` is set to `true` (or `"computed"`), allows you to append your
121+
variables at the end of your CSS.
122+
123+
#### `warnings`
124+
125+
Default: `true`
126+
Type: `Boolean|Object`
109127

110-
If `preserve` is set to `true` (or `"computed"`), allows you to append your variables at the end of your CSS.
128+
Allows you to enable/disable warnings. If true, will enable all warnings.
129+
For now, it only allow to disable messages about custom properties definition
130+
not scoped in a `:root` selector.
111131

112132
---
113133

114134
## Contributing
115135

116-
Work on a branch, install dev-dependencies, respect coding style & run tests before submitting a bug fix or a feature.
136+
Fork, work on a branch, install dependencies & run tests before submitting a PR.
117137

118138
```console
119-
$ git clone https://github.com/postcss/postcss-custom-properties.git
139+
$ git clone https://github.com/YOU/postcss-custom-properties.git
120140
$ git checkout -b patch-1
121141
$ npm install
122142
$ npm test

plugins/postcss-custom-properties/index.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ module.exports = postcss.plugin("postcss-custom-properties", function(options) {
156156
}
157157

158158
function plugin(style, result) {
159+
var warnings = options.warnings === undefined ? true : options.warnings
159160
var variables = prefixVariables(options.variables)
160161
var strict = options.strict === undefined ? true : options.strict
161162
var appendVariables = options.appendVariables
@@ -169,13 +170,17 @@ module.exports = postcss.plugin("postcss-custom-properties", function(options) {
169170

170171
// only variables declared for `:root` are supported for now
171172
if (
172-
rule.selectors.length !== 1 ||
173-
rule.selectors[0] !== ":root" ||
174-
rule.parent.type !== "root"
173+
rule.selectors.length !== 1 ||
174+
rule.selectors[0] !== ":root" ||
175+
rule.parent.type !== "root"
175176
) {
176177
rule.each(function(decl) {
177178
var prop = decl.prop
178-
if (prop && prop.indexOf(VAR_PROP_IDENTIFIER) === 0) {
179+
if (
180+
warnings &&
181+
prop &&
182+
prop.indexOf(VAR_PROP_IDENTIFIER) === 0
183+
) {
179184
result.warn(
180185
"Custom property ignored: not scoped to the top-level :root " +
181186
"element (" + rule.selectors + " { ... " + prop + ": ... })" +

plugins/postcss-custom-properties/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "postcss-custom-properties",
3-
"version": "4.1.0",
3+
"version": "4.2.0",
44
"description": "PostCSS plugin to polyfill W3C CSS Custom Properties for cascading variables",
55
"keywords": [
66
"css",

plugins/postcss-custom-properties/test/index.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ test(
6363
function(t) {
6464
var result = compareFixtures(t, "substitution-undefined")
6565
t.equal(
66-
result.warnings()[0].text,
66+
result.messages[0].text,
6767
"variable '--test' is undefined and used without a fallback",
6868
"should add a warning for undefined variable"
6969
)
@@ -74,12 +74,26 @@ test(
7474
test("substitutes defined variables in `:root` only", function(t) {
7575
var result = compareFixtures(t, "substitution-defined")
7676
t.ok(
77-
result.warnings()[0].text.match(/^Custom property ignored/),
77+
result.messages[0].text.match(/^Custom property ignored/),
7878
"should add a warning for non root custom properties"
7979
)
8080
t.end()
8181
})
8282

83+
test("allow to hide warnings", function(t) {
84+
var result = compareFixtures(
85+
t,
86+
"substitution-defined",
87+
{warnings: false}
88+
)
89+
t.equal(
90+
result.messages.length,
91+
0,
92+
"should not add warnings if option set to false"
93+
)
94+
t.end()
95+
})
96+
8397
test(
8498
"accepts variables defined from JavaScript, and overrides local definitions",
8599
function(t) {
@@ -181,7 +195,7 @@ test("circular variable references", function(t) {
181195
compareFixtures(t, "self-reference")
182196
var result = compareFixtures(t, "circular-reference")
183197
t.equal(
184-
result.warnings()[0].text,
198+
result.messages[0].text,
185199
"Circular variable reference: --color",
186200
"should add a warning for circular reference"
187201
)

0 commit comments

Comments
 (0)