From 8c021c2fab0a8658f2cc485a6ccf67afcd3cce8c Mon Sep 17 00:00:00 2001 From: mshanemc Date: Thu, 18 Nov 2021 11:14:42 -0600 Subject: [PATCH] fix: ts errors and changd eslint comment rule name --- src/sfdxCommand.ts | 10 +++++----- src/sfdxFlags.ts | 6 +++--- src/ux.ts | 1 + test/unit/sfdxCommand.test.ts | 18 ++++++++++-------- test/unit/sfdxFlags.test.ts | 2 +- test/unit/ux.test.ts | 10 +++++----- 6 files changed, 25 insertions(+), 22 deletions(-) diff --git a/src/sfdxCommand.ts b/src/sfdxCommand.ts index 8b0e605..10ec244 100644 --- a/src/sfdxCommand.ts +++ b/src/sfdxCommand.ts @@ -176,7 +176,7 @@ export abstract class SfdxCommand extends Command { await this.init(); return (this.result.data = await this.run()); } catch (e) { - err = e; + err = e as Error; await this.catch(e); } finally { await this.finally(err); @@ -190,7 +190,7 @@ export abstract class SfdxCommand extends Command { try { this.project = await SfdxProject.resolve(); } catch (err) { - if (err.name === 'InvalidProjectWorkspace') { + if (err instanceof Error && err.name === 'InvalidProjectWorkspace') { throw SfdxError.create('@salesforce/command', 'command', 'RequiresProjectError'); } throw err; @@ -210,7 +210,7 @@ export abstract class SfdxCommand extends Command { } } catch (err) { if (this.statics.requiresUsername) { - if (err.name === 'NoUsername' || err.name === 'AuthInfoCreationError') { + if (err instanceof Error && (err.name === 'NoUsername' || err.name === 'AuthInfoCreationError')) { throw SfdxError.create('@salesforce/command', 'command', 'RequiresUsernameError'); } throw err; @@ -233,7 +233,7 @@ export abstract class SfdxCommand extends Command { } catch (err) { // Throw an error if the command requires a devhub and there is no targetdevhubusername // flag set and no defaultdevhubusername set. - if (this.statics.requiresDevhubUsername) { + if (this.statics.requiresDevhubUsername && err instanceof Error) { if (err.name === 'AuthInfoCreationError' || err.name === 'NoUsername') { throw SfdxError.create('@salesforce/command', 'command', 'RequiresDevhubUsernameError'); } @@ -394,7 +394,7 @@ export abstract class SfdxCommand extends Command { } // Emit an event for the analytics plugin. The ts-ignore is necessary // because TS is strict about the events that can be emitted on process. - // eslint-disable-next-line @typescript-eslint/ban-ts-ignore + // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore process.emit('cmdError', err, Object.assign({}, this.flags, this.varargs), this.org || this.hubOrg); } diff --git a/src/sfdxFlags.ts b/src/sfdxFlags.ts index 6dd04e7..39e78a7 100644 --- a/src/sfdxFlags.ts +++ b/src/sfdxFlags.ts @@ -58,9 +58,9 @@ function merge( function merge(kind: flags.Kind, flag: IFlag, describable: flags.Describable): flags.Discriminated> { if (has(flag, 'validate') && hasFunction(flag, 'parse')) { const parse = flag.parse.bind(flag); - flag.parse = (val: string, ctx: unknown): T => { + flag.parse = (val: string, ctx: unknown): T2 => { validateValue(toValidatorFn(flag.validate)(val), val, kind); - return parse(val, ctx) as T; + return parse(val, ctx) as T2; }; } @@ -648,7 +648,7 @@ function validateCustomFlag(key: string, flag: flags.Any): flags.Any { return flag; } -function isBuiltin(flag: object): flag is flags.Builtin { +function isBuiltin(flag: Record): flag is flags.Builtin { return hasString(flag, 'type') && flag.type === 'builtin'; } diff --git a/src/ux.ts b/src/ux.ts index d8b8168..8879475 100644 --- a/src/ux.ts +++ b/src/ux.ts @@ -4,6 +4,7 @@ * Licensed under the BSD 3-Clause license. * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause */ +/* eslint-disable @typescript-eslint/ban-types */ import { env } from '@salesforce/kit'; import { ensure } from '@salesforce/ts-types'; diff --git a/test/unit/sfdxCommand.test.ts b/test/unit/sfdxCommand.test.ts index a105915..956551d 100644 --- a/test/unit/sfdxCommand.test.ts +++ b/test/unit/sfdxCommand.test.ts @@ -108,8 +108,8 @@ let jsonToStdout: boolean; async function mockStdout(test: (outLines: string[]) => Promise) { const oldStdoutWriter = process.stdout.write.bind(process.stdout); const lines: string[] = []; - // eslint-disable-next-line @typescript-eslint/ban-ts-ignore - // @ts-ignore + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore TODO: why isn't `create` invokable?! process.stdout.write = (message) => { if (message) { lines.push(message); @@ -1116,7 +1116,7 @@ describe('SfdxCommand', () => { const create = flags[flagType]; class TestCommand extends BaseTestCommand { public static flagsConfig = { - // eslint-disable-next-line @typescript-eslint/ban-ts-ignore + // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore TODO: why isn't `create` invokable?! doflag: create({ char: 'i', description: 'my desc' }), }; @@ -1125,6 +1125,8 @@ describe('SfdxCommand', () => { if (err) { const sfdxError = SfdxError.create('@salesforce/command', 'flags', 'InvalidFlagTypeError', [ val, + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore kind doesn't exist TestCommand.flagsConfig.doflag.kind, ERR_NEXT_STEPS[flagType] || '', ]); @@ -1221,7 +1223,7 @@ describe('SfdxCommand', () => { TestCommand.flagsConfig = { myflag: flags.string({ char: 'm', - // eslint-disable-next-line @typescript-eslint/ban-ts-ignore + // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore ignore invalid longDescription value longDescription: false, description: 'my desc', @@ -1235,7 +1237,7 @@ describe('SfdxCommand', () => { it('should validate description is defined', async () => { class TestCommand extends BaseTestCommand {} TestCommand.flagsConfig = { - // eslint-disable-next-line @typescript-eslint/ban-ts-ignore + // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore ignore error about not providing description myflag: flags.string({ char: 'm' }), }; @@ -1247,7 +1249,7 @@ describe('SfdxCommand', () => { class TestCommand extends BaseTestCommand {} TestCommand.flagsConfig = { myflag: flags.string({ - // eslint-disable-next-line @typescript-eslint/ban-ts-ignore + // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore ignore invalid char value length char: 'foo', description: 'bar', @@ -1260,7 +1262,7 @@ describe('SfdxCommand', () => { it('should validate char is alphabetical', async () => { class TestCommand extends BaseTestCommand {} TestCommand.flagsConfig = { - // eslint-disable-next-line @typescript-eslint/ban-ts-ignore + // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore ignore invalid char value myflag: flags.string({ char: '5', description: 'bar' }), }; @@ -1301,7 +1303,7 @@ describe('SfdxCommand', () => { }), }; - // eslint-disable-next-line @typescript-eslint/ban-ts-ignore + // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore Allow undefined array value against the compiler spec to test underlying engine const output = await TestCommand.run(['--myflag', undefined]); expect(output).to.equal(undefined); diff --git a/test/unit/sfdxFlags.test.ts b/test/unit/sfdxFlags.test.ts index ab097bf..da2d18f 100644 --- a/test/unit/sfdxFlags.test.ts +++ b/test/unit/sfdxFlags.test.ts @@ -58,7 +58,7 @@ describe('SfdxFlags', () => { it('should carry forward additional properties on builtins when forced (for legacy toolbelt compatibility)', () => { const rv = buildSfdxFlags( { - // eslint-disable-next-line @typescript-eslint/ban-ts-ignore + // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore force setting the char to simulate a legacy toolbelt use case apiversion: flags.builtin({ char: 'a' }), }, diff --git a/test/unit/ux.test.ts b/test/unit/ux.test.ts index 05be85f..772b82b 100644 --- a/test/unit/ux.test.ts +++ b/test/unit/ux.test.ts @@ -97,9 +97,9 @@ describe('UX', () => { }); it('logJson() should log to the logger (unformatted) and stdout (formatted)', () => { - let retVal: Optional; + let retVal: Optional>; const info = $$.SANDBOX.stub($$.TEST_LOGGER, 'info'); - const styledJsonGetter = () => (x: object) => (retVal = x); + const styledJsonGetter = () => (x: Record) => (retVal = x); $$.SANDBOX.stub(cli, 'styledJSON').get(styledJsonGetter); const ux = new UX($$.TEST_LOGGER, true, cli); const logMsg = { key1: 'foo', key2: 9, key3: true, key4: [1, 2, 3] }; @@ -269,9 +269,9 @@ describe('UX', () => { }); it('table() should only log to the logger when output IS NOT enabled', () => { - let retVal: Optional; + let retVal: Optional>; const info = $$.SANDBOX.stub($$.TEST_LOGGER, 'info'); - const tableGetter = () => (x: object) => (retVal = x); + const tableGetter = () => (x: Record) => (retVal = x); $$.SANDBOX.stub(cli, 'table').get(tableGetter); const ux = new UX($$.TEST_LOGGER, false, cli); const tableData = [ @@ -364,7 +364,7 @@ describe('UX', () => { const question = 'City?'; const answer = 'Louisville'; const ux = new UX($$.TEST_LOGGER, true, cli); - const promptGetter = () => (name: string, options: object) => { + const promptGetter = () => (name: string, options: Record) => { expect(name).to.equal(question); expect(options).to.eql({}); return answer;