Skip to content

ESLint Config Migration: Fixing consistent-return linter warnings#1075

Merged
filmaj merged 1 commit intotslint-to-eslintfrom
consistent-return-fixes
Aug 25, 2021
Merged

ESLint Config Migration: Fixing consistent-return linter warnings#1075
filmaj merged 1 commit intotslint-to-eslintfrom
consistent-return-fixes

Conversation

@filmaj
Copy link
Copy Markdown
Contributor

@filmaj filmaj commented Aug 23, 2021

Summary

This is a PR that should be merged into #1024 and incrementally addresses #842.

This PR is a separate approach to #1049: instead of turning off the consistent-return rule, here is my attempt at adhering to the rule.

Impact

Before

✖ 200 problems (11 errors, 189 warnings)

After

✖ 191 problems (2 errors, 189 warnings)

Requirements (place an x in each [ ])

@filmaj filmaj added the tests M-T: Testing work only label Aug 23, 2021
@filmaj filmaj requested review from misscoded and seratch August 23, 2021 20:50
@filmaj filmaj self-assigned this Aug 23, 2021
this.logger.warn('Authorization of incoming event did not succeed. No listeners will be called.');
error.code = 'slack_bolt_authorization_error';
// disabling due to https://github.com/typescript-eslint/typescript-eslint/issues/1277
// eslint-disable-next-line consistent-return
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TL;DR typescript-eslint/typescript-eslint#1277 core eslint consistent-return is not type aware, and typescript-eslint has not implemented an extension to avoid this issue (functions that return Promise<void> are not honoured in terms of return type)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The clear comment about the reason here is really nice 👍

return processMiddleware(
listenerMiddleware,
listenerArgs as AnyMiddlewareArgs,
if (listener === undefined) {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this part, we have to explicitly, instead of implicitly, return undefined in the case the listener is undefined. I like to nest/indent the exceptional case and return right away, which allows me to do further processing below this conditional nested one indent less. So that's what I did here. 🤷

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is fine.

@filmaj filmaj changed the title Fixing consistent-return linter warnings. ESLint Config Migration: Fixing consistent-return linter warnings Aug 23, 2021
Copy link
Copy Markdown
Contributor

@seratch seratch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for revising this! This pull request looks greater to me.

this.logger.warn('Authorization of incoming event did not succeed. No listeners will be called.');
error.code = 'slack_bolt_authorization_error';
// disabling due to https://github.com/typescript-eslint/typescript-eslint/issues/1277
// eslint-disable-next-line consistent-return
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The clear comment about the reason here is really nice 👍

return processMiddleware(
listenerMiddleware,
listenerArgs as AnyMiddlewareArgs,
if (listener === undefined) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is fine.

return reject(new ReceiverInconsistentStateError('The receiver cannot be stopped because it was not started.'));
}
this.server.close((error) => {
this.server?.close((error) => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm. I assume ? was introduced because it no longer connects the check done above with the logic that follows. Can that addition be avoided if we added an else with the alternative path inside?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@misscoded so if I remove the ?, when compiling the code (and, in particular, when trying to run the tests via npm run mocha), I get this:

➜ npm run mocha

> @slack/bolt@3.5.0 mocha /Users/fmaj/src/bolt-js
> TS_NODE_PROJECT=tsconfig.json nyc mocha --config .mocharc.json "src/**/*.spec.ts"


/Users/fmaj/src/bolt-js/node_modules/ts-node/src/index.ts:434
    return new TSError(diagnosticText, diagnosticCodes)
           ^
TSError: ⨯ Unable to compile TypeScript:
src/receivers/ExpressReceiver.ts:342:7 - error TS2532: Object is possibly 'undefined'.

342       this.server.close((error) => {

... which is silly, since we have a check for undefined right before, but I guess because of the intervening Promise function scope, the TS compiler can't assume that the two scopes are executed in the same context / immediately after one another? Not sure.

If anyone has suggestions how to avoid this, I'd love to learn!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm.. ok! No suggestions here at the moment

return reject(new ReceiverInconsistentStateError('The receiver cannot be stopped because it was not started.'));
}
this.server.close((error) => {
this.server?.close((error) => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment/question as stop() in the ExpressReceiver.ts

@filmaj filmaj force-pushed the consistent-return-fixes branch from 4c4fe5c to 93dbf74 Compare August 25, 2021 17:05
@filmaj filmaj merged commit e6b10eb into tslint-to-eslint Aug 25, 2021
@filmaj filmaj deleted the consistent-return-fixes branch August 25, 2021 18:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

tests M-T: Testing work only

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants