UNPKG

@coderwyd/eslint-config

Version:
110 lines (109 loc) 3.49 kB
import { createRequire } from "node:module"; import antfu, { GLOB_SRC, GLOB_VUE, interopDefault, resolveSubOptions } from "@antfu/eslint-config"; import process from "node:process"; //#region src/config/tailwindcss.ts async function tailwindcss(options = {}) { const { files = [GLOB_SRC, GLOB_VUE], overrides = {}, settings = {} } = options; return [{ name: "coderwyd/tailwindcss/setup", plugins: { tailwindcss: await interopDefault(import("eslint-plugin-better-tailwindcss")) } }, { files, name: "coderwyd/tailwindcss/rules", rules: { "tailwindcss/enforce-consistent-class-order": "warn", "tailwindcss/no-duplicate-classes": "warn", "tailwindcss/no-unnecessary-whitespace": "warn", "tailwindcss/no-deprecated-classes": "warn", "tailwindcss/no-conflicting-classes": "warn", "tailwindcss/enforce-shorthand-classes": "warn", "tailwindcss/enforce-consistent-variable-syntax": "warn", ...overrides }, settings: { "better-tailwindcss": settings } }]; } //#endregion //#region src/utils.ts const require = createRequire(import.meta.url); const cwd = process.cwd(); function isPackageExists(name) { try { require.resolve(name, { paths: [cwd] }); return true; } catch { return false; } } //#endregion //#region src/merge-options.ts function deepMerge(user, defaults) { const result = { ...user }; for (const key of Object.keys(defaults)) { const userVal = result[key]; const defaultVal = defaults[key]; if (userVal === void 0) result[key] = defaultVal; else if (userVal === true && typeof defaultVal === "object") result[key] = defaultVal; else if (typeof userVal === "object" && userVal !== null && typeof defaultVal === "object" && defaultVal !== null) result[key] = deepMerge(userVal, defaultVal); } return result; } function mergeOptions(options) { const { vueVersion = 3 } = resolveSubOptions(options ?? {}, "vue"); const defaults = { typescript: { overrides: { "ts/consistent-type-definitions": "off", "ts/promise-function-async": "off", "ts/strict-boolean-expressions": "off", "ts/return-await": "off", "ts/switch-exhaustiveness-check": "off", "ts/no-explicit-any": "warn" } }, vue: { overrides: { "vue/block-order": ["error", { order: vueVersion === 3 ? [ "script", "template", "style" ] : [ "template", "script", "style" ] }], "vue/html-self-closing": ["error", { html: { component: "always", normal: "always", void: "any" }, math: "always", svg: "always" }] } }, react: isPackageExists("react") || !!options?.react ? { overrides: { "react/no-context-provider": "off", "react/no-forward-ref": "off", "react/no-use-context": "off", "react-hooks/set-state-in-effect": "off", "react-hooks-extra/no-direct-set-state-in-use-effect": "error" } } : void 0, pnpm: false, stylistic: false, isInEditor: false }; if (!options) return defaults; return deepMerge(options, defaults); } //#endregion //#region src/index.ts function defineConfig(options, ...userConfigs) { const { tailwindcss: enableTailwindcss, ...antfuOptions } = options || {}; const configs = antfu(mergeOptions(antfuOptions)); if (enableTailwindcss) { const tailwindOptions = typeof enableTailwindcss === "boolean" ? {} : enableTailwindcss; configs.append(tailwindcss(tailwindOptions)).renamePlugins({ "better-tailwindcss": "tailwindcss" }); } configs.append(...userConfigs); return configs; } //#endregion export { defineConfig };