forked from typescript-eslint/typescript-eslint
-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.ts
178 lines (167 loc) · 4.76 KB
/
config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import { ESLintUtils } from '@typescript-eslint/utils';
import memoize from 'lodash/memoize';
import type { RuleSeverity } from 'tslint';
import { Configuration } from 'tslint';
import { CustomLinter } from '../custom-linter';
// 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-unsafe-assignment
const version: string = require('../../package.json');
const createRule = ESLintUtils.RuleCreator(
() =>
`https://github.com/typescript-eslint/typescript-eslint/blob/v${version}/packages/eslint-plugin-tslint/README.md`,
);
export type RawRulesConfig = Record<
string,
| null
| undefined
| boolean
| unknown[]
| {
severity?: RuleSeverity | 'warn' | 'none' | 'default';
options?: unknown;
}
>;
export type MessageIds = 'failure';
export type Options = [
{
rules?: RawRulesConfig;
rulesDirectory?: string[];
lintFile?: string;
},
];
/**
* Construct a configFile for TSLint
*/
const tslintConfig = memoize(
(
lintFile?: string,
tslintRules?: RawRulesConfig,
tslintRulesDirectory?: string[],
) => {
if (lintFile != null) {
return Configuration.loadConfigurationFromPath(lintFile);
}
return Configuration.parseConfigFile({
rules: tslintRules ?? {},
rulesDirectory: tslintRulesDirectory ?? [],
});
},
(
lintFile: string | undefined,
tslintRules = {},
tslintRulesDirectory: string[] = [],
) =>
`${lintFile}_${JSON.stringify(tslintRules)}_${tslintRulesDirectory.join()}`,
);
export default createRule<Options, MessageIds>({
name: 'config',
meta: {
docs: {
description:
'Wraps a TSLint configuration and lints the whole source using TSLint', // eslint-disable-line eslint-plugin/require-meta-docs-description
recommended: false,
},
fixable: 'code',
type: 'problem',
messages: {
failure: '{{message}} (tslint:{{ruleName}})',
},
schema: [
{
type: 'object',
properties: {
rules: {
type: 'object',
/**
* No fixed schema properties for rules, as this would be a permanently moving target
*/
additionalProperties: true,
},
rulesDirectory: {
type: 'array',
items: {
type: 'string',
},
},
lintFile: {
type: 'string',
},
},
additionalProperties: false,
},
],
},
defaultOptions: [{}],
create(
context,
[{ rules: tslintRules, rulesDirectory: tslintRulesDirectory, lintFile }],
) {
const fileName = context.getFilename();
const sourceCode = context.getSourceCode().text;
const services = ESLintUtils.getParserServices(context);
const program = services.program;
/**
* Create an instance of TSLint
* Lint the source code using the configured TSLint instance, and the rules which have been
* passed via the ESLint rule options for this rule (using "tslint/config")
*/
const tslintOptions = {
formatter: 'json',
fix: false,
};
const tslint = new CustomLinter(tslintOptions, program);
const configuration = tslintConfig(
lintFile,
tslintRules,
tslintRulesDirectory,
);
tslint.lint(fileName, sourceCode, configuration);
const result = tslint.getResult();
/**
* Format the TSLint results for ESLint
*/
if (result.failures?.length) {
result.failures.forEach(failure => {
const start = failure.getStartPosition().getLineAndCharacter();
const end = failure.getEndPosition().getLineAndCharacter();
context.report({
messageId: 'failure',
data: {
message: failure.getFailure(),
ruleName: failure.getRuleName(),
},
loc: {
start: {
line: start.line + 1,
column: start.character,
},
end: {
line: end.line + 1,
column: end.character,
},
},
fix: fixer => {
const replacements = failure.getFix();
return Array.isArray(replacements)
? replacements.map(replacement =>
fixer.replaceTextRange(
[replacement.start, replacement.end],
replacement.text,
),
)
: replacements !== undefined
? fixer.replaceTextRange(
[replacements.start, replacements.end],
replacements.text,
)
: [];
},
});
});
}
/**
* Return an empty object for the ESLint rule
*/
return {};
},
});