kcmr / vsce-remove-unused-imports

VS Code extension to remove unused ES6 imports inside JavaScript and TypeScript files
https://marketplace.visualstudio.com/items?itemName=kuscamara.remove-unused-imports
MIT License
17 stars 5 forks source link

Removed used function in decorator #38

Open jianzhou520 opened 3 weeks ago

jianzhou520 commented 3 weeks ago

I'm using this plugin in our NestJS projects, in many tims it works well, but misunderstand the following code

import { forwardRef, Inject, Injectable } from '@nestjs/common'
...
@Inject(forwardRef(() => InventoryCheckService))
...

after save

import { Inject, Injectable } from '@nestjs/common'
...
@Inject(forwardRef(() => InventoryCheckService))
kcmr commented 3 weeks ago

Hi @jianzhou520

Which version of the extension do you have? I'm not able to reproduce the issue you describe. forwardRef is not removed, but Injectable is.

I'm getting a SyntaxError Leading decorators must be attached to a class declaration

jianzhou520 commented 2 weeks ago

Hi @kcmr Our team are now using v1.2.4, there is more complete code:

import { forwardRef, Inject, Injectable } from '@nestjs/common'

@Injectable()
export class AutoSaveTestService {
  constructor(
     @Inject(forwardRef(() => TestRemoveService))
  ){}
}
kcmr commented 1 week ago

Hi @jianzhou520

I couldn't get the code you provided to work, but I have a similar use case in these tests: https://github.com/kcmr/vsce-remove-unused-imports/blob/master/__tests__/transform.spec.js#L101-L104.

I'm not familiar with Angular code and can't confirm its correctness, but could the following refactor potentially solve your issue? This way, the import won't be removed:

  import { forwardRef, Inject, Injectable } from '@nestjs/common'

  const testRemoveService = forwardRef(() => TestRemoveService);

  @Injectable()
  export class AutoSaveTestService {
    constructor(
      @Inject(testRemoveService) public readonly testRemoveService: TestRemoveService
    ){}
  }