From 5146fd40809de88e5d8e5db2ef20865f66b41a4c Mon Sep 17 00:00:00 2001 From: getlarge Date: Fri, 13 Sep 2024 20:27:55 +0200 Subject: [PATCH] refactor: move eslint-rules to eslint-plugin --- README.md | 2 +- .../.eslintrc.json | 0 .../CHANGELOG.md | 0 .../{eslint-rules => eslint-plugin}/README.md | 10 ++++----- .../jest.config.ts | 4 ++-- .../package.json | 13 ++++++++---- packages/eslint-plugin/project.json | 21 +++++++++++++++++++ packages/eslint-plugin/src/index.ts | 18 ++++++++++++++++ .../src/lib/return-class-instance.spec.ts | 0 .../src/lib/return-class-instance.ts | 19 +---------------- .../tsconfig.json | 0 .../tsconfig.lib.json | 0 .../tsconfig.spec.json | 0 packages/eslint-rules/project.json | 21 ------------------- packages/eslint-rules/src/index.ts | 5 ----- tsconfig.base.json | 2 +- 16 files changed, 58 insertions(+), 57 deletions(-) rename packages/{eslint-rules => eslint-plugin}/.eslintrc.json (100%) rename packages/{eslint-rules => eslint-plugin}/CHANGELOG.md (100%) rename packages/{eslint-rules => eslint-plugin}/README.md (64%) rename packages/{eslint-rules => eslint-plugin}/jest.config.ts (72%) rename packages/{eslint-rules => eslint-plugin}/package.json (63%) create mode 100644 packages/eslint-plugin/project.json create mode 100644 packages/eslint-plugin/src/index.ts rename packages/{eslint-rules => eslint-plugin}/src/lib/return-class-instance.spec.ts (100%) rename packages/{eslint-rules => eslint-plugin}/src/lib/return-class-instance.ts (86%) rename packages/{eslint-rules => eslint-plugin}/tsconfig.json (100%) rename packages/{eslint-rules => eslint-plugin}/tsconfig.lib.json (100%) rename packages/{eslint-rules => eslint-plugin}/tsconfig.spec.json (100%) delete mode 100644 packages/eslint-rules/project.json delete mode 100644 packages/eslint-rules/src/index.ts diff --git a/README.md b/README.md index ac96c72..2d1b3d7 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ This repository contains several helpful packages for NestJS that I have develop | [AMQP-Transport](./packages/amqp-transport/README.md) | A NestJS microservice adapter around [AMQPlib] supporting exchanges. | [![npm](https://img.shields.io/npm/v/@getlarge/nestjs-tools-amqp-transport.svg?style=flat)](https://npmjs.org/package/@getlarge/nestjs-tools-amqp-transport) | | [Async-Local-Storage](./packages/async-local-storage/README.md) | A NestJS module to track context with [AsyncLocalStorage]. | [![npm](https://img.shields.io/npm/v/@getlarge/nestjs-tools-async-local-storage?style=flat)](https://npmjs.org/package/@getlarge/nestjs-tools-async-local-storage) | | [Cluster](./packages/cluster/README.md) | A class to manage workers' lifecycle in a (Node.js) [cluster]. | [![npm](https://img.shields.io/npm/v/@getlarge/nestjs-tools-cluster?style=flat)](https://npmjs.org/package/@getlarge/nestjs-tools-cluster) | -| [ESLint-Rules](./packages/eslint-rules/README.md) | A set of ESLint rules for NestJS applications. | [![npm](https://img.shields.io/npm/v/@getlarge/nestjs-tools-eslint-rules?style=flat)](https://npmjs.org/package/@getlarge/nestjs-tools-eslint-rules) | +| [ESLint-Plugin](./packages/eslint-plugin-/README.md) | A set of ESLint rules for NestJS applications. | [![npm](https://img.shields.io/npm/v/@getlarge/eslint-plugin-nestjs-tools?style=flat)](https://npmjs.org/package/@getlarge/eslint-plugin-nestjs-tools) | | [File-Storage](./packages/file-storage/README.md) | A NestJS module supporting [FS], [S3] and [GCP] strategies. | [![npm](https://img.shields.io/npm/v/@getlarge/nestjs-tools-file-storage?style=flat)](https://npmjs.org/package/@getlarge/nestjs-tools-file-storage) | | [Lock](./packages/lock/README.md) | A NestJS module to provide a distributed lock for your application. | [![npm](https://img.shields.io/npm/v/@getlarge/nestjs-tools-lock?style=flat)](https://npmjs.org/package/@getlarge/nestjs-tools-lock) | | [Fastify-Upload](./packages/fastify-upload/README.md) | A NestJS module to provide file upload support for Fastify. | [![npm](https://img.shields.io/npm/v/@getlarge/nestjs-tools-fastify-upload?style=flat)](https://npmjs.org/package/@getlarge/nestjs-tools-fastify-upload) | diff --git a/packages/eslint-rules/.eslintrc.json b/packages/eslint-plugin/.eslintrc.json similarity index 100% rename from packages/eslint-rules/.eslintrc.json rename to packages/eslint-plugin/.eslintrc.json diff --git a/packages/eslint-rules/CHANGELOG.md b/packages/eslint-plugin/CHANGELOG.md similarity index 100% rename from packages/eslint-rules/CHANGELOG.md rename to packages/eslint-plugin/CHANGELOG.md diff --git a/packages/eslint-rules/README.md b/packages/eslint-plugin/README.md similarity index 64% rename from packages/eslint-rules/README.md rename to packages/eslint-plugin/README.md index 486f90d..44e22ad 100644 --- a/packages/eslint-rules/README.md +++ b/packages/eslint-plugin/README.md @@ -1,16 +1,16 @@ -# eslint-rules +# eslint-plugin [![npm][npm-image]][npm-url] -[npm-image]: https://img.shields.io/npm/v/@getlarge/nestjs-tools-eslint-rules.svg?style=flat -[npm-url]: https://npmjs.org/package/@getlarge/nestjs-tools-eslint-rules +[npm-image]: https://img.shields.io/npm/v/@getlarge/eslint-plugin-nestjs-tools.svg?style=flat +[npm-url]: https://npmjs.org/package/@getlarge/eslint-plugin-nestjs-tools This set of ESLint rules is provided to enforce a consistent patterns and practices across all NestJS projects. ## Installation ```bash -npm install --save @getlarge/nestjs-tools-eslint-rules +npm install --save @getlarge/eslint-plugin-nestjs-tools ``` ## Usage @@ -23,7 +23,7 @@ The purpose of this rule is to ensure you return class instances instead of plai ```json { "rules": { - "@getlarge/nestjs-tools-eslint-rules/return-class-instance": "error" + "@getlarge/eslint-plugin-nestjs-tools/return-class-instance": "error" } } ``` diff --git a/packages/eslint-rules/jest.config.ts b/packages/eslint-plugin/jest.config.ts similarity index 72% rename from packages/eslint-rules/jest.config.ts rename to packages/eslint-plugin/jest.config.ts index 6fdcf86..5bd05c3 100644 --- a/packages/eslint-rules/jest.config.ts +++ b/packages/eslint-plugin/jest.config.ts @@ -1,11 +1,11 @@ /* eslint-disable */ export default { - displayName: 'eslint-rules', + displayName: 'eslint-plugin', preset: '../../jest.preset.js', testEnvironment: 'node', transform: { '^.+\\.[tj]s$': ['ts-jest', { tsconfig: '/tsconfig.spec.json' }], }, moduleFileExtensions: ['ts', 'js', 'html'], - coverageDirectory: '../../coverage/packages/eslint-rules', + coverageDirectory: '../../coverage/packages/eslint-plugin', }; diff --git a/packages/eslint-rules/package.json b/packages/eslint-plugin/package.json similarity index 63% rename from packages/eslint-rules/package.json rename to packages/eslint-plugin/package.json index 1deffd5..e77fc62 100644 --- a/packages/eslint-rules/package.json +++ b/packages/eslint-plugin/package.json @@ -1,9 +1,11 @@ { - "name": "@getlarge/nestjs-tools-eslint-rules", + "name": "@getlarge/eslint-plugin-nestjs-tools", "version": "0.1.0", "description": "ESLint rules for NestJS applications", "keywords": [ "eslint", + "eslint-plugin", + "eslintplugin", "nestjs", "rules", "typescript", @@ -12,13 +14,16 @@ ], "license": "Apache-2.0", "author": "Edouard Maleix ", - "homepage": "https://github.com/getlarge/nestjs-tools/tree/main/packages/eslint-rules", + "homepage": "https://github.com/getlarge/nestjs-tools/tree/main/packages/eslint-plugin", "publishConfig": { "access": "public" }, - "dependencies": {}, + "dependencies": { + "@typescript-eslint/utils": "8.5.0" + }, "peerDependencies": { - "eslint": "7 || 8 || 9" + "@typescript-eslint/parser": "^8.0.0 || ^8.0.0-alpha.0", + "eslint": "^8.57.0 || ^9.0.0" }, "type": "commonjs", "main": "./src/index.js", diff --git a/packages/eslint-plugin/project.json b/packages/eslint-plugin/project.json new file mode 100644 index 0000000..87c06a1 --- /dev/null +++ b/packages/eslint-plugin/project.json @@ -0,0 +1,21 @@ +{ + "name": "eslint-plugin-nestjs-tools", + "$schema": "../../node_modules/nx/schemas/project-schema.json", + "sourceRoot": "packages/eslint-plugin/src", + "projectType": "library", + "tags": [], + "// targets": "to see all targets run: nx show project eslint-plugin-nestjs-tools --web", + "targets": { + "build": { + "executor": "@nx/js:tsc", + "outputs": ["{options.outputPath}"], + "options": { + "outputPath": "dist/packages/eslint-plugin", + "tsConfig": "packages/eslint-plugin/tsconfig.lib.json", + "packageJson": "packages/eslint-plugin/package.json", + "main": "packages/eslint-plugin/src/index.ts", + "assets": ["packages/eslint-plugin/*.md"] + } + } + } +} diff --git a/packages/eslint-plugin/src/index.ts b/packages/eslint-plugin/src/index.ts new file mode 100644 index 0000000..f37846c --- /dev/null +++ b/packages/eslint-plugin/src/index.ts @@ -0,0 +1,18 @@ +import type { Linter } from '@typescript-eslint/utils/dist/ts-eslint'; + +import { rule as returnClassInstance, RULE_NAME as returnClassInstanceName } from './lib/return-class-instance'; + +// note - cannot migrate this to an import statement because it will make TSC copy the package.json to the dist folder +// eslint-disable-next-line @typescript-eslint/no-var-requires +const { name, version } = require('../package.json') as { + name: string; + version: string; +}; + +export default { + meta: { + name, + version, + }, + rules: { [returnClassInstanceName]: returnClassInstance }, +} satisfies Linter.Plugin; diff --git a/packages/eslint-rules/src/lib/return-class-instance.spec.ts b/packages/eslint-plugin/src/lib/return-class-instance.spec.ts similarity index 100% rename from packages/eslint-rules/src/lib/return-class-instance.spec.ts rename to packages/eslint-plugin/src/lib/return-class-instance.spec.ts diff --git a/packages/eslint-rules/src/lib/return-class-instance.ts b/packages/eslint-plugin/src/lib/return-class-instance.ts similarity index 86% rename from packages/eslint-rules/src/lib/return-class-instance.ts rename to packages/eslint-plugin/src/lib/return-class-instance.ts index c7d8473..7d8035f 100644 --- a/packages/eslint-rules/src/lib/return-class-instance.ts +++ b/packages/eslint-plugin/src/lib/return-class-instance.ts @@ -1,24 +1,7 @@ /* eslint-disable max-lines-per-function */ -/** - * This file sets you up with structure needed for an ESLint rule. - * - * It leverages utilities from @typescript-eslint to allow TypeScript to - * provide autocompletions etc for the configuration. - * - * Your rule's custom logic will live within the create() method below - * and you can learn more about writing ESLint rules on the official guide: - * - * https://eslint.org/docs/developer-guide/working-with-rules - * - * You can also view many examples of existing rules here: - * - * https://github.com/typescript-eslint/typescript-eslint/tree/master/packages/eslint-plugin/src/rules - */ - import { ESLintUtils } from '@typescript-eslint/utils'; -// NOTE: The rule will be available in ESLint configs as "@nx/workspace/return-class-instance" -export const RULE_NAME = '@getlarge/nestjs-tools-eslint-rules/return-class-instance'; +export const RULE_NAME = '@getlarge/eslint-plugin-nestjs-tools/return-class-instance'; const getTypeNameFromReturnType = (rawReturnType: string) => { return rawReturnType.replaceAll(/Promise<([^<>]*)>/g, '$1').replace(': ', ''); diff --git a/packages/eslint-rules/tsconfig.json b/packages/eslint-plugin/tsconfig.json similarity index 100% rename from packages/eslint-rules/tsconfig.json rename to packages/eslint-plugin/tsconfig.json diff --git a/packages/eslint-rules/tsconfig.lib.json b/packages/eslint-plugin/tsconfig.lib.json similarity index 100% rename from packages/eslint-rules/tsconfig.lib.json rename to packages/eslint-plugin/tsconfig.lib.json diff --git a/packages/eslint-rules/tsconfig.spec.json b/packages/eslint-plugin/tsconfig.spec.json similarity index 100% rename from packages/eslint-rules/tsconfig.spec.json rename to packages/eslint-plugin/tsconfig.spec.json diff --git a/packages/eslint-rules/project.json b/packages/eslint-rules/project.json deleted file mode 100644 index 184b237..0000000 --- a/packages/eslint-rules/project.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "name": "nestjs-tools-eslint-rules", - "$schema": "../../node_modules/nx/schemas/project-schema.json", - "sourceRoot": "packages/eslint-rules/src", - "projectType": "library", - "tags": [], - "// targets": "to see all targets run: nx show project eslint-rules --web", - "targets": { - "build": { - "executor": "@nx/js:tsc", - "outputs": ["{options.outputPath}"], - "options": { - "outputPath": "dist/packages/eslint-rules", - "tsConfig": "packages/eslint-rules/tsconfig.lib.json", - "packageJson": "packages/eslint-rules/package.json", - "main": "packages/eslint-rules/src/index.ts", - "assets": ["packages/eslint-rules/*.md"] - } - } - } -} diff --git a/packages/eslint-rules/src/index.ts b/packages/eslint-rules/src/index.ts deleted file mode 100644 index 047c673..0000000 --- a/packages/eslint-rules/src/index.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { rule as returnClassInstance, RULE_NAME as returnClassInstanceName } from './lib/return-class-instance'; - -module.exports = { - rules: { [returnClassInstanceName]: returnClassInstance }, -}; diff --git a/tsconfig.base.json b/tsconfig.base.json index da82855..591fdff 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -18,7 +18,7 @@ "@getlarge/nestjs-tools-amqp-transport": ["packages/amqp-transport/src/index.ts"], "@getlarge/nestjs-tools-async-local-storage": ["packages/async-local-storage/src/index.ts"], "@getlarge/nestjs-tools-cluster": ["packages/cluster/src/index.ts"], - "@getlarge/nestjs-tools-eslint-rules": ["packages/eslint-rules/src/index.ts"], + "@getlarge/eslint-plugin-nestjs-tools": ["packages/eslint-plugin/src/index.ts"], "@getlarge/nestjs-tools-fastify-upload": ["packages/fastify-upload/src/index.ts"], "@getlarge/nestjs-tools-file-storage": ["packages/file-storage/src/index.ts"], "@getlarge/nestjs-tools-lock": ["packages/lock/src/index.ts"],