From ba6230cdf24796ebfae71717050620b2f9501dd7 Mon Sep 17 00:00:00 2001 From: Ashley Hunter Date: Thu, 26 Nov 2026 15:11:28 +0000 Subject: [PATCH] fix(sourcemap): map columns inside code copied through unchanged Unchanged source segments were mapped only at the segment start and at column 0 of each line, so a position inside a line (an indented `throw`, a mid-line call) resolved to the start of its line. Tools that symbolicate a single position, such as a stack frame, lost the column for component code. Add a mapping at the start of each token within a line: each run of word characters and each punctuation character, skipping whitespace, with columns in UTF-16 code units as `LineColCursor` counts them. --- .../oxc_angular_compiler/src/optimizer/mod.rs | 21 +++++- .../tests/integration_test.rs | 68 +++++++++++++++++++ 2 files changed, 88 insertions(+), 1 deletion(-) diff --git a/crates/oxc_angular_compiler/src/optimizer/mod.rs b/crates/oxc_angular_compiler/src/optimizer/mod.rs index 07f72c7568..c9ad01aa42 100644 --- a/crates/oxc_angular_compiler/src/optimizer/mod.rs +++ b/crates/oxc_angular_compiler/src/optimizer/mod.rs @@ -433,7 +433,10 @@ impl<'a> LineColCursor<'a> { /// Add source map mappings for an unchanged segment of source code. /// -/// Adds a mapping at the start of the segment and at the beginning of each new line. +/// Adds a mapping at the start of the segment, at the beginning of each new line, and at the +/// start of each token within a line (a run of word characters, or a single punctuation +/// character). A position inside a line, such as a stack frame's, then maps to its own column +/// in the source rather than to the start of the line. fn add_line_mappings_for_segment( builder: &mut oxc_sourcemap::SourceMapBuilder, segment: &str, @@ -444,6 +447,9 @@ fn add_line_mappings_for_segment( ) { // Add mapping at the start of this segment builder.add_token(out_line, out_col, src_line, src_col, Some(0), None); + // Whether the current position already has a mapping, so it is not added twice. + let mut mapped_here = true; + let mut previous_is_word = false; for ch in segment.chars() { if ch == '\n' { @@ -453,7 +459,20 @@ fn add_line_mappings_for_segment( src_col = 0; // Add mapping at the start of each new line builder.add_token(out_line, out_col, src_line, src_col, Some(0), None); + mapped_here = true; + continue; + } + + let is_word = ch.is_alphanumeric() || ch == '_' || ch == '$'; + if !(mapped_here || ch.is_whitespace() || (is_word && previous_is_word)) { + builder.add_token(out_line, out_col, src_line, src_col, Some(0), None); } + previous_is_word = is_word; + mapped_here = false; + // Columns are UTF-16 code units, as `LineColCursor` counts them. + let width = ch.len_utf16() as u32; + out_col += width; + src_col += width; } } diff --git a/crates/oxc_angular_compiler/tests/integration_test.rs b/crates/oxc_angular_compiler/tests/integration_test.rs index c63d67d4ea..ddcf0daff3 100644 --- a/crates/oxc_angular_compiler/tests/integration_test.rs +++ b/crates/oxc_angular_compiler/tests/integration_test.rs @@ -11336,6 +11336,74 @@ export class TestComponent { assert!(result.map.is_none(), "Source map should be None when sourcemap option is false"); } +#[test] +fn test_sourcemap_maps_columns_inside_unchanged_code() { + // Code the compiler copies through unchanged (a method body) keeps its columns, so a + // position inside a line maps to the same column in the source, not to the line's start. + // Stack traces point inside lines: `throw` here is indented, and `compute` is mid-line, + // after an emoji that is one character but two UTF-16 code units, as columns are counted. + let allocator = Allocator::default(); + let source = r"import { Component } from '@angular/core'; + +@Component({ + selector: 'app-test', + template: '

Hello

', +}) +export class TestComponent { + run(): number { + const total = this.count + '😀'.length + compute(); + if (total < 0) { + throw new Error('negative'); + } + return total; + } +} +"; + + let options = ComponentTransformOptions { sourcemap: true, ..Default::default() }; + let result = + transform_angular_file(&allocator, "app.component.ts", source, Some(&options), None); + let json = result.map.expect("map requested"); + let map = oxc_sourcemap::SourceMap::from_json_string(&json).unwrap(); + let table = map.generate_lookup_table(); + + let position = |text: &str, needle: &str| -> (u32, u32) { + let (line, at) = text + .lines() + .enumerate() + .find_map(|(line, l)| l.find(needle).map(|at| (line, l[..at].encode_utf16().count()))) + .unwrap_or_else(|| panic!("{needle:?} not in:\n{text}")); + (line as u32, at as u32) + }; + + for needle in ["throw new Error", "compute()"] { + let (src_line, src_col) = position(source, needle); + let (out_line, out_col) = position(&result.code, needle); + let token = map + .lookup_token(&table, out_line, out_col) + .unwrap_or_else(|| panic!("no mapping for {needle:?} at {out_line}:{out_col}")); + assert_eq!( + (token.get_src_line(), token.get_src_col()), + (src_line, src_col), + "{needle:?} at output {out_line}:{out_col} should map to source {src_line}:{src_col}:\n{}", + result.code + ); + } + + // One mapping per token, not per character: a position inside a word or on whitespace + // resolves to the token it belongs to, which keeps the map small. + for (needle, offset) in [("compute()", 2), ("if (total", 2)] { + let (out_line, out_col) = position(&result.code, needle); + let token = map.lookup_token(&table, out_line, out_col + offset).unwrap(); + assert_eq!( + token.get_dst_col(), + out_col, + "{needle:?}: column {} should resolve to the token at {out_col}", + out_col + offset + ); + } +} + #[test] fn test_sourcemap_with_external_template() { // Source map should work with resolved external templates