refactor(tree-wide): misc improvements (#1547)

This commit is contained in:
uncenter authored and GitHub committed 2024-12-31 17:52:08 -05:00
1 parent 3e6ecfbf86
commit 6121f23d7c
126 files changed
+8047 -8021

No files matched your search

@@ -1,85 +0,0 @@
import stylelint from "stylelint";
import valueParser from "postcss-value-parser";
const {
createPlugin,
utils: { report, ruleMessages, validateOptions },
} = stylelint;
const ruleName = "catppuccin/require-color-op-func-percentage";
const meta = {
fixable: true,
};
const messages = ruleMessages(ruleName, {
rejected: () =>
`Color operation function amount arguments must be percentages`,
});
/** @type {import('npm:stylelint').Rule} */
const ruleFunction = (primary, _secondary, context) => {
return (root, result) => {
const validOptions = validateOptions(result, ruleName, {
actual: primary,
possible: [true],
});
if (!validOptions) return;
root.walkDecls((decl) => {
const parsed = valueParser(decl.value);
if (
parsed.nodes.length === 1 &&
parsed.nodes[0].type === "function" &&
[
"saturate",
"desaturate",
"lighten",
"darken",
"fadein",
"fadeout",
"fade",
"mix",
"tint",
"shade",
].includes(parsed.nodes[0].value)
) {
const func = parsed.nodes[0];
const numArgLoc = parsed.nodes[0].value === "mix" ? 4 : 2;
if (
func.nodes.length === numArgLoc + 1 &&
!func.nodes[numArgLoc].value.endsWith("%")
) {
let value = func.nodes[numArgLoc].value;
if (!/^[\d.]+$/.test(value)) return;
if (value.includes(".")) {
value = Number.parseFloat(value);
if (value < 1) value *= 100;
}
if (context.fix) {
func.nodes[numArgLoc].value = value + "%";
decl.value = parsed.toString();
} else {
report({
result,
ruleName,
message: messages.rejected(),
node: decl,
});
}
}
}
});
};
};
ruleFunction.ruleName = ruleName;
ruleFunction.messages = messages;
ruleFunction.meta = meta;
export default createPlugin(ruleName, ruleFunction);
@@ -0,0 +1,181 @@
// @ts-check
import stylelint from "stylelint";
import valueParser from "postcss-value-parser";
import { flavors } from "@catppuccin/palette";
const LESS_COLOR_OP_FUNCTIONS = [
"saturate",
"desaturate",
"lighten",
"darken",
"fadein",
"fadeout",
"fade",
"mix",
"tint",
"shade",
];
const NUMERICAL_VALUE_REGEX = /^[\d.]+$/;
const VALID_COLOR_VARIABLES = [
...Object.keys(flavors.latte.colors).map((color) => "@" + color),
"@accent",
];
/**
* Accepts a color operation amount and normalizes it by multiplying it by 100 if it is less than 1.
* @param {string} amount
* @returns {number}
*
* @example
* normalizeColorOperationAmount("0.3") // 30
* normalizeColorOperationAmount("30") // 30
*/
function normalizeColorOperationAmount(amount) {
let value = Number.parseFloat(amount);
if (value < 1) value *= 100;
return value;
}
const {
createPlugin,
utils: { report, ruleMessages, validateOptions },
} = stylelint;
const ruleName = "catppuccin/color-operations";
const meta = {
fixable: true,
};
const messages = ruleMessages(ruleName, {
amount_not_percentage: () => `Function amount argument must be percentage`,
rgba_used_with_variable: () => `Use 'fade' instead of 'rgba' on variables`,
fadeout_used: () => `Use 'fade' instead of 'fadeout'`,
fadein_used: () => `Use 'fade' instead of 'fadein'`,
});
/** @type {import('npm:stylelint').Rule} */
const ruleFunction = (primary, _secondary, context) => {
return (root, result) => {
const validOptions = validateOptions(result, ruleName, {
actual: primary,
possible: [true],
});
if (!validOptions) return;
root.walkDecls((decl) => {
const parsed = valueParser(decl.value);
parsed.walk((node) => {
if (node.type !== "function") return;
const pre = decl.prop.length +
(decl.raws.between?.length || 0);
const startIndex = pre + node.sourceIndex;
const endIndex = pre + node.sourceEndIndex - 1;
// Prevent using `rgba` with variables to change the alpha value.
if (
node.value === "rgba" &&
node.nodes.length === 3 &&
node.nodes[0].value.startsWith("@") &&
NUMERICAL_VALUE_REGEX.test(node.nodes[2].value)
) {
if (context.fix) {
node.value = "fade";
node.nodes[2].value = normalizeColorOperationAmount(
node.nodes[2].value,
) + "%";
} else {
report({
result,
ruleName,
message: messages.rgba_used_with_variable(),
node: decl,
index: startIndex,
endIndex: endIndex,
});
}
}
// Use `fade` instead of `fadeout`.
if (
node.value === "fadeout"
) {
if (
context.fix &&
// Only autofixable if it follows the pattern of `fadeout(@<color>, <num>%)`. Otherwise, it's ambiguous.
node.nodes.length === 3 &&
VALID_COLOR_VARIABLES.includes(node.nodes[0].value)
) {
node.value = "fade";
const value = node.nodes[2].value.replace("%", "");
// Invert the value to get the fade amount. E.g. `fadeout(@color, 30%)` becomes `fade(@color, 70%)`.
node.nodes[2].value = (100 - normalizeColorOperationAmount(value)) +
"%";
} else {
report({
result,
ruleName,
message: messages.fadeout_used(),
node: decl,
index: startIndex,
endIndex: endIndex,
});
}
}
// Use `fade` instead of `fadein`. Not autofixable because a) on palette variables, it has no effect (increasing opaqueness on a fully opaque color) and b) it's ambiguous otherwise.
if (node.value === "fadein") {
report({
result,
ruleName,
message: messages.fadein_used(),
node: decl,
index: startIndex,
endIndex: endIndex,
});
}
// Use percentage amount values for Less color operation functions.
if (!LESS_COLOR_OP_FUNCTIONS.includes(node.value)) return;
const amountArgIndex = node.value === "mix" ? 4 : 2;
if (
node.nodes.length === amountArgIndex + 1 &&
!node.nodes[amountArgIndex].value.endsWith("%")
) {
const value = node.nodes[amountArgIndex].value;
if (!NUMERICAL_VALUE_REGEX.test(value)) return;
if (context.fix) {
node.nodes[amountArgIndex].value = normalizeColorOperationAmount(
value,
) + "%";
} else {
report({
result,
ruleName,
message: messages.amount_not_percentage(),
node: decl,
index: startIndex + node.nodes[amountArgIndex].sourceIndex,
endIndex: startIndex + node.nodes[amountArgIndex].sourceEndIndex -
1,
});
}
}
});
decl.value = parsed.toString();
});
};
};
ruleFunction.ruleName = ruleName;
ruleFunction.messages = messages;
ruleFunction.meta = meta;
export default createPlugin(ruleName, ruleFunction);
+9
View File
@@ -0,0 +1,9 @@
import colorOperation from "@/lint/stylelint-custom/color-operation.js";
import optimizedSvgs from "@/lint/stylelint-custom/optimized-svgs.js";
import redundantParentSelector from "@/lint/stylelint-custom/redundant-parent-selector.js";
export default [
colorOperation,
optimizedSvgs,
redundantParentSelector,
];
@@ -0,0 +1,99 @@
// @ts-check
import stylelint from "stylelint";
import { assert } from "@std/assert";
const {
createPlugin,
utils: { report, ruleMessages, validateOptions },
} = stylelint;
const ruleName = "catppuccin/no-redundant-parent-selector";
const meta = {
fixable: true,
};
const messages = ruleMessages(ruleName, {
rejected: () => `Redundant parent selector is not allowed`,
});
/** @type {import('npm:stylelint').Rule} */
const ruleFunction = (primary, _secondary, context) => {
return (root, result) => {
const validOptions = validateOptions(result, ruleName, {
actual: primary,
possible: [true],
});
if (!validOptions) return;
root.walkRules((rule) => {
if (rule.selectors.length === 1 && rule.selector === "&") {
if (context.fix) {
// Moves the rule's properties to the parent rule *in place* of the parent selector.
// & {
// color: red;
// }
// becomes
// color: red;
const parent = rule.parent;
if (parent) {
let idx = parent.index(rule);
assert(idx !== -1);
rule.each((node) => {
if (node.type === "decl") {
parent.insertAfter(
parent.nodes[idx],
node.clone(),
);
idx++;
}
});
rule.remove();
}
} else {
report({
result,
ruleName,
message: messages.rejected(),
node: rule,
});
}
}
rule.selector = rule.selector.replace(
// Match "& ", at the start or after a comma and optional whitespace, but not when followed by "when".
/(^|,\s*)&\s+(?!when\b)/g,
(match, prefix, offset) => {
const matchIndex = offset + prefix.length;
const matchEndIndex = matchIndex + 1; // `&` is one character long.
if (context.fix) {
return prefix; // Remove '& ' but keep the "prefix" (start or comma and whitespace).
} else {
report({
result,
ruleName,
message: messages.rejected(),
node: rule,
index: matchIndex,
endIndex: matchEndIndex,
});
}
return match;
},
);
});
};
};
ruleFunction.ruleName = ruleName;
ruleFunction.messages = messages;
ruleFunction.meta = meta;
export default createPlugin(ruleName, ruleFunction);
+1 -4
View File
@@ -1,7 +1,4 @@
import { REPO_ROOT } from "@/constants.ts";
import * as color from "@std/fmt/colors";
import * as path from "@std/path";
import "postcss-less";
import stylelint from "stylelint";
@@ -23,7 +20,7 @@ export async function runStylelint(
});
if (code) {
Deno.writeTextFileSync(path.join(REPO_ROOT, file), code);
Deno.writeTextFileSync(file, code);
}
for (const result of results) {