Skip to content

Fix(js_scanner): import hoisting corruption when import.meta.env …#1158

Open
bosmievoll wants to merge 2 commits intowithastro:mainfrom
bosmievoll:fix/import-meta-hoist-position-944
Open

Fix(js_scanner): import hoisting corruption when import.meta.env …#1158
bosmievoll wants to merge 2 commits intowithastro:mainfrom
bosmievoll:fix/import-meta-hoist-position-944

Conversation

@bosmievoll
Copy link
Copy Markdown

…precedes import statements (#944)

Changes

  • Fix position tracking in the DotToken handler sub-loop within NextImportStatement (js_scanner.go)
  • The sub-loop consumed tokens (meta, ., etc.) via l.Next() without updating the position counter i, causing subsequent import statement spans to be calculated with an offset
  • This corrupted hoisted output when import.meta.env.* appeared before import statements in Astro frontmatter
  • Add continue after the ImportToken block to prevent double-counting i += len(value) for the import keyword

Testing

Added 2 test cases to js_scanner_test.go:

  • import.meta.env before import — single import after import.meta.env expression
  • import.meta.env before multiple imports — two imports after bare import.meta.env reference

Docs

Bug fix only — no public API changes.

…precedes import statements (withastro#944)

Fix(js_scanner): track token positions in import.meta DotToken handler (withastro#944)

  The DotToken sub-loop in NextImportStatement consumed tokens via l.Next()
  without updating the position counter `i`, causing subsequent import
  statement spans to be offset. This corrupted the hoisted output when
  import.meta.env appeared before import statements.

  Two changes:
  - Capture consumed token lengths in the sub-loop and add to `i`
  - Add `continue` after ImportToken block to avoid double-counting

  Fixes withastro#944
@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Mar 23, 2026

🦋 Changeset detected

Latest commit: 3f26dd6

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@astrojs/compiler Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant