From 6f8d6eb8f0bf6463390b8833670d895ce3f1ac51 Mon Sep 17 00:00:00 2001 From: ArtemM Date: Sat, 27 Jan 2024 12:25:48 +0100 Subject: [PATCH] feat: add relaxed rules (#2) --- README.md | 6 +++++- src/controllers/doMeESLint.ts | 1 + src/controllers/getMeContext.ts | 4 +++- src/lib/context/paths.test.ts | 1 + src/lib/context/settings.ts | 14 ++++++++++++++ src/lib/eslint/getConfig.ts | 8 ++++++-- 6 files changed, 30 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 095a51f..1cc718e 100644 --- a/README.md +++ b/README.md @@ -85,6 +85,7 @@ DML_DEBUG=1 npx do-me-lint | ------------------------------------------------ | --------------------------------------------------------------- | -------------------------------------------------------------------------- | ------------------------------------------------------------- | | semi
`boolean` | Force semicolons to be required (`true`) or forbidden (`false`) | | DML_SEMI
`"1"` (other values are false) | | ignoredRules
`Array` | Ignored rules
will be not added | | DML_IGNORED_RULES
`comma-separated string` | +| relaxedRules
`Array` | Relaxed rules
generate warnings | | DML_RELAXED_RULES
`comma-separated string` | | jestFiles
`string` | Pattern for Jest specs | `src/**/{__tests__/*,*.{spec,test}}`
`.{js,ts,jsx,tsx}`
| DML_JEST_FILES
`string` | | — | Extended debug info | | DML_DEBUG
`"1"` (other values are false) | @@ -94,8 +95,11 @@ Your `.domelintrc.yml` may look like this: semi: true ignoredRules: - max-params - - func-names - prefer-template + +# generate warnings instead of errors +relaxedRules: + - func-names ``` ## Supported ESLint plugins diff --git a/src/controllers/doMeESLint.ts b/src/controllers/doMeESLint.ts index 66b2645..1101fa6 100644 --- a/src/controllers/doMeESLint.ts +++ b/src/controllers/doMeESLint.ts @@ -14,6 +14,7 @@ export const doMeESLint = async (context: Context): Promise => { projectDependencies: context.installedPackages.map(installedPackage => installedPackage.name), patterns: context.patterns, ignoredRules: context.ignoredRules, + relaxedRules: context.relaxedRules, semi: context.semi, }) diff --git a/src/controllers/getMeContext.ts b/src/controllers/getMeContext.ts index 9237986..4cadd49 100644 --- a/src/controllers/getMeContext.ts +++ b/src/controllers/getMeContext.ts @@ -33,6 +33,7 @@ export interface Context { monorepoRoot?: string patterns: Patterns ignoredRules: string[] + relaxedRules: string[] dependencyManager?: DependencyManager semi: boolean gitignore: string[] @@ -56,7 +57,7 @@ export const getMeContext = (): Context => { log.debug(`monorepo root:\t${monorepoRoot}`) } - const { ignoredRules, semi, debug } = settings + const { ignoredRules, relaxedRules, semi, debug } = settings const patterns = getPatterns(settings) const packageJson = getPackageJson(projectDirectory) @@ -78,6 +79,7 @@ export const getMeContext = (): Context => { monorepoRoot, patterns, ignoredRules, + relaxedRules, semi, gitignore, debug, diff --git a/src/lib/context/paths.test.ts b/src/lib/context/paths.test.ts index 2bd8f8a..451320d 100644 --- a/src/lib/context/paths.test.ts +++ b/src/lib/context/paths.test.ts @@ -7,6 +7,7 @@ test('should return values from settings', () => { semi: true, debug: false, ignoredRules: ['fizz', 'buzz'], + relaxedRules: ['fizz1', 'buzz1'], } expect(getPatterns(settings)).toHaveProperty('jestFiles', 'foo') }) diff --git a/src/lib/context/settings.ts b/src/lib/context/settings.ts index 4e05f9d..49521cc 100644 --- a/src/lib/context/settings.ts +++ b/src/lib/context/settings.ts @@ -32,6 +32,7 @@ const getRcSettings = (projectDirectory: string): JsonObject => { export interface Settings { jestFiles: string[] | string ignoredRules: string[] + relaxedRules: string[] semi: boolean debug: boolean } @@ -52,6 +53,18 @@ export const getSettings = (projectDirectory: string): Settings => { ignoredRules = [] } + let relaxedRules: string[] + if (process.env.DML_RELAXED_RULES !== undefined) { + relaxedRules = process.env.DML_RELAXED_RULES.toString().split(',') + } else if ( + Array.isArray(rcSettings.relaxedRules) && + rcSettings.relaxedRules.every(value => typeof value === 'string') + ) { + relaxedRules = rcSettings.relaxedRules as string[] + } else { + relaxedRules = [] + } + let semi: boolean if (process.env.DML_SEMI !== undefined) { semi = process.env.DML_SEMI === '1' @@ -68,6 +81,7 @@ export const getSettings = (projectDirectory: string): Settings => { rcSettings.jestFiles?.toString() ?? 'src/**/{__tests__/*,*.{spec,test}}.{js,ts,jsx,tsx}', ignoredRules, + relaxedRules, semi, debug: process.env.DML_DEBUG === '1', } diff --git a/src/lib/eslint/getConfig.ts b/src/lib/eslint/getConfig.ts index b123960..f55794e 100644 --- a/src/lib/eslint/getConfig.ts +++ b/src/lib/eslint/getConfig.ts @@ -18,6 +18,7 @@ import { getProperty } from './rulesConfig' interface Parameters { projectDependencies: string[] ignoredRules: string[] + relaxedRules: string[] patterns: Patterns semi: boolean } @@ -27,10 +28,11 @@ interface Result { dependencies: ExactDependency[] } export const getConfig = (parameters: Parameters): Result => { - const { projectDependencies, ignoredRules, patterns, semi } = parameters + const { projectDependencies, ignoredRules, relaxedRules, patterns, semi } = parameters const rules = getRules({ projectDependencies, ignoredRules, + relaxedRules, semi, }) const plugins = getPlugins(projectDependencies) @@ -96,11 +98,13 @@ const buildValue = ({ options, level }: BuildValueParameters): RuleValue => { interface GetRulesParameters { projectDependencies: string[] ignoredRules: string[] + relaxedRules: string[] semi: boolean } const getRules = ({ projectDependencies, ignoredRules, + relaxedRules, semi, }: GetRulesParameters): ByScope => { const input = { projectDependencies, semi } @@ -115,7 +119,7 @@ const getRules = ({ const enabled = getProperty(rule.enabled, input) const options = getProperty(rule.options, input) const scope = rule.scope || 'all' - const level = 'error' + const level = relaxedRules.includes(rulename) ? 'warn' : 'error' if (!enabled) { return false