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

Support built-in type definitions in ESLint@9 and @types/esllint@9 #367

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@
"@mizdra/prettier-config-mizdra": "^1.0.0",
"@tsconfig/node18": "^18.2.2",
"@tsconfig/strictest": "^2.0.2",
"@types/eslint": "^8.44.6",
"@types/estraverse": "^5.1.6",
"@types/estree": "^1.0.5",
"@types/eslint": "^9.6.1",
"@types/estraverse": "^5.1.7",
"@types/estree": "^1.0.6",
"@types/fs-extra": "^11.0.4",
"@types/node": "^20.8.10",
"@types/yargs": "^17.0.29",
Expand Down Expand Up @@ -93,10 +93,5 @@
"!src/**/__snapshots__",
"dist",
"static"
],
"pnpm": {
"patchedDependencies": {
"@types/[email protected]": "patches/@[email protected]"
}
}
]
}
89 changes: 0 additions & 89 deletions patches/@[email protected]

This file was deleted.

34 changes: 14 additions & 20 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { LegacyESLint, FlatESLint } from 'eslint/use-at-your-own-risk';
import type { ESLint } from 'eslint';
import { cliOptionsDefaults, ParsedCLIOptions } from './cli/parse-argv.js';
import { DeepPartial } from './util/type-check.js';
type LegacyESLintOptions = { type: 'eslintrc' } & Pick<
LegacyESLint.Options,
ESLint.LegacyOptions,
| 'useEslintrc'
| 'overrideConfigFile'
| 'extensions'
Expand All @@ -15,7 +15,7 @@ type LegacyESLintOptions = { type: 'eslintrc' } & Pick<
| 'resolvePluginsRelativeTo'
>;
type FlatESLintOptions = { type: 'flat' } & Pick<
FlatESLint.Options,
ESLint.Options,
'overrideConfigFile' | 'cache' | 'cacheLocation' | 'overrideConfig' | 'cwd'
>;

