Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion crates/oxc_angular_compiler/src/optimizer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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' {
Expand All @@ -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;
}
}

Expand Down
68 changes: 68 additions & 0 deletions crates/oxc_angular_compiler/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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: '<h1>Hello</h1>',
})
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
Expand Down
Loading