From 3b18989c17c5689842212c22eea8b7e776993ddc Mon Sep 17 00:00:00 2001 From: Griko Nibras Date: Wed, 29 Mar 2023 01:35:28 +0700 Subject: [PATCH] feat: add eslint extend fn --- eslint/extend.js | 59 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 eslint/extend.js diff --git a/eslint/extend.js b/eslint/extend.js new file mode 100644 index 0000000..2962959 --- /dev/null +++ b/eslint/extend.js @@ -0,0 +1,59 @@ +// @ts-check + +const { getTsconfigPath } = require("./utils/tsconfig"); + +/** + * @typedef EslintConfigName + * @type {"base" | "browser-node" | "config-authoring" | "jest" | "next" | "node" | "playwright-test" | "react" | "tailwindcss" | "tsup" | "typescript"} + */ + +/** + * @param {EslintConfigName[]} configNames + * @param {import("eslint").Linter.Config} extraConfig + */ +module.exports = (configNames = [], extraConfig = {}) => { + /** @type {import("eslint").Linter.Config} */ + const config = { ...extraConfig }; + + /** @type {import("eslint").Linter.Config["extends"]} */ + const newExtends = []; + configNames.forEach((name) => { + require.resolve(`@strangelovelabs/style-guide/eslint/${name}`); + }); + if (typeof extraConfig.extends === "string") { + newExtends.push(extraConfig.extends); + } + if (Array.isArray(extraConfig.extends)) { + newExtends.push(...extraConfig.extends); + } + config.extends = configNames.map((name) => { + return require.resolve(`@strangelovelabs/style-guide/eslint/${name}`); + }); + + /** @type {import("eslint").Linter.Config["ignorePatterns"]} */ + const newIgnorePatterns = []; + if (typeof extraConfig.ignorePatterns === "string") { + newIgnorePatterns.push(extraConfig.ignorePatterns); + } + if (Array.isArray(extraConfig.ignorePatterns)) { + newIgnorePatterns.push(...extraConfig.ignorePatterns); + } + config.ignorePatterns = newIgnorePatterns; + + if (configNames.includes("typescript")) { + config.parserOptions = { + ...config.parserOptions, + project: getTsconfigPath(), + }; + config.settings = { + ...config.settings, + "import/resolver": { + ...config.settings?.["import/resolver"], + typescript: { + ...config.settings?.["import/resolver"]?.typescript, + project: getTsconfigPath(), + }, + }, + }; + } +};