Skip to content

Commit

Permalink
feat(stylelint-theme-alignment): base filename input
Browse files Browse the repository at this point in the history
  • Loading branch information
castastrophe committed Nov 18, 2024
1 parent e914fca commit 9d788d8
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 20 deletions.
5 changes: 5 additions & 0 deletions .changeset/silent-frogs-care.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@spectrum-tools/theme-alignment": minor
---

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.
53 changes: 34 additions & 19 deletions plugins/stylelint-theme-alignment/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
*/

import fs from "node:fs";
import { relative, sep } from "node:path";
import { basename, relative, sep } from "node:path";

import postcss from "postcss";
import valuesParser from "postcss-values-parser";
import stylelint from "stylelint";
import { isString } from "stylelint/lib/utils/validateTypes.mjs";

const {
createPlugin,
Expand All @@ -34,7 +35,7 @@ const messages = ruleMessages(ruleName, {
});

/** @type {import('stylelint').Plugin} */
const ruleFunction = (enabled) => {
const ruleFunction = (enabled, options = {}) => {
return (root, result) => {
const validOptions = validateOptions(
result,
Expand All @@ -43,19 +44,29 @@ const ruleFunction = (enabled) => {
actual: enabled,
possible: [true],
},
{
actual: options,
possible: {
baseFilename: isString,
},
optional: true,
},
);

if (!validOptions) return;


const { baseFilename = "spectrum-two" } = options;

const sourceFile = root.source.input.file;
const parts = sourceFile ? sourceFile.split(sep) : [];
const isTheme = parts[parts.length - 2] === "themes";
const filename = parts[parts.length - 1];

if (!isTheme || filename === "spectrum.css") return;
if (!isTheme || basename(filename, ".css") === baseFilename) return;

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

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

/* Iterate over selectors in the base root */
baseRoot.walkRules((rule) => {
// Add this selector to the selectors set
baseSelectors.add(rule.selector);
rule.selectors.forEach((selector) => {
// Add this selector to the selectors set
baseSelectors.add(selector);
});

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

/* Iterate over selectors in the source root and validate that they align with the base */
root.walkRules((rule) => {
// Check if this selector exists in the base
if (!baseSelectors.has(rule.selector)) {
// Report any selectors that don't exist in the base
report({
message: messages.expected,
messageArgs: [rule.selector, baseFile, rootPath],
node: rule,
result,
ruleName,
});
return;
}
rule.selectors.forEach((selector) => {
// Check if this selector exists in the base
if (!baseSelectors.has(selector)) {
// Report any selectors that don't exist in the base
report({
message: messages.expected,
messageArgs: [selector, baseFile, rootPath],
node: rule,
result,
ruleName,
});
return;
}
});

rule.walkDecls((decl) => {
const isProperty = decl.prop.startsWith("--");
Expand Down
4 changes: 3 additions & 1 deletion stylelint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,9 @@ module.exports = {
/* Validate that the legacy themes don't introduce any new selectors or custom properties */
files: ["components/*/themes/express.css", "!components/*/themes/spectrum.css"],
rules: {
"spectrum-tools/theme-alignment": true,
"spectrum-tools/theme-alignment": [true, {
baseFilename: "spectrum.css",
}],
},
},
],
Expand Down

0 comments on commit 9d788d8

Please sign in to comment.