Skip to content
This repository was archived by the owner on Feb 18, 2022. It is now read-only.

Commit f1621c4

Browse files
SuperOl3gjonathantneal
authored andcommitted
Added: noValueNotifications option (#71)
1 parent bbf9294 commit f1621c4

File tree

3 files changed

+44
-9
lines changed

3 files changed

+44
-9
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,15 @@ Allows you to enable/disable warnings. If true, will enable all warnings.
129129
For now, it only allow to disable messages about custom properties definition
130130
not scoped in a `:root` selector.
131131

132+
133+
### `noValueNotifications`
134+
135+
Default: `'warning'`
136+
Values: `'warning'|'error'`
137+
138+
If it is set to `'error'`, using of undefined variable will throw an error.
139+
140+
132141
---
133142

134143
## Contributing

index.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const RE_VAR = /([\w-]+)(?:\s*,\s*)?\s*(.*)?/
1010
* Module variables
1111
*/
1212

13-
let globalWarnings
13+
let globalOpts
1414

1515
/**
1616
* Resolve CSS variables.
@@ -47,11 +47,16 @@ function resolveValue(value, variables, result, decl) {
4747
let post
4848
// undefined and without fallback, just keep original value
4949
if (!variable && !fallback) {
50-
if (globalWarnings) {
51-
result.warn(
52-
"variable '" + name + "' is undefined and used without a fallback",
53-
{node: decl}
54-
)
50+
if (globalOpts.warnings) {
51+
const errorStr =
52+
`variable '${name}' is undefined and used without a fallback`
53+
54+
if (globalOpts.noValueNotifications === "error") {
55+
throw decl.error(errorStr, {word: name})
56+
}
57+
else {
58+
result.warn(errorStr, {node: decl})
59+
}
5560
}
5661
post = matches.post
5762
? resolveValue(matches.post, variables, result, decl)
@@ -90,7 +95,7 @@ function resolveValue(value, variables, result, decl) {
9095
// circular reference encountered
9196
if (variable.deps.indexOf(name) !== -1) {
9297
if (!fallback) {
93-
if (globalWarnings) {
98+
if (globalOpts.warnings) {
9499
result.warn("Circular variable reference: " + name, {node: decl})
95100
}
96101
variable.value = [variable.value]
@@ -159,7 +164,10 @@ export default postcss.plugin("postcss-custom-properties", (options = {}) => {
159164
const map = {}
160165
const importantMap = {}
161166

162-
globalWarnings = options.warnings === undefined ? true : options.warnings
167+
globalOpts = {
168+
warnings: options.warnings === undefined ? true : options.warnings,
169+
noValueNotifications: options.noValueNotifications || "warning",
170+
}
163171

164172
// define variables
165173
style.walkRules((rule) => {
@@ -174,7 +182,7 @@ export default postcss.plugin("postcss-custom-properties", (options = {}) => {
174182
rule.each((decl) => {
175183
const prop = decl.prop
176184
if (
177-
globalWarnings &&
185+
globalOpts.warnings &&
178186
prop &&
179187
prop.indexOf(VAR_PROP_IDENTIFIER) === 0
180188
) {

test/index.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,24 @@ test(
7171
}
7272
)
7373

74+
test(
75+
"generate error for undefined var when flag is set",
76+
function(t) {
77+
t.throws(
78+
function() {
79+
return postcss(customProperties({
80+
noValueNotifications: "error",
81+
}))
82+
.process(fixture("substitution-undefined"))
83+
.css
84+
},
85+
"variable '--test' is undefined and used without a fallback",
86+
"should add a warning for undefined variable"
87+
)
88+
t.end()
89+
}
90+
)
91+
7492
test("substitutes defined variables in `:root` only", function(t) {
7593
const result = compareFixtures(t, "substitution-defined")
7694
t.ok(

0 commit comments

Comments
 (0)