Skip to content
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

Some imports in comments are taken into account #257

Open
antoinerey opened this issue Jul 21, 2023 · 0 comments
Open

Some imports in comments are taken into account #257

antoinerey opened this issue Jul 21, 2023 · 0 comments
Labels
bug Something isn't working

Comments

@antoinerey
Copy link

antoinerey commented Jul 21, 2023

Environment

The issue has been reproduced in the following environment.

  • unimport@3.1.0
  • node@18.16.1
  • npm@9.5.1

Reproduction

The simplest reproduction could look like this:

import { createUnimport } from 'unimport'

const { injectImports } = createUnimport({
  imports: [{ name: 'store', from: 'module-store' }],
  virtualImports: ['#imports'],
})

const input = "// import { store } from '#imports'"

const { code } = await injectImports(input)

console.log(code)

// import { store } from 'module-store';
// //

While, a more realistic reproduction would look like this:

import { createUnimport } from 'unimport'

const { injectImports } = createUnimport({
  imports: [{ name: 'store', from: 'module-store' }],
  virtualImports: ['#imports'],
})

const input = `export const store = {
  /**
   * @example
   * import { store } from '#imports'
   * store.fetch()
   */
  fetch: () => {}
}`

const { code } = await injectImports(input)

console.log(code)
// import { store } from 'module-store';
// export const store = {
//   /**
//    * @example
//    * * store.fetch()
//    */
//   fetch: () => {}
// }

Describe the bug

When unimport is given code with comments that include imports from virtual modules, those comments are taken into account to know whether imports should be injected or not.

In the reproduction above, we can see two things:

  1. The import from the comment section is removed.
  2. A new regular import is added at the top of the file.

In this specific scenario where comments are used to showcase how to use this piece of code, the result code will crash if executed. Indeed, there are now two variables with the same name:

  1. One from the import (import { store } from '#imports').
  2. One from the original code (const store).

From my understanding, I would expect imports in comments not to be taken into account when figuring out whether imports should be injected or not.

Additional context

I tracked this issue down from a Nuxt application where I first encountered the issue.

Logs

No response

@antoinerey antoinerey changed the title Imports in comments are taken into account Some imports in comments are taken into account Jul 21, 2023
@Hebilicious Hebilicious added the bug Something isn't working label Jul 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants