-
Notifications
You must be signed in to change notification settings - Fork 0
/
Linter.ts
395 lines (359 loc) · 10.4 KB
/
Linter.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/* eslint-disable @typescript-eslint/no-namespace */
import { Linter as ESLintLinter } from 'eslint';
import type { ParserServices, TSESTree } from '../ts-estree';
import type { ParserOptions as TSParserOptions } from './ParserOptions';
import type {
RuleCreateFunction,
RuleFix,
RuleModule,
SharedConfigurationSettings,
} from './Rule';
import type { Scope } from './Scope';
import type { SourceCode } from './SourceCode';
export type MinimalRuleModule<
TMessageIds extends string = string,
TOptions extends readonly unknown[] = [],
> = Partial<Omit<RuleModule<TMessageIds, TOptions>, 'create'>> &
Pick<RuleModule<TMessageIds, TOptions>, 'create'>;
declare class LinterBase {
/**
* Initialize the Linter.
* @param config the config object
*/
constructor(config?: Linter.LinterOptions);
/**
* Define a new parser module
* @param parserId Name of the parser
* @param parserModule The parser object
*/
defineParser(parserId: string, parserModule: Linter.ParserModule): void;
/**
* Defines a new linting rule.
* @param ruleId A unique rule identifier
* @param ruleModule Function from context to object mapping AST node types to event handlers
*/
defineRule<TMessageIds extends string, TOptions extends readonly unknown[]>(
ruleId: string,
ruleModule: MinimalRuleModule<TMessageIds, TOptions> | RuleCreateFunction,
): void;
/**
* Defines many new linting rules.
* @param rulesToDefine map from unique rule identifier to rule
*/
defineRules<TMessageIds extends string, TOptions extends readonly unknown[]>(
rulesToDefine: Record<
string,
| MinimalRuleModule<TMessageIds, TOptions>
| RuleCreateFunction<TMessageIds, TOptions>
>,
): void;
/**
* Gets an object with all loaded rules.
* @returns All loaded rules
*/
getRules(): Map<string, MinimalRuleModule<string, unknown[]>>;
/**
* Gets the `SourceCode` object representing the parsed source.
* @returns The `SourceCode` object.
*/
getSourceCode(): SourceCode;
/**
* Verifies the text against the rules specified by the second argument.
* @param textOrSourceCode The text to parse or a SourceCode object.
* @param config An ESLintConfig instance to configure everything.
* @param filenameOrOptions The optional filename of the file being checked.
* If this is not set, the filename will default to '<input>' in the rule context.
* If this is an object, then it has "filename", "allowInlineConfig", and some properties.
* @returns The results as an array of messages or an empty array if no messages.
*/
verify(
textOrSourceCode: SourceCode | string,
config: Linter.Config,
filenameOrOptions?: Linter.VerifyOptions | string,
): Linter.LintMessage[];
/**
* Performs multiple autofix passes over the text until as many fixes as possible have been applied.
* @param code The source text to apply fixes to.
* @param config The ESLint config object to use.
* @param options The ESLint options object to use.
* @returns The result of the fix operation as returned from the SourceCodeFixer.
*/
verifyAndFix(
code: string,
config: Linter.Config,
options: Linter.FixOptions,
): Linter.FixReport;
/**
* The version from package.json.
*/
readonly version: string;
////////////////////
// static members //
////////////////////
/**
* The version from package.json.
*/
static readonly version: string;
}
namespace Linter {
export interface LinterOptions {
/**
* path to a directory that should be considered as the current working directory.
*/
cwd?: string;
}
export type Severity = 0 | 1 | 2;
export type SeverityString = 'error' | 'off' | 'warn';
export type RuleLevel = Severity | SeverityString;
export type RuleLevelAndOptions = [RuleLevel, ...unknown[]];
export type RuleEntry = RuleLevel | RuleLevelAndOptions;
export type RulesRecord = Partial<Record<string, RuleEntry>>;
export type GlobalVariableOptionBase = 'off' | 'readonly' | 'writable';
export type GlobalVariableOption = GlobalVariableOptionBase | boolean;
export type GlobalsConfig = Record<string, GlobalVariableOption>;
export type EnvironmentConfig = Record<string, boolean>;
// https://github.com/eslint/eslint/blob/v6.8.0/conf/config-schema.js
interface BaseConfig {
$schema?: string;
/**
* The environment settings.
*/
env?: EnvironmentConfig;
/**
* The path to other config files or the package name of shareable configs.
*/
extends?: string[] | string;
/**
* The global variable settings.
*/
globals?: GlobalsConfig;
/**
* The flag that disables directive comments.
*/
noInlineConfig?: boolean;
/**
* The override settings per kind of files.
*/
overrides?: ConfigOverride[];
/**
* The path to a parser or the package name of a parser.
*/
parser?: string;
/**
* The parser options.
*/
parserOptions?: ParserOptions;
/**
* The plugin specifiers.
*/
plugins?: string[];
/**
* The processor specifier.
*/
processor?: string;
/**
* The flag to report unused `eslint-disable` comments.
*/
reportUnusedDisableDirectives?: boolean;
/**
* The rule settings.
*/
rules?: RulesRecord;
/**
* The shared settings.
*/
settings?: SharedConfigurationSettings;
}
export interface ConfigOverride extends BaseConfig {
excludedFiles?: string[] | string;
files: string[] | string;
}
export interface Config extends BaseConfig {
/**
* The glob patterns that ignore to lint.
*/
ignorePatterns?: string[] | string;
/**
* The root flag.
*/
root?: boolean;
}
export type ParserOptions = TSParserOptions;
export interface VerifyOptions {
/**
* Allow/disallow inline comments' ability to change config once it is set. Defaults to true if not supplied.
* Useful if you want to validate JS without comments overriding rules.
*/
allowInlineConfig?: boolean;
/**
* if `true` then the linter doesn't make `fix` properties into the lint result.
*/
disableFixes?: boolean;
/**
* the filename of the source code.
*/
filename?: string;
/**
* the predicate function that selects adopt code blocks.
*/
filterCodeBlock?: (filename: string, text: string) => boolean;
/**
* postprocessor for report messages.
* If provided, this should accept an array of the message lists
* for each code block returned from the preprocessor, apply a mapping to
* the messages as appropriate, and return a one-dimensional array of
* messages.
*/
postprocess?: Processor['postprocess'];
/**
* preprocessor for source text.
* If provided, this should accept a string of source text, and return an array of code blocks to lint.
*/
preprocess?: Processor['preprocess'];
/**
* Adds reported errors for unused `eslint-disable` directives.
*/
reportUnusedDisableDirectives?: SeverityString | boolean;
}
export interface FixOptions extends VerifyOptions {
/**
* Determines whether fixes should be applied.
*/
fix?: boolean;
}
export interface LintSuggestion {
desc: string;
fix: RuleFix;
messageId?: string;
}
export interface LintMessage {
/**
* The 1-based column number.
*/
column: number;
/**
* The 1-based column number of the end location.
*/
endColumn?: number;
/**
* The 1-based line number of the end location.
*/
endLine?: number;
/**
* If `true` then this is a fatal error.
*/
fatal?: true;
/**
* Information for autofix.
*/
fix?: RuleFix;
/**
* The 1-based line number.
*/
line: number;
/**
* The error message.
*/
message: string;
messageId?: string;
nodeType: string;
/**
* The ID of the rule which makes this message.
*/
ruleId: string | null;
/**
* The severity of this message.
*/
severity: Severity;
source: string | null;
/**
* Information for suggestions
*/
suggestions?: LintSuggestion[];
}
export interface FixReport {
/**
* True, if the code was fixed
*/
fixed: boolean;
/**
* Fixed code text (might be the same as input if no fixes were applied).
*/
output: string;
/**
* Collection of all messages for the given code
*/
messages: LintMessage[];
}
export type ParserModule =
| {
parse(text: string, options?: ParserOptions): TSESTree.Program;
}
| {
parseForESLint(
text: string,
options?: ParserOptions,
): ESLintParseResult;
};
export interface ESLintParseResult {
ast: TSESTree.Program;
services?: ParserServices;
scopeManager?: Scope.ScopeManager;
visitorKeys?: SourceCode.VisitorKeys;
}
export interface Processor {
/**
* The function to extract code blocks.
*/
preprocess?: (
text: string,
filename: string,
) => (string | { text: string; filename: string })[];
/**
* The function to merge messages.
*/
postprocess?: (
messagesList: Linter.LintMessage[][],
filename: string,
) => Linter.LintMessage[];
/**
* If `true` then it means the processor supports autofix.
*/
supportsAutofix?: boolean;
}
export interface Environment {
/**
* The definition of global variables.
*/
globals?: Record<string, Linter.Config>;
/**
* The parser options that will be enabled under this environment.
*/
parserOptions?: ParserOptions;
}
export interface Plugin {
/**
* The definition of plugin configs.
*/
configs?: Record<string, Linter.Config>;
/**
* The definition of plugin environments.
*/
environments?: Record<string, Environment>;
/**
* The definition of plugin processors.
*/
processors?: Record<string, Processor>;
/**
* The definition of plugin rules.
*/
rules?: Record<string, RuleCreateFunction | RuleModule<string, unknown[]>>;
}
}
/**
* The Linter object does the actual evaluation of the JavaScript code. It doesn't do any filesystem operations, it
* simply parses and reports on the code. In particular, the Linter object does not process configuration objects
* or files.
*/
class Linter extends (ESLintLinter as typeof LinterBase) {}
export { Linter };