Skip to content

Commit

Permalink
feat(eslint): add unused-imports
Browse files Browse the repository at this point in the history
  • Loading branch information
ModyQyW committed Nov 15, 2024
1 parent c65d5d7 commit 980df9b
Show file tree
Hide file tree
Showing 7 changed files with 440 additions and 162 deletions.
1 change: 1 addition & 0 deletions src/eslint/configs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ export * from "./toml.ts";
export * from "./typescript.ts";
export * from "./unicorn.ts";
export * from "./unocss.ts";
export * from "./unused-imports.ts";
export * from "./vue.ts";
export * from "./yml.ts";
2 changes: 2 additions & 0 deletions src/eslint/configs/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export function markdown(options: MarkdownOptions = {}): Config[] {
"n/no-unpublished-import": "off",
"n/no-unpublished-require": "off",
"unicorn/prefer-module": "off",
"unused-imports/no-unused-imports": "off",
"unused-imports/no-unused-vars": "off",
"vue/no-unused-emit-declarations": "off",

...rules,
Expand Down
55 changes: 55 additions & 0 deletions src/eslint/configs/unused-imports.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import {
GLOB_DTS,
GLOB_SCRIPT,
GLOB_TS,
GLOB_TSX,
GLOB_VUE,
} from "../../constants.ts";
import { hasTypeScript, hasVue } from "../../env.ts";
import { pluginUnusedImports } from "../plugins.ts";
import type { Config, UnusedImportsOptions } from "../types.ts";

export function unusedImports(options: UnusedImportsOptions = {}): Config[] {
const {
files = [GLOB_SCRIPT, GLOB_VUE],
rules = {},
typescriptFiles = hasTypeScript && hasVue
? [GLOB_DTS, GLOB_TS, GLOB_TSX, GLOB_VUE]
: [GLOB_DTS, GLOB_TS, GLOB_TSX],
typescriptRules = {},
} = options;
return [
{
name: "base/unused-imports",
files,
plugins: {
"unused-imports": pluginUnusedImports,
},
rules: {
"no-unused-vars": "off",
"unused-imports/no-unused-imports": "error",
"unused-imports/no-unused-vars": [
"warn",
{
args: "after-used",
argsIgnorePattern: "^_",
ignoreRestSiblings: true,
vars: "all",
varsIgnorePattern: "^_",
},
],

...rules,
},
},
{
name: "base/unused-imports/typescript",
files: typescriptFiles,
rules: {
"@typescript-eslint/no-unused-vars": "off",

...typescriptRules,
},
},
];
}
55 changes: 34 additions & 21 deletions src/eslint/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
typescript,
unicorn,
unocss,
unusedImports,
vue,
yml,
} from "./configs/index.ts";
Expand All @@ -32,11 +33,16 @@ export function eslint(
userConfigs: (Config | Config[])[] = [],
): Config[] {
const {
command: commandOptions,
// ---- Ignores ----
gitignore: gitignoreOptions,
ignores: ignoresOptions,
imports: importsOptions,
// ---- Languages ----
javascript: javascriptOptions,
typescript: typescriptOptions,
// ---- Base ----
command: commandOptions,
imports: importsOptions,
unusedImports: unusedImportsOptions,
jsdoc: jsdocOptions,
jsonc: jsoncOptions,
markdown: markdownOptions,
Expand All @@ -50,7 +56,7 @@ export function eslint(
regexp: regexpOptions,
tailwindcss: tailwindcssOptions,
toml: tomlOptions,
typescript: typescriptOptions,

unicorn: unicornOptions,
unocss: unocssOptions,
vue: vueOptions,
Expand All @@ -66,6 +72,7 @@ export function eslint(
if (isBoolean(gitignoreOptions)) configs.push(gitignore());
else configs.push(gitignore(gitignoreOptions));
}

// ignores
if (ignoresOptions) {
if (isBoolean(ignoresOptions)) configs.push(ignores());
Expand All @@ -88,34 +95,28 @@ export function eslint(

// ---- Base ----

// command
if (commandOptions) {
if (isBoolean(commandOptions)) configs.push(command());
else configs.push(command(commandOptions));
}

// JSDoc
if (jsdocOptions) {
if (isBoolean(jsdocOptions)) configs.push(jsdoc());
else configs.push(jsdoc(jsdocOptions));
}

// Command
if (commandOptions) {
if (isBoolean(commandOptions)) configs.push(command());
else configs.push(command(commandOptions));
}

// imports
if (importsOptions) {
if (isBoolean(importsOptions)) configs.push(imports());
else configs.push(imports(importsOptions));
}

// Node
if (nodeOptions) {
if (isBoolean(nodeOptions)) configs.push(node());
else configs.push(node(nodeOptions));
}

// Perfectionist
if (perfectionistOptions) {
if (isBoolean(perfectionistOptions)) configs.push(perfectionist());
else configs.push(perfectionist(perfectionistOptions));
// unused imports
if (unusedImportsOptions) {
if (isBoolean(unusedImportsOptions)) configs.push(unusedImports());
else configs.push(unusedImports(unusedImportsOptions));
}

// Promise
Expand All @@ -130,7 +131,19 @@ export function eslint(
else configs.push(regexp(regexpOptions));
}

// Unicorn
// Node
if (nodeOptions) {
if (isBoolean(nodeOptions)) configs.push(node());
else configs.push(node(nodeOptions));
}

// perfectionist
if (perfectionistOptions) {
if (isBoolean(perfectionistOptions)) configs.push(perfectionist());
else configs.push(perfectionist(perfectionistOptions));
}

// unicorn
if (unicornOptions) {
if (isBoolean(unicornOptions)) configs.push(unicorn());
else configs.push(unicorn(unicornOptions));
Expand Down Expand Up @@ -180,7 +193,7 @@ export function eslint(
else configs.push(unocss(unocssOptions));
}

// ---- Specify File Type ----
// ---- Special File Type ----

// Markdown
if (markdownOptions) {
Expand Down
Loading

0 comments on commit 980df9b

Please sign in to comment.