Skip to content

fix(codegen): swap mapping/indent order for top-level decls#22206

Merged
graphite-app[bot] merged 1 commit into
mainfrom
fix/codegen-indent-before-mapping-for-decls
May 7, 2026
Merged

fix(codegen): swap mapping/indent order for top-level decls#22206
graphite-app[bot] merged 1 commit into
mainfrom
fix/codegen-indent-before-mapping-for-decls

Conversation

@Dunqing

@Dunqing Dunqing commented May 7, 2026

Copy link
Copy Markdown
Member

Independent of #22199 / #22200. Fixes a separate sourcemap-indent issue.

Five gen.rs impls for top-level declarations were calling add_source_mapping(self.span) before print_indent():

  • Directive
  • ImportDeclaration
  • ExportNamedDeclaration
  • ExportAllDeclaration
  • ExportDefaultDeclaration

The mapping then anchored at the line's indent column (col 0) rather than at the actual keyword column. Sourcemap consumers querying the keyword's gen position would land on the leading whitespace.

Every other indented statement in the codebase (ExpressionStatement, IfStatement, VariableDeclaration, etc.) uses the inverse order — print_indent first, then add_source_mapping. This PR brings the five outliers in line.

Visual comparison

The bug surfaces when codegen is invoked with non-zero initial_indent (a common pattern for bundler callers that nest output in a wrapper). Before vs After this PR on a fixture with all five declaration kinds:

"use strict";
import { x } from "x";
export { y } from "y";
export * from "z";
export default 42;

Each declaration's source position now maps to gen col 1 (the keyword) instead of gen col 0 (the leading tab).

Test plan

  • New test top_level_decl_mappings_start_after_generated_indent — wraps a "use strict", import, three export forms inside an indented if (true) { ... } block, asserts each maps from src col 0 to gen col 1 (after the tab), not gen col 0.
  • Verified the test fails without the fix.
  • All 99 codegen tests pass.

@github-actions github-actions Bot added the A-codegen Area - Code Generation label May 7, 2026

Dunqing commented May 7, 2026

Copy link
Copy Markdown
Member Author

How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • 0-merge - adds this PR to the back of the merge queue
  • hotfix - for urgent changes, fast-track this PR to the front of the merge queue

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

This stack of pull requests is managed by Graphite. Learn more about stacking.

@codspeed-hq

codspeed-hq Bot commented May 7, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 48 untouched benchmarks
⏩ 3 skipped benchmarks1


Comparing fix/codegen-indent-before-mapping-for-decls (a623ceb) with main (69a0c89)2

Open in CodSpeed

