Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[no-unused-expressions] false positive on dynamic import() #2108

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

Closed
derekparsons718 opened this issue May 26, 2020 · 4 comments Β· Fixed by #2130
Closed

[no-unused-expressions] false positive on dynamic import() #2108

derekparsons718 opened this issue May 26, 2020 · 4 comments Β· Fixed by #2130
Labels
bug Something isn't working good first issue Good for newcomers package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@derekparsons718
Copy link

I believe I'm getting a false positive for no-unused-expressions when I use dynamic import statements.

Repro

{
  "rules": {
    "@typescript-eslint/no-unused-expressions": "error"
  }
}
//At the top of a file
import("./app/app.module"); //no-unused-expressions error

Expected Result

No linting error. The dynamic import is not unused or unnecessary.

Actual Result

The line is marked as a "no-unused-expressions" error.

Additional Info

Adding a .then() removes the error.

import("./app/app.module").then(); //no error

This error only appeared after I upgraded from v2.34.0 to v3.0.1. I didn't see anything in the release notes about this rule changing.

Versions

package version
@typescript-eslint/eslint-plugin 3.0.1
@typescript-eslint/parser 3.0.1
TypeScript 3.9.3
ESLint 7.1.0
node 13.11.0
npm 6.13.7
@derekparsons718 derekparsons718 added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels May 26, 2020
@bradzacher bradzacher added bug Something isn't working good first issue Good for newcomers and removed triage Waiting for team members to take a look labels May 26, 2020
@bradzacher
Copy link
Member

bradzacher commented May 26, 2020

yeah this is just not handled yet - it's not a common case at all.

OOC - how often do you have a dangling import('foo')? They're async imports so it seems weird to have one with no await or .then.
Or was this just something you ran into randomly whilst typing?

(note - adding an await or a .then fixes it because it converts it into an expression the base rule understands)

@derekparsons718
Copy link
Author

We use dynamic imports to accomplish code-splitting in webpack and to enable prefetching/preloading via webpack.

To my understanding, the example I gave doesn't need a .then because it is loading an angularjs application, which I guess ends up bootstrapping itself as a sort of side-effect. Another place we use a dangling import() is to load css files via webpack, which we could do with a static import but we wanted to take advantage of the code-splitting and prefetching that comes with the dynamic imports. Obviously, a css file loaded via webpack doesn't need a .then either.

To be honest, I'm not an expert in webpack so maybe these are bad reasons haha, but hopefully that answers your question.

@bradzacher
Copy link
Member

That makes sense yeah.

It wouldn't be a difficult fix if you'd like to give it a go!
I believe all you need to check for and ignore is an ImportExpression.

@derekparsons718
Copy link
Author

I've never contributed to an open source project before, but I'd love to take a crack at it.

Unfortunately I won't be able to start for a couple weeks, but if the issue is still open at that point I'll definitely come back to it.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 30, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working good first issue Good for newcomers package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants