UNPKG

10.8 kBTypeScriptView Raw
1import { TSESTree, ParserServices } from '../ts-estree';
2import { ParserOptions as TSParserOptions } from './ParserOptions';
3import { RuleCreateFunction, RuleFix, RuleModule } from './Rule';
4import { Scope } from './Scope';
5import { SourceCode } from './SourceCode';
6declare class LinterBase {
7 /**
8 * Initialize the Linter.
9 * @param config the config object
10 */
11 constructor(config?: Linter.LinterOptions);
12 /**
13 * Define a new parser module
14 * @param parserId Name of the parser
15 * @param parserModule The parser object
16 */
17 defineParser(parserId: string, parserModule: Linter.ParserModule): void;
18 /**
19 * Defines a new linting rule.
20 * @param ruleId A unique rule identifier
21 * @param ruleModule Function from context to object mapping AST node types to event handlers
22 */
23 defineRule<TMessageIds extends string, TOptions extends readonly unknown[]>(ruleId: string, ruleModule: RuleModule<TMessageIds, TOptions> | RuleCreateFunction): void;
24 /**
25 * Defines many new linting rules.
26 * @param rulesToDefine map from unique rule identifier to rule
27 */
28 defineRules<TMessageIds extends string, TOptions extends readonly unknown[]>(rulesToDefine: Record<string, RuleModule<TMessageIds, TOptions> | RuleCreateFunction>): void;
29 /**
30 * Gets an object with all loaded rules.
31 * @returns All loaded rules
32 */
33 getRules(): Map<string, RuleModule<string, unknown[]>>;
34 /**
35 * Gets the `SourceCode` object representing the parsed source.
36 * @returns The `SourceCode` object.
37 */
38 getSourceCode(): SourceCode;
39 /**
40 * Verifies the text against the rules specified by the second argument.
41 * @param textOrSourceCode The text to parse or a SourceCode object.
42 * @param config An ESLintConfig instance to configure everything.
43 * @param filenameOrOptions The optional filename of the file being checked.
44 * If this is not set, the filename will default to '<input>' in the rule context.
45 * If this is an object, then it has "filename", "allowInlineConfig", and some properties.
46 * @returns The results as an array of messages or an empty array if no messages.
47 */
48 verify(textOrSourceCode: SourceCode | string, config: Linter.Config, filenameOrOptions?: string | Linter.VerifyOptions): Linter.LintMessage[];
49 /**
50 * Performs multiple autofix passes over the text until as many fixes as possible have been applied.
51 * @param text The source text to apply fixes to.
52 * @param config The ESLint config object to use.
53 * @param options The ESLint options object to use.
54 * @returns The result of the fix operation as returned from the SourceCodeFixer.
55 */
56 verifyAndFix(code: string, config: Linter.Config, options: Linter.FixOptions): Linter.FixReport;
57 /**
58 * The version from package.json.
59 */
60 readonly version: string;
61 /**
62 * The version from package.json.
63 */
64 static readonly version: string;
65}
66declare namespace Linter {
67 export interface LinterOptions {
68 /**
69 * path to a directory that should be considered as the current working directory.
70 */
71 cwd?: string;
72 }
73 export type Severity = 0 | 1 | 2;
74 export type SeverityString = 'off' | 'warn' | 'error';
75 export type RuleLevel = Severity | SeverityString;
76 export type RuleLevelAndOptions = [RuleLevel, ...unknown[]];
77 export type RuleEntry = RuleLevel | RuleLevelAndOptions;
78 export type RulesRecord = Partial<Record<string, RuleEntry>>;
79 interface BaseConfig {
80 $schema?: string;
81 /**
82 * The environment settings.
83 */
84 env?: {
85 [name: string]: boolean;
86 };
87 /**
88 * The path to other config files or the package name of shareable configs.
89 */
90 extends?: string | string[];
91 /**
92 * The global variable settings.
93 */
94 globals?: {
95 [name: string]: boolean;
96 };
97 /**
98 * The flag that disables directive comments.
99 */
100 noInlineConfig?: boolean;
101 /**
102 * The override settings per kind of files.
103 */
104 overrides?: ConfigOverride[];
105 /**
106 * The path to a parser or the package name of a parser.
107 */
108 parser?: string;
109 /**
110 * The parser options.
111 */
112 parserOptions?: ParserOptions;
113 /**
114 * The plugin specifiers.
115 */
116 plugins?: string[];
117 /**
118 * The processor specifier.
119 */
120 processor?: string;
121 /**
122 * The flag to report unused `eslint-disable` comments.
123 */
124 reportUnusedDisableDirectives?: boolean;
125 /**
126 * The rule settings.
127 */
128 rules?: RulesRecord;
129 /**
130 * The shared settings.
131 */
132 settings?: {
133 [name: string]: unknown;
134 };
135 }
136 export interface ConfigOverride extends BaseConfig {
137 excludedFiles?: string | string[];
138 files: string | string[];
139 }
140 export interface Config extends BaseConfig {
141 /**
142 * The glob patterns that ignore to lint.
143 */
144 ignorePatterns?: string | string[];
145 /**
146 * The root flag.
147 */
148 root?: boolean;
149 }
150 export type ParserOptions = TSParserOptions;
151 export interface VerifyOptions {
152 /**
153 * Allow/disallow inline comments' ability to change config once it is set. Defaults to true if not supplied.
154 * Useful if you want to validate JS without comments overriding rules.
155 */
156 allowInlineConfig?: boolean;
157 /**
158 * if `true` then the linter doesn't make `fix` properties into the lint result.
159 */
160 disableFixes?: boolean;
161 /**
162 * the filename of the source code.
163 */
164 filename?: string;
165 /**
166 * the predicate function that selects adopt code blocks.
167 */
168 filterCodeBlock?: (filename: string, text: string) => boolean;
169 /**
170 * postprocessor for report messages.
171 * If provided, this should accept an array of the message lists
172 * for each code block returned from the preprocessor, apply a mapping to
173 * the messages as appropriate, and return a one-dimensional array of
174 * messages.
175 */
176 postprocess?: Processor['postprocess'];
177 /**
178 * preprocessor for source text.
179 * If provided, this should accept a string of source text, and return an array of code blocks to lint.
180 */
181 preprocess?: Processor['preprocess'];
182 /**
183 * Adds reported errors for unused `eslint-disable` directives.
184 */
185 reportUnusedDisableDirectives?: boolean | SeverityString;
186 }
187 export interface FixOptions extends VerifyOptions {
188 /**
189 * Determines whether fixes should be applied.
190 */
191 fix?: boolean;
192 }
193 export interface LintSuggestion {
194 desc: string;
195 fix: RuleFix;
196 messageId?: string;
197 }
198 export interface LintMessage {
199 /**
200 * The 1-based column number.
201 */
202 column: number;
203 /**
204 * The 1-based column number of the end location.
205 */
206 endColumn?: number;
207 /**
208 * The 1-based line number of the end location.
209 */
210 endLine?: number;
211 /**
212 * If `true` then this is a fatal error.
213 */
214 fatal?: true;
215 /**
216 * Information for autofix.
217 */
218 fix?: RuleFix;
219 /**
220 * The 1-based line number.
221 */
222 line: number;
223 /**
224 * The error message.
225 */
226 message: string;
227 messageId?: string;
228 nodeType: string;
229 /**
230 * The ID of the rule which makes this message.
231 */
232 ruleId: string | null;
233 /**
234 * The severity of this message.
235 */
236 severity: Severity;
237 source: string | null;
238 /**
239 * Information for suggestions
240 */
241 suggestions?: LintSuggestion[];
242 }
243 export interface FixReport {
244 /**
245 * True, if the code was fixed
246 */
247 fixed: boolean;
248 /**
249 * Fixed code text (might be the same as input if no fixes were applied).
250 */
251 output: string;
252 /**
253 * Collection of all messages for the given code
254 */
255 messages: LintMessage[];
256 }
257 export type ParserModule = {
258 parse(text: string, options?: ParserOptions): TSESTree.Program;
259 } | {
260 parseForESLint(text: string, options?: ParserOptions): ESLintParseResult;
261 };
262 export interface ESLintParseResult {
263 ast: TSESTree.Program;
264 parserServices?: ParserServices;
265 scopeManager?: Scope.ScopeManager;
266 visitorKeys?: SourceCode.VisitorKeys;
267 }
268 export interface Processor {
269 /**
270 * The function to extract code blocks.
271 */
272 preprocess?: (text: string, filename: string) => Array<string | {
273 text: string;
274 filename: string;
275 }>;
276 /**
277 * The function to merge messages.
278 */
279 postprocess?: (messagesList: Linter.LintMessage[][], filename: string) => Linter.LintMessage[];
280 /**
281 * If `true` then it means the processor supports autofix.
282 */
283 supportsAutofix?: boolean;
284 }
285 export interface Environment {
286 /**
287 * The definition of global variables.
288 */
289 globals?: Record<string, Linter.Config>;
290 /**
291 * The parser options that will be enabled under this environment.
292 */
293 parserOptions?: ParserOptions;
294 }
295 export interface Plugin {
296 /**
297 * The definition of plugin configs.
298 */
299 configs?: Record<string, Linter.Config>;
300 /**
301 * The definition of plugin environments.
302 */
303 environments?: Record<string, Environment>;
304 /**
305 * The definition of plugin processors.
306 */
307 processors?: Record<string, Processor>;
308 /**
309 * The definition of plugin rules.
310 */
311 rules?: Record<string, RuleCreateFunction | RuleModule<string, unknown[]>>;
312 }
313 export {};
314}
315declare const Linter_base: typeof LinterBase;
316/**
317 * The Linter object does the actual evaluation of the JavaScript code. It doesn't do any filesystem operations, it
318 * simply parses and reports on the code. In particular, the Linter object does not process configuration objects
319 * or files.
320 */
321declare class Linter extends Linter_base {
322}
323export { Linter };
324//# sourceMappingURL=Linter.d.ts.map
\No newline at end of file