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

ESLint Config Migration: Fixing lint errors for "no-use-before-define" rule. #1036

Merged
merged 1 commit into from
Aug 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 21 additions & 20 deletions src/App.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,27 @@ const noopMiddleware = async ({ next }: { next: NextFn }) => {
};
const noopAuthorize = () => Promise.resolve({});

// Fakes
class FakeReceiver implements Receiver {
private bolt: App | undefined;

public init = (bolt: App) => {
this.bolt = bolt;
};

public start = sinon.fake((...params: any[]): Promise<unknown> => {
return Promise.resolve([...params]);
});

public stop = sinon.fake((...params: any[]): Promise<unknown> => {
return Promise.resolve([...params]);
});

public async sendEvent(event: ReceiverEvent): Promise<void> {
return this.bolt?.processEvent(event);
}
}

// Dummies (values that have no real behavior but pass through the system opaquely)
function createDummyReceiverEvent(type: string = 'dummy_event_type'): ReceiverEvent {
// NOTE: this is a degenerate ReceiverEvent that would successfully pass through the App. it happens to look like a
Expand Down Expand Up @@ -2100,23 +2121,3 @@ function withConversationContext(spy: SinonSpy): Override {
};
}

// Fakes
class FakeReceiver implements Receiver {
private bolt: App | undefined;

public init = (bolt: App) => {
this.bolt = bolt;
};

public start = sinon.fake((...params: any[]): Promise<unknown> => {
return Promise.resolve([...params]);
});

public stop = sinon.fake((...params: any[]): Promise<unknown> => {
return Promise.resolve([...params]);
});

public async sendEvent(event: ReceiverEvent): Promise<void> {
return this.bolt?.processEvent(event);
}
}
21 changes: 11 additions & 10 deletions src/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ import allSettled = require('promise.allsettled'); // eslint-disable-line @types
// eslint-disable-next-line @typescript-eslint/no-require-imports
const packageJson = require('../package.json'); // eslint-disable-line @typescript-eslint/no-var-requires

// ----------------------------
// For listener registration methods

const validViewTypes = ['view_closed', 'view_submission'];

// ----------------------------
// For the constructor

const tokenUsage = 'Apps used in one workspace should be initialized with a token. Apps used in many workspaces ' +
'should be initialized with oauth installer or authorize.';

/** App initialization options */
export interface AppOptions {
signingSecret?: HTTPReceiverOptions['signingSecret'];
Expand Down Expand Up @@ -868,12 +879,6 @@ export default class App {
}
}

// ----------------------------
// For the constructor

const tokenUsage =
'Apps used in one workspace should be initialized with a token. Apps used in many workspaces ' +
'should be initialized with oauth installer or authorize.';

