From 6855df79b6b4f138f0d3b85820963d8992357a9b Mon Sep 17 00:00:00 2001 From: mizdra Date: Thu, 26 Sep 2024 00:49:09 +0900 Subject: [PATCH 1/2] update devDeps --- package.json | 13 ++--- patches/@types__eslint@8.44.6.patch | 89 ----------------------------- pnpm-lock.yaml | 34 +++++------ 3 files changed, 18 insertions(+), 118 deletions(-) delete mode 100644 patches/@types__eslint@8.44.6.patch diff --git a/package.json b/package.json index 418bd78e..5906415a 100644 --- a/package.json +++ b/package.json @@ -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", @@ -93,10 +93,5 @@ "!src/**/__snapshots__", "dist", "static" - ], - "pnpm": { - "patchedDependencies": { - "@types/eslint@8.44.6": "patches/@types__eslint@8.44.6.patch" - } - } + ] } diff --git a/patches/@types__eslint@8.44.6.patch b/patches/@types__eslint@8.44.6.patch deleted file mode 100644 index 5d031263..00000000 --- a/patches/@types__eslint@8.44.6.patch +++ /dev/null @@ -1,89 +0,0 @@ -diff --git a/index.d.ts b/index.d.ts -index 538c0dc1851122fbba808968e02100c9ca5728c4..d16f8776837a844f5e8be9b4053386e53d495ef5 100644 ---- a/index.d.ts -+++ b/index.d.ts -@@ -842,7 +842,7 @@ export class Linter { - - version: string; - -- constructor(options?: { cwd?: string | undefined; configType?: "flat" }); -+ constructor(options?: { cwd?: string | undefined; configType?: "flat" | "eslintrc" }); - - verify( - code: SourceCode | string, -diff --git a/use-at-your-own-risk.d.ts b/use-at-your-own-risk.d.ts -index 089a657babec22d7bfa526d10d2950c77af041f3..31c56d1ed865fb11ba484efba01edc6ba7c57da9 100644 ---- a/use-at-your-own-risk.d.ts -+++ b/use-at-your-own-risk.d.ts -@@ -1,6 +1,63 @@ -+import { ESLint, Linter, Rule } from "eslint"; -+ - /** @deprecated */ - export const builtinRules: Map; - /** @deprecated */ -+export namespace FlatESLint { -+ interface Options { -+ // File enumeration -+ cwd?: string | undefined; -+ errorOnUnmatchedPattern?: boolean | undefined; -+ globInputPaths?: boolean | undefined; -+ ignore?: boolean | undefined; -+ -+ // Linting -+ allowInlineConfig?: boolean | undefined; -+ baseConfig?: Linter.FlatConfig | Linter.FlatConfig[] | undefined; -+ overrideConfig?: Linter.FlatConfig | Linter.FlatConfig[] | undefined; -+ overrideConfigFile?: boolean | string | undefined; -+ reportUnusedDisableDirectives?: Linter.StringSeverity | undefined; -+ -+ // Autofix -+ fix?: boolean | ((message: Linter.LintMessage) => boolean) | undefined; -+ fixTypes?: Rule.RuleMetaData['type'][] | undefined; -+ -+ // Cache-related -+ cache?: boolean | undefined; -+ cacheLocation?: string | undefined; -+ cacheStrategy?: 'content' | 'metadata' | undefined; -+ } -+} -+/** @deprecated */ -+export class FlatESLint { -+ constructor(options?: FlatESLint.Options); -+ -+ static get version(): string; -+ -+ static outputFixes(results: ESLint.LintResult[]): Promise; -+ -+ static getErrorResults(results: ESLint.LintResult[]): ESLint.LintResult[]; -+ -+ getRulesMetaForResults(results: ESLint.LintResult[]): Record; -+ -+ lintFiles(patterns: string | string[]): Promise; -+ -+ lintText( -+ code: string, -+ options?: { filePath?: string | undefined; warnIgnored?: boolean | undefined }, -+ ): Promise; -+ -+ loadFormatter(name?: string): Promise; -+ -+ calculateConfigForFile(filePath: string): Promise; -+ -+ findConfigFile(): Promise; -+ -+ isPathIgnored(filePath: string): Promise; -+} -+/** @deprecated */ -+export function shouldUseFlatConfig(): Promise; -+/** @deprecated */ - export class FileEnumerator { - constructor( - params?: { -@@ -17,3 +74,7 @@ export class FileEnumerator { - patternOrPatterns: string | string[], - ): IterableIterator<{ config: any; filePath: string; ignored: boolean }>; - } -+export { -+ /** @deprecated */ -+ ESLint as LegacyESLint -+}; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 1be47508..fa3dcded 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -4,11 +4,6 @@ settings: autoInstallPeers: true excludeLinksFromLockfile: false -patchedDependencies: - '@types/eslint@8.44.6': - hash: qh2z4gts23iqxgbr3skltsgoyi - path: patches/@types__eslint@8.44.6.patch - importers: .: @@ -69,14 +64,14 @@ importers: specifier: ^2.0.2 version: 2.0.2 '@types/eslint': - specifier: ^8.44.6 - version: 8.44.6(patch_hash=qh2z4gts23iqxgbr3skltsgoyi) + specifier: ^9.6.1 + version: 9.6.1 '@types/estraverse': - specifier: ^5.1.6 - version: 5.1.6 + specifier: ^5.1.7 + version: 5.1.7 '@types/estree': - specifier: ^1.0.5 - version: 1.0.5 + specifier: ^1.0.6 + version: 1.0.6 '@types/fs-extra': specifier: ^11.0.4 version: 11.0.4 @@ -631,22 +626,21 @@ packages: resolution: {integrity: sha512-jt4jIsWKvUvuY6adJnQJlb/UR7DdjC8CjHI/OaSQruj2yX9/K6+KOvDt/vD6udqos/FUk5Op66CvYT7TBLYO5Q==} dev: true - /@types/eslint@8.44.6(patch_hash=qh2z4gts23iqxgbr3skltsgoyi): - resolution: {integrity: sha512-P6bY56TVmX8y9J87jHNgQh43h6VVU+6H7oN7hgvivV81K2XY8qJZ5vqPy/HdUoVIelii2kChYVzQanlswPWVFw==} + /@types/eslint@9.6.1: + resolution: {integrity: sha512-FXx2pKgId/WyYo2jXw63kk7/+TY7u7AziEJxJAnSFzHlqTAS3Ync6SvgYAN/k4/PQpnnVuzoMuVnByKK2qp0ag==} dependencies: - '@types/estree': 1.0.5 + '@types/estree': 1.0.6 '@types/json-schema': 7.0.14 dev: true - patched: true - /@types/estraverse@5.1.6: - resolution: {integrity: sha512-IL4TOP+KA2BvqAv4jWQ1HXzIeQ4AecgY8SBgdLMIlkMll8nki/OB1z0LGU/K8y3udZuyBvjOG97j/RE1cgUlCA==} + /@types/estraverse@5.1.7: + resolution: {integrity: sha512-JRVtdKYZz7VkNp7hMC/WKoiZ8DS3byw20ZGoMZ1R8eBrBPIY7iBaDAS1zcrnXQCwK44G4vbXkimeU7R0VLG8UQ==} dependencies: - '@types/estree': 1.0.5 + '@types/estree': 1.0.6 dev: true - /@types/estree@1.0.5: - resolution: {integrity: sha512-/kYRxGDLWzHOB7q+wtSUQlFrtcdUccpfy+X+9iMBpHK8QLLhx2wIPYuS5DYtR9Wa/YlZAbIovy7qVdB1Aq6Lyw==} + /@types/estree@1.0.6: + resolution: {integrity: sha512-AYnb1nQyY49te+VRAVgmzfcgjYS91mY5P0TKUDCLEM+gNnA+3T6rWITXRLYCpahpqSQbN5cE+gHpnPyXjHWxcw==} dev: true /@types/fs-extra@11.0.4: From 9ec7269f0d9fa9148c9d0c3d89de90137337b458 Mon Sep 17 00:00:00 2001 From: mizdra Date: Thu, 26 Sep 2024 01:21:22 +0900 Subject: [PATCH 2/2] fix type errors --- src/config.ts | 6 ++--- src/core.ts | 40 +++++++++++++++------------------ src/eslint/linter.ts | 2 +- src/eslint/rule-fixer.ts | 23 +++++++++---------- src/fix/make-fixable-and-fix.ts | 2 +- src/test-util/fix-tester.ts | 6 ++--- 6 files changed, 37 insertions(+), 42 deletions(-) diff --git a/src/config.ts b/src/config.ts index 7dea6406..8715e0b6 100644 --- a/src/config.ts +++ b/src/config.ts @@ -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' @@ -15,7 +15,7 @@ type LegacyESLintOptions = { type: 'eslintrc' } & Pick< | 'resolvePluginsRelativeTo' >; type FlatESLintOptions = { type: 'flat' } & Pick< - FlatESLint.Options, + ESLint.Options, 'overrideConfigFile' | 'cache' | 'cacheLocation' | 'overrideConfig' | 'cwd' >; diff --git a/src/core.ts b/src/core.ts index 85010558..f77bafd9 100644 --- a/src/core.ts +++ b/src/core.ts @@ -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'; @@ -27,7 +27,7 @@ const { LegacyESLint, FlatESLint } = eslintPkg; * @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 }; @@ -42,7 +42,7 @@ export type Undo = () => Promise; */ export class Core { readonly config: NormalizedConfig; - readonly eslint: LegacyESLintNS; + readonly eslint: InstanceType; constructor(config: Config) { this.config = normalizeConfig(config); @@ -80,7 +80,7 @@ export class Core { * @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 { + async formatResultDetails(results: ESLint.LintResult[], ruleIds: (string | null)[]): Promise { const formatterName = this.config.formatterName; // When eslint-interactive is installed globally, eslint-formatter-codeframe will also be installed globally. @@ -91,14 +91,15 @@ export class Core { : 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 }); } /** * Run `eslint --fix`. * @param ruleIds The rule ids to fix */ - async applyAutoFixes(results: LegacyESLintNS.LintResult[], ruleIds: string[]): Promise { + async applyAutoFixes(results: ESLint.LintResult[], ruleIds: string[]): Promise { return this.fix(results, ruleIds, (context) => createFixToApplyAutoFixes(context, {})); } @@ -110,7 +111,7 @@ export class Core { * @param descriptionPosition The position of the description */ async disablePerLine( - results: LegacyESLintNS.LintResult[], + results: ESLint.LintResult[], ruleIds: string[], description?: string, descriptionPosition?: DescriptionPosition, @@ -128,7 +129,7 @@ export class Core { * @param descriptionPosition The position of the description */ async disablePerFile( - results: LegacyESLintNS.LintResult[], + results: ESLint.LintResult[], ruleIds: string[], description?: string, descriptionPosition?: DescriptionPosition, @@ -145,7 +146,7 @@ export class Core { * @param description The comment explaining the reason for converting */ async convertErrorToWarningPerFile( - results: LegacyESLintNS.LintResult[], + results: ESLint.LintResult[], ruleIds: string[], description?: string, ): Promise { @@ -158,11 +159,7 @@ export class Core { * @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 { + async applySuggestions(results: ESLint.LintResult[], ruleIds: string[], filter: SuggestionFilter): Promise { return this.fix(results, ruleIds, (context) => createFixToApplySuggestions(context, { filter })); } @@ -172,11 +169,7 @@ export class Core { * @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 { + async makeFixableAndFix(results: ESLint.LintResult[], ruleIds: string[], fixableMaker: FixableMaker): Promise { return this.fix(results, ruleIds, (context) => createFixToMakeFixableAndFix(context, { fixableMaker })); } @@ -185,7 +178,7 @@ export class Core { * @param fix The fix information to do. */ private async fix( - resultsOfLint: LegacyESLintNS.LintResult[], + resultsOfLint: ESLint.LintResult[], ruleIds: string[], fixCreator: (context: FixContext) => Rule.Fix[], ): Promise { @@ -196,7 +189,7 @@ export class Core { // 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) @@ -220,7 +213,10 @@ export class Core { } } -async function calculateConfigForFile(eslint: ESLint, filePath: string): Promise { +async function calculateConfigForFile( + eslint: InstanceType, + filePath: string, +): Promise { 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. diff --git a/src/eslint/linter.ts b/src/eslint/linter.ts index 8677ead0..b9bb2712 100644 --- a/src/eslint/linter.ts +++ b/src/eslint/linter.ts @@ -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[], diff --git a/src/eslint/rule-fixer.ts b/src/eslint/rule-fixer.ts index 69f4ef4f..320f18eb 100644 --- a/src/eslint/rule-fixer.ts +++ b/src/eslint/rule-fixer.ts @@ -16,8 +16,7 @@ // Helpers // ------------------------------------------------------------------------------ -import { AST, Rule } from 'eslint'; -import type { Node } from 'estree'; +import { Rule } from 'eslint'; /** * Creates a fix command that inserts text at the specified index in the source text. @@ -42,7 +41,7 @@ function insertTextAt(index: number, text: string): Rule.Fix { */ /** @type {import('eslint').Rule.RuleFixer} */ -const ruleFixer = Object.freeze({ +const ruleFixer: Rule.RuleFixer = { /** * Creates a fix command that inserts text after the given node or token. * The fix is not applied until applyFixes() is called. @@ -50,7 +49,7 @@ const ruleFixer = Object.freeze({ * @param {string} text The text to insert. * @returns {Object} The fix command. */ - insertTextAfter(nodeOrToken: Node | AST.Token, text: string): Rule.Fix { + insertTextAfter(nodeOrToken, text) { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion return this.insertTextAfterRange(nodeOrToken.range!, text); }, @@ -63,7 +62,7 @@ const ruleFixer = Object.freeze({ * @param {string} text The text to insert. * @returns {Object} The fix command. */ - insertTextAfterRange(range: AST.Range, text: string): Rule.Fix { + insertTextAfterRange(range, text) { return insertTextAt(range[1], text); }, @@ -74,7 +73,7 @@ const ruleFixer = Object.freeze({ * @param {string} text The text to insert. * @returns {Object} The fix command. */ - insertTextBefore(nodeOrToken: Node | AST.Token, text: string): Rule.Fix { + insertTextBefore(nodeOrToken, text) { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion return this.insertTextBeforeRange(nodeOrToken.range!, text); }, @@ -87,7 +86,7 @@ const ruleFixer = Object.freeze({ * @param {string} text The text to insert. * @returns {Object} The fix command. */ - insertTextBeforeRange(range: AST.Range, text: string): Rule.Fix { + insertTextBeforeRange(range, text) { return insertTextAt(range[0], text); }, @@ -98,7 +97,7 @@ const ruleFixer = Object.freeze({ * @param {string} text The text to insert. * @returns {Object} The fix command. */ - replaceText(nodeOrToken: Node | AST.Token, text: string): Rule.Fix { + replaceText(nodeOrToken, text) { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion return this.replaceTextRange(nodeOrToken.range!, text); }, @@ -111,7 +110,7 @@ const ruleFixer = Object.freeze({ * @param {string} text The text to insert. * @returns {Object} The fix command. */ - replaceTextRange(range: AST.Range, text: string): Rule.Fix { + replaceTextRange(range, text) { return { range, text, @@ -124,7 +123,7 @@ const ruleFixer = Object.freeze({ * @param {ASTNode|Token} nodeOrToken The node or token to remove. * @returns {Object} The fix command. */ - remove(nodeOrToken: Node | AST.Token): Rule.Fix { + remove(nodeOrToken) { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion return this.removeRange(nodeOrToken.range!); }, @@ -136,12 +135,12 @@ const ruleFixer = Object.freeze({ * is end of range. * @returns {Object} The fix command. */ - removeRange(range: AST.Range): Rule.Fix { + removeRange(range) { return { range, text: '', }; }, -}); +}; export { ruleFixer }; diff --git a/src/fix/make-fixable-and-fix.ts b/src/fix/make-fixable-and-fix.ts index 70bb5732..f162b622 100644 --- a/src/fix/make-fixable-and-fix.ts +++ b/src/fix/make-fixable-and-fix.ts @@ -46,7 +46,7 @@ function getMessageToSourceNode(sourceCode: SourceCode, messages: Linter.LintMes traverse(sourceCode.ast, { // Required to traverse extension nodes such as `JSXElement`. fallback: 'iteration', - enter(node: Node) { + enter(node) { for (const message of messages) { if (isMessageSourceNode(sourceCode, node, message)) { result.set(message, node); diff --git a/src/test-util/fix-tester.ts b/src/test-util/fix-tester.ts index c840010e..1d95af58 100644 --- a/src/test-util/fix-tester.ts +++ b/src/test-util/fix-tester.ts @@ -36,11 +36,11 @@ export class FixTester { private linter: Linter; private fixCreator: (context: FixContext, args: FixArgs) => Rule.Fix[]; private defaultFixArgs: FixArgs; - private defaultLinterConfig: Linter.Config; + private defaultLinterConfig: Linter.LegacyConfig; constructor( fixCreator: (context: FixContext, args: FixArgs) => Rule.Fix[], defaultFixArgs: FixArgs, - defaultLinterConfig: Linter.Config, + defaultLinterConfig: Linter.LegacyConfig, ) { this.linter = new Linter({ configType: 'eslintrc' }); this.linter.defineRule('prefer-addition-shorthand', preferAdditionShorthandRule); @@ -58,7 +58,7 @@ export class FixTester { const filePath = testCase.filename ?? DEFAULT_FILENAME; - const config: Linter.Config = { + const config: Linter.LegacyConfig = { ...this.defaultLinterConfig, rules: { ...this.defaultLinterConfig.rules,