Skip to content

Commit 8cff139

Browse files
ryansolidclaude
andauthored
fix(babel-plugin): keep evaluated negative/non-finite numbers static (#4321)
* fix(babel-plugin): keep evaluated negative/non-finite numbers static #4309 switched evaluateAndInline to t.valueToNode for numbers, which builds -1 / 0/0 expressions; the SSR and universal transforms then treated them as dynamic, diverging from the oxc compiler (parity-probes "exotic literal holes"). Build NumericLiteral nodes directly where @babel/types 8 rejects the value. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_0117BoWMdD4K7882vob4J9vq * babel-plugin: keep -0 and prop: numbers as plain literals Export numberLiteral and use it for evaluated prop: numbers, keep -0's sign, and test the static attribute paths. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_018LY7F9czoRqdMEvk2QwPr1 --------- Co-authored-by: Claude <[email protected]>
1 parent 0090fe3 commit 8cff139

4 files changed

Lines changed: 49 additions & 2 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@solidjs/babel-plugin": patch
3+
---
4+
5+
Evaluated negative and non-finite numbers (`c={-1}`, `d={NaN}`) are static again. Since Babel 8 support, `evaluateAndInline` built them with `t.valueToNode`, which gives `-1` / `0 / 0` expressions rather than numeric literals, so they stopped being inlined into templates (SSR emitted `ssrAttribute("c", -1)` instead of `c="-1"`) and diverged from `@solidjs/compiler` (the parity probes' "exotic literal holes"). They are now NumericLiteral nodes again, built directly where @babel/types 8's builder would reject the value; both Babel majors print them as before.

‎packages/babel-plugin/src/dom/element.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
VoidElements
1212
} from "../../../web/src/constants.js";
1313
import {
14+
numberLiteral,
1415
evaluateAndInline,
1516
getAttributeNamed,
1617
getTagName,
@@ -997,7 +998,7 @@ function transformAttributes(
997998
((type = typeof evaluated) === "string" || type === "number" || type === "boolean")
998999
) {
9991000
if (type === "number" && key.startsWith("prop:")) {
1000-
value = t.jsxExpressionContainer(t.valueToNode(evaluated) as babelTypes.Expression);
1001+
value = t.jsxExpressionContainer(numberLiteral(evaluated));
10011002
} else if (type === "boolean") {
10021003
value = t.jsxExpressionContainer(t.booleanLiteral(evaluated));
10031004
} else value = t.stringLiteral(String(evaluated));

‎packages/babel-plugin/src/shared/utils.ts‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,18 @@ const templateEscapes = new Map([
759759
["\u2029", "\\u2029"]
760760
]);
761761

762+
// A NumericLiteral for any evaluated number, so `-1` and `NaN` stay static
763+
// literals downstream. @babel/types 8's builder rejects negative and
764+
// non-finite values (`t.valueToNode` builds `-1` / `0 / 0` expressions
765+
// instead), so those are built as plain nodes; generators print them as
766+
// `-1` / `NaN` / `Infinity`, as Babel 7's builder did.
767+
export function numberLiteral(value: number): t.NumericLiteral {
768+
if (value >= 0 && Number.isFinite(value) && !Object.is(value, -0)) return t.numericLiteral(value);
769+
const node = { type: "NumericLiteral", value } as t.NumericLiteral;
770+
if (Object.is(value, -0)) node.extra = { raw: "-0", rawValue: -0 };
771+
return node;
772+
}
773+
762774
export function evaluateAndInline(
763775
value: EvaluateAndInlineNode | null | undefined,
764776
valueNode: NodePath<EvaluateAndInlineNode>
@@ -786,7 +798,7 @@ export function evaluateAndInline(
786798
if (typeof r.value === "string") {
787799
valueNode.replaceWith(t.stringLiteral(r.value));
788800
} else if (typeof r.value === "number") {
789-
valueNode.replaceWith(t.valueToNode(r.value));
801+
valueNode.replaceWith(numberLiteral(r.value));
790802
} else if (typeof r.value === "boolean") {
791803
valueNode.replaceWith(t.booleanLiteral(r.value));
792804
}

‎packages/babel-plugin/test/numeric-literals.spec.js‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,32 @@ describe("evaluated numeric literals", () => {
2626
}
2727
});
2828
});
29+
30+
describe("evaluated numeric attributes stay static", () => {
31+
const attrs = `const a = <div c={-1} d={0 / 0} e={-0} />;
32+
const b = <input prop:value={-1} />;`;
33+
const compile = generate =>
34+
babel.transformSync(attrs, {
35+
plugins: [[plugin, { generate, moduleName: "r-dom" }]],
36+
configFile: false,
37+
babelrc: false,
38+
filename: "numeric.jsx"
39+
}).code;
40+
41+
test("dom", () => {
42+
const code = compile("dom");
43+
expect(code).toContain("<div c=-1 d=NaN e=0>");
44+
expect(code).toContain("_el$2.value = -1;");
45+
});
46+
47+
test("ssr", () => {
48+
expect(compile("ssr")).toContain('c=\\"-1\\" d=\\"NaN\\" e=\\"0\\"');
49+
});
50+
51+
test("universal", () => {
52+
const code = compile("universal");
53+
expect(code).toContain("c: -1");
54+
expect(code).toContain("d: NaN");
55+
expect(code).toContain("e: -0");
56+
});
57+
});

0 commit comments

Comments
 (0)