Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(utils): update eslint types #4896

Merged
merged 3 commits into from May 3, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
57 changes: 51 additions & 6 deletions packages/utils/src/ts-eslint/ESLint.ts
Expand Up @@ -140,11 +140,11 @@ namespace ESLint {
* If a predicate function is present, the methods pass each lint message to the function, then use only the
* lint messages for which the function returned true.
*/
fix?: boolean | ((message: LintMessage) => boolean);
fix?: boolean | ((message: ESLint.LintMessage) => boolean);
/**
* The types of the rules that the eslint.lintFiles() and eslint.lintText() methods use for autofix.
*/
fixTypes?: string[];
fixTypes?: ('directive' | 'problem' | 'suggestion' | 'layout')[] | null;
/**
* If false is present, the eslint.lintFiles() method doesn't interpret glob patterns.
*/
Expand Down Expand Up @@ -216,6 +216,11 @@ namespace ESLint {
* The number of errors. This includes fixable errors.
*/
errorCount: number;
/**
* The number of fatal errors.
* @since 7.32.0
*/
fatalErrorCount?: number;
/**
* The absolute path to the file of this result. This is the string "<text>" if the file path is unknown (when you
* didn't pass the options.filePath option to the eslint.lintText() method).
Expand All @@ -232,7 +237,7 @@ namespace ESLint {
/**
* The array of LintMessage objects.
*/
messages: Linter.LintMessage[];
messages: ESLint.LintMessage[];
/**
* The source code of the file that was linted, with as many fixes applied as possible.
*/
Expand All @@ -242,6 +247,12 @@ namespace ESLint {
* property exists.
*/
source?: string;
/**
* The array of SuppressedLintMessage objects.
*
* @since 8.8.0
*/
suppressedMessages?: SuppressedLintMessage[];
/**
* The information about the deprecated rules that were used to check this file.
*/
Expand Down Expand Up @@ -271,7 +282,7 @@ namespace ESLint {
/**
* The 1-based column number of the begin point of this message.
*/
column: number;
column: number | undefined;
/**
* The 1-based column number of the end point of this message. This property is undefined if this message
* is not a range.
Expand All @@ -282,14 +293,19 @@ namespace ESLint {
* message is not a range.
*/
endLine: number | undefined;
/**
* `true` if this is a fatal error unrelated to a rule, like a parsing error.
* @since 7.24.0
*/
fatal?: boolean | undefined;
/**
* The EditInfo object of autofix. This property is undefined if this message is not fixable.
*/
fix: EditInfo | undefined;
/**
* The 1-based line number of the begin point of this message.
*/
line: number;
line: number | undefined;
/**
* The error message
*/
Expand All @@ -308,7 +324,31 @@ namespace ESLint {
* users such as editor integrations can choose one of them to fix the problem of this message. This property is
* undefined if this message doesn't have any suggestions.
*/
suggestions: { desc: string; fix: EditInfo }[] | undefined;
suggestions:
| {
desc: string;
fix: EditInfo;
}[]
| undefined;
}

/**
* The SuppressedLintMessage value is the information of each suppressed linting error.
*/
export interface SuppressedLintMessage extends ESLint.LintMessage {
/**
* The list of suppressions.
*/
suppressions?: {
/**
* Right now, this is always `directive`
*/
kind: string;
/**
* The free text description added after the `--` in the comment
*/
justification: string;
}[];
}

/**
Expand Down Expand Up @@ -338,6 +378,11 @@ namespace ESLint {
* The method to convert the LintResult objects to text
*/
format(results: LintResult[]): string;
/**
* The method to asynchronously convert the LintResult objects to text.
* @since 8.4.0
*/
format(results: LintResult[]): Promise<string>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could be wrong, but does this actually work? Can you have overloads that differ only by return types? My test suggests TS never picks up this overload

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this should apparently be a union: string | Promise<string>. Which I find weird but 🤷 compatibility.

eslint/eslint#15242
eslint/eslint#15243
https://eslint.org/docs/developer-guide/nodejs-api#-loadedformatter-type

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was kind of hoping that TS would be smart here and merge the return value type.

I wanted to document the promise-based return separately as it is only accepted in a later version of ESLint.

But if TS won't let me, I'll just merge them together and describe it in the JSDoc 😢

}
}

Expand Down
2 changes: 0 additions & 2 deletions packages/utils/src/ts-eslint/Rule.ts
Expand Up @@ -221,7 +221,6 @@ interface RuleContext<
/**
* Returns the current working directory passed to Linter.
* It is a path to a directory that should be considered as the current working directory.
* This was added in v6.6.0
* @since 6.6.0
*/
getCwd?(): string;
Expand All @@ -233,7 +232,6 @@ interface RuleContext<

/**
* Returns the full path of the file on disk without any code block information (unlike `getFilename()`).
* This was added in v7.28.0
* @since 7.28.0
*/
getPhysicalFilename?(): string;
Expand Down
2 changes: 2 additions & 0 deletions packages/utils/src/ts-eslint/RuleTester.ts
Expand Up @@ -11,6 +11,7 @@ import {
interface ValidTestCase<TOptions extends Readonly<unknown[]>> {
/**
* Name for the test case.
* @since 8.1.0
*/
readonly name?: string;
/**
Expand Down Expand Up @@ -47,6 +48,7 @@ interface ValidTestCase<TOptions extends Readonly<unknown[]>> {
readonly settings?: Readonly<SharedConfigurationSettings>;
/**
* Run this case exclusively for debugging in supported test frameworks.
* @since 7.29.0
*/
readonly only?: boolean;
}
Expand Down
1 change: 0 additions & 1 deletion packages/utils/src/ts-eslint/SourceCode.ts
Expand Up @@ -288,7 +288,6 @@ declare class SourceCodeBase extends TokenStore {
* Determines if two nodes or tokens have at least one whitespace character
* between them. Order does not matter. Returns false if the given nodes or
* tokens overlap.
* This was added in v6.7.0.
* @since 6.7.0
* @param first The first node or token to check between.
* @param second The second node or token to check between.
Expand Down