Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 366f23f

Browse files
authored
feat(utils): add types for both flat and legacy eslint classes (typescript-eslint#8861)
1 parent edb3f68 commit 366f23f

File tree

6 files changed

+622
-389
lines changed

6 files changed

+622
-389
lines changed
Lines changed: 7 additions & 386 deletions
Original file line numberDiff line numberDiff line change
@@ -1,390 +1,11 @@
11
/* eslint-disable @typescript-eslint/no-namespace */
22

3-
import { ESLint as ESLintESLint } from 'eslint';
4-
5-
import type { ClassicConfig } from './Config';
6-
import type { Linter } from './Linter';
7-
8-
declare class ESLintBase {
9-
/**
10-
* Creates a new instance of the main ESLint API.
11-
* @param options The options for this instance.
12-
*/
13-
constructor(options?: ESLint.ESLintOptions);
14-
15-
/**
16-
* This method calculates the configuration for a given file, which can be useful for debugging purposes.
17-
* - It resolves and merges extends and overrides settings into the top level configuration.
18-
* - It resolves the parser setting to absolute paths.
19-
* - It normalizes the plugins setting to align short names. (e.g., eslint-plugin-foo → foo)
20-
* - It adds the processor setting if a legacy file extension processor is matched.
21-
* - It doesn't interpret the env setting to the globals and parserOptions settings, so the result object contains
22-
* the env setting as is.
23-
* @param filePath The path to the file whose configuration you would like to calculate. Directory paths are forbidden
24-
* because ESLint cannot handle the overrides setting.
25-
* @returns The promise that will be fulfilled with a configuration object.
26-
*/
27-
calculateConfigForFile(filePath: string): Promise<Linter.ConfigType>;
28-
/**
29-
* This method checks if a given file is ignored by your configuration.
30-
* @param filePath The path to the file you want to check.
31-
* @returns The promise that will be fulfilled with whether the file is ignored or not. If the file is ignored, then
32-
* it will return true.
33-
*/
34-
isPathIgnored(filePath: string): Promise<boolean>;
35-
/**
36-
* This method lints the files that match the glob patterns and then returns the results.
37-
* @param patterns The lint target files. This can contain any of file paths, directory paths, and glob patterns.
38-
* @returns The promise that will be fulfilled with an array of LintResult objects.
39-
*/
40-
lintFiles(patterns: string[] | string): Promise<ESLint.LintResult[]>;
41-
/**
42-
* This method lints the given source code text and then returns the results.
43-
*
44-
* By default, this method uses the configuration that applies to files in the current working directory (the cwd
45-
* constructor option). If you want to use a different configuration, pass options.filePath, and ESLint will load the
46-
* same configuration that eslint.lintFiles() would use for a file at options.filePath.
47-
*
48-
* If the options.filePath value is configured to be ignored, this method returns an empty array. If the
49-
* options.warnIgnored option is set along with the options.filePath option, this method returns a LintResult object.
50-
* In that case, the result may contain a warning that indicates the file was ignored.
51-
* @param code The source code text to check.
52-
* @returns The promise that will be fulfilled with an array of LintResult objects. This is an array (despite there
53-
* being only one lint result) in order to keep the interfaces between this and the eslint.lintFiles()
54-
* method similar.
55-
*/
56-
lintText(
57-
code: string,
58-
options?: ESLint.LintTextOptions,
59-
): Promise<ESLint.LintResult[]>;
60-
/**
61-
* This method loads a formatter. Formatters convert lint results to a human- or machine-readable string.
62-
* @param name TThe path to the file you want to check.
63-
* The following values are allowed:
64-
* - undefined. In this case, loads the "stylish" built-in formatter.
65-
* - A name of built-in formatters.
66-
* - A name of third-party formatters. For examples:
67-
* -- `foo` will load eslint-formatter-foo.
68-
* -- `@foo` will load `@foo/eslint-formatter`.
69-
* -- `@foo/bar` will load `@foo/eslint-formatter-bar`.
70-
* - A path to the file that defines a formatter. The path must contain one or more path separators (/) in order to distinguish if it's a path or not. For example, start with ./.
71-
* @returns The promise that will be fulfilled with a Formatter object.
72-
*/
73-
loadFormatter(name?: string): Promise<ESLint.Formatter>;
74-
75-
////////////////////
76-
// static members //
77-
////////////////////
78-
79-
/**
80-
* This method copies the given results and removes warnings. The returned value contains only errors.
81-
* @param results The LintResult objects to filter.
82-
* @returns The filtered LintResult objects.
83-
*/
84-
static getErrorResults(results: ESLint.LintResult): ESLint.LintResult;
85-
/**
86-
* This method writes code modified by ESLint's autofix feature into its respective file. If any of the modified
87-
* files don't exist, this method does nothing.
88-
* @param results The LintResult objects to write.
89-
* @returns The promise that will be fulfilled after all files are written.
90-
*/
91-
static outputFixes(results: ESLint.LintResult[]): Promise<void>;
3+
export {
4+
// TODO - remove this in the next major
925
/**
93-
* The version text.
6+
* @deprecated - use FlatESLint or LegacyESLint instead
947
*/
95-
static readonly version: string;
96-
}
97-
98-
namespace ESLint {
99-
export interface ESLintOptions {
100-
/**
101-
* If false is present, ESLint suppresses directive comments in source code.
102-
* If this option is false, it overrides the noInlineConfig setting in your configurations.
103-
*/
104-
allowInlineConfig?: boolean;
105-
/**
106-
* Configuration object, extended by all configurations used with this instance.
107-
* You can use this option to define the default settings that will be used if your configuration files don't
108-
* configure it.
109-
*/
110-
baseConfig?: Linter.ConfigType | null;
111-
/**
112-
* If true is present, the eslint.lintFiles() method caches lint results and uses it if each target file is not
113-
* changed. Please mind that ESLint doesn't clear the cache when you upgrade ESLint plugins. In that case, you have
114-
* to remove the cache file manually. The eslint.lintText() method doesn't use caches even if you pass the
115-
* options.filePath to the method.
116-
*/
117-
cache?: boolean;
118-
/**
119-
* The eslint.lintFiles() method writes caches into this file.
120-
*/
121-
cacheLocation?: string;
122-
/**
123-
* The working directory. This must be an absolute path.
124-
*/
125-
cwd?: string;
126-
/**
127-
* Unless set to false, the eslint.lintFiles() method will throw an error when no target files are found.
128-
*/
129-
errorOnUnmatchedPattern?: boolean;
130-
/**
131-
* If you pass directory paths to the eslint.lintFiles() method, ESLint checks the files in those directories that
132-
* have the given extensions. For example, when passing the src/ directory and extensions is [".js", ".ts"], ESLint
133-
* will lint *.js and *.ts files in src/. If extensions is null, ESLint checks *.js files and files that match
134-
* overrides[].files patterns in your configuration.
135-
* Note: This option only applies when you pass directory paths to the eslint.lintFiles() method.
136-
* If you pass glob patterns, ESLint will lint all files matching the glob pattern regardless of extension.
137-
*/
138-
extensions?: string[] | null;
139-
/**
140-
* If true is present, the eslint.lintFiles() and eslint.lintText() methods work in autofix mode.
141-
* If a predicate function is present, the methods pass each lint message to the function, then use only the
142-
* lint messages for which the function returned true.
143-
*/
144-
fix?: boolean | ((message: ESLint.LintMessage) => boolean);
145-
/**
146-
* The types of the rules that the eslint.lintFiles() and eslint.lintText() methods use for autofix.
147-
*/
148-
fixTypes?: ('directive' | 'layout' | 'problem' | 'suggestion')[] | null;
149-
/**
150-
* If false is present, the eslint.lintFiles() method doesn't interpret glob patterns.
151-
*/
152-
globInputPaths?: boolean;
153-
/**
154-
* If false is present, the eslint.lintFiles() method doesn't respect `.eslintignore` files or ignorePatterns in
155-
* your configuration.
156-
*/
157-
ignore?: boolean;
158-
/**
159-
* The path to a file ESLint uses instead of `$CWD/.eslintignore`.
160-
* If a path is present and the file doesn't exist, this constructor will throw an error.
161-
*/
162-
ignorePath?: string;
163-
/**
164-
* Configuration object, overrides all configurations used with this instance.
165-
* You can use this option to define the settings that will be used even if your configuration files configure it.
166-
*/
167-
overrideConfig?: ClassicConfig.ConfigOverride | null;
168-
/**
169-
* The path to a configuration file, overrides all configurations used with this instance.
170-
* The options.overrideConfig option is applied after this option is applied.
171-
*/
172-
overrideConfigFile?: string | null;
173-
/**
174-
* The plugin implementations that ESLint uses for the plugins setting of your configuration.
175-
* This is a map-like object. Those keys are plugin IDs and each value is implementation.
176-
*/
177-
plugins?: Record<string, Linter.Plugin> | null;
178-
/**
179-
* The severity to report unused eslint-disable directives.
180-
* If this option is a severity, it overrides the reportUnusedDisableDirectives setting in your configurations.
181-
*/
182-
reportUnusedDisableDirectives?: Linter.SeverityString | null;
183-
/**
184-
* The path to a directory where plugins should be resolved from.
185-
* If null is present, ESLint loads plugins from the location of the configuration file that contains the plugin
186-
* setting.
187-
* If a path is present, ESLint loads all plugins from there.
188-
*/
189-
resolvePluginsRelativeTo?: string | null;
190-
/**
191-
* An array of paths to directories to load custom rules from.
192-
*/
193-
rulePaths?: string[];
194-
/**
195-
* If false is present, ESLint doesn't load configuration files (.eslintrc.* files).
196-
* Only the configuration of the constructor options is valid.
197-
*/
198-
useEslintrc?: boolean;
199-
}
200-
201-
export interface DeprecatedRuleInfo {
202-
/**
203-
* The rule ID.
204-
*/
205-
ruleId: string;
206-
/**
207-
* The rule IDs that replace this deprecated rule.
208-
*/
209-
replacedBy: string[];
210-
}
211-
212-
/**
213-
* The LintResult value is the information of the linting result of each file.
214-
*/
215-
export interface LintResult {
216-
/**
217-
* The number of errors. This includes fixable errors.
218-
*/
219-
errorCount: number;
220-
/**
221-
* The number of fatal errors.
222-
*/
223-
fatalErrorCount: number;
224-
/**
225-
* The absolute path to the file of this result. This is the string "<text>" if the file path is unknown (when you
226-
* didn't pass the options.filePath option to the eslint.lintText() method).
227-
*/
228-
filePath: string;
229-
/**
230-
* The number of errors that can be fixed automatically by the fix constructor option.
231-
*/
232-
fixableErrorCount: number;
233-
/**
234-
* The number of warnings that can be fixed automatically by the fix constructor option.
235-
*/
236-
fixableWarningCount: number;
237-
/**
238-
* The array of LintMessage objects.
239-
*/
240-
messages: ESLint.LintMessage[];
241-
/**
242-
* The source code of the file that was linted, with as many fixes applied as possible.
243-
*/
244-
output?: string;
245-
/**
246-
* The original source code text. This property is undefined if any messages didn't exist or the output
247-
* property exists.
248-
*/
249-
source?: string;
250-
/**
251-
* The array of SuppressedLintMessage objects.
252-
*/
253-
suppressedMessages: SuppressedLintMessage[];
254-
/**
255-
* The information about the deprecated rules that were used to check this file.
256-
*/
257-
usedDeprecatedRules: DeprecatedRuleInfo[];
258-
/**
259-
* The number of warnings. This includes fixable warnings.
260-
*/
261-
warningCount: number;
262-
}
263-
264-
export interface LintTextOptions {
265-
/**
266-
* The path to the file of the source code text. If omitted, the result.filePath becomes the string "<text>".
267-
*/
268-
filePath?: string;
269-
/**
270-
* If true is present and the options.filePath is a file ESLint should ignore, this method returns a lint result
271-
* contains a warning message.
272-
*/
273-
warnIgnored?: boolean;
274-
}
275-
276-
/**
277-
* The LintMessage value is the information of each linting error.
278-
*/
279-
export interface LintMessage {
280-
/**
281-
* The 1-based column number of the begin point of this message.
282-
*/
283-
column: number | undefined;
284-
/**
285-
* The 1-based column number of the end point of this message. This property is undefined if this message
286-
* is not a range.
287-
*/
288-
endColumn: number | undefined;
289-
/**
290-
* The 1-based line number of the end point of this message. This property is undefined if this
291-
* message is not a range.
292-
*/
293-
endLine: number | undefined;
294-
/**
295-
* `true` if this is a fatal error unrelated to a rule, like a parsing error.
296-
*/
297-
fatal?: boolean | undefined;
298-
/**
299-
* The EditInfo object of autofix. This property is undefined if this message is not fixable.
300-
*/
301-
fix: EditInfo | undefined;
302-
/**
303-
* The 1-based line number of the begin point of this message.
304-
*/
305-
line: number | undefined;
306-
/**
307-
* The error message
308-
*/
309-
message: string;
310-
/**
311-
* The rule name that generates this lint message. If this message is generated by the ESLint core rather than
312-
* rules, this is null.
313-
*/
314-
ruleId: string | null;
315-
/**
316-
* The severity of this message. 1 means warning and 2 means error.
317-
*/
318-
severity: 1 | 2;
319-
/**
320-
* The list of suggestions. Each suggestion is the pair of a description and an EditInfo object to fix code. API
321-
* users such as editor integrations can choose one of them to fix the problem of this message. This property is
322-
* undefined if this message doesn't have any suggestions.
323-
*/
324-
suggestions:
325-
| {
326-
desc: string;
327-
fix: EditInfo;
328-
}[]
329-
| undefined;
330-
}
331-
332-
/**
333-
* The SuppressedLintMessage value is the information of each suppressed linting error.
334-
*/
335-
export interface SuppressedLintMessage extends ESLint.LintMessage {
336-
/**
337-
* The list of suppressions.
338-
*/
339-
suppressions?: {
340-
/**
341-
* Right now, this is always `directive`
342-
*/
343-
kind: string;
344-
/**
345-
* The free text description added after the `--` in the comment
346-
*/
347-
justification: string;
348-
}[];
349-
}
350-
351-
/**
352-
* The EditInfo value is information to edit text.
353-
*
354-
* This edit information means replacing the range of the range property by the text property value. It's like
355-
* sourceCodeText.slice(0, edit.range[0]) + edit.text + sourceCodeText.slice(edit.range[1]). Therefore, it's an add
356-
* if the range[0] and range[1] property values are the same value, and it's removal if the text property value is
357-
* empty string.
358-
*/
359-
export interface EditInfo {
360-
/**
361-
* The pair of 0-based indices in source code text to remove.
362-
*/
363-
range: [number, number];
364-
/**
365-
* The text to add.
366-
*/
367-
text: string;
368-
}
369-
370-
/**
371-
* The Formatter value is the object to convert the LintResult objects to text.
372-
*/
373-
export interface Formatter {
374-
/**
375-
* The method to convert the LintResult objects to text.
376-
* Promise return supported since 8.4.0
377-
*/
378-
format(results: LintResult[]): Promise<string> | string;
379-
}
380-
}
381-
382-
/**
383-
* The ESLint class is the primary class to use in Node.js applications.
384-
* This class depends on the Node.js fs module and the file system, so you cannot use it in browsers.
385-
*
386-
* If you want to lint code on browsers, use the Linter class instead.
387-
*/
388-
class ESLint extends (ESLintESLint as typeof ESLintBase) {}
389-
390-
export { ESLint };
8+
LegacyESLint as ESLint,
9+
} from './eslint/LegacyESLint';
10+
export { FlatESLint } from './eslint/FlatESLint';
11+
export { LegacyESLint } from './eslint/LegacyESLint';

0 commit comments

Comments
 (0)