Skip to content

Commit

Permalink
lint: code bad smell
Browse files Browse the repository at this point in the history
  • Loading branch information
WanQuanXie committed Aug 22, 2024
1 parent 723a2bf commit 848ce28
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 14 deletions.
35 changes: 35 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module.exports = {
extends: "./node_modules/kcd-scripts/eslint.js",
rules: {
"import/order": [
"error",
{
pathGroups: [
{
pattern: "*.*css",
group: "object",
patternOptions: { matchBase: true },
position: "after",
},
{
pattern: "@/**",
group: "internal",
position: "after",
},
],
pathGroupsExcludedImportTypes: ["type"],
// 导入的排序顺序 内置、外部库、内部别名路径导入、父级目录、同级目录、当前目录、对象类型变量(如:import log = console.log;)、import type
groups: [
"builtin",
"external",
"internal",
"parent",
"sibling",
"index",
"object",
"type",
],
},
],
},
};
12 changes: 8 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import type babelCore from "@babel/core";

type Core = typeof babelCore;

// for better type hinting
const createNullObj = () =>
Object.create(null) as unknown as Record<string, never>;

function isSpecialTypes(t: Core["types"], node: babelCore.Node) {
return t.isMemberExpression(node) || t.isProperty(node);
}
Expand All @@ -24,9 +28,9 @@ export default function lucideReactNativeImport({
}: Core): babelCore.PluginObj<Config> {
// Track the icons that have already been used to prevent dupe imports
let selectedIcons: Record<IconName, babelCore.types.Identifier> =
Object.create(null);
createNullObj();
let specifiedLocal4Imported: Record<LocalName, ImportedName> =
Object.create(null);
createNullObj();
let removablePaths: babelCore.NodePath[] = [];

// Import a Lucide icon and return the computed import identifier
Expand Down Expand Up @@ -74,8 +78,8 @@ export default function lucideReactNativeImport({
visitor: {
Program: {
enter() {
selectedIcons = Object.create(null);
specifiedLocal4Imported = Object.create(null);
selectedIcons = createNullObj();
specifiedLocal4Imported = createNullObj();
removablePaths = [];
},
exit() {
Expand Down
4 changes: 2 additions & 2 deletions src/modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ export const resolveModule = (useES: boolean, name: string) => {

const icons = fs
.readdirSync(path.join(lucideIconsRealPath, `${moduleDir}/icons`))
.filter((name) => path.extname(name) === ".js")
.map((name) => path.basename(name, ".js"));
.filter((n) => path.extname(n) === ".js")
.map((n) => path.basename(n, ".js"));

if (icons.includes(iconFileName)) {
return `${iconFileSubRelativePath}/${moduleDir}/icons/${iconFileName}`;
Expand Down
9 changes: 1 addition & 8 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ export function partition<T, S extends T>(
list: readonly T[],
fn: (a: T) => a is S
): [S[], Exclude<T, S>[]];
export function partition<T, S extends T>(
list: readonly T[],
fn: (a: T) => boolean
): [T[], T[]];
export function partition<T, S extends T>(
list: readonly T[],
fn: ((a: T) => a is S) | ((a: T) => boolean)
Expand All @@ -14,9 +10,6 @@ export function partition<T, S extends T>(
list: readonly T[],
fn: ((a: T) => a is S) | ((a: T) => boolean)
) {
const [pass, fail]: [any[], any[]] = [[], []];
list.forEach((item) => (fn(item) ? pass.push(item) : fail.push(item)));

return list.reduce<[unknown[], unknown[]]>(
([left, right], item) => {
if (fn(item)) {
Expand All @@ -31,7 +24,7 @@ export function partition<T, S extends T>(

export const camel2Dash = (str: string) =>
str
.replace(/([A-Z]|[0-9]+)/g, (match) => "-" + match.toLowerCase())
.replace(/([A-Z]|[0-9]+)/g, (match) => `-${match.toLowerCase()}`)
.replace(/^-/, "");

export const generateUnknownIconError = (name: string) =>
Expand Down

0 comments on commit 848ce28

Please sign in to comment.