Footnotes

  1. 3 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

  2. No successful run was found on main (af27cf9) during the generation of this report, so 69a0c89 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@Dunqing Dunqing changed the title feat(linter): split jest/no-test-prefixes into separate vitest rule (#21876) fix(codegen): swap mapping/indent order for top-level decls May 7, 2026
@Dunqing Dunqing force-pushed the fix/codegen-indent-before-mapping-for-decls branch from f1a5ace to a623ceb Compare May 7, 2026 06:36
@Dunqing Dunqing marked this pull request as ready for review May 7, 2026 08:37
@graphite-app graphite-app Bot added the 0-merge Merge with Graphite Merge Queue label May 7, 2026
@graphite-app

graphite-app Bot commented May 7, 2026

Copy link
Copy Markdown
Contributor

Merge activity

Independent of #22199 / #22200. Fixes a separate sourcemap-indent issue.

Five `gen.rs` impls for top-level declarations were calling `add_source_mapping(self.span)` *before* `print_indent()`:

- `Directive`
- `ImportDeclaration`
- `ExportNamedDeclaration`
- `ExportAllDeclaration`
- `ExportDefaultDeclaration`

The mapping then anchored at the line's indent column (col 0) rather than at the actual keyword column. Sourcemap consumers querying the keyword's gen position would land on the leading whitespace.

Every other indented statement in the codebase (`ExpressionStatement`, `IfStatement`, `VariableDeclaration`, etc.) uses the inverse order — `print_indent` first, then `add_source_mapping`. This PR brings the five outliers in line.

## Visual comparison

The bug surfaces when codegen is invoked with non-zero `initial_indent` (a common pattern for bundler callers that nest output in a wrapper). [Before vs After this PR](https://source-map-viewer.void.app/compare?a=5KX8dYtn&b=ZFhNIEAv) on a fixture with all five declaration kinds:

```js
"use strict";
import { x } from "x";
export { y } from "y";
export * from "z";
export default 42;
```

Each declaration's source position now maps to gen col 1 (the keyword) instead of gen col 0 (the leading tab).

## Test plan

- [x] New test `top_level_decl_mappings_start_after_generated_indent` — wraps a `"use strict"`, `import`, three `export` forms inside an indented `if (true) { ... }` block, asserts each maps from `src col 0` to `gen col 1` (after the tab), not `gen col 0`.
- [x] Verified the test fails without the fix.
- [x] All 99 codegen tests pass.
@graphite-app graphite-app Bot force-pushed the fix/codegen-indent-before-mapping-for-decls branch from a623ceb to 29a3cd7 Compare May 7, 2026 14:11
graphite-app Bot pushed a commit that referenced this pull request May 7, 2026
)

Stacked on #22206. Pure refactor — no behavior change.

The `print_next_indent_as_space` flag was being read inline at **five** sites in `gen.rs` via copy-pasted `if flag { print_hard_space(); flag = false }` blocks. Two named private helpers replace those:

- `consume_pending_indent_space() -> bool` — emit space + clear flag, returns whether anything was emitted
- `clear_pending_indent_space()` — drop the flag without emitting

`print_indent` continues to call `consume_pending_indent_space` internally, preserving the flag-as-space contract at every existing `print_indent` site.

## Test plan

- [x] `cargo test -p oxc_codegen` passes (100 integration tests, no new tests added since the existing `top_level_decl_mappings_start_after_generated_indent` from #22206 already covers the `Directive` indent path)
- [x] `cargo test -p oxc_isolated_declarations` passes (the namespace / `declare module` snapshot fixtures that already exercise indented import/export decls)
- [x] No snapshot updates needed
@graphite-app graphite-app Bot merged commit 29a3cd7 into main May 7, 2026
28 checks passed
@graphite-app graphite-app Bot removed the 0-merge Merge with Graphite Merge Queue label May 7, 2026
@graphite-app graphite-app Bot deleted the fix/codegen-indent-before-mapping-for-decls branch May 7, 2026 14:16
camc314 added a commit that referenced this pull request May 11, 2026
### 🚀 Features

- 66c9b01 transformer/typescript: Debug_assert that `enum_eval` ran in
semantic (#22252) (Dunqing)
- ffe6475 minifier: Fold `Array` constructor with safe spreads (#22215)
(camc314)

### 🐛 Bug Fixes

- d3d0b18 traverse: Handle `ChainElement::TSNonNullExpression` in
`GatherNodeParts` (#22247) (leaysgur)
- 4e880de transformer/object-rest-spread: Declare temp vars for computed
keys (#22284) (camc314)
- a7c3e22 semantic: Clear member write target for computed keys (#22302)
(camc314)
- 6a8852d codegen: Emit newline after legal-comment orphan flush
(#22304) (Dunqing)
- 5da9fda transformer/explicit-resource-management: Preserve class names
(#22306) (Dunqing)
- b5d970f transformer/explicit-resource-management: Preserve class names
(#22290) (camc314)
- bc54fd4 minifier: Keep function / class names if direct eval is
present in the scope (#22241) (sapphi-red)
- 7a810c0 minifier: Refresh direct eval flags after DCE (#21787)
(Dunqing)
- dd88726 transformer/legacy-decorator: Preserve accessor type
annotation for emitDecoratorMetadata (#21966) (Dunqing)
- 29a3cd7 codegen: Swap mapping/indent order for top-level decls
(#22206) (Dunqing)
- 73b4f40 minifier: Preserve catch binding with direct eval (#22221)
(camc314)
- 0e13d17 minifier: Preserve optional chain base side effects (#22219)
(camc314)
- 0c7c01c transformer/typescript: Inline optional-chain enum member
access (#21834) (Dunqing)
- a6aff7e codegen: Emit block/array/object end mapping at close char
(#22200) (Dunqing)
- a099b03 codegen: Emit call end mapping at `)` position, not past it
(#22199) (Dunqing)
- 5753774 minifier: Cap if-return ternary collapse for firefox (#21841)
(Gurupungav Narayanan)
- 2493bdd codegen: Correct sourcemap end mappings for closing delimiters
(#22001) (Mark Dalgleish)
- 3b385e2 minifier: Bail optimizing `Array` with unknown arg count
(#22188) (camc314)
- 9fa2122 parser: Parse array computed class keys (#22159) (camc314)

### 📚 Documentation

- a4a6892 napi/parser: Correct code comment (#22278) (overlookmotel)
- 9305373 oxc: Update README (#22178) (camc314)

Co-authored-by: Cameron <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-codegen Area - Code Generation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant