From d349de4aefbc528b1801f44e913b91488589ed9d Mon Sep 17 00:00:00 2001 From: Yevhenii Date: Thu, 29 Oct 2026 11:00:17 +0200 Subject: [PATCH 01/10] fix: keep a list's separator when it is read through a variable MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A comma-separated list compiled to the same array as a space-separated one whenever each item was a single token, so the runtime guessed the separator from how its arrays nested: box-shadow: var(--o) var(--b) drew the second variable as a shadow of its own, a transform held in a variable reached React Native as a nested list, and a comma in a filter or transform list went unnoticed. The compiler keeps each item of a comma-separated list as the array of its tokens, and the runtime substitutes a var() by its tokens as css-variables-1 §3 does, so box-shadow, filter, transform, the arguments of translate(), scale() and rotate() and the function printer read the separator a list was written with. A transform list holds one function per token: translate()'s second offset, which React Native refused nested, joins the list, and a function the compiler writes no form for drops out without taking the list with it. React Native's list props still take a comma-separated list with one-token items bare. --- src/__tests__/compiler/compiler.test.tsx | 66 ++- src/__tests__/compiler/property.test.ts | 13 + src/__tests__/native/variable-lists.test.tsx | 445 ++++++++++++++++++ src/compiler/compiler.ts | 8 +- src/compiler/declarations.ts | 80 ++-- src/compiler/stylesheet.ts | 10 +- src/native/api.tsx | 5 +- src/native/objects.ts | 8 +- .../styles/functions/animation-timing.ts | 4 +- src/native/styles/functions/filters.ts | 10 +- .../styles/functions/platform-functions.ts | 4 +- .../styles/functions/string-functions.ts | 12 +- .../styles/functions/transform-functions.ts | 53 ++- src/native/styles/resolve.ts | 119 ++++- src/native/styles/shorthands/box-shadow.ts | 45 +- src/native/styles/shorthands/transform.ts | 7 +- src/utilities/style-descriptor.ts | 19 + 17 files changed, 757 insertions(+), 151 deletions(-) create mode 100644 src/__tests__/native/variable-lists.test.tsx diff --git a/src/__tests__/compiler/compiler.test.tsx b/src/__tests__/compiler/compiler.test.tsx index 20743e03..6031aa94 100644 --- a/src/__tests__/compiler/compiler.test.tsx +++ b/src/__tests__/compiler/compiler.test.tsx @@ -181,23 +181,65 @@ test("animations preserve named keyframes and their timing contract", () => { ]); }); -test("breaks apart comma separated variables", () => { - const compiled = compile( - ` - :root { - --test: blue, green; - } - `, - { - inlineVariables: false, - }, - ); +// css-variables-1 §2.1: a custom property's value is its tokens, its commas among them +test.each([ + ["blue, green", [["blue"], ["green"]]], + ["blue ,green", [["blue"], ["green"]]], + ["blue/**/, green", [["blue"], ["green"]]], + ["blue green", ["blue", "green"]], + [ + "1px 2px #000, 3px 4px #fff", + [ + [1, 2, "#000"], + [3, 4, "#fff"], + ], + ], + ["1px 2px #000 3px 4px #fff", [1, 2, "#000", 3, 4, "#fff"]], +])("--test: %s keeps its separator", (value, expected) => { + const compiled = compile(`:root { --test: ${value}; }`, { + inlineVariables: false, + }); expect(compiled.stylesheet()).toStrictEqual({ - vr: [["test", [[["blue", "green"]]]]], + vr: [["test", [[expected]]]], }); }); +test.each([ + ["var(--a), var(--b)", [[[{}, "var", "a", 1]], [[{}, "var", "b", 1]]]], + [ + "var(--a) var(--b)", + [ + [{}, "var", "a", 1], + [{}, "var", "b", 1], + ], + ], +])("box-shadow: %s keeps its separator", (value, expected) => { + const compiled = compile(`.my-class { box-shadow: ${value}; }`); + + expect(compiled.stylesheet().s?.[0]?.[1][0]?.d).toStrictEqual([ + [[{}, "boxShadow", expected, 1], "boxShadow", 1], + ]); +}); + +// A transform is a space-separated list, so it holds each function as a token of its own +test.each([ + [ + "translate(10px, 20px)", + [ + [{}, "translateX", 10], + [{}, "translateY", 20], + ], + ], + ["translateZ(10px) rotate(45deg)", [[{}, "rotate", "45deg"]]], +])("transform: %s compiles to a flat list of functions", (value, expected) => { + const compiled = compile(`.my-class { transform: ${value}; }`); + + expect(compiled.stylesheet().s?.[0]?.[1][0]?.d).toStrictEqual([ + [[{}, "transform", expected], "transform"], + ]); +}); + test("light-dark()", () => { const compiled = compile(` .my-class { diff --git a/src/__tests__/compiler/property.test.ts b/src/__tests__/compiler/property.test.ts index f6f0e37f..d2a277f6 100644 --- a/src/__tests__/compiler/property.test.ts +++ b/src/__tests__/compiler/property.test.ts @@ -591,3 +591,16 @@ test("an @property under an @supports that fails is not registered", () => { expect(result.vp).toBeUndefined(); }); + +test("@property with a comma-separated repeated value keeps each item apart", () => { + const compiled = compile(` +@property --my-offsets { + syntax: "#"; + inherits: false; + initial-value: 10px, 20px; +} +`); + + const vrMap = new Map(compiled.stylesheet().vr); + expect(vrMap.get("my-offsets")).toStrictEqual([[[[10], [20]]]]); +}); diff --git a/src/__tests__/native/variable-lists.test.tsx b/src/__tests__/native/variable-lists.test.tsx new file mode 100644 index 00000000..ac57d846 --- /dev/null +++ b/src/__tests__/native/variable-lists.test.tsx @@ -0,0 +1,445 @@ +import { processColor, StyleSheet } from "react-native"; + +import { render, screen } from "@testing-library/react-native"; +import type { StyleDescriptor } from "react-native-css/compiler"; +import { Text } from "react-native-css/components/Text"; +import { View } from "react-native-css/components/View"; +import { registerCSS, testID } from "react-native-css/jest"; +import { useNativeVariable } from "react-native-css/native"; + +import type { Getter } from "../../native/reactivity"; +import { resolveValue } from "../../native/styles/resolve"; + +// Observe the transition contract before Reanimated consumes it, as transitions.test.tsx does +jest.mock("../../native/reanimated", () => ({ + animatedComponentFamily: (component: unknown) => component, +})); + +const style = () => + StyleSheet.flatten(screen.getByTestId(testID).props.style) as + | Record + | undefined; + +// The compiler folds a variable declared once into the declaration that reads it, unless told to keep it +describe.each([ + ["folded", {}], + ["kept", { inlineVariables: false }], +] as const)( + "a list read through a variable the compiler has %s", + (_, options) => { + // css-variables-1 §3 substitutes a var() by its tokens, commas among them + test.each([ + [ + "box-shadow: var(--o) var(--b)", + "--o: 1px 2px; --b: 3px 4px #123;", + { + boxShadow: [ + { + offsetX: 1, + offsetY: 2, + blurRadius: 3, + spreadDistance: 4, + color: "#123", + }, + ], + }, + ], + [ + "box-shadow: var(--o), var(--b)", + "--o: 1px 2px 0 0 #123; --b: 3px 4px 0 0 #456;", + { + boxShadow: [ + { + offsetX: 1, + offsetY: 2, + blurRadius: 0, + spreadDistance: 0, + color: "#123", + }, + { + offsetX: 3, + offsetY: 4, + blurRadius: 0, + spreadDistance: 0, + color: "#456", + }, + ], + }, + ], + [ + "box-shadow: var(--a)", + "--b: 1px 2px; --a: var(--b) 3px 4px #123;", + { + boxShadow: [ + { + offsetX: 1, + offsetY: 2, + blurRadius: 3, + spreadDistance: 4, + color: "#123", + }, + ], + }, + ], + [ + "filter: var(--f)", + "--f: blur(2px) brightness(1.5);", + { filter: [{ blur: 2 }, { brightness: 1.5 }] }, + ], + [ + "transform: var(--t)", + "--t: rotate(45deg) translateX(10px);", + { transform: [{ rotate: "45deg" }, { translateX: 10 }] }, + ], + [ + "filter: var(--a) var(--b)", + "--a: blur(2px); --b: brightness(1.5);", + { filter: [{ blur: 2 }, { brightness: 1.5 }] }, + ], + [ + "box-shadow: var(--s), var(--s)", + "--s: 1px 1px 0 0 #123, 2px 2px 0 0 #456;", + { + boxShadow: [ + { + offsetX: 1, + offsetY: 1, + blurRadius: 0, + spreadDistance: 0, + color: "#123", + }, + { + offsetX: 2, + offsetY: 2, + blurRadius: 0, + spreadDistance: 0, + color: "#456", + }, + { + offsetX: 1, + offsetY: 1, + blurRadius: 0, + spreadDistance: 0, + color: "#123", + }, + { + offsetX: 2, + offsetY: 2, + blurRadius: 0, + spreadDistance: 0, + color: "#456", + }, + ], + }, + ], + [ + "box-shadow: var(--s) #456", + "--s: 1px 1px 0 0 #123, 2px 2px 0 0;", + { + boxShadow: [ + { + offsetX: 1, + offsetY: 1, + blurRadius: 0, + spreadDistance: 0, + color: "#123", + }, + { + offsetX: 2, + offsetY: 2, + blurRadius: 0, + spreadDistance: 0, + color: "#456", + }, + ], + }, + ], + [ + "transform: translate(var(--x), 20px)", + "--x: 10px;", + { transform: [{ translateX: 10 }, { translateY: 20 }] }, + ], + [ + "transform: scale(var(--s), 2)", + "--s: 3;", + { transform: [{ scaleX: 3 }, { scaleY: 2 }] }, + ], + [ + "transform: translate(var(--t))", + "--t: 10px, 20px;", + { transform: [{ translateX: 10 }, { translateY: 20 }] }, + ], + [ + "transform: scale(var(--s))", + "--s: 2, 3;", + { transform: [{ scaleX: 2 }, { scaleY: 3 }] }, + ], + [ + "transition-property: var(--p), height", + "--p: width;", + { transitionProperty: ["width", "height"] }, + ], + ])("%s over %s", (declaration, variables, expected) => { + registerCSS(`.a { ${variables} ${declaration}; }`, options); + render(); + expect(style()).toStrictEqual(expected); + }); + + // Reanimated's easing builders return instances of their own classes, so the held value is compared with the written one + test.each(["steps(4, end)", "cubic-bezier(0.25, 0.5, 0.75, 1)"])( + "transition-timing-function: var(--e) over --e: %s", + (easing) => { + registerCSS(`.a { transition-timing-function: ${easing}; }`, options); + render(); + const written = style(); + screen.unmount(); + registerCSS( + `.a { --e: ${easing}; transition-timing-function: var(--e); }`, + options, + ); + render(); + expect(style()).toEqual(written); + }, + ); + + // react-native-css maps translateZ() to nothing, so the list keeps the functions it maps + test("transform: var(--t) over --t: translateZ(10px) rotate(45deg)", () => { + registerCSS( + `.a { --t: translateZ(10px) rotate(45deg); transform: var(--t); }`, + options, + ); + render(); + expect(style()).toStrictEqual({ transform: [{ rotate: "45deg" }] }); + }); + + test("background-image: linear-gradient(to right, var(--c)) over --c: #123, #456", () => { + registerCSS( + `.a { --c: #123, #456; background-image: linear-gradient(to right, var(--c)); }`, + options, + ); + render(); + expect(style()).toStrictEqual({ + experimental_backgroundImage: "linear-gradient(to right, #123, #456)", + }); + }); + + test("background-color: rgb(var(--c) / 0.5) over --c: 255 0 0", () => { + registerCSS( + `.a { --c: 255 0 0; background-color: rgb(var(--c) / 0.5); }`, + options, + ); + render(); + expect(processColor(style()?.backgroundColor as string)).toBe( + processColor("rgba(255, 0, 0, 0.5)"), + ); + }); + + test("text-shadow: var(--o) var(--c) over --o: 1px 2px, --c: #123", () => { + registerCSS( + `.a { --o: 1px 2px; --c: #123; text-shadow: var(--o) var(--c); }`, + options, + ); + render(); + expect(style()).toStrictEqual({ + textShadowOffset: { width: 1, height: 2 }, + textShadowRadius: 0, + textShadowColor: "#123", + }); + }); + }, +); + +// A list the property's grammar refuses makes the declaration invalid at computed-value time (css-variables-1 §3.1), so it unsets what an earlier rule set +test.each([ + [ + "box-shadow: var(--o) var(--b)", + "--o: 1px 2px 0 0 #123; --b: 3px 4px 0 0 #456;", + "box-shadow: 5px 5px green;", + { boxShadow: [] }, + ], + [ + "filter: var(--f)", + "--f: blur(2px), brightness(1.5);", + "filter: grayscale(1);", + { filter: undefined }, + ], + [ + "transform: var(--t)", + "--t: rotate(45deg), translateX(10px);", + "transform: rotate(10deg);", + { transform: undefined }, + ], + [ + "filter: var(--a), var(--missing)", + "--a: blur(2px);", + "filter: grayscale(1);", + { filter: undefined }, + ], +])( + "%s over %s replaces an earlier rule's value with none", + (declaration, variables, earlier, expected) => { + registerCSS(`.earlier { ${earlier} } .a { ${variables} ${declaration}; }`, { + inlineVariables: false, + }); + render(); + expect(style()).toStrictEqual(expected); + }, +); + +test("linear-gradient(var(--c)) over --c: #123 #456 prints the space it was written with", () => { + registerCSS( + `.a { --c: #123 #456; background-image: linear-gradient(var(--c)); }`, + { inlineVariables: false }, + ); + render(); + expect(style()).toStrictEqual({ + experimental_backgroundImage: "linear-gradient(#123 #456)", + }); +}); + +test.each([ + [ + "box-shadow: var(--missing, 1px 2px) 3px 4px #123", + [ + { + offsetX: 1, + offsetY: 2, + blurRadius: 3, + spreadDistance: 4, + color: "#123", + }, + ], + ], + [ + "box-shadow: var(--missing, 1px 1px 0 0 #123, 2px 2px 0 0 #456)", + [ + { + offsetX: 1, + offsetY: 1, + blurRadius: 0, + spreadDistance: 0, + color: "#123", + }, + { + offsetX: 2, + offsetY: 2, + blurRadius: 0, + spreadDistance: 0, + color: "#456", + }, + ], + ], +])( + "%s substitutes its fallback's list as a variable's", + (declaration, boxShadow) => { + registerCSS(`.a { ${declaration}; }`); + render(); + expect(style()).toStrictEqual({ boxShadow }); + }, +); + +// The compiler folds a variable read in an unresolved colour's alpha away and drops the alpha, so this case keeps it +test("background-color: rgb(255 0 0 / var(--a)) over --a: 0.5 with the variable kept", () => { + registerCSS(`.a { --a: 0.5; background-color: rgb(255 0 0 / var(--a)); }`, { + inlineVariables: false, + }); + render(); + expect(processColor(style()?.backgroundColor as string)).toBe( + processColor("rgba(255, 0, 0, 0.5)"), + ); +}); + +// The compiler writes a folded translate() with its second offset, which defaults to 0 +test("transform: translate(var(--x)) over --x: 10px with the variable kept", () => { + registerCSS(`.a { --x: 10px; transform: translate(var(--x)); }`, { + inlineVariables: false, + }); + render(); + expect(style()).toStrictEqual({ transform: [{ translateX: 10 }] }); +}); + +// React Native's list props and the variable hook's callers take a list of one-token items as a flat array +test("transition-property: var(--p) over --p: width, opacity hands React Native a flat list", () => { + registerCSS(`.a { --p: width, opacity; transition-property: var(--p); }`, { + inlineVariables: false, + }); + render(); + expect(style()).toStrictEqual({ transitionProperty: ["width", "opacity"] }); +}); + +test("useNativeVariable reads --p: width, opacity as a flat list", () => { + registerCSS(`:root { --p: width, opacity; }`, { inlineVariables: false }); + function Sample() { + return ( + {JSON.stringify(useNativeVariable("--p"))} + ); + } + render(); + expect(screen.getByTestId(testID).props.children).toBe( + JSON.stringify(["width", "opacity"]), + ); +}); + +// A variable read twice in one element resolves the second time from the value the first read keeps +test.each<[string, readonly unknown[]]>([ + ["a space-separated list of tokens", [1, 2, "#123"]], + [ + "a space-separated list of objects", + [{ rotate: "45deg" }, { translateX: 10 }], + ], + [ + "a comma-separated list", + [ + [1, 1, 0, 0, "#123"], + [2, 2, 0, 0, "#456"], + ], + ], + [ + "a comma-separated list whose items start with an object and a string", + [ + [{ rotate: "45deg" }, "x"], + [{ rotate: "90deg" }, "y"], + ], + ], +])("%s resolves to itself once resolved", (_, list) => { + const get = (() => undefined) as unknown as Getter; + expect(resolveValue(list as StyleDescriptor, get, {})).toStrictEqual(list); +}); + +test.each<[string, readonly unknown[], readonly unknown[]]>([ + [ + "a list whose tokens all resolve to nothing", + [ + [{}, "var", "a", 1], + [{}, "var", "b", 1], + ], + [], + ], + [ + "a token list nested in a space-separated list", + [1, [2, 3], 4], + [1, 2, 3, 4], + ], + [ + "a comma-separated list with a lone token in an item's place, as React Native's list props take it", + [[1, 2, "#123"], "#456"], + [[1, 2, "#123"], ["#456"]], + ], +])("%s resolves as the list it stands for", (_, list, expected) => { + const get = (() => undefined) as unknown as Getter; + expect(resolveValue(list as StyleDescriptor, get, {})).toStrictEqual( + expected, + ); +}); + +test.each<[string, StyleDescriptor, unknown]>([ + [ + "rotate() over three comma-separated arguments", + [{}, "rotate", [["10deg"], ["20deg"], ["30deg"]]], + [{ rotateX: "10deg" }, { rotateY: "20deg" }, { rotateZ: "30deg" }], + ], + ["join() over one value", [{}, "join", "#123"], "#123"], +])("%s reads each argument", (_, descriptor, expected) => { + const get = (() => undefined) as unknown as Getter; + expect(resolveValue(descriptor, get, {})).toStrictEqual(expected); +}); diff --git a/src/compiler/compiler.ts b/src/compiler/compiler.ts index 4feefe92..d410171f 100644 --- a/src/compiler/compiler.ts +++ b/src/compiler/compiler.ts @@ -588,7 +588,13 @@ function parsePropertyInitialValue( // Unwrap single-child repeated values so downstream consumers get a // scalar instead of a 1-element array. For example, `+` with // initial-value `10px` should produce the same shape as ``. - return results.length === 1 ? results[0] : results; + if (results.length === 1) { + return results[0]; + } + + return component.value.multiplier.type === "comma" + ? results.map((result) => [result]) + : results; } default: return undefined; diff --git a/src/compiler/declarations.ts b/src/compiler/declarations.ts index 2f0b8a4a..59586d51 100644 --- a/src/compiler/declarations.ts +++ b/src/compiler/declarations.ts @@ -755,11 +755,9 @@ function parseTransform( parseLengthOrCoercePercentageToRuntime(t.value[0], builder), ], [ - [ - {}, - "translateY", - parseLengthOrCoercePercentageToRuntime(t.value[1], builder), - ], + {}, + "translateY", + parseLengthOrCoercePercentageToRuntime(t.value[1], builder), ], ]; case "translateX": @@ -811,7 +809,7 @@ function parseTransform( case "rotate3d": case "matrix": case "matrix3d": - return [[]]; + return []; } }), ]); @@ -1094,8 +1092,8 @@ export function reduceParseUnparsed( return undefined; } - let currentGroup: StyleDescriptor = []; - let groups: StyleDescriptor[] = [currentGroup]; + let currentGroup: StyleDescriptor[] = []; + const groups: StyleDescriptor[][] = [currentGroup]; for (const value of result) { if ((value as unknown) === CommaSeparator) { @@ -1106,40 +1104,24 @@ export function reduceParseUnparsed( } } - // Groups are the tokens grouped together by comma location - // If a group only has 1 item, it shouldn't be an array - groups = groups.flatMap((group): StyleDescriptor[] => { - if (!Array.isArray(group)) { - return []; - } + const items = groups.filter((group) => group.length > 0).map(reduceRatio); - if (group.length === 0) { - return []; - } else if (group.length === 1) { - const first = group[0]; + if (items.length > 1) { + return items; + } - if (first === undefined) { - return []; - } else { - return [first]; - } - } else if ( - // This is a special case for values - group.includes("/") && - group.every((item) => - typeof item === "string" && item === "/" - ? item - : typeof item === "number", - ) - ) { - // eslint-disable-next-line @typescript-eslint/no-base-to-string - return [group.join(" ")]; - } else { - return [group]; - } - }); + const item = items[0] ?? []; + return item.length === 1 ? item[0] : item; +} - return groups.length === 1 ? groups[0] : groups; +// React Native takes a such as `16 / 9` as one string +function reduceRatio(group: StyleDescriptor[]): StyleDescriptor[] { + return group.includes("/") && + group.every((item): item is number | "/" => { + return item === "/" || typeof item === "number"; + }) + ? [group.join(" ")] + : group; } export function unparsedFunction( @@ -2936,10 +2918,10 @@ export function parseUnresolvedColor( {}, "rgba", [ - round(color.r * 255), - round(color.g * 255), - round(color.b * 255), - parseUnparsed(color.alpha, builder, property), + [round(color.r * 255)], + [round(color.g * 255)], + [round(color.b * 255)], + [parseUnparsed(color.alpha, builder, property)], ], ]; case "hsl": @@ -2947,10 +2929,10 @@ export function parseUnresolvedColor( {}, color.type, [ - color.h, - color.s, - color.l, - parseUnparsed(color.alpha, builder, property), + [color.h], + [color.s], + [color.l], + [parseUnparsed(color.alpha, builder, property)], ], ]; case "light-dark": { @@ -3220,8 +3202,8 @@ function parseGradient( {}, "linear-gradient", [ - parseLineDirection(gradient.direction, builder), - ...gradient.items.map((item) => parseGradientItem(item, builder)), + [parseLineDirection(gradient.direction, builder)], + ...gradient.items.map((item) => [parseGradientItem(item, builder)]), ], ]; } diff --git a/src/compiler/stylesheet.ts b/src/compiler/stylesheet.ts index aaa5e0d5..97427904 100644 --- a/src/compiler/stylesheet.ts +++ b/src/compiler/stylesheet.ts @@ -430,7 +430,7 @@ export class StylesheetBuilder { } else { declarations.push([value, propPath]); } - } else if (Array.isArray(value) && value.some(isStyleFunction)) { + } else if (Array.isArray(value) && value.some(holdsStyleFunction)) { declarations.push([value, propPath]); } else if (typeof value === "string" && keywords.has(value)) { declarations.push([value, propPath]); @@ -636,6 +636,14 @@ function isStyleFunction( ); } +/** A comma-separated list holds each item as the array of its tokens, so a function may be one of an item's tokens. */ +function holdsStyleFunction(entry: StyleDescriptor): boolean { + return ( + isStyleFunction(entry) || + (Array.isArray(entry) && entry.some(isStyleFunction)) + ); +} + function postProcessStyleFunction(value: StyleDescriptor): [ // Should it be delayed boolean, diff --git a/src/native/api.tsx b/src/native/api.tsx index a83d997c..c6c5effb 100644 --- a/src/native/api.tsx +++ b/src/native/api.tsx @@ -4,6 +4,7 @@ import { Appearance, type ViewStyle } from "react-native"; import type { StyleDescriptor } from "react-native-css/compiler"; import { VariableContext } from "react-native-css/native-internal"; +import { commaListItems } from "react-native-css/utilities"; import type { ColorScheme, @@ -115,7 +116,9 @@ export function useNativeVariable(name: string) { return () => cleanupEffect(effect); }, [effect]); cleanupEffect(effect); - return resolveValue([{}, "var", [name]], effect.get, { inheritedVariables }); + return commaListItems( + resolveValue([{}, "var", [name]], effect.get, { inheritedVariables }), + ); } /** diff --git a/src/native/objects.ts b/src/native/objects.ts index 12e69ccd..4e49ccde 100644 --- a/src/native/objects.ts +++ b/src/native/objects.ts @@ -1,4 +1,6 @@ /* eslint-disable */ +import { commaListItems } from "react-native-css/utilities"; + import { ShortHandSymbol } from "../native/styles/constants"; import { transformKeys } from "../native/styles/defaults"; @@ -84,7 +86,11 @@ export function applyValue( return; } - target[prop] = value; + // commaListItems changes only a list of lists, and Hermes pays for a call on every declaration + target[prop] = + Array.isArray(value) && Array.isArray(value[0]) + ? commaListItems(value) + : value; } export function setDeepPath( diff --git a/src/native/styles/functions/animation-timing.ts b/src/native/styles/functions/animation-timing.ts index 2bd58078..1e942ca6 100644 --- a/src/native/styles/functions/animation-timing.ts +++ b/src/native/styles/functions/animation-timing.ts @@ -1,3 +1,5 @@ +import { commaListItems } from "react-native-css/utilities"; + import type { StyleFunctionResolver } from "../resolve"; /* eslint-disable @typescript-eslint/no-require-imports */ @@ -26,7 +28,7 @@ const timingFunctionResolver: StyleFunctionResolver = (resolveValue, value) => { return; } - const args = resolveValue(value[2]); + const args = commaListItems(resolveValue(value[2])); if (!Array.isArray(args)) { return; diff --git a/src/native/styles/functions/filters.ts b/src/native/styles/functions/filters.ts index 4a88a98a..dd5f6671 100644 --- a/src/native/styles/functions/filters.ts +++ b/src/native/styles/functions/filters.ts @@ -1,4 +1,7 @@ -import { isStyleDescriptorArray } from "react-native-css/utilities"; +import { + isCommaList, + isStyleDescriptorArray, +} from "react-native-css/utilities"; import type { StyleFunctionResolver } from "../resolve"; import { shorthandHandler } from "../shorthands/_handler"; @@ -98,11 +101,12 @@ export const dropShadow: StyleFunctionResolver = ( : undefined; }; -/** CSS variables may expand to multiple filters; native filter arrays must be flat. */ export const filter: StyleFunctionResolver = (resolveValue, descriptor) => { const value: unknown = resolveValue(descriptor[2]); + // filter-effects-1 §5: a filter list is space-separated, so a comma makes the declaration invalid at computed-value time + if (isCommaList(value)) return null; if (Array.isArray(value)) { - const filters: unknown[] = value.flat(Infinity); + const filters: readonly unknown[] = value; return filters.filter( (entry) => entry !== undefined && entry !== "initial", ); diff --git a/src/native/styles/functions/platform-functions.ts b/src/native/styles/functions/platform-functions.ts index 57f095f5..2fe21849 100644 --- a/src/native/styles/functions/platform-functions.ts +++ b/src/native/styles/functions/platform-functions.ts @@ -1,9 +1,11 @@ import { PixelRatio, PlatformColor, StyleSheet } from "react-native"; +import { commaListItems } from "react-native-css/utilities"; + import type { StyleFunctionResolver } from "../resolve"; export const platformColor: StyleFunctionResolver = (resolveValue, value) => { - const color: unknown = resolveValue(value[2]); + const color: unknown = commaListItems(resolveValue(value[2])); if (Array.isArray(color)) { return PlatformColor(...(color as string[])); } else if (typeof color === "string") { diff --git a/src/native/styles/functions/string-functions.ts b/src/native/styles/functions/string-functions.ts index 8ab29a98..700a6dce 100644 --- a/src/native/styles/functions/string-functions.ts +++ b/src/native/styles/functions/string-functions.ts @@ -1,3 +1,5 @@ +import { isStyleDescriptorArray } from "react-native-css/utilities"; + import type { StyleFunctionResolver } from "../resolve"; export const gradientPosition: StyleFunctionResolver = ( @@ -19,14 +21,14 @@ export const colorStop: StyleFunctionResolver = (resolveValue, value) => { }; export const join: StyleFunctionResolver = (resolveValue, value) => { - const args = resolveValue(value[2]); + const args = value[2]; - if (!Array.isArray(args)) { - return args; + if (!isStyleDescriptorArray(args)) { + return resolveValue(args); } - const array: unknown = args[0]; - const separator: unknown = args[1] ?? ","; + const array: unknown = resolveValue(args[0]); + const separator: unknown = resolveValue(args[1]) ?? ","; if (!Array.isArray(array)) { return array; diff --git a/src/native/styles/functions/transform-functions.ts b/src/native/styles/functions/transform-functions.ts index 9a38d15e..c6db1c48 100644 --- a/src/native/styles/functions/transform-functions.ts +++ b/src/native/styles/functions/transform-functions.ts @@ -1,17 +1,22 @@ -import { isStyleDescriptorArray } from "react-native-css/utilities"; - -import type { StyleFunctionResolver } from "../resolve"; +import type { StyleDescriptor } from "react-native-css/compiler"; +import { + commaListItems, + isCommaList, + isStyleDescriptorArray, +} from "react-native-css/utilities"; + +import type { SimpleResolveValue, StyleFunctionResolver } from "../resolve"; import { scaleFactor } from "../scale-factor"; export const scale: StyleFunctionResolver = (resolveValue, descriptor) => { - const args = descriptor[2]; + const values = transformArguments(resolveValue, descriptor[2]); - if (!isStyleDescriptorArray(args)) { - return { scale: scaleFactor(resolveValue(args)) }; + if (values.length === 1) { + return { scale: scaleFactor(values[0]) }; } - const x = scaleFactor(resolveValue(args[0])); - const y = scaleFactor(resolveValue(args[1])); + const x = scaleFactor(values[0]); + const y = scaleFactor(values[1]); const isXValid = typeof x === "string" || typeof x === "number"; const isYValid = typeof y === "string" || typeof y === "number"; @@ -28,15 +33,13 @@ export const scale: StyleFunctionResolver = (resolveValue, descriptor) => { }; export const rotate: StyleFunctionResolver = (resolveValue, descriptor) => { - const args = descriptor[2]; + const values = transformArguments(resolveValue, descriptor[2]); - if (!isStyleDescriptorArray(args)) { - return { rotate: resolveValue(args) }; + if (values.length === 1) { + return { rotate: values[0] }; } - const x = resolveValue(args[0]); - const y = resolveValue(args[1]); - const z = resolveValue(args[2]); + const [x, y, z] = values; const isXValid = typeof x === "string" || typeof x === "number"; const isYValid = typeof y === "string" || typeof y === "number"; @@ -62,14 +65,7 @@ export const rotate: StyleFunctionResolver = (resolveValue, descriptor) => { }; export const translate: StyleFunctionResolver = (resolveValue, descriptor) => { - const args = descriptor[2]; - - if (!isStyleDescriptorArray(args)) { - return; - } - - const x = resolveValue(args[0]); - const y = resolveValue(args[1]); + const [x, y] = transformArguments(resolveValue, descriptor[2]); const isXValid = typeof x === "string" || typeof x === "number"; const isYValid = typeof y === "string" || typeof y === "number"; @@ -84,3 +80,16 @@ export const translate: StyleFunctionResolver = (resolveValue, descriptor) => { return; }; + +/** The values in their written places, so one that resolves to nothing moves no other, or the items of a comma-separated list a lone value resolves to. */ +function transformArguments( + resolveValue: SimpleResolveValue, + args: StyleDescriptor, +): unknown[] { + if (isStyleDescriptorArray(args)) { + return commaListItems(args).map((value) => resolveValue(value)); + } + + const resolved: unknown = resolveValue(args); + return isCommaList(resolved) ? commaListItems(resolved) : [resolved]; +} diff --git a/src/native/styles/resolve.ts b/src/native/styles/resolve.ts index 0cb40385..92b19d76 100644 --- a/src/native/styles/resolve.ts +++ b/src/native/styles/resolve.ts @@ -5,7 +5,11 @@ import type { StyleDescriptor, StyleFunction, } from "react-native-css/compiler"; -import { isStyleDescriptorArray } from "react-native-css/utilities"; +import { + isCommaList, + isStyleDescriptorArray, + isStyleFunction, +} from "react-native-css/utilities"; import type { RenderGuard } from "../conditions/guards"; import { type Getter, type VariableContextValue } from "../reactivity"; @@ -91,15 +95,15 @@ export function resolveValue( } if (isStyleDescriptorArray(value)) { - value = value - .map((d) => resolveValue(d, get, options)) - .filter((d) => d !== undefined); - - if (castToArray && !Array.isArray(value)) { - return [value]; - } else { - return value; - } + const first = value[0]; + + // An item is an array that does not start as a function does, with its marker object then its name, tested inline as Hermes pays for a call on every list + return Array.isArray(first) && + (typeof first[0] !== "object" || + Array.isArray(first[0]) || + typeof first[1] !== "string") + ? resolveCommaList(value, get, options) + : resolveTokens(value, get, options); } const name = value[1]; @@ -141,15 +145,9 @@ export function resolveValue( if (args === undefined) { return; } else if (Array.isArray(args)) { - let joinedArgs = args - .map((arg: unknown) => { - if (Array.isArray(arg)) { - return arg.flat().join(" "); - } - return arg; - }) - .filter((value) => value !== "/") - .join(", "); + let joinedArgs = isCommaList(args) + ? args.map((item: readonly unknown[]) => item.join(" ")).join(", ") + : args.join(" "); if (name === "radial-gradient") { // Nativewind / Tailwind CSS hack which can force the 'in oklab' color space @@ -166,3 +164,86 @@ export function resolveValue( } } } + +/** Resolves the items of a comma-separated list, each the array of its tokens. */ +function resolveCommaList( + list: StyleDescriptor[], + get: Getter, + options: ResolveValueOptions, +): unknown[] { + const items: unknown[] = []; + + for (const tokens of list) { + // A function is one token though it is an array; a list taken for one is still resolved whole, as resolveValue tells them apart + const isTokens = + Array.isArray(tokens) && + (typeof tokens[0] !== "object" || + Array.isArray(tokens[0]) || + typeof tokens[1] !== "string"); + const item = resolveTokens( + isTokens ? (tokens as StyleDescriptor[]) : [tokens], + get, + options, + ); + + if (Array.isArray(item[0])) { + for (const split of item) items.push(split); + } else if (item.length > 0) { + items.push(item); + } + } + + return items; +} + +/** Resolves a space-separated list's tokens as css-variables-1 §3 substitutes a var(): a list joins it in its place, and a comma-separated one splits it. */ +function resolveTokens( + tokens: StyleDescriptor[], + get: Getter, + options: ResolveValueOptions, +): unknown[] { + if (tokens.length === 1) { + // A lone token's value is the list + const resolved: unknown = resolveValue(tokens[0], get, options); + return Array.isArray(resolved) + ? resolved + : resolved === undefined + ? [] + : [resolved]; + } + + let item: unknown[] = []; + const items: unknown[][] = [item]; + let isFirst = true; + + for (const token of tokens) { + const resolved: unknown = resolveValue(token, get, options); + + // Only a function or a nested list can resolve to a list + if (typeof token !== "object" || !Array.isArray(resolved)) { + if (resolved !== undefined) item.push(resolved); + } else if (isFirst && !isStyleFunction(token)) { + // A list led by an item, which here starts with an object as only a resolved list can, is comma-separated + return resolveCommaList(tokens, get, options); + } else if (!Array.isArray(resolved[0])) { + for (const value of resolved) item.push(value); + } else { + // A resolved list holds no functions, so one led by an array is comma-separated: its first item continues this one, each later one starts its own + let startsItem = false; + + for (const group of resolved) { + if (startsItem) { + item = []; + items.push(item); + } + + startsItem = true; + for (const value of group) item.push(value); + } + } + + isFirst = false; + } + + return items.length === 1 ? item : items; +} diff --git a/src/native/styles/shorthands/box-shadow.ts b/src/native/styles/shorthands/box-shadow.ts index 20936e49..60f62d03 100644 --- a/src/native/styles/shorthands/box-shadow.ts +++ b/src/native/styles/shorthands/box-shadow.ts @@ -1,5 +1,7 @@ -import type { StyleDescriptor } from "react-native-css/compiler"; -import { isStyleDescriptorArray } from "react-native-css/utilities"; +import { + isCommaList, + isStyleDescriptorArray, +} from "react-native-css/utilities"; import type { StyleFunctionResolver } from "../resolve"; import { shorthandHandler } from "./_handler"; @@ -45,42 +47,17 @@ export const boxShadow: StyleFunctionResolver = ( return args; } - // A flat array of primitives (e.g. [0, 4, 6, -1, "#000"]) is a single shadow - // resolved from a runtime variable. Pass it directly to the pattern handler. - // A nested array (e.g. [[0, 4, 6, -1, "#000"], [...]]) is multiple shadows. - if (args.length > 0 && !Array.isArray(args[0])) { - const result = handler(resolveValue, args, get, options); - if (result === undefined) { - return []; - } - const filtered = omitTransparentShadows(result); - return filtered !== undefined ? [normalizeInsetValue(filtered)] : []; - } + const shadows = isCommaList(args) ? args : [args]; - return args - .flatMap(flattenShadowDescriptor) - .map((shadows) => { - if (shadows === undefined) { - return; - } else { - return normalizeInsetValue( - omitTransparentShadows(handler(resolveValue, shadows, get, options)), - ); - } - }) + return shadows + .map((shadow) => + normalizeInsetValue( + omitTransparentShadows(handler(resolveValue, shadow, get, options)), + ), + ) .filter((v) => v !== undefined); }; -function flattenShadowDescriptor(arg: StyleDescriptor): StyleDescriptor[] { - if (isStyleDescriptorArray(arg) && isStyleDescriptorArray(arg[0])) { - return arg.map((arg) => { - return flattenShadowDescriptor(arg); - }); - } - - return [arg]; -} - function omitTransparentShadows(style: unknown) { if (typeof style === "object" && style && "color" in style) { if (style.color === "#0000" || style.color === "transparent") { diff --git a/src/native/styles/shorthands/transform.ts b/src/native/styles/shorthands/transform.ts index ef603dd8..efa26afe 100644 --- a/src/native/styles/shorthands/transform.ts +++ b/src/native/styles/shorthands/transform.ts @@ -1,3 +1,5 @@ +import { isCommaList } from "react-native-css/utilities"; + import type { StyleFunctionResolver } from "../resolve"; /** @@ -10,7 +12,10 @@ export const transform: StyleFunctionResolver = ( ) => { const transforms = resolveValue(transformDescriptor[2]); - if (Array.isArray(transforms)) { + // css-transforms-1 §4: a transform list is space-separated, so a comma makes the declaration invalid at computed-value time + if (isCommaList(transforms)) { + return null; + } else if (Array.isArray(transforms)) { return transforms.filter( (transform) => transform !== undefined && transform !== "initial", ) as unknown; diff --git a/src/utilities/style-descriptor.ts b/src/utilities/style-descriptor.ts index 7a166fa1..01d2e3c9 100644 --- a/src/utilities/style-descriptor.ts +++ b/src/utilities/style-descriptor.ts @@ -17,3 +17,22 @@ export function isStyleFunction(value: unknown): value is StyleFunction { typeof value[1] === "string" ); } + +/** A comma-separated list holds each item as the array of its tokens, and a token is never such an array, so its first item tells. */ +export function isCommaList(value: unknown): value is StyleDescriptor[][] { + return ( + Array.isArray(value) && + Array.isArray(value[0]) && + !isStyleFunction(value[0]) + ); +} + +/** A comma-separated list with a lone token in place of its array, the form function arguments and React Native's list props take. */ +export function commaListItems(value: StyleDescriptor[]): StyleDescriptor[]; +export function commaListItems(value: StyleDescriptor): StyleDescriptor; +export function commaListItems(value: unknown): unknown; +export function commaListItems(value: unknown): unknown { + return isCommaList(value) + ? value.map((item) => (item.length === 1 ? item[0] : item)) + : value; +} From 7c9fcfa89181f1aec984216856ed472037905fc4 Mon Sep 17 00:00:00 2001 From: Sam Moore Date: Mon, 9 Nov 2026 17:00:47 -0500 Subject: [PATCH 02/10] refactor(native): resolve a list in one pass and test resolved lists cheaply Follow-up to the separator fix. resolveCommaList and resolveTokens, which handed lists back and forth and re-detected a comma-separated list led by an object, become one resolveList: each entry's tokens are resolved and appended to the current item, and a resolved list is substituted by appendList (a space-separated one joins the item, a comma-separated one splits it). A resolved value holds no style functions, so isCommaList and commaListItems now test only resolved values, with Array.isArray alone instead of isStyleFunction's Object.keys call; the one reader of a descriptor, transformArguments, tests its arguments with isStyleDescriptorArray. Behaviour is unchanged. Co-Authored-By: Claude Opus 5.5 --- src/native/objects.ts | 3 +- src/native/styles/functions/filters.ts | 4 +- .../styles/functions/transform-functions.ts | 16 +- src/native/styles/resolve.ts | 140 +++++++++--------- src/utilities/style-descriptor.ts | 24 +-- 5 files changed, 98 insertions(+), 89 deletions(-) diff --git a/src/native/objects.ts b/src/native/objects.ts index 4e49ccde..ea6df36f 100644 --- a/src/native/objects.ts +++ b/src/native/objects.ts @@ -86,7 +86,8 @@ export function applyValue( return; } - // commaListItems changes only a list of lists, and Hermes pays for a call on every declaration + // React Native's list props take a comma-separated list with one-token items bare. + // The guard is commaListItems' own test, inlined to skip a call on every declaration. target[prop] = Array.isArray(value) && Array.isArray(value[0]) ? commaListItems(value) diff --git a/src/native/styles/functions/filters.ts b/src/native/styles/functions/filters.ts index dd5f6671..1f406cd8 100644 --- a/src/native/styles/functions/filters.ts +++ b/src/native/styles/functions/filters.ts @@ -101,13 +101,13 @@ export const dropShadow: StyleFunctionResolver = ( : undefined; }; +/** A var() in a filter list joins it as its filters, which resolveValue splices in */ export const filter: StyleFunctionResolver = (resolveValue, descriptor) => { const value: unknown = resolveValue(descriptor[2]); // filter-effects-1 §5: a filter list is space-separated, so a comma makes the declaration invalid at computed-value time if (isCommaList(value)) return null; if (Array.isArray(value)) { - const filters: readonly unknown[] = value; - return filters.filter( + return (value as unknown[]).filter( (entry) => entry !== undefined && entry !== "initial", ); } diff --git a/src/native/styles/functions/transform-functions.ts b/src/native/styles/functions/transform-functions.ts index c6db1c48..43d31341 100644 --- a/src/native/styles/functions/transform-functions.ts +++ b/src/native/styles/functions/transform-functions.ts @@ -81,15 +81,25 @@ export const translate: StyleFunctionResolver = (resolveValue, descriptor) => { return; }; -/** The values in their written places, so one that resolves to nothing moves no other, or the items of a comma-separated list a lone value resolves to. */ +/** + * The arguments as written, each in its place so one that resolves to nothing + * moves no other, or the items of the comma-separated list a lone argument + * resolves to, as `translate(var(--t))` over `--t: 10px, 20px`. + */ function transformArguments( resolveValue: SimpleResolveValue, args: StyleDescriptor, ): unknown[] { if (isStyleDescriptorArray(args)) { - return commaListItems(args).map((value) => resolveValue(value)); + return args.map((arg) => + resolveValue( + isStyleDescriptorArray(arg) && arg.length === 1 ? arg[0] : arg, + ), + ); } const resolved: unknown = resolveValue(args); - return isCommaList(resolved) ? commaListItems(resolved) : [resolved]; + return isCommaList(resolved) + ? (commaListItems(resolved) as unknown[]) + : [resolved]; } diff --git a/src/native/styles/resolve.ts b/src/native/styles/resolve.ts index 92b19d76..fd97bac7 100644 --- a/src/native/styles/resolve.ts +++ b/src/native/styles/resolve.ts @@ -8,7 +8,6 @@ import type { import { isCommaList, isStyleDescriptorArray, - isStyleFunction, } from "react-native-css/utilities"; import type { RenderGuard } from "../conditions/guards"; @@ -95,15 +94,7 @@ export function resolveValue( } if (isStyleDescriptorArray(value)) { - const first = value[0]; - - // An item is an array that does not start as a function does, with its marker object then its name, tested inline as Hermes pays for a call on every list - return Array.isArray(first) && - (typeof first[0] !== "object" || - Array.isArray(first[0]) || - typeof first[1] !== "string") - ? resolveCommaList(value, get, options) - : resolveTokens(value, get, options); + return resolveList(value, get, options); } const name = value[1]; @@ -165,46 +156,28 @@ export function resolveValue( } } -/** Resolves the items of a comma-separated list, each the array of its tokens. */ -function resolveCommaList( +/** + * Resolves a list descriptor. A comma-separated list holds each item as the + * array of its tokens; anything else is a space-separated list of tokens. + * + * A token that resolves to a list is substituted by its tokens, as + * css-variables-1 §3 substitutes a var(): a space-separated list joins the + * current item, and a comma-separated one splits it. + * + * Resolved values are resolved again (variables cache them), so the result + * keeps the same shape: a flat array for a space-separated list, an array of + * arrays for a comma-separated one. + */ +function resolveList( list: StyleDescriptor[], get: Getter, options: ResolveValueOptions, ): unknown[] { - const items: unknown[] = []; - - for (const tokens of list) { - // A function is one token though it is an array; a list taken for one is still resolved whole, as resolveValue tells them apart - const isTokens = - Array.isArray(tokens) && - (typeof tokens[0] !== "object" || - Array.isArray(tokens[0]) || - typeof tokens[1] !== "string"); - const item = resolveTokens( - isTokens ? (tokens as StyleDescriptor[]) : [tokens], - get, - options, - ); - - if (Array.isArray(item[0])) { - for (const split of item) items.push(split); - } else if (item.length > 0) { - items.push(item); - } - } + const isCommaSeparated = isStyleDescriptorArray(list[0]); - return items; -} - -/** Resolves a space-separated list's tokens as css-variables-1 §3 substitutes a var(): a list joins it in its place, and a comma-separated one splits it. */ -function resolveTokens( - tokens: StyleDescriptor[], - get: Getter, - options: ResolveValueOptions, -): unknown[] { - if (tokens.length === 1) { - // A lone token's value is the list - const resolved: unknown = resolveValue(tokens[0], get, options); + if (!isCommaSeparated && list.length === 1) { + // Fast path for a lone token: its list, if it resolves to one, is the value + const resolved: unknown = resolveValue(list[0], get, options); return Array.isArray(resolved) ? resolved : resolved === undefined @@ -212,38 +185,61 @@ function resolveTokens( : [resolved]; } + const items: unknown[][] = []; let item: unknown[] = []; - const items: unknown[][] = [item]; - let isFirst = true; - - for (const token of tokens) { - const resolved: unknown = resolveValue(token, get, options); - - // Only a function or a nested list can resolve to a list - if (typeof token !== "object" || !Array.isArray(resolved)) { - if (resolved !== undefined) item.push(resolved); - } else if (isFirst && !isStyleFunction(token)) { - // A list led by an item, which here starts with an object as only a resolved list can, is comma-separated - return resolveCommaList(tokens, get, options); - } else if (!Array.isArray(resolved[0])) { - for (const value of resolved) item.push(value); - } else { - // A resolved list holds no functions, so one led by an array is comma-separated: its first item continues this one, each later one starts its own - let startsItem = false; - for (const group of resolved) { - if (startsItem) { - item = []; - items.push(item); - } - - startsItem = true; - for (const value of group) item.push(value); + for (const entry of list) { + if (isCommaSeparated && isStyleDescriptorArray(entry)) { + for (const token of entry) { + item = appendToken(resolveValue(token, get, options), item, items); } + } else { + // A lone token in an item's place, or a space-separated list's token + item = appendToken(resolveValue(entry, get, options), item, items); + } + + if (isCommaSeparated) { + if (item.length > 0) items.push(item); + item = []; } + } + + if (items.length === 0) { + return item; + } + + if (item.length > 0) items.push(item); + return items; +} - isFirst = false; +/** + * Appends a resolved token to the current item and returns the item later + * tokens join. Resolved values hold no style functions, so an array led by an + * array is a comma-separated list: its first item continues the current one + * and each later item starts its own. + */ +function appendToken( + resolved: unknown, + item: unknown[], + items: unknown[][], +): unknown[] { + if (!Array.isArray(resolved)) { + if (resolved !== undefined) item.push(resolved); + return item; + } + + if (!Array.isArray(resolved[0])) { + for (const value of resolved) item.push(value); + return item; + } + + for (let index = 0; index < resolved.length; index++) { + if (index > 0) { + items.push(item); + item = []; + } + for (const value of resolved[index] as unknown[]) item.push(value); } - return items.length === 1 ? item : items; + return item; } diff --git a/src/utilities/style-descriptor.ts b/src/utilities/style-descriptor.ts index 01d2e3c9..59850f23 100644 --- a/src/utilities/style-descriptor.ts +++ b/src/utilities/style-descriptor.ts @@ -18,19 +18,21 @@ export function isStyleFunction(value: unknown): value is StyleFunction { ); } -/** A comma-separated list holds each item as the array of its tokens, and a token is never such an array, so its first item tells. */ -export function isCommaList(value: unknown): value is StyleDescriptor[][] { - return ( - Array.isArray(value) && - Array.isArray(value[0]) && - !isStyleFunction(value[0]) - ); +/** + * Whether a resolved value is a comma-separated list: an array of items, each + * the array of its tokens. A resolved value holds no style functions, so an + * array led by an array is one. For a descriptor, which can be led by a + * function, test its first entry with isStyleDescriptorArray instead. + */ +export function isCommaList(value: unknown): value is unknown[][] { + return Array.isArray(value) && Array.isArray(value[0]); } -/** A comma-separated list with a lone token in place of its array, the form function arguments and React Native's list props take. */ -export function commaListItems(value: StyleDescriptor[]): StyleDescriptor[]; -export function commaListItems(value: StyleDescriptor): StyleDescriptor; -export function commaListItems(value: unknown): unknown; +/** + * A resolved comma-separated list with each one-token item unwrapped, the + * form function arguments and React Native's list props take. Anything else + * is returned as it is. + */ export function commaListItems(value: unknown): unknown { return isCommaList(value) ? value.map((item) => (item.length === 1 ? item[0] : item)) From cb6025411f1d7e24b346e585967702923fb0c905 Mon Sep 17 00:00:00 2001 From: Sam Moore Date: Mon, 9 Nov 2026 16:15:02 -0500 Subject: [PATCH 03/10] perf(native): match resolved shadows without resolving them again boxShadow resolves its argument list once, then handed each shadow to the shorthand handler with resolveValue, which resolved every token again. Pass an identity resolver so the handler only matches the shadow. Co-Authored-By: Claude Opus 5.5 --- src/native/styles/shorthands/box-shadow.ts | 23 +++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/native/styles/shorthands/box-shadow.ts b/src/native/styles/shorthands/box-shadow.ts index 60f62d03..b3066f01 100644 --- a/src/native/styles/shorthands/box-shadow.ts +++ b/src/native/styles/shorthands/box-shadow.ts @@ -35,6 +35,8 @@ const handler = shorthandHandler( "object", ); +const alreadyResolved = (value: unknown) => value; + export const boxShadow: StyleFunctionResolver = ( resolveValue, func, @@ -47,15 +49,22 @@ export const boxShadow: StyleFunctionResolver = ( return args; } + // A flat array of tokens (e.g. [0, 4, 6, -1, "#000"]) is a single shadow, + // written out or substituted from a variable. An array of arrays + // (e.g. [[0, 4, 6, -1, "#000"], [...]]) is a comma-separated list, one + // shadow per item. The compiler keeps the separator via the nested array. const shadows = isCommaList(args) ? args : [args]; + const result: unknown[] = []; + + for (const shadow of shadows) { + const value = normalizeInsetValue( + // alreadyResolved rather than resolveValue: don't walk through tokens and resolve all shadows when we've already resolved this one + omitTransparentShadows(handler(alreadyResolved, shadow, get, options)), + ); + if (value !== undefined) result.push(value); + } - return shadows - .map((shadow) => - normalizeInsetValue( - omitTransparentShadows(handler(resolveValue, shadow, get, options)), - ), - ) - .filter((v) => v !== undefined); + return result; }; function omitTransparentShadows(style: unknown) { From d3c616dee18e1a1e0a43560875b6d523193b2dd4 Mon Sep 17 00:00:00 2001 From: Sam Moore Date: Mon, 9 Nov 2026 19:02:31 -0500 Subject: [PATCH 04/10] refactor(compiler): keep a list's separator with a smaller change The separator fix rewrote reduceParseUnparsed and moved its case into a helper. Two changes to the original are enough: each comma group stays an array, so a one-token item stays distinct from a token of a space-separated list, and a lone group is unwrapped at the end as before. The compiled output is unchanged. Co-Authored-By: Claude Opus 5.5 --- src/compiler/declarations.ts | 57 +++++++++++++++++++++++++----------- 1 file changed, 40 insertions(+), 17 deletions(-) diff --git a/src/compiler/declarations.ts b/src/compiler/declarations.ts index 59586d51..aa1903b1 100644 --- a/src/compiler/declarations.ts +++ b/src/compiler/declarations.ts @@ -1092,8 +1092,8 @@ export function reduceParseUnparsed( return undefined; } - let currentGroup: StyleDescriptor[] = []; - const groups: StyleDescriptor[][] = [currentGroup]; + let currentGroup: StyleDescriptor = []; + let groups: StyleDescriptor[] = [currentGroup]; for (const value of result) { if ((value as unknown) === CommaSeparator) { @@ -1104,24 +1104,47 @@ export function reduceParseUnparsed( } } - const items = groups.filter((group) => group.length > 0).map(reduceRatio); + // Groups are the tokens grouped together by comma location. Each stays an + // array, so a one-token item of a comma-separated list stays distinct from + // a token of a space-separated one (css-variables-1 §2.1) + groups = groups.flatMap((group): StyleDescriptor[] => { + if (!Array.isArray(group)) { + return []; + } - if (items.length > 1) { - return items; - } + if (group.length === 0) { + return []; + } else if (group.length === 1) { + const first = group[0]; - const item = items[0] ?? []; - return item.length === 1 ? item[0] : item; -} + if (first === undefined) { + return []; + } else { + return [[first]]; + } + } else if ( + // This is a special case for values + group.includes("/") && + group.every((item) => + typeof item === "string" && item === "/" + ? item + : typeof item === "number", + ) + ) { + // eslint-disable-next-line @typescript-eslint/no-base-to-string + return [[group.join(" ")]]; + } else { + return [group]; + } + }); -// React Native takes a such as `16 / 9` as one string -function reduceRatio(group: StyleDescriptor[]): StyleDescriptor[] { - return group.includes("/") && - group.every((item): item is number | "/" => { - return item === "/" || typeof item === "number"; - }) - ? [group.join(" ")] - : group; + if (groups.length !== 1) { + return groups; + } + + // A lone group is the value, and a lone token the group + const group = groups[0] as StyleDescriptor[]; + return group.length === 1 ? group[0] : group; } export function unparsedFunction( From 32ae460df5fc1e710321a68da7ff2cb52928a299 Mon Sep 17 00:00:00 2001 From: Sam Moore Date: Mon, 9 Nov 2026 19:04:37 -0500 Subject: [PATCH 05/10] refactor(native): read transform arguments in place, without spreading a var() The separator fix compiles a comma-separated argument list as items, so translate(), scale() and rotate() must unwrap each one-token argument. It also added a reader that spreads a lone var() holding a comma-separated list into the arguments (translate(var(--t)) over --t: 10px, 20px) and reads translate() with one var() argument. Neither concerns the separator, so this keeps the original resolvers and only unwraps each argument; translate(var(--t)), scale(var(--s)) over a comma list and a lone translate(var(--x)) render as they do on main, and their cases go. Co-Authored-By: Claude Opus 5.5 --- src/__tests__/native/variable-lists.test.tsx | 19 ------ .../styles/functions/transform-functions.ts | 61 ++++++++----------- 2 files changed, 25 insertions(+), 55 deletions(-) diff --git a/src/__tests__/native/variable-lists.test.tsx b/src/__tests__/native/variable-lists.test.tsx index ac57d846..8be7688f 100644 --- a/src/__tests__/native/variable-lists.test.tsx +++ b/src/__tests__/native/variable-lists.test.tsx @@ -164,16 +164,6 @@ describe.each([ "--s: 3;", { transform: [{ scaleX: 3 }, { scaleY: 2 }] }, ], - [ - "transform: translate(var(--t))", - "--t: 10px, 20px;", - { transform: [{ translateX: 10 }, { translateY: 20 }] }, - ], - [ - "transform: scale(var(--s))", - "--s: 2, 3;", - { transform: [{ scaleX: 2 }, { scaleY: 3 }] }, - ], [ "transition-property: var(--p), height", "--p: width;", @@ -349,15 +339,6 @@ test("background-color: rgb(255 0 0 / var(--a)) over --a: 0.5 with the variable ); }); -// The compiler writes a folded translate() with its second offset, which defaults to 0 -test("transform: translate(var(--x)) over --x: 10px with the variable kept", () => { - registerCSS(`.a { --x: 10px; transform: translate(var(--x)); }`, { - inlineVariables: false, - }); - render(); - expect(style()).toStrictEqual({ transform: [{ translateX: 10 }] }); -}); - // React Native's list props and the variable hook's callers take a list of one-token items as a flat array test("transition-property: var(--p) over --p: width, opacity hands React Native a flat list", () => { registerCSS(`.a { --p: width, opacity; transition-property: var(--p); }`, { diff --git a/src/native/styles/functions/transform-functions.ts b/src/native/styles/functions/transform-functions.ts index 43d31341..e6fcacaf 100644 --- a/src/native/styles/functions/transform-functions.ts +++ b/src/native/styles/functions/transform-functions.ts @@ -1,22 +1,18 @@ import type { StyleDescriptor } from "react-native-css/compiler"; -import { - commaListItems, - isCommaList, - isStyleDescriptorArray, -} from "react-native-css/utilities"; +import { isStyleDescriptorArray } from "react-native-css/utilities"; import type { SimpleResolveValue, StyleFunctionResolver } from "../resolve"; import { scaleFactor } from "../scale-factor"; export const scale: StyleFunctionResolver = (resolveValue, descriptor) => { - const values = transformArguments(resolveValue, descriptor[2]); + const args = descriptor[2]; - if (values.length === 1) { - return { scale: scaleFactor(values[0]) }; + if (!isStyleDescriptorArray(args)) { + return { scale: scaleFactor(resolveValue(args)) }; } - const x = scaleFactor(values[0]); - const y = scaleFactor(values[1]); + const x = scaleFactor(argument(resolveValue, args[0])); + const y = scaleFactor(argument(resolveValue, args[1])); const isXValid = typeof x === "string" || typeof x === "number"; const isYValid = typeof y === "string" || typeof y === "number"; @@ -33,13 +29,15 @@ export const scale: StyleFunctionResolver = (resolveValue, descriptor) => { }; export const rotate: StyleFunctionResolver = (resolveValue, descriptor) => { - const values = transformArguments(resolveValue, descriptor[2]); + const args = descriptor[2]; - if (values.length === 1) { - return { rotate: values[0] }; + if (!isStyleDescriptorArray(args)) { + return { rotate: resolveValue(args) }; } - const [x, y, z] = values; + const x = argument(resolveValue, args[0]); + const y = argument(resolveValue, args[1]); + const z = argument(resolveValue, args[2]); const isXValid = typeof x === "string" || typeof x === "number"; const isYValid = typeof y === "string" || typeof y === "number"; @@ -65,7 +63,14 @@ export const rotate: StyleFunctionResolver = (resolveValue, descriptor) => { }; export const translate: StyleFunctionResolver = (resolveValue, descriptor) => { - const [x, y] = transformArguments(resolveValue, descriptor[2]); + const args = descriptor[2]; + + if (!isStyleDescriptorArray(args)) { + return; + } + + const x = argument(resolveValue, args[0]); + const y = argument(resolveValue, args[1]); const isXValid = typeof x === "string" || typeof x === "number"; const isYValid = typeof y === "string" || typeof y === "number"; @@ -81,25 +86,9 @@ export const translate: StyleFunctionResolver = (resolveValue, descriptor) => { return; }; -/** - * The arguments as written, each in its place so one that resolves to nothing - * moves no other, or the items of the comma-separated list a lone argument - * resolves to, as `translate(var(--t))` over `--t: 10px, 20px`. - */ -function transformArguments( - resolveValue: SimpleResolveValue, - args: StyleDescriptor, -): unknown[] { - if (isStyleDescriptorArray(args)) { - return args.map((arg) => - resolveValue( - isStyleDescriptorArray(arg) && arg.length === 1 ? arg[0] : arg, - ), - ); - } - - const resolved: unknown = resolveValue(args); - return isCommaList(resolved) - ? (commaListItems(resolved) as unknown[]) - : [resolved]; +/** A comma-separated argument list holds each argument as the array of its tokens */ +function argument(resolveValue: SimpleResolveValue, arg: StyleDescriptor) { + return resolveValue( + isStyleDescriptorArray(arg) && arg.length === 1 ? arg[0] : arg, + ); } From a80bc62028036b9ad69f2c3b143aae277d879d9e Mon Sep 17 00:00:00 2001 From: Sam Moore Date: Mon, 9 Nov 2026 19:06:01 -0500 Subject: [PATCH 06/10] refactor(native): leave filter and transform as they read a list on main The separator fix made filter and transform return null for a comma-separated list, as browsers drop the declaration. That enforces a grammar rather than keeping a separator, so this keeps main's lenient reading: - filter flattens a comma-separated list into its filters, as main's flat(Infinity) did. Only a comma-separated list nests now that resolveValue splices a var() in, so it flattens one level and only for such a list. - transform renders a comma-separated list's functions in order. Neither hands React Native anything it refuses. Tailwind never writes a comma in either property, and its filter, transform and shadow utilities render identically on main, the separator fix and here. The filter and transform cases of the "replaces an earlier rule's value" table go. Co-Authored-By: Claude Opus 5.5 --- src/__tests__/native/variable-lists.test.tsx | 18 ------------------ src/native/styles/functions/filters.ts | 13 +++++-------- src/native/styles/shorthands/transform.ts | 7 +------ 3 files changed, 6 insertions(+), 32 deletions(-) diff --git a/src/__tests__/native/variable-lists.test.tsx b/src/__tests__/native/variable-lists.test.tsx index 8be7688f..3088e33c 100644 --- a/src/__tests__/native/variable-lists.test.tsx +++ b/src/__tests__/native/variable-lists.test.tsx @@ -247,24 +247,6 @@ test.each([ "box-shadow: 5px 5px green;", { boxShadow: [] }, ], - [ - "filter: var(--f)", - "--f: blur(2px), brightness(1.5);", - "filter: grayscale(1);", - { filter: undefined }, - ], - [ - "transform: var(--t)", - "--t: rotate(45deg), translateX(10px);", - "transform: rotate(10deg);", - { transform: undefined }, - ], - [ - "filter: var(--a), var(--missing)", - "--a: blur(2px);", - "filter: grayscale(1);", - { filter: undefined }, - ], ])( "%s over %s replaces an earlier rule's value with none", (declaration, variables, earlier, expected) => { diff --git a/src/native/styles/functions/filters.ts b/src/native/styles/functions/filters.ts index 1f406cd8..75198dc6 100644 --- a/src/native/styles/functions/filters.ts +++ b/src/native/styles/functions/filters.ts @@ -1,7 +1,4 @@ -import { - isCommaList, - isStyleDescriptorArray, -} from "react-native-css/utilities"; +import { isStyleDescriptorArray } from "react-native-css/utilities"; import type { StyleFunctionResolver } from "../resolve"; import { shorthandHandler } from "../shorthands/_handler"; @@ -101,13 +98,13 @@ export const dropShadow: StyleFunctionResolver = ( : undefined; }; -/** A var() in a filter list joins it as its filters, which resolveValue splices in */ +/** CSS variables may expand to multiple filters; native filter arrays must be flat. */ export const filter: StyleFunctionResolver = (resolveValue, descriptor) => { const value: unknown = resolveValue(descriptor[2]); - // filter-effects-1 §5: a filter list is space-separated, so a comma makes the declaration invalid at computed-value time - if (isCommaList(value)) return null; if (Array.isArray(value)) { - return (value as unknown[]).filter( + // resolveValue splices a var() in, so only a comma-separated list nests + const filters: unknown[] = Array.isArray(value[0]) ? value.flat() : value; + return filters.filter( (entry) => entry !== undefined && entry !== "initial", ); } diff --git a/src/native/styles/shorthands/transform.ts b/src/native/styles/shorthands/transform.ts index efa26afe..ef603dd8 100644 --- a/src/native/styles/shorthands/transform.ts +++ b/src/native/styles/shorthands/transform.ts @@ -1,5 +1,3 @@ -import { isCommaList } from "react-native-css/utilities"; - import type { StyleFunctionResolver } from "../resolve"; /** @@ -12,10 +10,7 @@ export const transform: StyleFunctionResolver = ( ) => { const transforms = resolveValue(transformDescriptor[2]); - // css-transforms-1 §4: a transform list is space-separated, so a comma makes the declaration invalid at computed-value time - if (isCommaList(transforms)) { - return null; - } else if (Array.isArray(transforms)) { + if (Array.isArray(transforms)) { return transforms.filter( (transform) => transform !== undefined && transform !== "initial", ) as unknown; From 815b6a5b607150b82a5908fd6cf27447442facdc Mon Sep 17 00:00:00 2001 From: Sam Moore Date: Wed, 11 Nov 2026 14:59:55 -0500 Subject: [PATCH 07/10] refactor(compiler): leave parseTransform as it is on main Flattening translate()'s second offset and writing nothing for an unsupported function such as translateZ() is a transform fix that #389 makes too, in its rewrite of parseTransform. It isn't needed to keep a list's separator, so this branch leaves it to #389. Co-Authored-By: Claude Opus 5.5 --- src/__tests__/compiler/compiler.test.tsx | 18 ------------------ src/compiler/declarations.ts | 10 ++++++---- 2 files changed, 6 insertions(+), 22 deletions(-) diff --git a/src/__tests__/compiler/compiler.test.tsx b/src/__tests__/compiler/compiler.test.tsx index 6031aa94..01854195 100644 --- a/src/__tests__/compiler/compiler.test.tsx +++ b/src/__tests__/compiler/compiler.test.tsx @@ -222,24 +222,6 @@ test.each([ ]); }); -// A transform is a space-separated list, so it holds each function as a token of its own -test.each([ - [ - "translate(10px, 20px)", - [ - [{}, "translateX", 10], - [{}, "translateY", 20], - ], - ], - ["translateZ(10px) rotate(45deg)", [[{}, "rotate", "45deg"]]], -])("transform: %s compiles to a flat list of functions", (value, expected) => { - const compiled = compile(`.my-class { transform: ${value}; }`); - - expect(compiled.stylesheet().s?.[0]?.[1][0]?.d).toStrictEqual([ - [[{}, "transform", expected], "transform"], - ]); -}); - test("light-dark()", () => { const compiled = compile(` .my-class { diff --git a/src/compiler/declarations.ts b/src/compiler/declarations.ts index aa1903b1..52be116b 100644 --- a/src/compiler/declarations.ts +++ b/src/compiler/declarations.ts @@ -755,9 +755,11 @@ function parseTransform( parseLengthOrCoercePercentageToRuntime(t.value[0], builder), ], [ - {}, - "translateY", - parseLengthOrCoercePercentageToRuntime(t.value[1], builder), + [ + {}, + "translateY", + parseLengthOrCoercePercentageToRuntime(t.value[1], builder), + ], ], ]; case "translateX": @@ -809,7 +811,7 @@ function parseTransform( case "rotate3d": case "matrix": case "matrix3d": - return []; + return [[]]; } }), ]); From 4ab1ad160363729a2c9cf9ed1a1b51a7c6a5b46d Mon Sep 17 00:00:00 2001 From: Sam Moore Date: Tue, 17 Nov 2026 16:37:38 -0500 Subject: [PATCH 08/10] perf(native): cut the Hermes cost of resolving and applying lists Keeping a list's separator made a comma-separated list read through a var() (transition-property, font-family) and a list led by a var() cost more on Hermes than main's map + filter. This trims that work without changing any output: - resolveList classifies its first entry once, and tells resolveValue when it is already known to be a style function, so isStyleFunction's Object.keys (the cheapest empty-object test on Hermes) runs once for that token instead of twice. - A one-token item of a comma-separated list is pushed as [token] without building and resetting an item, and the list of items is allocated only once a comma appears. - applyValue tests for a comma-separated list only inside its existing object branch, so a string or number declaration pays nothing for it; the guard had cost 1-2.5% on single-token declarations. - Indexed loops, and push instead of map() or new Array(n), which Hermes runs slower. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01D4jU1dW7KKQntNrr9i2xGi --- src/native/objects.ts | 24 +++--- src/native/styles/resolve.ts | 132 ++++++++++++++++++++++++------ src/utilities/style-descriptor.ts | 12 ++- 3 files changed, 128 insertions(+), 40 deletions(-) diff --git a/src/native/objects.ts b/src/native/objects.ts index ea6df36f..433bf774 100644 --- a/src/native/objects.ts +++ b/src/native/objects.ts @@ -80,18 +80,22 @@ export function applyValue( target.transform.push(value); } return; - } else if (typeof value === "object" && value && ShortHandSymbol in value) { - delete value[ShortHandSymbol]; - Object.assign(target, value); - return; + } else if (typeof value === "object" && value) { + if (ShortHandSymbol in value) { + delete value[ShortHandSymbol]; + Object.assign(target, value); + return; + } + + // React Native's list props take a comma-separated list with one-token + // items bare. Tested only here, for objects, so a string or number pays + // nothing for it: applyValue runs for every dynamic declaration. + if (Array.isArray(value[0])) { + value = commaListItems(value); + } } - // React Native's list props take a comma-separated list with one-token items bare. - // The guard is commaListItems' own test, inlined to skip a call on every declaration. - target[prop] = - Array.isArray(value) && Array.isArray(value[0]) - ? commaListItems(value) - : value; + target[prop] = value; } export function setDeepPath( diff --git a/src/native/styles/resolve.ts b/src/native/styles/resolve.ts index fd97bac7..e68f779a 100644 --- a/src/native/styles/resolve.ts +++ b/src/native/styles/resolve.ts @@ -8,6 +8,7 @@ import type { import { isCommaList, isStyleDescriptorArray, + isStyleFunction, } from "react-native-css/utilities"; import type { RenderGuard } from "../conditions/guards"; @@ -65,6 +66,8 @@ export function resolveValue( value: StyleDescriptor, get: Getter, options: ResolveValueOptions, + /** The caller has already found value to be a style function */ + isFunction?: boolean, ): any { const { castToArray } = options; @@ -93,18 +96,18 @@ export function resolveValue( return value; } - if (isStyleDescriptorArray(value)) { + if (!isFunction && isStyleDescriptorArray(value)) { return resolveList(value, get, options); } - const name = value[1]; + const name = (value as StyleFunction)[1]; const simpleResolve: SimpleResolveValue = (value) => { return resolveValue(value, get, options); }; if (name === "var") { - return varResolver(simpleResolve, value, get, options); + return varResolver(simpleResolve, value as StyleFunction, get, options); } else if (name in functionResolvers) { const fn = functionResolvers[name as keyof typeof functionResolvers]; @@ -173,11 +176,31 @@ function resolveList( get: Getter, options: ResolveValueOptions, ): unknown[] { - const isCommaSeparated = isStyleDescriptorArray(list[0]); + // Classify the first entry once: a style function is a token, any other + // array is an item of a comma-separated list. Only an array led by a plain + // object needs isStyleFunction's Object.keys, and resolveValue is told the + // answer so it does not pay for it twice. + const first = list[0]; + let isCommaSeparated = false; + let firstIsFunction = false; + + if (Array.isArray(first)) { + const lead: unknown = first[0]; + if ( + typeof lead === "object" && + lead !== null && + !Array.isArray(lead) && + isStyleFunction(first) + ) { + firstIsFunction = true; + } else { + isCommaSeparated = true; + } + } if (!isCommaSeparated && list.length === 1) { // Fast path for a lone token: its list, if it resolves to one, is the value - const resolved: unknown = resolveValue(list[0], get, options); + const resolved: unknown = resolveValue(first, get, options, firstIsFunction); return Array.isArray(resolved) ? resolved : resolved === undefined @@ -185,26 +208,63 @@ function resolveList( : [resolved]; } - const items: unknown[][] = []; + // Allocated only once the list turns out to be comma-separated + let items: unknown[][] | undefined = isCommaSeparated ? [] : undefined; let item: unknown[] = []; - for (const entry of list) { - if (isCommaSeparated && isStyleDescriptorArray(entry)) { - for (const token of entry) { - item = appendToken(resolveValue(token, get, options), item, items); + // Indexed loops and the inlined common case of appendList: this runs for + // every list on every render, and Hermes pays for iterators and calls + for (let index = 0; index < list.length; index++) { + const entry = list[index]; + + if (isCommaSeparated && (index === 0 || isItem(entry))) { + const tokens = entry as StyleDescriptor[]; + + // In a comma-separated list the item is empty here, as each entry closes it + if (tokens.length === 1) { + // Fast path for a one-token item, as most are: no item to build and reset + const resolved: unknown = resolveValue(tokens[0], get, options); + if (!Array.isArray(resolved)) { + if (resolved !== undefined) items!.push([resolved]); + continue; + } + item = appendList(resolved, item, (items ??= [])); + } else { + for (let tokenIndex = 0; tokenIndex < tokens.length; tokenIndex++) { + const resolved: unknown = resolveValue( + tokens[tokenIndex], + get, + options, + ); + if (!Array.isArray(resolved)) { + if (resolved !== undefined) item.push(resolved); + } else { + item = appendList(resolved, item, (items ??= [])); + } + } } } else { // A lone token in an item's place, or a space-separated list's token - item = appendToken(resolveValue(entry, get, options), item, items); + const resolved: unknown = resolveValue( + entry, + get, + options, + index === 0 && firstIsFunction, + ); + if (!Array.isArray(resolved)) { + if (resolved !== undefined) item.push(resolved); + } else { + item = appendList(resolved, item, (items ??= [])); + } } - if (isCommaSeparated) { - if (item.length > 0) items.push(item); + if (isCommaSeparated && item.length > 0) { + items!.push(item); item = []; } } - if (items.length === 0) { + if (items === undefined || items.length === 0) { return item; } @@ -213,23 +273,38 @@ function resolveList( } /** - * Appends a resolved token to the current item and returns the item later - * tokens join. Resolved values hold no style functions, so an array led by an - * array is a comma-separated list: its first item continues the current one - * and each later item starts its own. + * Whether an entry after the first of a comma-separated list is an item, an + * array of tokens, rather than a lone token in an item's place. Only a style + * function, `[{}, name]`, is an array led by a plain object, so the full + * check runs only for one of those or for a resolved item led by an object, + * such as `[{ rotate }, "x"]`. + */ +function isItem(entry: StyleDescriptor): entry is StyleDescriptor[] { + if (!Array.isArray(entry)) return false; + const lead: unknown = entry[0]; + return ( + typeof lead !== "object" || + lead === null || + Array.isArray(lead) || + !isStyleFunction(entry) + ); +} + +/** + * Substitutes a resolved list into the current item and returns the item + * later tokens join. A resolved value holds no style functions, so a list led + * by an array is comma-separated: its first item continues the current one + * and each later item starts its own. Any other list joins the current item. */ -function appendToken( - resolved: unknown, +function appendList( + resolved: unknown[], item: unknown[], items: unknown[][], ): unknown[] { - if (!Array.isArray(resolved)) { - if (resolved !== undefined) item.push(resolved); - return item; - } - if (!Array.isArray(resolved[0])) { - for (const value of resolved) item.push(value); + for (let index = 0; index < resolved.length; index++) { + item.push(resolved[index]); + } return item; } @@ -238,7 +313,10 @@ function appendToken( items.push(item); item = []; } - for (const value of resolved[index] as unknown[]) item.push(value); + const group = resolved[index] as unknown[]; + for (let valueIndex = 0; valueIndex < group.length; valueIndex++) { + item.push(group[valueIndex]); + } } return item; diff --git a/src/utilities/style-descriptor.ts b/src/utilities/style-descriptor.ts index 59850f23..fc52ee59 100644 --- a/src/utilities/style-descriptor.ts +++ b/src/utilities/style-descriptor.ts @@ -34,7 +34,13 @@ export function isCommaList(value: unknown): value is unknown[][] { * is returned as it is. */ export function commaListItems(value: unknown): unknown { - return isCommaList(value) - ? value.map((item) => (item.length === 1 ? item[0] : item)) - : value; + if (!isCommaList(value)) return value; + + // push, not map() or new Array(n), which Hermes runs slower: React + // Native's list props pass through here on every render + const items: unknown[] = []; + for (const item of value) { + items.push(item.length === 1 ? item[0] : item); + } + return items; } From e36030c433eeb420384296eee5d1535aa07d1543 Mon Sep 17 00:00:00 2001 From: Sam Moore Date: Tue, 17 Nov 2026 16:38:31 -0500 Subject: [PATCH 09/10] test: read a @property's comma-separated initial value from vp Since #567 a registered custom property's initial value is compiled into vp (name, inherits, initial value) instead of vr, so this test, written against the old shape, found nothing and failed once the branch was tested against main. The compiler keeps the separator either way: the value is [[10], [20]], one item per comma. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01D4jU1dW7KKQntNrr9i2xGi --- src/__tests__/compiler/property.test.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/__tests__/compiler/property.test.ts b/src/__tests__/compiler/property.test.ts index d2a277f6..20ae04db 100644 --- a/src/__tests__/compiler/property.test.ts +++ b/src/__tests__/compiler/property.test.ts @@ -601,6 +601,7 @@ test("@property with a comma-separated repeated value keeps each item apart", () } `); - const vrMap = new Map(compiled.stylesheet().vr); - expect(vrMap.get("my-offsets")).toStrictEqual([[[[10], [20]]]]); + expect(compiled.stylesheet().vp).toStrictEqual([ + ["my-offsets", false, [[10], [20]]], + ]); }); From 64003a8d98e94e6aed344a84dfd58e8fbc0f7f13 Mon Sep 17 00:00:00 2001 From: Sam Moore Date: Tue, 17 Nov 2026 17:02:54 -0500 Subject: [PATCH 10/10] perf(native): unwrap a transform argument without a second style-function test argument() unwraps each one-token argument of translate(), scale() and rotate() with isStyleDescriptorArray, whose isStyleFunction runs Object.keys, and resolveValue then runs the same test on the token. An argument is always a compiled descriptor, where only a style function starts with its {} marker, so an array of one entry that isn't a plain object is an item, and typeof and Array.isArray tell them apart. On Hermes (list-bench, against bench), Tailwind's translate-x-4 rotate-45 scale-110 with its variables resolved at runtime goes from +6.1% to +1.6%. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01D4jU1dW7KKQntNrr9i2xGi --- src/native/styles/functions/transform-functions.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/native/styles/functions/transform-functions.ts b/src/native/styles/functions/transform-functions.ts index e6fcacaf..851f469f 100644 --- a/src/native/styles/functions/transform-functions.ts +++ b/src/native/styles/functions/transform-functions.ts @@ -88,7 +88,13 @@ export const translate: StyleFunctionResolver = (resolveValue, descriptor) => { /** A comma-separated argument list holds each argument as the array of its tokens */ function argument(resolveValue: SimpleResolveValue, arg: StyleDescriptor) { + // A descriptor, never a resolved value: a style function starts with its {} + // marker, so an array of one entry that isn't a plain object is an item. + // This skips isStyleFunction's Object.keys, which resolveValue runs anyway + const token = Array.isArray(arg) && arg.length === 1 ? arg[0] : undefined; return resolveValue( - isStyleDescriptorArray(arg) && arg.length === 1 ? arg[0] : arg, + token !== undefined && (typeof token !== "object" || Array.isArray(token)) + ? token + : arg, ); }