Skip to content

Commit

Permalink
Apply bunch of workaround for TypeScript 4.7 compiler compatibility (#…
Browse files Browse the repository at this point in the history
…1466)

* Apply bunch of workaround for TypeScript 4.7 compiler compatibility
  • Loading branch information
seratch committed May 27, 2022
1 parent 098f279 commit 23cc0e1
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 50 deletions.
31 changes: 24 additions & 7 deletions src/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -555,10 +555,12 @@ export default class App {
'If you want to filter message events, you can use event.channel_type for it.',
);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const _listeners = listeners as any; // FIXME: workaround for TypeScript 4.7 breaking changes
this.listeners.push([
onlyEvents,
matchEventType(eventNameOrPattern),
...listeners,
..._listeners,
] as Middleware<AnyMiddlewareArgs>[]);
}

Expand Down Expand Up @@ -605,7 +607,8 @@ export default class App {
return matchMessage(patternOrMiddleware);
}
return patternOrMiddleware;
});
// eslint-disable-next-line @typescript-eslint/no-explicit-any
}) as any; // FIXME: workaround for TypeScript 4.7 breaking changes

this.listeners.push([
onlyEvents,
Expand Down Expand Up @@ -645,10 +648,12 @@ export default class App {
return;
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const _listeners = listeners as any; // FIXME: workaround for TypeScript 4.7 breaking changes
this.listeners.push([
onlyShortcuts,
matchConstraints(constraints),
...listeners,
..._listeners,
] as Middleware<AnyMiddlewareArgs>[]);
}

Expand Down Expand Up @@ -689,11 +694,19 @@ export default class App {
return;
}

this.listeners.push([onlyActions, matchConstraints(constraints), ...listeners] as Middleware<AnyMiddlewareArgs>[]);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const _listeners = listeners as any; // FIXME: workaround for TypeScript 4.7 breaking changes
this.listeners.push([onlyActions, matchConstraints(constraints), ..._listeners] as Middleware<AnyMiddlewareArgs>[]);
}

public command(commandName: string | RegExp, ...listeners: Middleware<SlackCommandMiddlewareArgs>[]): void {
this.listeners.push([onlyCommands, matchCommandName(commandName), ...listeners] as Middleware<AnyMiddlewareArgs>[]);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const _listeners = listeners as any; // FIXME: workaround for TypeScript 4.7 breaking changes
this.listeners.push([
onlyCommands,
matchCommandName(commandName),
..._listeners,
] as Middleware<AnyMiddlewareArgs>[]);
}

public options<Source extends OptionsSource = 'block_suggestion'>(
Expand All @@ -714,7 +727,9 @@ export default class App {
{ action_id: actionIdOrConstraints } :
actionIdOrConstraints;

this.listeners.push([onlyOptions, matchConstraints(constraints), ...listeners] as Middleware<AnyMiddlewareArgs>[]);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const _listeners = listeners as any; // FIXME: workaround for TypeScript 4.7 breaking changes
this.listeners.push([onlyOptions, matchConstraints(constraints), ..._listeners] as Middleware<AnyMiddlewareArgs>[]);
}

public view<ViewActionType extends SlackViewAction = SlackViewAction>(
Expand Down Expand Up @@ -746,10 +761,12 @@ export default class App {
return;
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const _listeners = listeners as any; // FIXME: workaround for TypeScript 4.7 breaking changes
this.listeners.push([
onlyViewActions,
matchConstraints(constraints),
...listeners,
..._listeners,
] as Middleware<AnyMiddlewareArgs>[]);
}

Expand Down
108 changes: 75 additions & 33 deletions src/middleware/builtin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ describe('Built-in global middleware', () => {
it('should detect valid requests', async () => {
const payload: SlashCommand = { ...validCommandPayload };
const fakeNext = sinon.fake();
await onlyCommands({
const args = {
logger,
client,
payload,
Expand All @@ -511,14 +511,15 @@ describe('Built-in global middleware', () => {
ack: noop,
next: fakeNext,
context: {},
});
};
await onlyCommands(args);
assert.isTrue(fakeNext.called);
});

it('should skip other requests', async () => {
const payload: any = {};
const fakeNext = sinon.fake();
await onlyCommands({
const args = {
logger,
client,
payload,
Expand All @@ -530,7 +531,8 @@ describe('Built-in global middleware', () => {
ack: noop,
next: fakeNext,
context: {},
});
};
await onlyCommands(args);
assert.isTrue(fakeNext.notCalled);
});
});
Expand Down Expand Up @@ -580,7 +582,9 @@ describe('Built-in global middleware', () => {

it('should detect valid requests', async () => {
const fakeNext = sinon.fake();
const args: SlackEventMiddlewareArgs<'app_mention'> & { event?: SlackEvent } = {
// FIXME: Removing type def here is a workaround for TypeScript 4.7 breaking changes
// TS2589: Type instantiation is excessively deep and possibly infinite.
const args /* : SlackEventMiddlewareArgs<'app_mention'> & { event?: SlackEvent } */ = {
payload: appMentionEvent,
event: appMentionEvent,
message: null as never, // a bit hackey to satisfy TS compiler as 'null' cannot be assigned to type 'never'
Expand All @@ -596,20 +600,23 @@ describe('Built-in global middleware', () => {
},
say: sayNoop,
};
await onlyEvents({
const allArgs = {
logger,
client,
next: fakeNext,
context: {},
...args,
});
};
// FIXME: Using any is a workaround for TypeScript 4.7 breaking changes
// TS2589: Type instantiation is excessively deep and possibly infinite.
await onlyEvents(allArgs as any);
assert.isTrue(fakeNext.called);
});

it('should skip other requests', async () => {
const payload: SlashCommand = { ...validCommandPayload };
const fakeNext = sinon.fake();
await onlyEvents({
const args = {
logger,
client,
payload,
Expand All @@ -620,7 +627,8 @@ describe('Built-in global middleware', () => {
ack: noop,
next: fakeNext,
context: {},
});
};
await onlyEvents(args);
assert.isFalse(fakeNext.called);
});
});
Expand All @@ -630,6 +638,8 @@ describe('Built-in global middleware', () => {
const client = new WebClient(undefined, { logger, slackApiUrl: undefined });

function buildArgs(): SlackEventMiddlewareArgs<'app_mention'> & { event?: SlackEvent } {
// FIXME: Using any here is a workaround for TypeScript 4.7 breaking changes
// TS2589: Type instantiation is excessively deep and possibly infinite.
return {
payload: appMentionEvent,
event: appMentionEvent,
Expand All @@ -645,12 +655,14 @@ describe('Built-in global middleware', () => {
authed_users: [],
},
say: sayNoop,
};
} as any;
}

function buildArgsAppHomeOpened(): SlackEventMiddlewareArgs<'app_home_opened'> & {
event?: SlackEvent;
} {
// FIXME: Using any here is a workaround for TypeScript 4.7 breaking changes
// TS2589: Type instantiation is excessively deep and possibly infinite.
return {
payload: appHomeOpenedEvent,
event: appHomeOpenedEvent,
Expand All @@ -666,66 +678,86 @@ describe('Built-in global middleware', () => {
authed_users: [],
},
say: sayNoop,
};
} as any;
}

it('should detect valid requests', async () => {
const fakeNext = sinon.fake();
await matchEventType('app_mention')({
// FIXME: Using any here is a workaround for TypeScript 4.7 breaking changes
// TS2589: Type instantiation is excessively deep and possibly infinite.
const _args = buildArgs() as any;
const args = {
logger,
client,
next: fakeNext,
context: {},
...buildArgs(),
});
..._args,
};
await matchEventType('app_mention')(args);
assert.isTrue(fakeNext.called);
});

it('should detect valid RegExp requests with app_mention', async () => {
const fakeNext = sinon.fake();
await matchEventType(/app_mention|app_home_opened/)({
// FIXME: Using any here is a workaround for TypeScript 4.7 breaking changes
// TS2589: Type instantiation is excessively deep and possibly infinite.
const _args = buildArgs() as any;
const args = {
logger,
client,
next: fakeNext,
context: {},
...buildArgs(),
});
..._args,
};
await matchEventType(/app_mention|app_home_opened/)(args);
assert.isTrue(fakeNext.called);
});

it('should detect valid RegExp requests with app_home_opened', async () => {
const fakeNext = sinon.fake();
await matchEventType(/app_mention|app_home_opened/)({
// FIXME: Using any here is a workaround for TypeScript 4.7 breaking changes
// TS2589: Type instantiation is excessively deep and possibly infinite.
const _args = buildArgsAppHomeOpened() as any;
const args = {
logger,
client,
next: fakeNext,
context: {},
...buildArgsAppHomeOpened(),
});
..._args,
};
await matchEventType(/app_mention|app_home_opened/)(args);
assert.isTrue(fakeNext.called);
});

it('should skip other requests', async () => {
const fakeNext = sinon.fake();
await matchEventType('app_home_opened')({
// FIXME: Using any here is a workaround for TypeScript 4.7 breaking changes
// TS2589: Type instantiation is excessively deep and possibly infinite.
const _args = buildArgs() as any;
const args = {
logger,
client,
next: fakeNext,
context: {},
...buildArgs(),
});
..._args,
};
await matchEventType('app_home_opened')(args);
assert.isFalse(fakeNext.called);
});

it('should skip other requests for RegExp', async () => {
const fakeNext = sinon.fake();
await matchEventType(/foo/)({
// FIXME: Using any here is a workaround for TypeScript 4.7 breaking changes
// TS2589: Type instantiation is excessively deep and possibly infinite.
const _args = buildArgs() as any;
const args = {
logger,
client,
next: fakeNext,
context: {},
...buildArgs(),
});
..._args,
} as any;
await matchEventType(/foo/)(args);
assert.isFalse(fakeNext.called);
});
});
Expand All @@ -735,6 +767,8 @@ describe('Built-in global middleware', () => {
const client = new WebClient(undefined, { logger, slackApiUrl: undefined });

function buildArgs(): SlackEventMiddlewareArgs<'message'> & { event?: SlackEvent } {
// FIXME: Using any here is a workaround for TypeScript 4.7 breaking changes
// TS2589: Type instantiation is excessively deep and possibly infinite.
return {
payload: botMessageEvent,
event: botMessageEvent,
Expand All @@ -750,30 +784,38 @@ describe('Built-in global middleware', () => {
authed_users: [],
},
say: sayNoop,
};
} as any;
}

it('should detect valid requests', async () => {
const fakeNext = sinon.fake();
await subtype('bot_message')({
// FIXME: Using any here is a workaround for TypeScript 4.7 breaking changes
// TS2589: Type instantiation is excessively deep and possibly infinite.
const _args = buildArgs() as any;
const args = {
logger,
client,
next: fakeNext,
context: {},
...buildArgs(),
});
..._args,
};
await subtype('bot_message')(args);
assert.isTrue(fakeNext.called);
});

it('should skip other requests', async () => {
const fakeNext = sinon.fake();
await subtype('me_message')({
// FIXME: Using any here is a workaround for TypeScript 4.7 breaking changes
// TS2589: Type instantiation is excessively deep and possibly infinite.
const _args = buildArgs() as any;
const args = {
logger,
client,
next: fakeNext,
context: {},
...buildArgs(),
});
..._args,
};
await subtype('me_message')(args);
assert.isFalse(fakeNext.called);
});
});
Expand Down

0 comments on commit 23cc0e1

Please sign in to comment.