Expand Down
40 changes: 18 additions & 22 deletions src/core.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { writeFile } from 'node:fs/promises';
import { fileURLToPath } from 'node:url';
import { ESLint, Linter, Rule } from 'eslint';
import eslintPkg, { LegacyESLint as LegacyESLintNS } from 'eslint/use-at-your-own-risk';
import eslintPkg from 'eslint/use-at-your-own-risk';
import isInstalledGlobally from 'is-installed-globally';
import { DescriptionPosition } from './cli/prompt.js';
import { Config, NormalizedConfig, normalizeConfig } from './config.js';
Expand All @@ -27,7 +27,7 @@
* @param resultsOfLint The results of lint.
* @returns The results to undo.
*/
function generateResultsToUndo(resultsOfLint: LegacyESLintNS.LintResult[]): LegacyESLintNS.LintResult[] {
function generateResultsToUndo(resultsOfLint: ESLint.LintResult[]): ESLint.LintResult[] {
return resultsOfLint.map((resultOfLint) => {
// NOTE: THIS IS HACK.
return { ...resultOfLint, output: resultOfLint.source };
Expand All @@ -42,7 +42,7 @@
*/
export class Core {
readonly config: NormalizedConfig;
readonly eslint: LegacyESLintNS;
readonly eslint: InstanceType<typeof LegacyESLint | typeof FlatESLint>;

constructor(config: Config) {
this.config = normalizeConfig(config);
Expand Down Expand Up @@ -80,7 +80,7 @@
* @param results The lint results of the project to print summary
* @param ruleIds The rule ids to print details
*/
async formatResultDetails(results: LegacyESLintNS.LintResult[], ruleIds: (string | null)[]): Promise<string> {
async formatResultDetails(results: ESLint.LintResult[], ruleIds: (string | null)[]): Promise<string> {
const formatterName = this.config.formatterName;

// When eslint-interactive is installed globally, eslint-formatter-codeframe will also be installed globally.
Expand All @@ -91,14 +91,15 @@
: formatterName;

const formatter = await this.eslint.loadFormatter(resolvedFormatterNameOrPath);
return formatter.format(filterResultsByRuleId(results, ruleIds));
const rulesMeta = this.eslint.getRulesMetaForResults(results);
return formatter.format(filterResultsByRuleId(results, ruleIds), { rulesMeta, cwd: this.config.cwd });

Check failure on line 95 in src/core.ts

View workflow job for this annotation

GitHub Actions / e2e (18, 9, ubuntu-latest)

Argument of type '{ rulesMeta: { [ruleId: string]: Rule.RuleMetaData; }; cwd: string; }' is not assignable to parameter of type 'ResultsMeta'.

Check failure on line 95 in src/core.ts

View workflow job for this annotation

GitHub Actions / e2e (18, 9, macos-latest)

Argument of type '{ rulesMeta: { [ruleId: string]: Rule.RuleMetaData; }; cwd: string; }' is not assignable to parameter of type 'ResultsMeta'.

Check failure on line 95 in src/core.ts

View workflow job for this annotation

GitHub Actions / e2e (18, 9, windows-latest)

Argument of type '{ rulesMeta: { [ruleId: string]: Rule.RuleMetaData; }; cwd: string; }' is not assignable to parameter of type 'ResultsMeta'.

Check failure on line 95 in src/core.ts

View workflow job for this annotation

GitHub Actions / e2e (20, 9, ubuntu-latest)

Argument of type '{ rulesMeta: { [ruleId: string]: Rule.RuleMetaData; }; cwd: string; }' is not assignable to parameter of type 'ResultsMeta'.

Check failure on line 95 in src/core.ts

View workflow job for this annotation

GitHub Actions / e2e (20, 9, macos-latest)

Argument of type '{ rulesMeta: { [ruleId: string]: Rule.RuleMetaData; }; cwd: string; }' is not assignable to parameter of type 'ResultsMeta'.

Check failure on line 95 in src/core.ts

View workflow job for this annotation

GitHub Actions / e2e (20, 9, windows-latest)

Argument of type '{ rulesMeta: { [ruleId: string]: Rule.RuleMetaData; }; cwd: string; }' is not assignable to parameter of type 'ResultsMeta'.
}

/**
* Run `eslint --fix`.
* @param ruleIds The rule ids to fix
*/
async applyAutoFixes(results: LegacyESLintNS.LintResult[], ruleIds: string[]): Promise<Undo> {
async applyAutoFixes(results: ESLint.LintResult[], ruleIds: string[]): Promise<Undo> {
return this.fix(results, ruleIds, (context) => createFixToApplyAutoFixes(context, {}));
}

Expand All @@ -110,7 +111,7 @@
* @param descriptionPosition The position of the description
*/
async disablePerLine(
results: LegacyESLintNS.LintResult[],
results: ESLint.LintResult[],
ruleIds: string[],
description?: string,
descriptionPosition?: DescriptionPosition,
Expand All @@ -128,7 +129,7 @@
* @param descriptionPosition The position of the description
*/
async disablePerFile(
results: LegacyESLintNS.LintResult[],
results: ESLint.LintResult[],
ruleIds: string[],
description?: string,
descriptionPosition?: DescriptionPosition,
Expand All @@ -145,7 +146,7 @@
* @param description The comment explaining the reason for converting
*/
async convertErrorToWarningPerFile(
results: LegacyESLintNS.LintResult[],
results: ESLint.LintResult[],
ruleIds: string[],
description?: string,
): Promise<Undo> {
Expand All @@ -158,11 +159,7 @@
* @param ruleIds The rule ids to apply suggestions
* @param filter The script to filter suggestions
*/
async applySuggestions(
results: LegacyESLintNS.LintResult[],
ruleIds: string[],
filter: SuggestionFilter,
): Promise<Undo> {
async applySuggestions(results: ESLint.LintResult[], ruleIds: string[], filter: SuggestionFilter): Promise<Undo> {
return this.fix(results, ruleIds, (context) => createFixToApplySuggestions(context, { filter }));
}

Expand All @@ -172,11 +169,7 @@
* @param ruleIds The rule ids to apply suggestions
* @param fixableMaker The function to make `Linter.LintMessage` forcibly fixable.
*/
async makeFixableAndFix(
results: LegacyESLintNS.LintResult[],
ruleIds: string[],
fixableMaker: FixableMaker,
): Promise<Undo> {
async makeFixableAndFix(results: ESLint.LintResult[], ruleIds: string[], fixableMaker: FixableMaker): Promise<Undo> {
return this.fix(results, ruleIds, (context) => createFixToMakeFixableAndFix(context, { fixableMaker }));
}

Expand All @@ -185,7 +178,7 @@
* @param fix The fix information to do.
*/
private async fix(
resultsOfLint: LegacyESLintNS.LintResult[],
resultsOfLint: ESLint.LintResult[],
ruleIds: string[],
fixCreator: (context: FixContext) => Rule.Fix[],
): Promise<Undo> {
Expand All @@ -196,7 +189,7 @@
// eslint-disable-next-line prefer-const
for (let { filePath, source } of filteredResultsOfLint) {
if (!source) throw new Error('Source code is required to apply fixes.');
const config: Linter.Config | Linter.FlatConfig[] =
const config: Linter.LegacyConfig | Linter.FlatConfig[] =
this.config.eslintOptions.type === 'eslintrc'
? // eslint-disable-next-line no-await-in-loop
await this.eslint.calculateConfigForFile(filePath)
Expand All @@ -220,7 +213,10 @@
}
}

async function calculateConfigForFile(eslint: ESLint, filePath: string): Promise<Linter.FlatConfig> {
async function calculateConfigForFile(
eslint: InstanceType<typeof LegacyESLint | typeof FlatESLint>,
filePath: string,
): Promise<Linter.FlatConfig> {
const config = await eslint.calculateConfigForFile(filePath);
// `language` property has been added to the object returned by `ESLint.prototype.calculateConfigForFile(filePath)` since ESLint v9.5.0.
// But, `Linter.prototype.verify()` does not accept `language` option. So, remove it.
Expand Down
2 changes: 1 addition & 1 deletion src/eslint/linter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type FixedResult = {
export function verifyAndFix(
linter: Linter,
text: string,
config: Linter.Config | Linter.FlatConfig[],
config: Linter.LegacyConfig | Linter.FlatConfig[],
filePath: string,
ruleIds: string[],
fixCreator: (context: FixContext) => Rule.Fix[],
Expand Down
Loading
Loading