function defaultErrorHandler(logger: Logger): ErrorHandler {
return (error) => {
Expand Down Expand Up @@ -1135,10 +1140,6 @@ function buildRespondFn(
};
}

// ----------------------------
// For listener registration methods

const validViewTypes = ['view_closed', 'view_submission'];

// ----------------------------
// Instrumentation
Expand Down
24 changes: 12 additions & 12 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ export enum ErrorCode {
WorkflowStepInitializationError = 'slack_bolt_workflow_step_initialization_error',
}

export class UnknownError extends Error implements CodedError {
public code = ErrorCode.UnknownError;

public original: Error;

constructor(original: Error) {
super(original.message);

this.original = original;
}
}

export function asCodedError(error: CodedError | Error): CodedError {
if ((error as CodedError).code !== undefined) {
return error as CodedError;
Expand Down Expand Up @@ -113,18 +125,6 @@ export class MultipleListenerError extends Error implements CodedError {
}
}

export class UnknownError extends Error implements CodedError {
public code = ErrorCode.UnknownError;

public original: Error;

constructor(original: Error) {
super(original.message);

this.original = original;
}
}

export class WorkflowStepInitializationError extends Error implements CodedError {
public code = ErrorCode.WorkflowStepInitializationError;
}
113 changes: 57 additions & 56 deletions src/middleware/builtin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,63 @@ import { SlashCommand } from '../types/command';
import { AppMentionEvent, AppHomeOpenedEvent } from '../types/events';
import { GenericMessageEvent } from '../types/events/message-events';

// Test fixtures
const validCommandPayload: SlashCommand = {
token: 'token-value',
command: '/hi',
text: 'Steve!',
response_url: 'https://hooks.slack.com/foo/bar',
trigger_id: 'trigger-id-value',
user_id: 'U1234567',
user_name: 'steve',
team_id: 'T1234567',
team_domain: 'awesome-eng-team',
channel_id: 'C1234567',
channel_name: 'random',
api_app_id: 'A123456',
};

const appMentionEvent: AppMentionEvent = {
type: 'app_mention',
username: 'USERNAME',
user: 'U1234567',
text: 'this is my message',
ts: '123.123',
channel: 'C1234567',
event_ts: '123.123',
thread_ts: '123.123',
};

const appHomeOpenedEvent: AppHomeOpenedEvent = {
type: 'app_home_opened',
user: 'USERNAME',
channel: 'U1234567',
tab: 'home',
view: {
type: 'home',
blocks: [],
clear_on_close: false,
notify_on_close: false,
external_id: '',
},
event_ts: '123.123',
};

const botMessageEvent: MessageEvent = {
type: 'message',
subtype: 'bot_message',
channel: 'CHANNEL_ID',
event_ts: '123.123',
user: 'U1234567',
ts: '123.123',
text: 'this is my message',
bot_id: 'B1234567',
channel_type: 'channel',
};

const noop = () => Promise.resolve(undefined);
const sayNoop = () => Promise.resolve({ ok: true });

describe('matchMessage()', () => {
function initializeTestCase(pattern: string | RegExp): Mocha.AsyncFunc {
return async () => {
Expand Down Expand Up @@ -782,59 +839,3 @@ function createFakeAppMentionEvent(text: string = ''): AppMentionEvent {
};
return event as AppMentionEvent;
}

const validCommandPayload: SlashCommand = {
token: 'token-value',
command: '/hi',
text: 'Steve!',
response_url: 'https://hooks.slack.com/foo/bar',
trigger_id: 'trigger-id-value',
user_id: 'U1234567',
user_name: 'steve',
team_id: 'T1234567',
team_domain: 'awesome-eng-team',
channel_id: 'C1234567',
channel_name: 'random',
api_app_id: 'A123456',
};

const appMentionEvent: AppMentionEvent = {
type: 'app_mention',
username: 'USERNAME',
user: 'U1234567',
text: 'this is my message',
ts: '123.123',
channel: 'C1234567',
event_ts: '123.123',
thread_ts: '123.123',
};

const appHomeOpenedEvent: AppHomeOpenedEvent = {
type: 'app_home_opened',
user: 'USERNAME',
channel: 'U1234567',
tab: 'home',
view: {
type: 'home',
blocks: [],
clear_on_close: false,
notify_on_close: false,
external_id: '',
},
event_ts: '123.123',
};

const botMessageEvent: MessageEvent = {
type: 'message',
subtype: 'bot_message',
channel: 'CHANNEL_ID',
event_ts: '123.123',
user: 'U1234567',
ts: '123.123',
text: 'this is my message',
bot_id: 'B1234567',
channel_type: 'channel',
};

const noop = () => Promise.resolve(undefined);
const sayNoop = () => Promise.resolve({ ok: true });
53 changes: 28 additions & 25 deletions src/receivers/ExpressReceiver.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,34 @@ import ExpressReceiver, {
verifySignatureAndParseRawBody,
} from './ExpressReceiver';

// Fakes
class FakeServer extends EventEmitter {
public on = sinon.fake();

public listen = sinon.fake((...args: any[]) => {
if (this.listeningFailure !== undefined) {
this.emit('error', this.listeningFailure);
return;
}
setImmediate(() => {
args[1]();
});
});

public close = sinon.fake((...args: any[]) => {
setImmediate(() => {
this.emit('close');
setImmediate(() => {
args[0]();
});
});
});

public constructor(private listeningFailure?: Error) {
super();
}
}

describe('ExpressReceiver', function () {
beforeEach(function () {
this.fakeServer = new FakeServer();
Expand Down Expand Up @@ -679,28 +707,3 @@ function withHttpsCreateServer(spy: SinonSpy): Override {
};
}

// Fakes
class FakeServer extends EventEmitter {
public on = sinon.fake();
public listen = sinon.fake((...args: any[]) => {
if (this.listeningFailure !== undefined) {
this.emit('error', this.listeningFailure);
return;
}
setImmediate(() => {
args[1]();
});
});
public close = sinon.fake((...args: any[]) => {
setImmediate(() => {
this.emit('close');
setImmediate(() => {
args[0]();
});
});
});

constructor(private listeningFailure?: Error) {
super();
}
}