Skip to content

Local var detection swallows auto imports after for loop destructuring #487

@dword-design

Description

@dword-design

Environment

unimport@5.5.0
node@22.21.1

Reproduction

https://github.com/dword-design/demo-unimport-for-loop-destructured

Describe the bug

If there is an array destructuring in a for loop and is followed by some other array declaration, the local variable detection regex will expand until that next closing array and swallow all potential auto imports. The same applies for object destructurings. Example:

for (const [index] of [1,2,3].entries()) {
  console.log(`.${AUTO_IMPORTED}`);
  const a = [];
}

https://regex101.com/r/nSu02A/1
Image

Result is that the auto imported variable is not initialized.

I think the root cause is that the local variable detection assumes that there is an assignment going on after the variable but this is not the case for for loops.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions