Skip to content

Commit a13d6e4

Browse files
committed
refactor(tslint): simplify convertRule signature and fix handling of withFix
1 parent e32fb17 commit a13d6e4

File tree

1 file changed

+28
-35
lines changed

1 file changed

+28
-35
lines changed

packages/tslint/index.ts

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,26 @@
11
import type * as TSSLint from '@tsslint/types';
2-
import type * as TSLint from 'tslint';
2+
import type { IOptions, IRule, IRuleMetadata, ITypedRule } from 'tslint';
33
import type * as ts from 'typescript';
44

5-
type TSLintRule = import('tslint/lib/language/rule/rule').RuleConstructor;
6-
7-
export function convertRule<T extends Partial<TSLintRule> | TSLintRule>(
8-
Rule: T,
5+
export function convertRule(
6+
Rule: {
7+
metadata?: IRuleMetadata;
8+
new(options: IOptions): IRule;
9+
},
910
ruleArguments: any[] = [],
1011
category: ts.DiagnosticCategory = 3 satisfies ts.DiagnosticCategory.Message,
1112
): TSSLint.Rule {
12-
const rule = new (Rule as TSLintRule)({
13+
const rule = new Rule({
1314
ruleName: Rule.metadata?.ruleName ?? 'unknown',
1415
ruleArguments,
1516
ruleSeverity: 'warning',
1617
disabledIntervals: [],
17-
}) as TSLint.IRule | TSLint.ITypedRule;
18+
}) as IRule | ITypedRule;
1819
return ({ file, languageService, report }) => {
1920
const failures = 'applyWithProgram' in rule
2021
? rule.applyWithProgram(file, languageService.getProgram()!)
2122
: rule.apply(file);
22-
for (const failure of new Set(failures)) {
23-
onAddFailure(failure);
24-
}
25-
function onAddFailure(failure: TSLint.RuleFailure) {
23+
for (const failure of failures) {
2624
const reporter = report(
2725
failure.getFailure(),
2826
failure.getStartPosition().getPosition(),
@@ -31,30 +29,25 @@ export function convertRule<T extends Partial<TSLintRule> | TSLintRule>(
3129
[new Error(), Number.MAX_VALUE]
3230
);
3331
if (failure.hasFix()) {
34-
const fix = failure.getFix();
35-
const replaces = Array.isArray(fix) ? fix : [fix];
36-
for (const replace of replaces) {
37-
if (replace) {
38-
reporter.withFix(
39-
replace.length === 0
40-
? 'Insert ' + replace.text
41-
: replace.text.length === 0
42-
? 'Delete ' + replace.start + ' to ' + replace.end
43-
: 'Replace with ' + replace.text,
44-
() => [{
45-
fileName: file.fileName,
46-
textChanges: [{
47-
newText: replace.text,
48-
span: {
49-
start: replace.start,
50-
length: replace.length,
51-
},
52-
}],
53-
}]
54-
);
55-
}
56-
}
32+
const ruleName = Rule.metadata?.ruleName;
33+
reporter.withFix(
34+
ruleName ? `Fix with ${ruleName}` : 'Fix',
35+
() => {
36+
const fix = failure.getFix();
37+
const replaces = Array.isArray(fix) ? fix : fix ? [fix] : [];
38+
return [{
39+
fileName: file.fileName,
40+
textChanges: replaces.map(replace => ({
41+
newText: replace.text,
42+
span: {
43+
start: replace.start,
44+
length: replace.length,
45+
},
46+
})),
47+
}];
48+
},
49+
);
5750
}
58-
};
51+
}
5952
};
6053
}

0 commit comments

Comments
 (0)