Skip to content

Commit 0460d39

Browse files
committed
feat: error and warning reporting for variable use
fail when undefined variables are used, allow but warn when locally defined variables used
1 parent f1ef983 commit 0460d39

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

tools/bundle-builder/subrunner/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ function runTaskOnPackages(task, packages) {
9191

9292
if (packageDir) {
9393
runComponentTask(packageDir, task, function(err) {
94+
if (err) {
95+
process.exit();
96+
}
9497
processPackage();
9598
});
9699
}

tools/component-builder/css/lib/varUtils.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,23 @@ function getVarsFromCSS(css) {
3838
return variableList;
3939
}
4040

41+
function getVarsDefinedInCSS(css) {
42+
let variableList = [];
43+
let root = postcss.parse(css);
44+
45+
root.walkRules((rule, ruleIndex) => {
46+
rule.walkDecls((decl) => {
47+
if (decl.prop.startsWith('--')) {
48+
let varName = decl.prop;
49+
if (variableList.indexOf(varName) === -1) {
50+
variableList.push(varName);
51+
}
52+
}
53+
});
54+
});
55+
return variableList;
56+
}
57+
4158
function getVarValues(css) {
4259
let root = postcss.parse(css);
4360
let variables = {};
@@ -174,6 +191,7 @@ function getAllComponentVars() {
174191

175192
exports.getAllComponentVars = getAllComponentVars;
176193
exports.getAllVars = getAllVars;
194+
exports.getVarsDefinedInCSS = getVarsDefinedInCSS;
177195
exports.getVarsFromCSS = getVarsFromCSS;
178196
exports.getVarValues = getVarValues;
179197
exports.getClassNames = getClassNames;

tools/component-builder/css/vars.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,26 +44,39 @@ function bakeVars() {
4444
// Find all custom properties used in the component
4545
let variableList = varUtils.getVarsFromCSS(file.contents);
4646

47+
// Get vars defined inside of the component
48+
let componentVars = varUtils.getVarsDefinedInCSS(file.contents);
49+
4750
// Get vars in ALL components
4851
let vars = await varUtils.getAllComponentVars();
4952

5053
// Get literally all of the possible vars (even overridden vars that are different between themes/scales)
5154
let allVars = await varUtils.getAllVars();
5255

5356
// For each color stop and scale, filter the variables for those matching the component
57+
let errors = [];
5458
let usedVars = {};
5559
variableList.forEach(varName => {
5660
if (varName.indexOf('spectrum-global') !== -1) {
5761
logger.warn(`⚠️ ${pkg.name} directly uses global variable ${varName}`);
5862
}
5963
else if (!allVars[varName]) {
60-
logger.error(`🚨 ${pkg.name} uses undefined variable ${varName}`);
64+
if (componentVars.indexOf(varName) === -1) {
65+
errors.push(`${pkg.name} uses undefined variable ${varName}`);
66+
}
67+
else {
68+
logger.warn(`🔶 ${pkg.name} uses locally defined variable ${varName}`);
69+
}
6170
}
6271
else {
6372
usedVars[varName] = vars[varName];
6473
}
6574
});
6675

76+
if (errors.length) {
77+
return cb(new Error(errors.join('\n')), file);
78+
}
79+
6780
let contents = varUtils.getVariableDeclarations(classNames, usedVars);
6881
let newFile = file.clone({contents: false});
6982
newFile.path = path.join(file.base, `vars.css`);

0 commit comments

Comments
 (0)