You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import{createUnimport}from'unimport'const{ injectImports }=createUnimport({imports: [{name: 'store',from: 'module-store'}],virtualImports: ['#imports'],})constinput="// import { store } from '#imports'"const{ code }=awaitinjectImports(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'],})constinput=`export const store = { /** * @example * import { store } from '#imports' * store.fetch() */ fetch: () => {}}`const{ code }=awaitinjectImports(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:
The import from the comment section is removed.
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:
One from the import (import { store } from '#imports').
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
The text was updated successfully, but these errors were encountered:
antoinerey
changed the title
Imports in comments are taken into account
Some imports in comments are taken into account
Jul 21, 2023
Environment
The issue has been reproduced in the following environment.
[email protected]
[email protected]
[email protected]
Reproduction
The simplest reproduction could look like this:
While, a more realistic reproduction would look like this:
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:
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:
import { store } from '#imports'
).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
The text was updated successfully, but these errors were encountered: