Repository navigation
fix(compiler): older template bugs (deep input crash, lexer errors, escapes, ngNonBindable ICUs, …) - #756
Merged
Conversation
The expression lexer is now a port of Angular's `_Scanner`:
- Errors are error tokens with Angular's text ("Lexer Error: ... at column N
in expression [input]", N in UTF-16 units) and the parser reports each one
when it reaches it (in `skip`, with Angular's location quirk, and in
template literals), instead of collecting them all up front. The count and
order now follow the parse, as in Angular.
- Characters Angular does not know (`~`, `@`, `\`, `é`, ...) are
"Unexpected character" errors; a character outside the BMP gives two (one
per UTF-16 half). "Unterminated quote", "Invalid unicode escape" and the
rest use Angular's text, position and recovery.
- Numbers read like Angular's parseInt / parseFloat:
`100000000000000000000000` is 1e+23 (was 0), `1.2.3` is 1.2, and there are
no hex / octal / binary numbers (`0x10` is an error, as in ngc).
- Strings only know \n \f \r \t \v and \uXXXX (`'\x41'` is "x41");
surrogate escapes pair up, a lone one is kept.
- Template literals use Angular's brace stack, so `${...}` holds any token.
- `/` starts a regex where Angular's isStartOfRegex says so; regex flags are
checked by the parser with Angular's messages.
- @defer trigger errors print numbers and template parts as Angular does.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…time without trim - A lone surrogate in a template literal part (`\`\uD83D\``, also tagged) is printed as U+FFFD, as ngtsc's UTF-8 output does, instead of the internal lone-surrogate marker. - "Unexpected token" messages show a token's lone surrogate as U+FFFD (Token::message_text, shared by the expression parser and @defer triggers). - @defer time values are matched like Angular's TIME_PATTERN, with no trim, so `timer(~ 1)`, `timer(é 1)` and `idle(~ 1)` again give "Could not parse time value of trigger". Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…racterCursor ngtsc parses an inline template string or template literal from its raw text with Angular's EscapedCharacterCursor. `cook_literal` returned None for a `\u` surrogate pair, a lone surrogate, a legacy octal escape and `\8`/`\9`, so template errors fell back to "line L, column C of the template" and `ɵɵattachSourceLocations` was left out. It now maps every escape to its backslash as the cursor does. The template Angular compiles is what the cursor reads, which is not always the literal's value: `\400`..`\777` take three octal digits, a line continuation skips one character (`\` CR LF keeps the LF, `\` U+2028 keeps U+2028), and after `\` LF the next character is read as written. The component metadata now holds that text, so the output matches ngc. A template with a lone surrogate was left uncompiled with no error; it now compiles, and i18n message ids hash a lone surrogate as U+FFFD like TextEncoder. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…unit A template with a lone surrogate (`'\uD83D'`) now compiles, so its stored form (U+FDD0 + U+10F8xx) could leak: - Template error messages show it as U+FFFD (what ngc's message reads as once written out as UTF-8), and expression error columns count it as one UTF-16 unit, like Angular. The expression lexer reads it as one character. - Function names built from it get one `_`, like Angular's `sanitizeIdentifier` (`Cut_Template_p_cl_k_0_listener`, `Cut_p__0_Template`). - `extractComponentMetadataSync` returns the template with the lone code unit itself (as a UTF-16 JS string) instead of the stored characters. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
HTML lexer and parser errors now carry Angular's message, count, order and span, and empty spans are widened to one character as ngc does: - unexpected closing tag and void end tag spans, full name and link - "Unclosed block" errors over the block start, outermost first - "Opening tag not terminated" for unterminated attribute values - self-close, @let, ICU Missing '}' / '{', block close messages - entity, doctype, raw text, close tag and processing instruction errors - `@iffy` is an unrecognized block, `:a` attributes are rejected Block parameters keep their text as written (trailing whitespace too) and are split like Angular's lexer, so `[a ? ]` and `@for (x of a(; track x)` match ngc. A `}` closes the elements left open in its block. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…d tag cost like Angular
- `@` inside an ICU case or interpolation does not end the text, as Angular's
`_isTextEnd`: `me@forum.com` in a case is text again. `a@letter` outside
ICUs is an incomplete `@let`, and `@content` is lexed as a block, as in
Angular 22.3's SUPPORTED_BLOCKS.
- An ICU case finds its `}` first, as `_collectExpansionExpTokens`, so a
nested ICU with an error no longer adds a "Missing '}'" to the outer case.
Nested case ends are kept from the outer scan, so nothing is scanned twice.
- An entity error in an attribute value stops the tag, as `_consumeTagOpen`
does ("Opening tag not terminated" and the stray end tag). Our entity error
is kept. Unquoted attribute values decode entities, as ngc does.
- Each open element and block keeps the namespace it passes down, so naming
an end tag reads one container instead of walking the stack, and the
"Unexpected closing tag" message is built only for an error.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…value-less template attrs like Angular
- A text with an HTML entity parses its interpolations from the text value
(whitespace folded unless preserved), as Angular's parseInterpolation does,
not from the raw tokens. `&{{ a + }}` now prints `[ a + ]`,
`&{{ <U+3000>// c }}` reports the comment-only error, and
`& {{ "a b" }}` compiles to "a b" like ngc.
- An ICU inside ngNonBindable is dropped (NonBindableVisitor.visitExpansion),
and an element inside ngNonBindable has no i18n. An i18n element starts an
i18n block when its message is not empty (from its HTML children), so
`<div ngNonBindable i18n>{a, plural, ...}</div>` keeps ɵɵi18n like ngc.
- A microsyntax key with no value (`*ngIf="c; else"`) is a literal template
attribute, listed before the bound ones: [4, "ngIfElse", "ngIf"].
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…nBindable Keep the old "R3 children not empty" check in ingest and add only the ngNonBindable case, whose ICU children are dropped. An i18n element whose only children are @let, <script>, <style> or <link rel=stylesheet> compiles as on main again. Move the doc comment of wrap_component_in_template back above it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…cess A deep template or expression (a 500-term `a.b + a.b + ...` binding in a debug build, a few thousand levels in a release build) overflowed the calling thread's stack and killed the process (SIGSEGV / SIGILL). - The NAPI binding compiles on threads with a large stack (64 MiB, 1 GiB in debug builds): one compile thread per calling thread, kept for later calls, for every entry point that parses or compiles. - The template parser and the expression parser stop at 2000 levels (`MAX_TEMPLATE_DEPTH`, `MAX_EXPRESSION_DEPTH`), and so do `@if` / `@else if` chains and `@switch` cases (they compile to nested conditionals). Deeper input is reported with the error ngc reports when its own stack overflows, `RangeError: Maximum call stack size exceeded`; for a binding at the span ngc's binding parser uses. ngc overflows below 1000 levels, so everything it compiles still compiles. Review round: Review fixes for deep templates and expressions: - The expression parser's nesting guard now sits on every recursive cycle: an operand, a conditional's branches, and the right side of `**`. Long `a ? b : …` and `a ** a ** …` chains no longer overflow any stack. - The template parser goes on past the depth limit (its open elements and blocks are in a list). The template's own errors are reported, as ngc reports them. `RangeError` is reported only when there are no others. For too-deep ICUs, which are parsed recursively, the rest of the input is still skipped, and the errors of the cut parse are dropped. - A `@switch` with too many cases still visits its cases. The `RangeError` is reported only if the template has no other errors (ngc overflows only when it prints output). - The expression lexer scans nested template literals and substitutions with a list, not recursion. - The NAPI binding no longer sends each compilation to another thread. That made compiles up to ~3x slower on a busy machine. Now a compilation runs on the calling thread with low depth limits (`with_depth_limits`: 32/64 levels when optimized, 8/8 unoptimized). Only an input that nests deeper runs again, on a new thread with a large stack. Entry points that compile no template no longer hop at all, as on main. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- A bare lone surrogate in an expression now gives Angular's two errors (the lexer error token, then the lexer error), now that the escapes fix reaches the Angular-style expression lexer. - The closing-tag message now has Angular's 'For more info see' tail. - Conformance snapshot: 1449/1456 (main 1441), no test newly failing. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Angular's lexer reads a line break as part of a regex, and ngc then emits invalid JavaScript with no error. Main reported the regex as unterminated; keep that error (also for U+2028 and U+2029). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Member
Author
|
@codex review |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex Review: Didn't find any major issues. Bravo. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to #753 and #754: the older template bugs they listed as "not covered". Every expected value comes from Angular 22.3.0 (
parseTemplate, and ngcNgtscProgramfor emitted code andfile:line:col).A. Deep input no longer crashes the process
Before, a long
a + a + …chain, deep( ( ( a ) ) ), nested ternaries,**chains, deep<div>s or@ifs overflowed the native stack. Node died with exit 139 / 132 and no message.${}used to recurse)._TreeBuilderkeeps open tags in a list and stops at HTML errors before the deep walk.a.b + a.b + …×1900(,[,<div>,@ifa ? a : …×300000,a ** …×1M<p></span></p>+ 2100 nested<div>Unexpected closing tag "span"1:4@switchwith 2100 cases,{{ a b }}in the firstUnexpected token 'b'…1:25Cost: nothing for normal templates (calls stay on the calling thread). Best of 4 runs, interleaved:
[a.b, …]×8000{{ a.b }}×8000[a.b, …]×32000Templates deeper than the inline limit (release: 32 tag levels / 64 expression levels) are compiled twice; e.g. 40 nested divs 109 → 189 µs.
B. Expression lexer errors
'abcUnexpected token Lexer Error: Unterminated quote at column 4 …2.
Lexer Error: Unterminated quote … at the end of the expression ['abc]Unterminated string+Unexpected token Unterminated string at column 1a # bUnexpected token 'Lexer Error: Invalid character [#] …'2.
Lexer Error: … at column 5 in [a # b]'\u12'Unterminated quote at column 6a; 'b… chained expression at column 6, thenUnterminated quote at column 5Unterminated string,… at column 4{{ a \uD83D }}(inline)Unexpected token 'Lexer Error: Unexpected character […]…'+ the lexer errorLexer errors now use Angular's text (
Lexer Error: {msg} at column N in expression [input]), and the parser reports them where Angular does, in Angular's order. A lone surrogate prints as U+FFFD (what ngc's output shows too).C. Number literals
1000000000000000000000001e+2301e+230x10Unexpected token 'x10''\x41'"x41""A"D. HTML parser errors
{a, select, x {</i>}}Unexpected closing tag "i"…, label width 4<p [x]="\"abc"></p>Opening tag "p" not terminated.Unexpected end of tag 'p'<p>@content</p>,<p>a@letter</p>Incomplete block …/Incomplete @let …{a, select, x {a@error.com}}Incomplete block "error"</p>… For more info see https://www.w3.org/TR/html5/syntax.html#closing-elements-that-have-implied-end-tagsEntities in unquoted attribute values are now decoded, as Angular does:
<div title=a&b>→ consts["title","a&b"](main kepta&b).E. Block parameters
@if (a + ) {x}… [a + ](spaces kept)[a +]@for (x of a(; track x) {}Unexpected end of expression: a(+Missing expected )Incomplete block "for"@for (x of y; track ) {}must have a "track" expressionUnrecognized @for loop parameter "track"F. Whitespace folding after an entity
<p>& {{ "a b" }}</p>textInterpolate1("& ", "a b")"a b"<p>&{{ a + }}</p>[ a + ][ a + ]<pre>keeps the spaces, as in ngc.G. ICUs under ngNonBindable
<div ngNonBindable>{a, plural, =0 {x}}</div>domElement(0,"div")onlyi18nExp(ctx.a)H. Value-less microsyntax keys come first in consts
<div *ngIf="a; else">[4,"ngIfElse","ngIf"][4,"ngIf","ngIfElse"]<li *ngFor="let x of xs; as">[4,"ngFor","ngForAs","ngForOf"][4,"ngFor","ngForOf","ngForAs"]I. Escapes in inline templates
Before, these fell back to "line L, column C of the template" or were not compiled at all.
'<p>a😀b</p>{{ a + }}'errorcut.ts:4:35'<p>a\777b</p>{{ a + }}'errorcut.ts:4:27'<p>a\400b</p>'text"aĀb""a 0b"'<p>a\<CR><LF>b</p>'text"a\nb""ab"'<p>a\uD83Db</p>'(lone surrogate)text(1,"a\uD83Db")ɵcmp, no error'<p (cl\uD83Dk)="…">'listener…_p_cl_k_0_listenerA lone surrogate is kept inside Rust strings as two private-use chars (
util/lone_surrogate.rs); every lexer, column count and printer counts it as one UTF-16 unit.Checks
main: 145 repo components × AOT / JIT / HMR (435 pairs), 0 differences.cargo test --workspace: 4118 passed. napi: 623 passed. Conformance 1441 → 1449 / 1456 (8 now pass, none regress).deep_nesting_test.rs,napi/test/deep-nesting.test.ts,expression_lexer_error_text_test.rs,html_parser_angular_errors_test.rs,r3_transform_ngc_parity_test.rs,inline_template_escapes_test.rs.as u32casts follow the file's style).[x]="/a⏎b/"emitted invalid JS with no error. ngc does the same, but main reportedUnterminated regular expression; that error is back (also for U+2028 / U+2029).Known differences from Angular (left on purpose)
:is(×10000) still crashes as on main: that recursion is in the oxc parser / CSS selector parser, not in templates.Unknown entity "foo") that Angular swallows, then give ngc's 2 errors too. Dropping an error is a product call.@content(new in Angular 22.3) is lexed as a block but not supported:Unrecognized block @content.<div ngNonBindable i18n>a{x, plural, =0 {y}}b</div>: ngc crashes on it; we report a placeholder error.constwith a lone surrogate is still not compiled (as on main).<div i18n>@let x = 1;</div>keeps main's output (ngc drops the@lethere).[x]="/a⏎b/": we reportUnterminated regular expression; Angular is silent and ngc emits invalid JS.1e2e+×3000) gives 3001 errors, like Angular, and ~780 MB peak memory (Angular ~90 MB, because JS strings share the input). Main already does this for other repeated errors (1__0+×3000: 3001 errors, 310 MB). A cap would drop errors Angular reports, so it is left for a separate decision.🤖 Generated with Claude Code