Skip to content

Commit

Permalink
feat: add relaxed rules (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
mav-rik authored Jan 27, 2024
1 parent df3aafd commit 6f8d6eb
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 4 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ DML_DEBUG=1 npx do-me-lint
| ------------------------------------------------ | --------------------------------------------------------------- | -------------------------------------------------------------------------- | ------------------------------------------------------------- |
| semi <br> <small>`boolean`</small> | Force semicolons to be required (`true`) or forbidden (`false`) | | DML_SEMI <br> <small>`"1"` (other values are false)</small> |
| ignoredRules <br> <small>`Array<string>`</small> | Ignored rules <br> <small>will be not added</small> | | DML_IGNORED_RULES<br> <small>`comma-separated string`</small> |
| relaxedRules <br> <small>`Array<string>`</small> | Relaxed rules <br> <small>generate warnings</small> | | DML_RELAXED_RULES<br> <small>`comma-separated string`</small> |
| jestFiles<br> <small>`string`</small> | Pattern for Jest specs | <small> `src/**/{__tests__/*,*.{spec,test}}`<br>`.{js,ts,jsx,tsx}`</small> | DML_JEST_FILES <br> <small>`string`</small> |
|| Extended debug info | | DML_DEBUG <br> <small>`"1"` (other values are false)</small> |

Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/controllers/doMeESLint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const doMeESLint = async (context: Context): Promise<void> => {
projectDependencies: context.installedPackages.map(installedPackage => installedPackage.name),
patterns: context.patterns,
ignoredRules: context.ignoredRules,
relaxedRules: context.relaxedRules,
semi: context.semi,
})

Expand Down
4 changes: 3 additions & 1 deletion src/controllers/getMeContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export interface Context {
monorepoRoot?: string
patterns: Patterns
ignoredRules: string[]
relaxedRules: string[]
dependencyManager?: DependencyManager
semi: boolean
gitignore: string[]
Expand All @@ -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)
Expand All @@ -78,6 +79,7 @@ export const getMeContext = (): Context => {
monorepoRoot,
patterns,
ignoredRules,
relaxedRules,
semi,
gitignore,
debug,
Expand Down
1 change: 1 addition & 0 deletions src/lib/context/paths.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
14 changes: 14 additions & 0 deletions src/lib/context/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const getRcSettings = (projectDirectory: string): JsonObject => {
export interface Settings {
jestFiles: string[] | string
ignoredRules: string[]
relaxedRules: string[]
semi: boolean
debug: boolean
}
Expand All @@ -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'
Expand All @@ -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',
}
Expand Down
8 changes: 6 additions & 2 deletions src/lib/eslint/getConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { getProperty } from './rulesConfig'
interface Parameters {
projectDependencies: string[]
ignoredRules: string[]
relaxedRules: string[]
patterns: Patterns
semi: boolean
}
Expand All @@ -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)
Expand Down Expand Up @@ -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<ESLintRules> => {
const input = { projectDependencies, semi }
Expand All @@ -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
Expand Down

0 comments on commit 6f8d6eb

Please sign in to comment.