Skip to content

Commit 7a0052f

Browse files
committed
Lint Typescript
1 parent 4d617d3 commit 7a0052f

File tree

7 files changed

+52
-32
lines changed

7 files changed

+52
-32
lines changed

.eslintrc

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
11
{
2-
"extends": ["standard"],
2+
"extends": ["standard-with-typescript"],
3+
"parserOptions": {
4+
"project": "./tsconfig.json"
5+
},
6+
"overrides": [
7+
{
8+
"files": "*.ts",
9+
"extends": ["plugin:prettier/recommended", "prettier/@typescript-eslint"],
10+
"rules": {
11+
"@typescript-eslint/explicit-function-return-type": "off",
12+
"@typescript-eslint/no-non-null-assertion": "warn",
13+
"@typescript-eslint/promise-function-async": "off",
14+
"@typescript-eslint/strict-boolean-expressions": "warn"
15+
}
16+
}
17+
],
318
"plugins": ["ava"]
419
}

.prettierrc.json

-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
{
2-
"insertPragma": true,
3-
"requirePragma": true,
42
"semi": false,
53
"singleQuote": true
64
}

package.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,17 @@
5454
},
5555
"devDependencies": {
5656
"@types/node": "^13.1.0",
57+
"@typescript-eslint/eslint-plugin": "^3.5.0",
58+
"@typescript-eslint/parser": "^3.5.0",
5759
"ava": "^3.0.0",
58-
"eslint": "^6.2.2",
60+
"eslint": "^7.4.0",
61+
"eslint-config-prettier": "^6.11.0",
5962
"eslint-config-standard": "^14.1.0",
63+
"eslint-config-standard-with-typescript": "^18.0.2",
6064
"eslint-plugin-ava": "^10.0.0",
6165
"eslint-plugin-import": "^2.2.0",
6266
"eslint-plugin-node": "^11.0.0",
67+
"eslint-plugin-prettier": "^3.1.4",
6368
"eslint-plugin-promise": "^4.0.0",
6469
"eslint-plugin-standard": "^4.0.0",
6570
"husky": "^4.2.0",

src/composer.ts

+12-8
Original file line numberDiff line numberDiff line change
@@ -190,12 +190,14 @@ class Composer<TContext extends Context> implements Middleware.Obj<TContext> {
190190

191191
/**
192192
* Generates middleware that runs in the background.
193+
* @deprecated
193194
*/
194195
static fork<TContext extends Context>(
195196
middleware: Middleware<TContext>
196197
): Middleware.Fn<TContext> {
197198
const handler = Composer.unwrap(middleware)
198199
return (ctx, next) => {
200+
// eslint-disable-next-line @typescript-eslint/no-misused-promises
199201
setImmediate(handler, ctx, Composer.safePassThru())
200202
return next()
201203
}
@@ -316,6 +318,7 @@ class Composer<TContext extends Context> implements Middleware.Obj<TContext> {
316318
return entities?.some((entity) =>
317319
predicate(
318320
entity,
321+
// eslint-disable-next-line @typescript-eslint/restrict-plus-operands
319322
text.substring(entity.offset, entity.offset + entity.length),
320323
ctx
321324
)
@@ -333,19 +336,19 @@ class Composer<TContext extends Context> implements Middleware.Obj<TContext> {
333336
if (fns.length === 0) {
334337
// prettier-ignore
335338
return Array.isArray(predicate)
336-
//@ts-expect-error
339+
// @ts-expect-error
337340
? Composer.entity(entityType, ...predicate)
338-
//@ts-expect-error
341+
// @ts-expect-error
339342
: Composer.entity(entityType, predicate)
340343
}
341344
const triggers = normalizeTriggers(predicate)
342-
//@ts-expect-error
345+
// @ts-expect-error
343346
return Composer.entity(({ type }, value, ctx) => {
344347
if (type !== entityType) {
345348
return false
346349
}
347350
for (const trigger of triggers) {
348-
//@ts-expect-error
351+
// @ts-expect-error
349352
ctx.match = trigger(value, ctx)
350353
if (ctx.match) {
351354
return true
@@ -435,8 +438,8 @@ class Composer<TContext extends Context> implements Middleware.Obj<TContext> {
435438
(ctx.message && (ctx.message.caption || ctx.message.text)) ||
436439
(ctx.channelPost &&
437440
(ctx.channelPost.caption || ctx.channelPost.text)) ||
438-
(ctx.callbackQuery && ctx.callbackQuery.data) ||
439-
(ctx.inlineQuery && ctx.inlineQuery.query)
441+
ctx.callbackQuery?.data ||
442+
ctx.inlineQuery?.query
440443
for (const trigger of triggers) {
441444
const match = trigger(text, ctx)
442445
if (match) {
@@ -468,7 +471,7 @@ class Composer<TContext extends Context> implements Middleware.Obj<TContext> {
468471
...fns: NonemptyReadonlyArray<Middleware<TContext>>
469472
) {
470473
if (fns.length === 0) {
471-
//@ts-expect-error
474+
// @ts-expect-error
472475
return Composer.entity(['bot_command'], command)
473476
}
474477
const commands = normalizeTextArguments(command, '/')
@@ -478,7 +481,8 @@ class Composer<TContext extends Context> implements Middleware.Obj<TContext> {
478481
const groupCommands =
479482
ctx.me &&
480483
(ctx.chat.type === 'group' || ctx.chat.type === 'supergroup')
481-
? commands.map((command) => `${command}@${ctx.me}`)
484+
? // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
485+
commands.map((command) => `${command}@${ctx.me}`)
482486
: []
483487
return Composer.entity(
484488
({ offset, type }, value) =>

src/router.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class Router<TContext extends Context> implements Middleware.Obj<TContext> {
4848
}
4949
Object.assign(ctx, result.context)
5050
Object.assign(ctx.state, result.state)
51-
return this.handlers.get(result.route) || this.otherwiseHandler
51+
return this.handlers.get(result.route) ?? this.otherwiseHandler
5252
})
5353
})
5454
}

src/types.ts

+14-12
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
/** @format */
22

3-
//@ts-ignore
4-
import Context from './context'
3+
import type Context from './context'
54

5+
// eslint-disable-next-line @typescript-eslint/no-namespace
66
export namespace Middleware {
7-
export interface Fn<TContext extends Context> {
8-
/*
9-
next's parameter is in a contravariant position, and thus, trying to type it
10-
prevents assigning `MiddlewareFn<ContextMessageUpdate>`
11-
to `MiddlewareFn<CustomContext>`.
12-
Middleware passing the parameter should be a separate type instead.
13-
*/
14-
(ctx: TContext, next: () => Promise<void>): void | Promise<unknown>
15-
}
7+
/*
8+
next's parameter is in a contravariant position, and thus, trying to type it
9+
prevents assigning `MiddlewareFn<ContextMessageUpdate>`
10+
to `MiddlewareFn<CustomContext>`.
11+
Middleware passing the parameter should be a separate type instead.
12+
*/
13+
export type Fn<TContext extends Context> = (
14+
ctx: TContext,
15+
next: () => Promise<void>
16+
// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
17+
) => void | Promise<unknown>
1618
export interface Obj<TContext extends Context> {
17-
middleware(): Fn<TContext>
19+
middleware: () => Fn<TContext>
1820
}
1921
}
2022
export type Middleware<TContext extends Context> =

typings/telegram-types.d.ts

+3-7
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,7 @@ export type InputFile =
174174
| InputFileByBuffer
175175
| InputFileByURL
176176

177-
178-
export type DiceEmoji =
179-
'🎲' |
180-
'🎯' |
181-
'🏀'
177+
export type DiceEmoji = '🎲' | '🎯' | '🏀'
182178

183179
/**
184180
* Sending video notes by a URL is currently unsupported
@@ -786,8 +782,8 @@ export interface Dice {
786782
/**
787783
* Emoji of the dice
788784
*/
789-
emoji: DiceEmoji,
790-
785+
emoji: DiceEmoji
786+
791787
/**
792788
* Value of the dice
793789
*/

0 commit comments

Comments
 (0)