Fix noEscape numeric values being added instead of concatenated - #2167
Merged
kibertoad merged 2 commits intoJun 24, 2026
Merged
Conversation
In the inline output path, an unescaped mustache emitted the raw value
(`value != null ? value : ""`) rather than a string. When two such
appends were merged with ` + ` and both values were numbers, JavaScript
performed numeric addition: `{{a}}{{b}}` with noEscape and a=1, b=2
produced "3" instead of "12". The same affected triple-stash `{{{a}}}`.
Coerce non-null values to a String in the inline append, matching the
escaped path and the documented contract of `append` ("Coerces value to
a String").
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a codegen edge case where unescaped adjacent mustaches (noEscape: true and {{{...}}}) could be numerically added during source-merge, instead of concatenated as strings.
Changes:
- Update
JavaScriptCompiler#append(inline path) to coerce non-null values to strings via"" + valuebefore merge-concatenation. - Add regression tests covering adjacent numeric mustaches with
noEscape: trueand with triple-stash raw output.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
lib/handlebars/compiler/javascript-compiler.js |
Ensures inline append output is string-coerced to prevent + merges from triggering numeric addition. |
spec/basic.js |
Adds regression tests for adjacent numeric output under noEscape and raw mustaches. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
kibertoad
approved these changes
Jun 24, 2026
Contributor
|
thank you! |
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.
Problem
Closes #1838.
With
noEscape: true, adjacent mustaches that resolve to numbers are added instead of concatenated:The default (escaped) path is unaffected because values go through
escapeExpression, which returns a string. The triple-stash raw form{{{a}}}{{{b}}}is affected by the same root cause.Root cause
In
JavaScriptCompiler#append, the inline output path emitted the raw value:Adjacent buffer appends are merged with
+inmergeSource. When the merged expression starts with these raw values and both are numbers, JavaScript performs numeric addition (1 + 2 === 3) before the top-level'' +in the runtime ever runs.The escaped path wraps each value in
escapeExpression(...)(always a string), so the merge concatenates correctly. The unescaped path should likewise produce a string — the method's own doc comment states it "Coercesvalueto a String", but the inline branch did not.Fix
Coerce the non-null value to a string in the inline append, mirroring the escaped path:
null/undefinedstill produce"",SafeStrings still emit their raw (unescaped) HTML, and escaped output is untouched. +4/−1 in the compiler.Tests
Added a regression test in
spec/basic.js(noEscapefor{{a}}{{b}}/{{a}}{{b}}{{c}}and triple-stash{{{a}}}{{{b}}}). Verified it fails without the fix (Expected "12", Received "3") and passes with it. Full node suite green (604 passing).oxlintandoxfmtclean.No public API change, so
types/index.d.tsis unchanged.