@salesforce-ux/eslint-plugin-slds
Version:
ESLint plugin provides custom linting rules specifically built for Salesforce Lightning Design System 2 (SLDS 2 beta)
170 lines (163 loc) • 5.83 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/utils/css-utils.ts
var css_utils_exports = {};
__export(css_utils_exports, {
forEachLwcVariable: () => forEachLwcVariable,
forEachNamespacedVariable: () => forEachNamespacedVariable,
forEachSldsVariable: () => forEachSldsVariable,
formatSuggestionHooks: () => formatSuggestionHooks,
isTargetProperty: () => isTargetProperty
});
module.exports = __toCommonJS(css_utils_exports);
// src/utils/hardcoded-shared-utils.ts
var import_css_tree2 = require("@eslint/css-tree");
// src/utils/color-lib-utils.ts
var import_chroma_js = __toESM(require("chroma-js"));
var import_css_tree = require("@eslint/css-tree");
// src/utils/css-functions.ts
var CSS_FUNCTIONS = [
"attr",
"calc",
"color-mix",
"conic-gradient",
"counter",
"cubic-bezier",
"linear-gradient",
"max",
"min",
"radial-gradient",
"repeating-conic-gradient",
"repeating-linear-gradient",
"repeating-radial-gradient",
"var"
];
var CSS_MATH_FUNCTIONS = ["calc", "min", "max"];
var cssFunctionsRegex = new RegExp(`(?:${CSS_FUNCTIONS.join("|")})`);
var cssFunctionsExactRegex = new RegExp(`^(?:${CSS_FUNCTIONS.join("|")})$`);
var cssMathFunctionsRegex = new RegExp(`^(?:${CSS_MATH_FUNCTIONS.join("|")})$`);
// src/utils/hardcoded-shared-utils.ts
function forEachValue(valueText, extractValue, shouldSkipNode, callback) {
if (!valueText || typeof valueText !== "string") {
return;
}
try {
const ast = (0, import_css_tree2.parse)(valueText, { context: "value", positions: true });
(0, import_css_tree2.walk)(ast, {
enter(node) {
if (shouldSkipNode(node)) {
return this.skip;
}
const value = extractValue(node);
if (value !== null) {
const positionInfo = {
start: node.loc?.start,
end: node.loc?.end
};
callback(value, positionInfo);
}
}
});
} catch (error) {
return;
}
}
// src/utils/css-utils.ts
function isTargetProperty(property, propertyTargets = []) {
if (typeof property !== "string") return false;
return property.startsWith("--sds-") || property.startsWith("--slds-") || property.startsWith("--lwc-") || propertyTargets.length === 0 || propertyTargets.includes(property);
}
function extractCssVariable(node, filter) {
if (!node || node.type !== "Function" || node.name !== "var") {
return null;
}
if (!node.children) {
return null;
}
const childrenArray = Array.from(node.children);
if (childrenArray.length === 0) {
return null;
}
const firstChild = childrenArray[0];
if (!firstChild || firstChild.type !== "Identifier") {
return null;
}
const variableName = firstChild.name;
if (!variableName) {
return null;
}
return filter(variableName, childrenArray);
}
function forEachSldsVariable(valueText, callback) {
const extractor = (node) => extractCssVariable(node, (variableName, childrenArray) => {
if (!variableName.startsWith("--slds-")) {
return null;
}
const hasFallback = childrenArray.some(
(child) => child.type === "Operator" && child.value === ","
);
return { name: variableName, hasFallback };
});
forEachValue(valueText, extractor, () => false, callback);
}
function forEachNamespacedVariable(valueText, callback) {
const extractor = (node) => extractCssVariable(node, (variableName) => {
if (variableName.startsWith("--slds-") || variableName.startsWith("--sds-")) {
return { name: variableName, hasFallback: false };
}
return null;
});
forEachValue(valueText, extractor, () => false, callback);
}
function forEachLwcVariable(valueText, callback) {
const extractor = (node) => extractCssVariable(node, (variableName, childrenArray) => {
if (!variableName.startsWith("--lwc-")) {
return null;
}
const hasFallback = childrenArray.some(
(child) => child.type === "Operator" && child.value === ","
);
return { name: variableName, hasFallback };
});
forEachValue(valueText, extractor, () => false, callback);
}
function formatSuggestionHooks(hooks) {
if (hooks.length === 1) {
return `${hooks[0]}`;
}
return "\n" + hooks.map((hook, index) => `${index + 1}. ${hook}`).join("\n");
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
forEachLwcVariable,
forEachNamespacedVariable,
forEachSldsVariable,
formatSuggestionHooks,
isTargetProperty
});
//# sourceMappingURL=css-utils.js.map