Skip to content

Commit 48626b8

Browse files
authored
feat(stylelint-theme-alignment): base filename input (#3403)
1 parent dd18ceb commit 48626b8

File tree

3 files changed

+42
-20
lines changed

3 files changed

+42
-20
lines changed

.changeset/silent-frogs-care.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@spectrum-tools/theme-alignment": minor
3+
---
4+
5+
Allow the base filename to be passed into the tool so that the core theme can be updated. Great preparations for S2 roll-out when spectrum-two.css theme files will become our primary source of theme content.

plugins/stylelint-theme-alignment/index.js

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@
1111
*/
1212

1313
import fs from "node:fs";
14-
import { relative, sep } from "node:path";
14+
import { basename, relative, sep } from "node:path";
1515

1616
import postcss from "postcss";
1717
import valuesParser from "postcss-values-parser";
1818
import stylelint from "stylelint";
19+
import { isString } from "stylelint/lib/utils/validateTypes.mjs";
1920

2021
const {
2122
createPlugin,
@@ -34,7 +35,7 @@ const messages = ruleMessages(ruleName, {
3435
});
3536

3637
/** @type {import('stylelint').Plugin} */
37-
const ruleFunction = (enabled) => {
38+
const ruleFunction = (enabled, options = {}) => {
3839
return (root, result) => {
3940
const validOptions = validateOptions(
4041
result,
@@ -43,19 +44,29 @@ const ruleFunction = (enabled) => {
4344
actual: enabled,
4445
possible: [true],
4546
},
47+
{
48+
actual: options,
49+
possible: {
50+
baseFilename: isString,
51+
},
52+
optional: true,
53+
},
4654
);
4755

4856
if (!validOptions) return;
4957

58+
59+
const { baseFilename = "spectrum-two" } = options;
60+
5061
const sourceFile = root.source.input.file;
5162
const parts = sourceFile ? sourceFile.split(sep) : [];
5263
const isTheme = parts[parts.length - 2] === "themes";
5364
const filename = parts[parts.length - 1];
5465

55-
if (!isTheme || filename === "spectrum.css") return;
66+
if (!isTheme || basename(filename, ".css") === baseFilename) return;
5667

57-
// All the parts of the source file but replace the filename with spectrum-two.css
58-
const baseFile = [...parts.slice(0, -1), "spectrum.css"].join(sep);
68+
// All the parts of the source file but replace the filename with the baseFilename
69+
const baseFile = [...parts.slice(0, -1), `${baseFilename}.css`].join(sep);
5970
const rootPath = parts.slice(0, -2).join(sep);
6071

6172
// If the base file doesn't exist, throw an error
@@ -81,8 +92,10 @@ const ruleFunction = (enabled) => {
8192

8293
/* Iterate over selectors in the base root */
8394
baseRoot.walkRules((rule) => {
84-
// Add this selector to the selectors set
85-
baseSelectors.add(rule.selector);
95+
rule.selectors.forEach((selector) => {
96+
// Add this selector to the selectors set
97+
baseSelectors.add(selector);
98+
});
8699

87100
rule.walkDecls((decl) => {
88101
// If this is a custom property, add it to the properties set
@@ -102,18 +115,20 @@ const ruleFunction = (enabled) => {
102115

103116
/* Iterate over selectors in the source root and validate that they align with the base */
104117
root.walkRules((rule) => {
105-
// Check if this selector exists in the base
106-
if (!baseSelectors.has(rule.selector)) {
107-
// Report any selectors that don't exist in the base
108-
report({
109-
message: messages.expected,
110-
messageArgs: [rule.selector, baseFile, rootPath],
111-
node: rule,
112-
result,
113-
ruleName,
114-
});
115-
return;
116-
}
118+
rule.selectors.forEach((selector) => {
119+
// Check if this selector exists in the base
120+
if (!baseSelectors.has(selector)) {
121+
// Report any selectors that don't exist in the base
122+
report({
123+
message: messages.expected,
124+
messageArgs: [selector, baseFile, rootPath],
125+
node: rule,
126+
result,
127+
ruleName,
128+
});
129+
return;
130+
}
131+
});
117132

118133
rule.walkDecls((decl) => {
119134
const isProperty = decl.prop.startsWith("--");

stylelint.config.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,9 @@ module.exports = {
196196
/* Validate that the legacy themes don't introduce any new selectors or custom properties */
197197
files: ["components/*/themes/express.css", "!components/*/themes/spectrum.css"],
198198
rules: {
199-
"spectrum-tools/theme-alignment": true,
199+
"spectrum-tools/theme-alignment": [true, {
200+
baseFilename: "spectrum.css",
201+
}],
200202
},
201203
},
202204
],

0 commit comments

Comments
 (0)