question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Incorrectly removing used imports

See original GitHub issue

I have the following file:

import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { UserEntity } from './user.entity';

@Injectable()
export class UsersService {
    constructor(
        @InjectRepository(UserEntity)
        private readonly _usersRepository: Repository<UserEntity>,
    ) {}

    findAll(): Promise<UserEntity[]> {
        return this._usersRepository.find();
    }
}

After running eslint fix, it deletes import { Repository } from 'typeorm'; while @typescript-eslint/no-unused-vars does not detect it as unused import originally eslint output:

  3:10  error  'Repository' is defined but never used  unused-imports/no-unused-imports-ts

✖ 1 problem (1 error, 0 warnings)
  1 error and 0 warnings potentially fixable with the `--fix` option.

Eslint config:

{
    "no-unused-vars": 0,
    "@typescript-eslint/no-unused-vars": "off",
    "unused-imports/no-unused-imports-ts": "error",
}

This is only a single file, but there are more files throughout my whole project where this is happening. Only after adding "unused-imports/no-unused-imports-ts": "error", a lot of used imports are being deleted. When running ESLint with only:

{
        "no-unused-vars": 0,
        "@typescript-eslint/no-unused-vars": 2,
}

No unused import are detected

I’d be happy to provide more examples/code. Let me know!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
stemiscommented, Feb 8, 2021

Works like a charm now, thanks @sweepline !

1reaction
Farenheithcommented, Feb 5, 2021

@sweepline nice! Updating @typescript-eslint/parser seems to solve the problem!

Thank you!

Read more comments on GitHub >

github_iconTop Results From Across the Web

x/tools/cmd/goimports: incorrect removal of imports #29557
I believe that goimports has always removed apparently-unused imports, even when the file isn't fully satisfied, and that's the behavior I tried ...
Read more >
Removing unused import in typescript removes wrong import
When using code action on an unused import it removes the import properly if the cursor was on the right declaration. However if...
Read more >
Any reason to clean up unused imports in Java, other than ...
There was an improper import that a developer had used and abandoned (they used the class, auto-imported it, and then realized it was...
Read more >
Java: Why You Remove Unused Imports - DataDrivenInvestor
It's a quick fix, literally just delete them. However, it led me to wonder what's wrong if they stay? There weren't any issues...
Read more >
How to remove unused imports in VSCode. - LinkedIn
Follow these steps to setup settings in VS Code. Press [Command + , ] to open settings. Click on that top-right icon (Open...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found