Skip to content

Commit

Permalink
fix(W-17692101): migrate to eslint 9 (#1305)
Browse files Browse the repository at this point in the history
* chore: test files

* chore: all the other files

* chore: more eslint changes
  • Loading branch information
mdonnalley authored Jan 28, 2025
1 parent 835d908 commit e42713c
Show file tree
Hide file tree
Showing 59 changed files with 987 additions and 656 deletions.
2 changes: 0 additions & 2 deletions .eslintignore

This file was deleted.

15 changes: 0 additions & 15 deletions .eslintrc.json

This file was deleted.

65 changes: 65 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import {includeIgnoreFile} from '@eslint/compat'
import oclif from 'eslint-config-oclif'
import prettier from 'eslint-config-prettier'
import path from 'node:path'
import {fileURLToPath} from 'node:url'

const gitignorePath = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '.gitignore')

export default [
includeIgnoreFile(gitignorePath),
...oclif,
prettier,
{
rules: {
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-namespace': 'off',
'@typescript-eslint/no-require-imports': 'off',
'import/no-named-as-default-member': 'off',
'no-useless-constructor': 'off',
'perfectionist/sort-intersection-types': 'off',
'perfectionist/sort-object-types': 'off',
'perfectionist/sort-union-types': 'off',
'unicorn/no-array-reduce': 'off',
'unicorn/prefer-module': 'off',
},
},
{
files: ['test/**/*'],
rules: {
'@stylistic/lines-between-class-members': 'off',
'@typescript-eslint/ban-ts-comment': 'off',
'@typescript-eslint/ban-ts-ignore': 'off',
'@typescript-eslint/no-empty-function': 'off',
'max-lines': 'off',
'mocha/max-top-level-suites': ['warn', {limit: 8}],
'no-import-assign': 'off',
'perfectionist/sort-classes': 'off',
'perfectionist/sort-objects': 'off',
'unicorn/consistent-function-scoping': 'off',
'unicorn/no-abusive-eslint-disable': 'off',
'unicorn/no-static-only-class': 'off',
'unicorn/no-useless-undefined': 'off',
},
},
{
files: ['test/integration/*'],
rules: {
'@typescript-eslint/no-unused-expressions': 'off',
'mocha/no-nested-tests': 'off',
'mocha/no-sibling-hooks': 'off',
'mocha/no-top-level-hooks': 'off',
'unicorn/prefer-object-from-entries': 'off',
'unicorn/prefer-top-level-await': 'off',
},
},
{
files: ['test/module-loader/fixtures/**/*'],
rules: {
'@typescript-eslint/no-unused-expressions': 'off',
camelcase: 'off',
'no-undef': 'off',
'unicorn/filename-case': 'off',
},
},
]
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
},
"devDependencies": {
"@commitlint/config-conventional": "^19",
"@eslint/compat": "^1.2.5",
"@oclif/plugin-help": "^6",
"@oclif/plugin-plugins": "^5",
"@oclif/prettier-config": "^0.2.1",
Expand All @@ -49,10 +50,9 @@
"chai-as-promised": "^7.1.2",
"commitlint": "^19",
"cross-env": "^7.0.3",
"eslint": "^8.57.1",
"eslint-config-oclif": "^5.2.2",
"eslint-config-oclif-typescript": "^3.1.13",
"eslint-config-prettier": "^9.1.0",
"eslint": "^9",
"eslint-config-oclif": "^6",
"eslint-config-prettier": "^10",
"husky": "^9.1.7",
"lint-staged": "^15",
"madge": "^6.1.0",
Expand Down Expand Up @@ -96,6 +96,7 @@
"./hooks": "./lib/interfaces/hooks.js",
"./interfaces": "./lib/interfaces/index.js",
"./logger": "./lib/logger.js",
"./package.json": "./package.json",
"./parser": "./lib/parser/index.js",
"./performance": "./lib/performance.js",
"./run": "./lib/main.js",
Expand All @@ -118,7 +119,7 @@
"build": "shx rm -rf lib && tsc",
"compile": "tsc",
"format": "prettier --write \"+(src|test)/**/*.+(ts|js|json)\"",
"lint": "eslint . --ext .ts",
"lint": "eslint",
"posttest": "yarn lint && yarn test:circular-deps",
"prepack": "yarn run build",
"prepare": "husky",
Expand Down
2 changes: 1 addition & 1 deletion src/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {isNotFalsy} from './util/util'
* },
* })
*/
export function custom<T = string, P = Record<string, unknown>>(defaults: Partial<Arg<T, P>>): ArgDefinition<T, P>
export function custom<T = string, P = Record<string, unknown>>(_defaults: Partial<Arg<T, P>>): ArgDefinition<T, P>
export function custom<T, P = Record<string, unknown>>(defaults: Partial<Arg<T, P>>): ArgDefinition<T, P> {
return (options: any = {}) => ({
parse: async (i: string, _context: Command, _opts: P) => i,
Expand Down
13 changes: 7 additions & 6 deletions src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,23 @@ import {join} from 'node:path'
import {Config} from './config/config'
import {OclifConfiguration, Plugin} from './interfaces'

type OclifCoreInfo = {name: string; version: string}

type CacheContents = {
rootPlugin: Plugin
config: Config
exitCodes: OclifConfiguration['exitCodes']
'@oclif/core': OclifCoreInfo
}

type OclifCoreInfo = {name: string; version: string}

type ValueOf<T> = T[keyof T]

/**
* A simple cache for storing values that need to be accessed globally.
*/
export default class Cache extends Map<keyof CacheContents, ValueOf<CacheContents>> {
static instance: Cache

public constructor() {
super()
this.set('@oclif/core', this.getOclifCoreMeta())
Expand All @@ -33,10 +34,10 @@ export default class Cache extends Map<keyof CacheContents, ValueOf<CacheContent
return Cache.instance
}

public get(key: 'config'): Config | undefined
public get(key: '@oclif/core'): OclifCoreInfo
public get(key: 'rootPlugin'): Plugin | undefined
public get(key: 'exitCodes'): OclifConfiguration['exitCodes'] | undefined
public get(_key: 'config'): Config | undefined
public get(_key: '@oclif/core'): OclifCoreInfo
public get(_key: 'rootPlugin'): Plugin | undefined
public get(_key: 'exitCodes'): OclifConfiguration['exitCodes'] | undefined
public get(key: keyof CacheContents): ValueOf<CacheContents> | undefined {
return super.get(key)
}
Expand Down
79 changes: 29 additions & 50 deletions src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,30 +47,24 @@ process.stdout.on('error', (err: any) => {
*/

export abstract class Command {
private static readonly _base = `${pjson.name}@${pjson.version}`
/** An array of aliases for this command. */
public static aliases: string[] = []

/** An order-dependent object of arguments for the command */
public static args: ArgInput = {}

public static baseFlags: FlagInput

/**
* Emit deprecation warning when a command alias is used
*/
static deprecateAliases?: boolean

public static deprecationOptions?: Deprecation

/**
* A full description of how to use the command.
*
* If no summary, the first line of the description will be used as the summary.
*/
public static description: string | undefined

public static enableJsonFlag = false

/**
* An array of examples to show at the end of the command's help.
*
Expand All @@ -86,35 +80,24 @@ export abstract class Command {
* ```
*/
public static examples: Command.Example[]

/** A hash of flags for the command */
public static flags: FlagInput

public static hasDynamicHelp = false

public static help: string | undefined

/** Hide the command from help */
public static hidden: boolean

/** An array of aliases for this command that are hidden from help. */
public static hiddenAliases: string[] = []

/** A command ID, used mostly in error or verbose reporting. */
public static id: string

public static plugin: Plugin | undefined

public static readonly pluginAlias?: string
public static readonly pluginName?: string
public static readonly pluginType?: string

/** Mark the command as a given state (e.g. beta or deprecated) in help */
public static state?: 'beta' | 'deprecated' | string

/** When set to false, allows a variable amount of arguments */
public static strict = true

/**
* The tweet-sized description for your class, used in a parent-commands
* sub-command listing and as the header for the command help.
Expand All @@ -124,13 +107,9 @@ export abstract class Command {
* An override string (or strings) for the default usage documentation.
*/
public static usage: string | string[] | undefined

protected debug: (...args: any[]) => void

public id: string | undefined

private static readonly _base = `${pjson.name}@${pjson.version}`

public constructor(
public argv: string[],
public config: Config,
Expand All @@ -145,11 +124,6 @@ export abstract class Command {
}
}

/**
* actual command run code goes here
*/
public abstract run(): Promise<any>

/**
* instantiate and run the command
*
Expand Down Expand Up @@ -188,6 +162,26 @@ export abstract class Command {
return this.constructor as typeof Command
}

protected async _run<T>(): Promise<T> {
let err: Error | undefined
let result: T | undefined
try {
// remove redirected env var to allow subsessions to run autoupdated client
this.removeEnvVar('REDIRECTED')
await this.init()
result = await this.run()
} catch (error: any) {
err = error
await this.catch(error)
} finally {
await this.finally(err)
}

if (result && this.jsonEnabled()) this.logJson(this.toSuccessJson(result))

return result as T
}

protected async catch(err: CommandError): Promise<any> {
process.exitCode = process.exitCode ?? err.exitCode ?? 1
if (this.jsonEnabled()) {
Expand Down Expand Up @@ -221,7 +215,7 @@ export abstract class Command {

protected async init(): Promise<any> {
this.debug('init version: %s argv: %o', this.ctor._base, this.argv)
const g: any = global
const g: any = globalThis
g['http-call'] = g['http-call'] || {}
g['http-call']!.userAgent = this.config.userAgent
this.warnIfCommandDeprecated()
Expand All @@ -243,9 +237,9 @@ export abstract class Command {
const jsonIndex = this.argv.indexOf('--json')
return passThroughIndex === -1
? // If '--' is not present, then check for `--json` in this.argv
jsonIndex > -1
jsonIndex !== -1
: // If '--' is present, return true only the --json flag exists and is before the '--'
jsonIndex > -1 && jsonIndex < passThroughIndex
jsonIndex !== -1 && jsonIndex < passThroughIndex
}

public log(message = '', ...args: any[]): void {
Expand Down Expand Up @@ -293,6 +287,11 @@ export abstract class Command {
return results
}

/**
* actual command run code goes here
*/
public abstract run(): Promise<any>

protected toErrorJson(err: unknown): any {
return {error: err}
}
Expand Down Expand Up @@ -350,26 +349,6 @@ export abstract class Command {
}
}

protected async _run<T>(): Promise<T> {
let err: Error | undefined
let result: T | undefined
try {
// remove redirected env var to allow subsessions to run autoupdated client
this.removeEnvVar('REDIRECTED')
await this.init()
result = await this.run()
} catch (error: any) {
err = error
await this.catch(error)
} finally {
await this.finally(err)
}

if (result && this.jsonEnabled()) this.logJson(this.toSuccessJson(result))

return result as T
}

private removeEnvVar(envVar: string): void {
const keys: string[] = []
try {
Expand Down
Loading

0 comments on commit e42713c

Please sign in to comment.