@greguintow/eslint-plugin-nestjs-typed

中文文档
2.2.0发布于 2024/03/31ISCTypeScriptCommonJS近 30 天下载 8533

Eslint rules for nestjs projects

原始使用文档
文档目录10 节
  1. A note on versions
  2. Have an idea for a rule?
  3. Index of available rules
  4. Who is this package for?
  5. 1. Detect Nest Dependency Injection issues
  6. 2. Using Open Api / Swagger decorators and automatically ge…
  7. 3. Helping prevent bugs
  8. 4. Security
  9. To install
  10. To configure

commit npm npm-tag size types

A note on versions

  • Version 5.x supports Eslint version >=8.x and typescript eslint parser ^7
  • Version 4.x supports Eslint version >=8.x and typescript eslint parser ^6
  • Version 3.x supports Eslint version >=8.x and typescript eslint parser ^5
  • Version 2.x supports Eslint version <=7.x and typescript eslint parser ^4

There are breaking changes between versions ofr ts-eslint.

typescript eslint parser supports a range of typescript versions but there can be a delay in supporting the latest versions.

This plugin only supports typescript up to the version typescript eslint parser supports. See https://github.com/typescript-eslint/typescript-eslint#supported-typescript-version for the versions.

Have an idea for a rule?

Awsome! Click here to submit a new issue!

Index of available rules

CategoryRuleis on in recommended ruleset?
Nest Modules and Dependency Injectionprovided-injected-should-match-factory-parametersY
injectable-should-be-providedY
Nest Swaggerapi-property-matches-property-optionalityY
controllers-should-supply-api-tagsY
api-method-should-specify-api-responseN
api-method-should-specify-api-operationY
api-enum-property-best-practicesY
api-property-returning-array-should-set-arrayY
Preventing bugsparam-decorator-name-matches-route-paramY
validate-nested-of-array-should-set-eachY
validated-non-primitive-property-needs-type-decoratorY
all-properties-are-whitelistedY
all-properties-have-explicit-definedY
no-duplicate-decoratorsY
Securityvalidation-pipe-should-forbid-unknownY
api-methods-should-be-guardedN
Code Consistencysort-module-metadata-arraysN

The "recommended" ruleset are the default rules that are turned on when you configure the plugin as described in this document.

The name "recommended" is an eslint convention. Some rules in this plugin are opinionated and have to be turned on explicitly in your eslintrc file.

Who is this package for?

If you use NestJs (https://nestjs.com/) these ESLint rules will help you to prevent common bugs and issues in NestJs applications.

They mostly check that you are using decorators correctly.

The primary groupings of rules in this plugin are...

1. Detect Nest Dependency Injection issues

The Nest DI is declarative and if you forget to provide an injectable you wont see an error until run time. Nest is good at telling you where these are but sometimes it's not.

In particular if you're using custom providers the errors can be really tricky to figure out because they won't explicitly error about mismatched injected items, you will just get unexpected operation.

These are described in the "Common Errors" section of the nest js docs.

2. Using Open Api / Swagger decorators and automatically generating a clients

When working with NestJS I generate my front end client and models using the swagger/Open API specification generated directly from the nest controllers and models.

I have a bunch of rules here that enforce strict Open API typing with decorators for NestJs controllers and models.

These rules are opinionated, but necessary for clean model generation if using an Open Api client generator later in your build.

3. Helping prevent bugs

There are some tightly coupled but untyped decorators and things like that in nest that will catch you out if your not careful. There are some rules to help prevent issues that have caught me out before.

4. Security

There is a CVE for class-transformer when using random javascript objects. You need to be careful about configuring the ValidationPipe in NestJs. See https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2019-18413 https://github.com/typestack/class-validator/issues/438

To install

The plugin is on npm here: https://www.npmjs.com/package/@darraghor/eslint-plugin-nestjs-typed You can see how the rules are used in this NestJS project: https://github.com/darraghoriordan/use-miller

npm install --save-dev @darraghor/eslint-plugin-nestjs-typed

// or

yarn add -D @darraghor/eslint-plugin-nestjs-typed
// or

pnpm add -D @darraghor/eslint-plugin-nestjs-typed

If you don't already have class-validator you should install that

npm install class-validator

// or

yarn add class-validator

// or

pnpm add class-validator

To configure

Update your eslint with the plugin import and add the recommended rule set

module.exports = {
    env: {
        es6: true,
    },
    extends: ["plugin:@darraghor/nestjs-typed/recommended"],
    parser: "@typescript-eslint/parser",
    parserOptions: {
        project: ["./tsconfig.json"],
        sourceType: "module",
        ecmaVersion: "es2019",
    },
    plugins: ["@darraghor/nestjs-typed"],
};

Note: the injectables test scans your whole project. It's best to filter out ts things that don't matter - use filterFromPaths configuration setting for this. See the rule documentation for more info.

There are some defaults already applied.

Note: You can easily turn off all the swagger rules if you don't use swagger by adding the no-swagger rule set AFTER the recommended rule set.

// all the other config
    extends: ["plugin:@darraghor/nestjs-typed/recommended",
    "plugin:@darraghor/nestjs-typed/no-swagger"
    ],
 // more config

Disable a single rule with the full name e.g. in your eslint configuration...

   rules: {
   "@darraghor/nestjs-typed/api-property-returning-array-should-set-array":
            "off",
   }

版本与发布记录

@greguintow/eslint-plugin-nestjs-typed 共发布 21 个版本,首次发布于 2022/01/27;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.2.0latest2024/03/312 年前
2.1.12024/03/302 年前
2.1.02024/03/302 年前
2.0.02024/03/172 年前
1.3.42023/11/143 年前
1.3.32023/11/143 年前

关于 @greguintow/eslint-plugin-nestjs-typed 的常见问题

@greguintow/eslint-plugin-nestjs-typed 是什么?

Eslint rules for nestjs projects

如何安装 @greguintow/eslint-plugin-nestjs-typed?

在项目目录运行 npm install @greguintow/eslint-plugin-nestjs-typed,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@typescript-eslint/parser(>=7.0.0)、@typescript-eslint/scope-manager(>=7.0.0)、@typescript-eslint/utils(>=7.0.0)、class-validator(*)、reflect-metadata(*)、eslint(^8.56.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@greguintow/eslint-plugin-nestjs-typed 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@greguintow/eslint-plugin-nestjs-typed 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@greguintow/eslint-plugin-nestjs-typed 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js ^18.18.0 || >=20.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@greguintow/eslint-plugin-nestjs-typed 可以商用吗?

@greguintow/eslint-plugin-nestjs-typed 使用 ISC 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@greguintow/eslint-plugin-nestjs-typed 的下载量有多少?

根据 npm 官方下载统计,@greguintow/eslint-plugin-nestjs-typed 在 2026/10/17 – 2026/11/15 这 30 天内被下载 8,533 次,日均约 284 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@greguintow/eslint-plugin-nestjs-typed 还在维护吗?

最新版本 2.2.0 发布于 2024/03/31(2 年前);至今共发布 21 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/01/27。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。