This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
const LUCIDE_PACKAGE = "@lucide/svelte";
|
||||
|
||||
function toKebabCase(iconName) {
|
||||
return iconName
|
||||
.replace(/^Lucide/, "")
|
||||
.replace(/Icon$/, "")
|
||||
.replace(/([a-z\d])([A-Z])/g, "$1-$2")
|
||||
.replace(/([A-Z]+)([A-Z][a-z])/g, "$1-$2")
|
||||
.toLowerCase();
|
||||
}
|
||||
|
||||
function getReplacement(specifier) {
|
||||
if (specifier.type !== "ImportSpecifier" || specifier.importKind === "type") {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (specifier.imported.type !== "Identifier") {
|
||||
return null;
|
||||
}
|
||||
|
||||
const importedName = specifier.imported.name;
|
||||
const iconPath = toKebabCase(importedName);
|
||||
|
||||
if (!iconPath || iconPath === "lucide") {
|
||||
return null;
|
||||
}
|
||||
|
||||
return `import ${specifier.local.name} from "${LUCIDE_PACKAGE}/icons/${iconPath}";`;
|
||||
}
|
||||
|
||||
const preferIconImports = {
|
||||
meta: {
|
||||
type: "suggestion",
|
||||
docs: {
|
||||
description: "Prefer direct Lucide icon imports over barrel imports",
|
||||
},
|
||||
fixable: "code",
|
||||
messages: {
|
||||
preferDirectImport:
|
||||
"Replace barrel Lucide imports with direct icon imports.",
|
||||
},
|
||||
},
|
||||
create(context) {
|
||||
return {
|
||||
ImportDeclaration(node) {
|
||||
if (
|
||||
node.source.value !== LUCIDE_PACKAGE ||
|
||||
node.importKind === "type" ||
|
||||
node.specifiers.length === 0
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
const replacements = node.specifiers.map(getReplacement);
|
||||
|
||||
if (replacements.some((replacement) => replacement === null)) {
|
||||
return;
|
||||
}
|
||||
|
||||
context.report({
|
||||
node,
|
||||
messageId: "preferDirectImport",
|
||||
fix(fixer) {
|
||||
return fixer.replaceText(node, replacements.join("\n"));
|
||||
},
|
||||
});
|
||||
},
|
||||
};
|
||||
},
|
||||
};
|
||||
|
||||
export default {
|
||||
meta: {
|
||||
name: "lucide",
|
||||
},
|
||||
rules: {
|
||||
"prefer-icon-imports": preferIconImports,
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user