Skip to content

Commit 81b1871

Browse files
committed
feat(config): Implicitly compatible with TSLint/ESLint rule severity values
fix #83
1 parent 1ad2d1a commit 81b1871

File tree

3 files changed

+39
-8
lines changed

3 files changed

+39
-8
lines changed

packages/config/lib/eslint.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as fs from 'fs';
44
import * as path from 'path';
55
import type * as ts from 'typescript';
66
import type { ESLintRulesConfig } from './eslint-types.js';
7+
import { normalizeRuleSeverity, type RuleSeverity } from './utils.js';
78

89
const noop = () => {};
910
const plugins: Record<
@@ -30,8 +31,6 @@ const loader = async (moduleName: string) => {
3031
return mod as any;
3132
};
3233

33-
type Severity = boolean | 'error' | 'warn';
34-
3534
/**
3635
* Converts an ESLint rules configuration to TSSLint rules.
3736
*
@@ -40,7 +39,7 @@ type Severity = boolean | 'error' | 'warn';
4039
* Please run `npx tsslint-docgen` to update them.
4140
*/
4241
export async function importESLintRules(
43-
config: { [K in keyof ESLintRulesConfig]: Severity | [Severity, ...ESLintRulesConfig[K]] },
42+
config: { [K in keyof ESLintRulesConfig]: RuleSeverity | [RuleSeverity, ...ESLintRulesConfig[K]] },
4443
context: Partial<ESLint.Rule.RuleContext> = {},
4544
) {
4645
let convertRule: typeof import('@tsslint/compat-eslint').convertRule;
@@ -53,7 +52,7 @@ export async function importESLintRules(
5352

5453
const rules: TSSLint.Rules = {};
5554
for (const [rule, severityOrOptions] of Object.entries(config)) {
56-
let severity: Severity;
55+
let severity: RuleSeverity;
5756
let options: any[];
5857
if (Array.isArray(severityOrOptions)) {
5958
[severity, ...options] = severityOrOptions;
@@ -62,6 +61,7 @@ export async function importESLintRules(
6261
severity = severityOrOptions;
6362
options = [];
6463
}
64+
severity = normalizeRuleSeverity(severity);
6565
if (!severity) {
6666
rules[rule] = noop;
6767
continue;

packages/config/lib/tslint.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ import * as path from 'path';
44
import type { IOptions, IRule, IRuleMetadata, ITypedRule } from 'tslint';
55
import type * as ts from 'typescript';
66
import type { TSLintRulesConfig } from './tslint-types.js';
7+
import { normalizeRuleSeverity, type RuleSeverity } from './utils.js';
78

89
const noop = () => {};
910

10-
type Severity = boolean | 'error' | 'warn';
11-
1211
/**
1312
* Converts a TSLint rules configuration to TSSLint rules.
1413
*
@@ -17,13 +16,13 @@ type Severity = boolean | 'error' | 'warn';
1716
* Please run `npx tsslint-docgen` to update them.
1817
*/
1918
export async function importTSLintRules(
20-
config: { [K in keyof TSLintRulesConfig]: Severity | [Severity, ...TSLintRulesConfig[K]] },
19+
config: { [K in keyof TSLintRulesConfig]: RuleSeverity | [RuleSeverity, ...TSLintRulesConfig[K]] },
2120
) {
2221
const rules: TSSLint.Rules = {};
2322
const rulesDirectories = getTSLintRulesDirectories();
2423

2524
for (const [ruleName, severityOrOptions] of Object.entries(config)) {
26-
let severity: Severity;
25+
let severity: RuleSeverity;
2726
let options: any[];
2827
if (Array.isArray(severityOrOptions)) {
2928
[severity, ...options] = severityOrOptions;
@@ -32,6 +31,7 @@ export async function importTSLintRules(
3231
severity = severityOrOptions;
3332
options = [];
3433
}
34+
severity = normalizeRuleSeverity(severity);
3535
if (!severity) {
3636
rules[ruleName] = noop;
3737
continue;

packages/config/lib/utils.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
export type RuleSeverity = boolean | 'error' | 'warn';
2+
3+
type ESLintStringSeverity = 'off' | 'warn' | 'error';
4+
type ESLintNumericSeverity = 0 | 1 | 2;
5+
type TSLintStringSeverity = 'default' | 'warning' | 'warn' | 'error' | 'off' | 'none';
6+
type TSLintBooleanSeverity = true | false;
7+
8+
export function normalizeRuleSeverity(
9+
severity:
10+
| RuleSeverity
11+
| ESLintStringSeverity
12+
| ESLintNumericSeverity
13+
| TSLintStringSeverity
14+
| TSLintBooleanSeverity,
15+
): RuleSeverity {
16+
switch (severity) {
17+
case 'default':
18+
return true;
19+
case 0:
20+
case 'off':
21+
case 'none':
22+
return false;
23+
case 1:
24+
case 'warning':
25+
return 'warn';
26+
case 2:
27+
return 'error';
28+
default:
29+
return severity;
30+
}
31+
}

0 commit comments

Comments
 (0)