From 4357426415cc330208b90d7b0ff5050372068ead Mon Sep 17 00:00:00 2001 From: Filip Maj Date: Thu, 5 Aug 2021 09:54:54 -0400 Subject: [PATCH] Tweak the operator-linebreak lint rule: forbid line breaks around the = operator, and prefix for other operators to be at the end of the line. --- .eslintrc.js | 3 ++ src/App.spec.ts | 22 ++++---- src/App.ts | 68 +++++++++++-------------- src/receivers/AwsLambdaReceiver.spec.ts | 6 +-- src/receivers/AwsLambdaReceiver.ts | 3 +- src/receivers/ExpressReceiver.spec.ts | 3 +- src/receivers/ExpressReceiver.ts | 10 ++-- src/receivers/HTTPReceiver.ts | 3 +- src/receivers/SocketModeReceiver.ts | 3 +- 9 files changed, 56 insertions(+), 65 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index dcb95ab48..0206f4a0b 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -159,6 +159,9 @@ module.exports = { // override section below because a distinct override is necessary in JavaScript files. 'no-use-before-define': 'off', '@typescript-eslint/no-use-before-define': ['error', 'nofunc'], + 'operator-linebreak': ['error', 'after', { overrides: { + '=': 'none' + }}], }, }, { diff --git a/src/App.spec.ts b/src/App.spec.ts index 022450e8a..2318298e1 100644 --- a/src/App.spec.ts +++ b/src/App.spec.ts @@ -508,18 +508,16 @@ describe('App', () => { * @param orderDown The order it should be called when processing middleware down the chain * @param orderUp The order it should be called when processing middleware up the chain */ - const assertOrderMiddleware = - (orderDown: number, orderUp: number) => - async ({ next }: { next?: NextFn }) => { - await delay(100); - middlewareCount += 1; - assert.equal(middlewareCount, orderDown); - if (next !== undefined) { - await next(); - } - middlewareCount += 1; - assert.equal(middlewareCount, orderUp); - }; + const assertOrderMiddleware = (orderDown: number, orderUp: number) => async ({ next }: { next?: NextFn }) => { + await delay(100); + middlewareCount += 1; + assert.equal(middlewareCount, orderDown); + if (next !== undefined) { + await next(); + } + middlewareCount += 1; + assert.equal(middlewareCount, orderUp); + }; app.use(assertOrderMiddleware(1, 8)); app.message(message, assertOrderMiddleware(3, 6), assertOrderMiddleware(4, 5)); diff --git a/src/App.ts b/src/App.ts index cf3a54c57..6d7e8c074 100644 --- a/src/App.ts +++ b/src/App.ts @@ -522,10 +522,9 @@ export default class App { callbackIdOrConstraints: string | RegExp | Constraints, ...listeners: Middleware>>[] ): void { - const constraints: ShortcutConstraints = - typeof callbackIdOrConstraints === 'string' || util.types.isRegExp(callbackIdOrConstraints) - ? { callback_id: callbackIdOrConstraints } - : callbackIdOrConstraints; + const constraints: ShortcutConstraints = typeof callbackIdOrConstraints === 'string' || util.types.isRegExp(callbackIdOrConstraints) ? + { callback_id: callbackIdOrConstraints } : + callbackIdOrConstraints; // Fail early if the constraints contain invalid keys const unknownConstraintKeys = Object.keys(constraints).filter((k) => k !== 'callback_id' && k !== 'type'); @@ -565,10 +564,9 @@ export default class App { ...listeners: Middleware>>[] ): void { // Normalize Constraints - const constraints: ActionConstraints = - typeof actionIdOrConstraints === 'string' || util.types.isRegExp(actionIdOrConstraints) - ? { action_id: actionIdOrConstraints } - : actionIdOrConstraints; + const constraints: ActionConstraints = typeof actionIdOrConstraints === 'string' || util.types.isRegExp(actionIdOrConstraints) ? + { action_id: actionIdOrConstraints } : + actionIdOrConstraints; // Fail early if the constraints contain invalid keys const unknownConstraintKeys = Object.keys(constraints).filter( @@ -602,10 +600,9 @@ export default class App { actionIdOrConstraints: string | RegExp | ActionConstraints, ...listeners: Middleware>[] ): void { - const constraints: ActionConstraints = - typeof actionIdOrConstraints === 'string' || util.types.isRegExp(actionIdOrConstraints) - ? { action_id: actionIdOrConstraints } - : actionIdOrConstraints; + const constraints: ActionConstraints = typeof actionIdOrConstraints === 'string' || util.types.isRegExp(actionIdOrConstraints) ? + { action_id: actionIdOrConstraints } : + actionIdOrConstraints; this.listeners.push([onlyOptions, matchConstraints(constraints), ...listeners] as Middleware[]); } @@ -622,10 +619,9 @@ export default class App { callbackIdOrConstraints: string | RegExp | ViewConstraints, ...listeners: Middleware>[] ): void { - const constraints: ViewConstraints = - typeof callbackIdOrConstraints === 'string' || util.types.isRegExp(callbackIdOrConstraints) - ? { callback_id: callbackIdOrConstraints, type: 'view_submission' } - : callbackIdOrConstraints; + const constraints: ViewConstraints = typeof callbackIdOrConstraints === 'string' || util.types.isRegExp(callbackIdOrConstraints) ? + { callback_id: callbackIdOrConstraints, type: 'view_submission' } : + callbackIdOrConstraints; // Fail early if the constraints contain invalid keys const unknownConstraintKeys = Object.keys(constraints).filter((k) => k !== 'callback_id' && k !== 'type'); if (unknownConstraintKeys.length > 0) { @@ -731,23 +727,22 @@ export default class App { ack?: AckFn; } = { body: bodyArg, - payload: - type === IncomingEventType.Event - ? (bodyArg as SlackEventMiddlewareArgs['body']).event - : type === IncomingEventType.ViewAction - ? (bodyArg as SlackViewMiddlewareArgs['body']).view - : type === IncomingEventType.Shortcut - ? (bodyArg as SlackShortcutMiddlewareArgs['body']) - : type === IncomingEventType.Action && - isBlockActionOrInteractiveMessageBody(bodyArg as SlackActionMiddlewareArgs['body']) - ? (bodyArg as SlackActionMiddlewareArgs['body']).actions[0] - : (bodyArg as ( - | Exclude< - AnyMiddlewareArgs, - SlackEventMiddlewareArgs | SlackActionMiddlewareArgs | SlackViewMiddlewareArgs - > - | SlackActionMiddlewareArgs> - )['body']), + payload: type === IncomingEventType.Event ? + (bodyArg as SlackEventMiddlewareArgs['body']).event : + type === IncomingEventType.ViewAction ? + (bodyArg as SlackViewMiddlewareArgs['body']).view : + type === IncomingEventType.Shortcut ? + (bodyArg as SlackShortcutMiddlewareArgs['body']) : + type === IncomingEventType.Action && + isBlockActionOrInteractiveMessageBody(bodyArg as SlackActionMiddlewareArgs['body']) ? + (bodyArg as SlackActionMiddlewareArgs['body']).actions[0] : + (bodyArg as ( + | Exclude< + AnyMiddlewareArgs, + SlackEventMiddlewareArgs | SlackActionMiddlewareArgs | SlackViewMiddlewareArgs + > + | SlackActionMiddlewareArgs> + )['body']), }; // Set aliases @@ -879,7 +874,6 @@ export default class App { } } - function defaultErrorHandler(logger: Logger): ErrorHandler { return (error) => { logger.error(error); @@ -896,9 +890,9 @@ function runAuthTestForBotToken( authorization: Partial & { botToken: Required['botToken'] }, ): Promise<{ botUserId: string; botId: string }> { // TODO: warn when something needed isn't found - return authorization.botUserId !== undefined && authorization.botId !== undefined - ? Promise.resolve({ botUserId: authorization.botUserId, botId: authorization.botId }) - : client.auth.test({ token: authorization.botToken }).then((result) => ({ + return authorization.botUserId !== undefined && authorization.botId !== undefined ? + Promise.resolve({ botUserId: authorization.botUserId, botId: authorization.botId }) : + client.auth.test({ token: authorization.botToken }).then((result) => ({ botUserId: result.user_id as string, botId: result.bot_id as string, })); diff --git a/src/receivers/AwsLambdaReceiver.spec.ts b/src/receivers/AwsLambdaReceiver.spec.ts index e058e1c86..2b0e63b68 100644 --- a/src/receivers/AwsLambdaReceiver.spec.ts +++ b/src/receivers/AwsLambdaReceiver.spec.ts @@ -222,8 +222,7 @@ describe('AwsLambdaReceiver', function () { }); const handler = awsReceiver.toHandler(); const timestamp = Math.floor(Date.now() / 1000); - const body = - 'payload=%7B%22type%22%3A%22shortcut%22%2C%22token%22%3A%22fixed-value%22%2C%22action_ts%22%3A%221612879511.716075%22%2C%22team%22%3A%7B%22id%22%3A%22T111%22%2C%22domain%22%3A%22domain-value%22%2C%22enterprise_id%22%3A%22E111%22%2C%22enterprise_name%22%3A%22Sandbox+Org%22%7D%2C%22user%22%3A%7B%22id%22%3A%22W111%22%2C%22username%22%3A%22primary-owner%22%2C%22team_id%22%3A%22T111%22%7D%2C%22is_enterprise_install%22%3Afalse%2C%22enterprise%22%3A%7B%22id%22%3A%22E111%22%2C%22name%22%3A%22Kaz+SDK+Sandbox+Org%22%7D%2C%22callback_id%22%3A%22bolt-js-aws-lambda-shortcut%22%2C%22trigger_id%22%3A%22111.222.xxx%22%7D'; + const body = 'payload=%7B%22type%22%3A%22shortcut%22%2C%22token%22%3A%22fixed-value%22%2C%22action_ts%22%3A%221612879511.716075%22%2C%22team%22%3A%7B%22id%22%3A%22T111%22%2C%22domain%22%3A%22domain-value%22%2C%22enterprise_id%22%3A%22E111%22%2C%22enterprise_name%22%3A%22Sandbox+Org%22%7D%2C%22user%22%3A%7B%22id%22%3A%22W111%22%2C%22username%22%3A%22primary-owner%22%2C%22team_id%22%3A%22T111%22%7D%2C%22is_enterprise_install%22%3Afalse%2C%22enterprise%22%3A%7B%22id%22%3A%22E111%22%2C%22name%22%3A%22Kaz+SDK+Sandbox+Org%22%7D%2C%22callback_id%22%3A%22bolt-js-aws-lambda-shortcut%22%2C%22trigger_id%22%3A%22111.222.xxx%22%7D'; const signature = crypto.createHmac('sha256', 'my-secret').update(`v0:${timestamp}:${body}`).digest('hex'); const awsEvent = { resource: '/slack/events', @@ -277,8 +276,7 @@ describe('AwsLambdaReceiver', function () { }); const handler = awsReceiver.toHandler(); const timestamp = Math.floor(Date.now() / 1000); - const body = - 'token=fixed-value&team_id=T111&team_domain=domain-value&channel_id=C111&channel_name=random&user_id=W111&user_name=primary-owner&command=%2Fhello-bolt-js&text=&api_app_id=A111&is_enterprise_install=false&enterprise_id=E111&enterprise_name=Sandbox+Org&response_url=https%3A%2F%2Fhooks.slack.com%2Fcommands%2FT111%2F111%2Fxxx&trigger_id=111.222.xxx'; + const body = 'token=fixed-value&team_id=T111&team_domain=domain-value&channel_id=C111&channel_name=random&user_id=W111&user_name=primary-owner&command=%2Fhello-bolt-js&text=&api_app_id=A111&is_enterprise_install=false&enterprise_id=E111&enterprise_name=Sandbox+Org&response_url=https%3A%2F%2Fhooks.slack.com%2Fcommands%2FT111%2F111%2Fxxx&trigger_id=111.222.xxx'; const signature = crypto.createHmac('sha256', 'my-secret').update(`v0:${timestamp}:${body}`).digest('hex'); const awsEvent = { resource: '/slack/events', diff --git a/src/receivers/AwsLambdaReceiver.ts b/src/receivers/AwsLambdaReceiver.ts index c9142d85e..9adafbc4c 100644 --- a/src/receivers/AwsLambdaReceiver.ts +++ b/src/receivers/AwsLambdaReceiver.ts @@ -59,8 +59,7 @@ export default class AwsLambdaReceiver implements Receiver { constructor({ signingSecret, logger = undefined, logLevel = LogLevel.INFO }: AwsLambdaReceiverOptions) { // Initialize instance variables, substituting defaults for each value this.signingSecret = signingSecret; - this.logger = - logger ?? + this.logger = logger ?? (() => { const defaultLogger = new ConsoleLogger(); defaultLogger.setLevel(logLevel); diff --git a/src/receivers/ExpressReceiver.spec.ts b/src/receivers/ExpressReceiver.spec.ts index 0a71efabd..6d0e2461e 100644 --- a/src/receivers/ExpressReceiver.spec.ts +++ b/src/receivers/ExpressReceiver.spec.ts @@ -379,8 +379,7 @@ describe('ExpressReceiver', function () { const signingSecret = '8f742231b10e8888abcd99yyyzzz85a5'; const signature = 'v0=a2114d57b48eac39b9ad189dd8316235a7b4a8d21a10bd27519666489c69b503'; const requestTimestamp = 1531420618; - const body = - 'token=xyzz0WbapA4vBCDEFasx0q6G&team_id=T1DC2JH3J&team_domain=testteamnow&channel_id=G8PSS9T3V&channel_name=foobar&user_id=U2CERLKJA&user_name=roadrunner&command=%2Fwebhook-collect&text=&response_url=https%3A%2F%2Fhooks.slack.com%2Fcommands%2FT1DC2JH3J%2F397700885554%2F96rGlfmibIGlgcZRskXaIFfN&trigger_id=398738663015.47445629121.803a0bc887a14d10d2c447fce8b6703c'; + const body = 'token=xyzz0WbapA4vBCDEFasx0q6G&team_id=T1DC2JH3J&team_domain=testteamnow&channel_id=G8PSS9T3V&channel_name=foobar&user_id=U2CERLKJA&user_name=roadrunner&command=%2Fwebhook-collect&text=&response_url=https%3A%2F%2Fhooks.slack.com%2Fcommands%2FT1DC2JH3J%2F397700885554%2F96rGlfmibIGlgcZRskXaIFfN&trigger_id=398738663015.47445629121.803a0bc887a14d10d2c447fce8b6703c'; function buildExpressRequest(): Request { const reqAsStream = new Readable(); diff --git a/src/receivers/ExpressReceiver.ts b/src/receivers/ExpressReceiver.ts index d2a9ef84b..492b3c8fa 100644 --- a/src/receivers/ExpressReceiver.ts +++ b/src/receivers/ExpressReceiver.ts @@ -184,8 +184,9 @@ export default class ExpressReceiver implements Receiver { // Add OAuth routes to receiver if (this.installer !== undefined) { - const redirectUriPath = - installerOptions.redirectUriPath === undefined ? '/slack/oauth_redirect' : installerOptions.redirectUriPath; + const redirectUriPath = installerOptions.redirectUriPath === undefined ? + '/slack/oauth_redirect' : + installerOptions.redirectUriPath; this.router.use(redirectUriPath, async (req, res) => { await this.installer!.handleCallback(req, res, installerOptions.callbackOptions); }); @@ -399,8 +400,9 @@ export function verifySignatureAndParseRawBody( } function logError(logger: Logger, message: string, error: any): void { - const logMessage = - 'code' in error ? `${message} (code: ${error.code}, message: ${error.message})` : `${message} (error: ${error})`; + const logMessage = 'code' in error ? + `${message} (code: ${error.code}, message: ${error.message})` : + `${message} (error: ${error})`; logger.warn(logMessage); } diff --git a/src/receivers/HTTPReceiver.ts b/src/receivers/HTTPReceiver.ts index b37aee6c1..c3d696dc0 100644 --- a/src/receivers/HTTPReceiver.ts +++ b/src/receivers/HTTPReceiver.ts @@ -123,8 +123,7 @@ export default class HTTPReceiver implements Receiver { // Initialize instance variables, substituting defaults for each value this.signingSecret = signingSecret; this.processBeforeResponse = processBeforeResponse; - this.logger = - logger ?? + this.logger = logger ?? (() => { const defaultLogger = new ConsoleLogger(); defaultLogger.setLevel(logLevel); diff --git a/src/receivers/SocketModeReceiver.ts b/src/receivers/SocketModeReceiver.ts index 8e343c648..4a9436d06 100644 --- a/src/receivers/SocketModeReceiver.ts +++ b/src/receivers/SocketModeReceiver.ts @@ -96,8 +96,7 @@ export default class SocketModeReceiver implements Receiver { // Add OAuth routes to receiver if (this.installer !== undefined) { // use default or passed in redirect path - const redirectUriPath = - installerOptions.redirectUriPath === undefined ? '/slack/oauth_redirect' : installerOptions.redirectUriPath; + const redirectUriPath = installerOptions.redirectUriPath === undefined ? '/slack/oauth_redirect' : installerOptions.redirectUriPath; // use default or passed in installPath const installPath = installerOptions.installPath === undefined ? '/slack/install' : installerOptions.installPath;