Skip to content

Commit

Permalink
feat(eslint): add promise support
Browse files Browse the repository at this point in the history
  • Loading branch information
ModyQyW committed Aug 17, 2024
1 parent e17f533 commit 82fe32b
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 4 deletions.
4 changes: 4 additions & 0 deletions docs/guide/linter/eslint.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ export default eslint({
// Check orders related issues
// Default true (enabled)
perfectionist: true,
// Based on eslint-plugin-promise
// Check Promise related issues
// Default true (enabled)
promise: true,
// Based on eslint-plugin-react, eslint-plugin-react-hooks, eslint-plugin-react-perf and eslint-plugin-react-refresh
// Check React related issues, support Taro
// Enabled by default if you have React installed
Expand Down
4 changes: 4 additions & 0 deletions docs/zh-Hans/guide/linter/eslint.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ export default eslint({
// 检查排序相关问题
// 默认为 true,即启用
perfectionist: true,
// 基于 eslint-plugin-promise
// 检查 Promise 相关问题
// 默认为 true,即启用
promise: true,
// 基于 eslint-plugin-react、eslint-plugin-react-hooks、eslint-plugin-react-perf 和 eslint-plugin-react-refresh
// 检查 React 相关问题,支持 Taro
// 安装 React 后默认启用,否则默认禁用
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@
"eslint-plugin-nuxt": "^4.0.0",
"eslint-plugin-package-json": "^0.15.2",
"eslint-plugin-perfectionist": "^3.2.0",
"eslint-plugin-promise": "^7.1.0",
"eslint-plugin-react": "^7.35.0",
"eslint-plugin-react-hooks": "^4.6.2",
"eslint-plugin-react-native": "^4.1.0",
Expand Down
13 changes: 13 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/eslint/configs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export * from "./markdown";
export * from "./next";
export * from "./node";
export * from "./nuxt";
export * from "./promise";
export * from "./perfectionist";
export * from "./react";
export * from "./react-native";
Expand Down
58 changes: 58 additions & 0 deletions src/eslint/configs/promise.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import {
GLOB_DTS,
GLOB_SCRIPT,
GLOB_TS,
GLOB_TSX,
GLOB_VUE,
} from "../../constants";
import { hasTypeScript, hasVue } from "../../env";
import { pluginPromise } from "../plugins";
import type { Config, PromiseOptions } from "../types";

export function promise(options: PromiseOptions = {}): 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: "promise",
files,
plugins: {
promise: pluginPromise,
},
rules: {
"promise/always-return": "error",
// 'promise/avoid-new': 'off',
"promise/catch-or-return": "error",
"promise/no-callback-in-promise": "warn",
"promise/no-multiple-resolved": "warn",
// 'promise/no-native': 'off',
"promise/no-nesting": "warn",
"promise/no-new-statics": "error",
"promise/no-promise-in-callback": "warn",
"promise/no-return-in-finally": "warn",
"promise/no-return-wrap": "error",
"promise/param-names": "error",
"promise/prefer-await-to-callbacks": "warn",
"promise/prefer-await-to-then": "warn",
"promise/spec-only": "warn",
"promise/valid-params": "warn",

...rules,
},
},
{
name: "promise-typescript",
files: typescriptFiles,
rules: {
...typescriptRules,
},
},
];
}
6 changes: 6 additions & 0 deletions src/eslint/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
node,
nuxt,
perfectionist,
promise,
react,
reactNative,
regexp,
Expand Down Expand Up @@ -41,6 +42,7 @@ export function eslint(
node: nodeOptions,
nuxt: nuxtOptions,
perfectionist: perfectionistOptions,
promise: promiseOptions,
react: reactOptions,
reactNative: reactNativeOptions,
regexp: regexpOptions,
Expand Down Expand Up @@ -75,6 +77,10 @@ export function eslint(
if (isBoolean(perfectionistOptions)) configs.push(perfectionist());
else configs.push(perfectionist(perfectionistOptions));
}
if (promiseOptions) {
if (isBoolean(promiseOptions)) configs.push(promise());
else configs.push(promise(promiseOptions));
}
if (regexpOptions) {
if (isBoolean(regexpOptions)) configs.push(regexp());
else configs.push(regexp(regexpOptions));
Expand Down
3 changes: 3 additions & 0 deletions src/eslint/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import * as _pluginVueScopedCss from "eslint-plugin-vue-scoped-css";
import * as _pluginNuxt2 from "eslint-plugin-nuxt";
import * as _pluginNuxt3 from "@nuxt/eslint-plugin";
/* @ts-expect-error missing types */
import * as _pluginPromise from "eslint-plugin-promise";
/* @ts-expect-error missing types */
import * as _pluginReact from "eslint-plugin-react";
/* @ts-expect-error missing types */
import * as _pluginReactHooks from "eslint-plugin-react-hooks";
Expand Down Expand Up @@ -60,6 +62,7 @@ export const pluginVue = interopDefault(_pluginVue);
export const pluginVueScopedCss = interopDefault(_pluginVueScopedCss);
export const pluginNuxt2 = interopDefault(_pluginNuxt2);
export const pluginNuxt3 = interopDefault(_pluginNuxt3);
export const pluginPromise = interopDefault(_pluginPromise);
export const pluginReact = interopDefault(_pluginReact);
export const pluginReactHooks = fixupPluginRules(
interopDefault(_pluginReactHooks),
Expand Down
14 changes: 10 additions & 4 deletions src/eslint/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,11 @@ export interface PerfectionistOptions {
typescriptFiles?: string[];
typescriptRules?: Rules;
}
export interface PrettierOptions {
export interface PromiseOptions {
files?: string[];
rules?: Rules;
typescriptFiles?: string[];
typescriptRules?: Rules;
}
export interface ReactNativeOptions {
files?: string[];
Expand Down Expand Up @@ -182,12 +185,15 @@ export interface Options {
/**
* Based on eslint-plugin-perfectionist.
*
* Conflicts with ianvs/prettier-plugin-sort-imports,
* trivago/prettier-plugin-sort-imports and prettier-organize-imports.
*
* @default true
*/
perfectionist?: PerfectionistOptions | boolean;
/**
* Detect promise issues.
*
* @default true
*/
promise?: PromiseOptions | boolean;
/**
* Detect react issues.
*
Expand Down
1 change: 1 addition & 0 deletions src/eslint/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export function parseOptions(options: Options = {}): Required<Options> {
node: options.node ?? true,
nuxt: options.nuxt ?? hasNuxt,
perfectionist: options.perfectionist ?? true,
promise: options.promise ?? true,
react: options.react ?? hasReact,
reactNative: options.reactNative ?? hasReactNative,
regexp: options.regexp ?? true,
Expand Down

0 comments on commit 82fe32b

Please sign in to comment.