Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat(eslint-plugin): [no-floating-promise] add option to ignore IIFEs#1799

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
bradzacher merged 18 commits intotypescript-eslint:masterfromanikethsaha:fixes-647
Apr 20, 2020

Conversation

anikethsaha
Copy link
Contributor

@anikethsahaanikethsaha commentedMar 25, 2020
edited
Loading

fixes#647
added a no check for iife

  • created a new function to detect iifeisIife (please do suggest if the logic there doesn't cover all cases)
  • added tests

@typescript-eslint
Copy link
Contributor

Thanks for the PR,@anikethsaha!

typescript-eslint is a 100% community driven project, and we are incredibly grateful that you are contributing to that community.

The core maintainers work on this in their personal time, so please understand that it may not be possible for them to review your work immediately.

Thanks again!


🙏Please, if you or your company is finding typescript-eslint valuable, help us sustain the project by sponsoring it transparently onhttps://opencollective.com/typescript-eslint. As a thank you, your profile/company logo will be added to our main README which receives thousands of unique visitorsper day.

@bradzacherbradzacher added the enhancement: plugin rule optionNew rule option for an existing eslint-plugin rule labelMar 25, 2020
Copy link
Member

@bradzacherbradzacher left a comment

Choose a reason for hiding this comment

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

could you please gate this functionality behind an option, as per my comment:#647 (comment)

I want end-users to opt-in to this functionality so that they're consciously making the decision for a little unsafety from the rule.

@bradzacherbradzacher added the awaiting responseIssues waiting for a reply from the OP or another party labelMar 25, 2020
@anikethsaha
Copy link
ContributorAuthor

could you please gate this functionality behind an option, as per my comment:#647 (comment)

I want end-users to opt-in to this functionality so that they're consciously making the decision for a little unsafety from the rule.

ok cool, also it should only check for async iife right ? not just iife ?

@bradzacher
Copy link
Member

it should check both - it should just be looking for an IIFE that returns a promise.

@bradzacherbradzacher changed the titlefix: ignores iife for no-floating-promise (fixes #647)fix(eslint-plugin): [no-floating-promise] add option to ignore IIFEsMar 25, 2020
@anikethsaha
Copy link
ContributorAuthor

it should check both - it should just be looking for an IIFE that returns a promise.

make sense , ok creating an options nameignoreIife

bradzacher reacted with thumbs up emoji

@anikethsaha
Copy link
ContributorAuthor

anikethsaha commentedMar 26, 2020
edited
Loading

@bradzacher I think we need to change the selector for AST as well, cause

currently inExpressionStatement

I have this check

if(options.ignoreIife&&isAsyncIife(node)){return;}

And, here is the definition ofisAsyncIife

functionisAsyncIife(node:TSESTree.ExpressionStatement):Boolean{if(node?.expression.type===AST_NODE_TYPES.AwaitExpression){returntrue}if(node?.expression.type===AST_NODE_TYPES.CallExpression){if(possibleIifeCalleType.has(node?.expression?.callee.type)){if(node?.expression?.callee?.hasAsync){returntrue;}returnnode?.expression?.callee?.body?.body.some(hasPromise);}}returnfalse;}

And here ishasPromise

functionhasPromise(node:TSESTree.ExpressionStatement|TSESTree.ReturnStatement):Boolean{// has promise statementif(node?.expression.type==="NewExpression"&&node?.expression?.callee.name==="Promise"){returntrue;}if(// returning promisenode.type===AST_NODE_TYPES.ReturnStatement&&node?.argument.type===AST_NODE_TYPES.NewExpression&&node?.argument?.callee.name==="Promise"){returntrue;}returnfalse}

It looks like when I give input like this

//  options : [{ ignoreIife : true }](asyncfunction(){// error cause of thisawaitres(1);})();

still the there is a floating error.

I did try changing the selectors

["ExpressionStatement > :not(ExpressionStatement )"](node){check(node)},"ExpressionStatement > ExpressionStatement "(node){if(node?.expression.type===AST_NODE_TYPES.AwaitExpression){returntrue}check(node)}

Still no luck

@bradzacher
Copy link
Member

the code you've got currently in the PR doesn't look too far off.

I'd expect something along the lines of:

ExpressionStatement(node):void{if(options.ignoreIIFE&&isIIFE(node)){return;}// old code}functionisIIFE(node:TSESTree.ExpressionStatement):boolean{constexpr=node.expression;if(expr.type!==AST_NODE_TYPES.CallExpression){returnfalse;}return(expr.callee.type===AST_NODE_TYPES.ArrowFunctionExpression||expr.callee.type===AST_NODE_TYPES.FunctionExpression);}

To clarify what this option should do:

These cases should error withignoreIIFE: false (the default)
They shouldnot error withignoreIIFE: true

(functionfoo(){returnPromise.resolve()})();(asyncfunctionfoo(){returnPromise.resolve()})();(async()=>{returnPromise.resolve()})();

However, setting the option to true should not ignore the contents of an IIFE. I.e. this should still error withignoreIIFE: true

(async()=>{Promise.resolve();// error - floating promise})();// no other lint error

@anikethsaha
Copy link
ContributorAuthor

@bradzacher should I add custom type forisAsyncIife method as I addedTSESTree.ExpressionStatement | TSESTree.ArrowFunctionExpression | TSESTree.TSTypeAnnotation type tonode but still typecheck is failing.
also, i triednode?.expression.typeAnnotation.typeand still it failed

Copy link
Member

@bradzacherbradzacher left a comment

Choose a reason for hiding this comment

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

a few suggestions for you

@codecov
Copy link

codecovbot commentedMar 29, 2020
edited
Loading

Codecov Report

Merging#1799 intomaster willincrease coverage by0.00%.
The diff coverage is100.00%.

@@           Coverage Diff           @@##           master    #1799   +/-   ##=======================================  Coverage   94.37%   94.37%           =======================================  Files         164      164             Lines        7572     7579    +7       Branches     2175     2178    +3     =======================================+ Hits         7146     7153    +7  Misses        183      183             Partials      243      243
FlagCoverage Δ
#unittest94.37% <100.00%> (+<0.01%)⬆️
Impacted FilesCoverage Δ
...es/eslint-plugin/src/rules/no-floating-promises.ts100.00% <100.00%> (ø)

Copy link
Member

@bradzacherbradzacher left a comment

Choose a reason for hiding this comment

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

a few final comments, otherwise LGTM - thanks for this.

@bradzacher
Copy link
Member

@anikethsaha
Copy link
ContributorAuthor

same issue here as well

image

@bradzacherbradzacher removed the awaiting responseIssues waiting for a reply from the OP or another party labelApr 4, 2020
@bradzacherbradzacher changed the titlefix(eslint-plugin): [no-floating-promise] add option to ignore IIFEsfeat(eslint-plugin): [no-floating-promise] add option to ignore IIFEsApr 20, 2020
bradzacher
bradzacher previously approved these changesApr 20, 2020
Copy link
Member

@bradzacherbradzacher left a comment

Choose a reason for hiding this comment

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

thanks!

bradzacher
bradzacher previously approved these changesApr 20, 2020
Sign up for freeto subscribe to this conversation on GitHub. Already have an account?Sign in.
Reviewers

@JoshuaKGoldbergJoshuaKGoldbergJoshuaKGoldberg left review comments

@bradzacherbradzacherbradzacher left review comments

Assignees
No one assigned
Labels
enhancement: plugin rule optionNew rule option for an existing eslint-plugin rule
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

[no-floating-promises] Should not report on async IIFEs
3 participants
@anikethsaha@bradzacher@JoshuaKGoldberg

[8]ページ先頭

©2009-2025 Movatter.jp