1
0
mirror of https://github.com/bitwarden/browser synced 2026-02-10 21:50:15 +00:00
Files
browser/libs/eslint/components/no-bwi-class-usage.mjs
Will Martin 20bc6036c6 [CL] fix no-bwi-class-usage eslint rule to allow helper classes (#18782)
The eslint rule now distinguishes between icon classes (bwi, bwi-lock, etc.)
and helper utility classes (bwi-fw, bwi-sm, bwi-lg, etc.) defined in the SCSS.
Helper classes like bwi-fw are legitimate utility classes that modify appearance
and can be used with bit-icon or other components without triggering warnings.

Updated the rule to maintain an allowlist of helper classes and only error when
actual icon classes are used directly.

Co-authored-by: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-02-05 12:07:40 -05:00

72 lines
2.0 KiB
JavaScript

export const errorMessage =
"Use <bit-icon> component instead of applying 'bwi' classes directly. Example: <bit-icon name=\"bwi-lock\"></bit-icon>";
// Helper classes from libs/angular/src/scss/bwicons/styles/style.scss
// These are utility classes that can be used independently
const ALLOWED_BWI_HELPER_CLASSES = new Set([
"bwi-fw", // Fixed width
"bwi-sm", // Small
"bwi-lg", // Large
"bwi-2x", // 2x size
"bwi-3x", // 3x size
"bwi-4x", // 4x size
"bwi-spin", // Spin animation
"bwi-ul", // List
"bwi-li", // List item
"bwi-rotate-270", // Rotation
]);
export default {
meta: {
type: "suggestion",
docs: {
description:
"Discourage using 'bwi' font icon classes directly in favor of the <bit-icon> component",
category: "Best Practices",
recommended: true,
},
schema: [],
},
create(context) {
return {
Element(node) {
// Get all class-related attributes
const classAttrs = [
...(node.attributes?.filter((attr) => attr.name === "class") ?? []),
...(node.inputs?.filter((input) => input.name === "class") ?? []),
...(node.templateAttrs?.filter((attr) => attr.name === "class") ?? []),
];
for (const classAttr of classAttrs) {
const classValue = classAttr.value || "";
if (typeof classValue !== "string") {
continue;
}
// Extract all bwi classes from the class string
const bwiClassMatches = classValue.match(/\bbwi(?:-[\w-]+)?\b/g);
if (!bwiClassMatches) {
continue;
}
// Check if any bwi class is NOT in the allowed helper classes list
const hasDisallowedBwiClass = bwiClassMatches.some(
(cls) => !ALLOWED_BWI_HELPER_CLASSES.has(cls),
);
if (hasDisallowedBwiClass) {
context.report({
node,
message: errorMessage,
});
// Only report once per element
break;
}
}
},
};
},
};