/
Linter.ts
136 lines (114 loc) · 3.29 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
/* eslint-disable @typescript-eslint/no-namespace */
import { Linter as ESLintLinter } from 'eslint';
import { TSESTree, ParserServices } from '../ts-estree';
import { ParserOptions as TSParserOptions } from './ParserOptions';
import { RuleModule, RuleFix } from './Rule';
import { Scope } from './Scope';
import { SourceCode } from './SourceCode';
interface Linter {
version: string;
verify(
code: SourceCode | string,
config: Linter.Config,
filename?: string,
): Linter.LintMessage[];
verify(
code: SourceCode | string,
config: Linter.Config,
options: Linter.LintOptions,
): Linter.LintMessage[];
verifyAndFix(
code: string,
config: Linter.Config,
filename?: string,
): Linter.FixReport;
verifyAndFix(
code: string,
config: Linter.Config,
options: Linter.FixOptions,
): Linter.FixReport;
getSourceCode(): SourceCode;
defineRule<TMessageIds extends string, TOptions extends readonly unknown[]>(
name: string,
rule: {
meta?: RuleModule<TMessageIds, TOptions>['meta'];
create: RuleModule<TMessageIds, TOptions>['create'];
},
): void;
defineRules<TMessageIds extends string, TOptions extends readonly unknown[]>(
rules: Record<string, RuleModule<TMessageIds, TOptions>>,
): void;
getRules<
TMessageIds extends string,
TOptions extends readonly unknown[]
>(): Map<string, RuleModule<TMessageIds, TOptions>>;
defineParser(name: string, parser: Linter.ParserModule): void;
}
namespace Linter {
export type Severity = 0 | 1 | 2;
export type RuleLevel = Severity | 'off' | 'warn' | 'error';
export type RuleLevelAndOptions = [RuleLevel, ...unknown[]];
export interface Config {
rules?: {
[name: string]: RuleLevel | RuleLevelAndOptions;
};
parser?: string;
parserOptions?: ParserOptions;
settings?: { [name: string]: unknown };
env?: { [name: string]: boolean };
globals?: { [name: string]: boolean };
}
export type ParserOptions = TSParserOptions;
export interface LintOptions {
filename?: string;
preprocess?: (code: string) => string[];
postprocess?: (problemLists: LintMessage[][]) => LintMessage[];
allowInlineConfig?: boolean;
reportUnusedDisableDirectives?: boolean;
}
export interface LintSuggestion {
desc: string;
fix: RuleFix;
messageId?: string;
}
export interface LintMessage {
column: number;
line: number;
endColumn?: number;
endLine?: number;
ruleId: string | null;
message: string;
messageId?: string;
nodeType: string;
fatal?: true;
severity: Severity;
fix?: RuleFix;
source: string | null;
suggestions?: LintSuggestion[];
}
export interface FixOptions extends LintOptions {
fix?: boolean;
}
export interface FixReport {
fixed: boolean;
output: string;
messages: LintMessage[];
}
export type ParserModule =
| {
parse(text: string, options?: unknown): TSESTree.Program;
}
| {
parseForESLint(text: string, options?: unknown): ESLintParseResult;
};
export interface ESLintParseResult {
ast: TSESTree.Program;
parserServices?: ParserServices;
scopeManager?: Scope.ScopeManager;
visitorKeys?: SourceCode.VisitorKeys;
}
}
const Linter = ESLintLinter as {
new (): Linter;
};
export { Linter };