Skip to content
Merged
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
8 changes: 6 additions & 2 deletions crates/oxc_angular_compiler/src/parser/html/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1205,8 +1205,12 @@ impl<'a> HtmlLexer<'a> {
for _ in 0..4 {
self.advance();
}
// Skip whitespace (but not newlines for detecting invalid @let)
while self.peek() == ' ' || self.peek() == '\t' {
// Skip whitespace after `@let`, including newlines. Angular consumes all
// whitespace between `@let` and the declared name, so `@let\nfoo = ...;` is a
// valid declaration. Stopping at a newline here misclassified it as incomplete,
// leaving the value (and any object-literal braces in it) to be re-lexed as
// block content, which corrupted block nesting.
while chars::is_whitespace(self.peek()) {
self.advance();
}

Expand Down
21 changes: 21 additions & 0 deletions crates/oxc_angular_compiler/tests/html_lexer_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1603,6 +1603,27 @@ mod let_declarations {
// Should also have LET_VALUE
assert!(types.contains(&HtmlTokenType::LetValue));
}

#[test]
fn should_tokenize_let_declaration_with_newline_before_name() {
// Angular allows any whitespace - including newlines - between `@let` and the
// declared name (it skips whitespace via `isNotWhitespace`). A newline there must
// still produce a complete declaration, not an incomplete one.
let result = tokenize("@let\nfoo = 123;");

let types: Vec<_> = result.tokens.iter().map(|t| t.token_type).collect();

assert!(types.contains(&HtmlTokenType::LetStart), "Should have LetStart, got {types:?}");
assert!(types.contains(&HtmlTokenType::LetValue), "Should have LetValue, got {types:?}");
assert!(types.contains(&HtmlTokenType::LetEnd), "Should have LetEnd, got {types:?}");
assert!(
!types.contains(&HtmlTokenType::IncompleteLet),
"Should not be treated as incomplete, got {types:?}"
);

let let_start = result.tokens.iter().find(|t| t.token_type == HtmlTokenType::LetStart);
assert_eq!(let_start.unwrap().value(), "foo");
}
}

// ============================================================================
Expand Down
15 changes: 15 additions & 0 deletions crates/oxc_angular_compiler/tests/html_parser_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,21 @@ mod let_declarations {
let result = parse_and_humanize_no_ws("@let a = 1; @let b = 2;");
assert_eq!(result, vec![let_decl("a"), let_decl("b")]);
}

#[test]
fn should_parse_let_with_object_literal_value_and_newline_in_block() {
// Regression: when `@let` was followed by a newline (as prettier formats it), the
// declaration was misclassified as incomplete, so its value was never consumed. The
// object-literal braces in the value were then re-lexed as a stray block close,
// corrupting block nesting and surfacing as an "Unexpected closing tag" error.
let result = parse_and_humanize_no_ws(
"@if (cond) { @let\nlabel = value | translate: { section: id }; <button></button> }",
);
assert!(
result.iter().any(|n| n == &let_decl("label")),
"Expected a LetDeclaration named 'label', got {result:?}"
);
}
}

// ============================================================================
Expand Down
Loading