Preserve source text for negative numeric literals in declaration emit#4158
Draft
Copilot wants to merge 3 commits into
Draft
Preserve source text for negative numeric literals in declaration emit#4158Copilot wants to merge 3 commits into
Copilot wants to merge 3 commits into
Conversation
…aration emit Co-authored-by: jakebailey <5341706+jakebailey@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix declaration emit for negative numeric const literals
Preserve source text for negative numeric literals in declaration emit
Jun 1, 2026
Member
|
@copilot no test?? |
Co-authored-by: jakebailey <5341706+jakebailey@users.noreply.github.com>
Contributor
Author
Added a compiler test in 6da6... — |
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.
Declaration emit normalized negative numeric const literals to their canonical JS-number form instead of preserving the authored text:
The scanner stores the canonical form as a numeric literal's text. On the negative (
PrefixUnaryExpression)as constpath, node reuse clones the operand and detaches it from its source file, so the printer can no longer recover the original text.Changes
reuseNegativeNumericLiteralType(pseudotypenodebuilder.go) — rebuilds the operand of a negated numeric literal from the original source viascanner.GetSourceTextOfNodeFromSourceFile. Skips invalid/separator literals and bigints, mirroring the printer'scanUseOriginalTextrules.inReusedLiteralCollectionflag (NodeBuilderImpl) — set while serializing object/tuple members so nested literals retain the canonical form.testdata/tests/cases/compiler/declarationEmitNegativeNumericLiteralAsConst.tswith accepted.js,.symbols, and.typesbaselines covering the full behavior matrix below.Behavior matrix (verified against built submodule TypeScript 6.0.0)
-1e500,-0xFF,-1e3)1e3→1000,0xFF→255)[-1e500] as const)-Infinity(unchanged)-0xFFn)-1_000)-1000(unchanged)The asymmetry (top-level source vs. nested canonical) intentionally matches tsc, which canonicalizes literals nested inside reused object/tuple
as consttypes regardless of inner assertions.