Rename package from jfx to jsonsx

Co-authored-by: Ona <no-reply@ona.com>
This commit is contained in:
2026-02-28 01:37:10 +00:00
parent c9d4ff28e3
commit 99fe5e1db9
21 changed files with 142 additions and 142 deletions

View File

@@ -1,5 +1,5 @@
{
"name": "jfx",
"name": "jsonsx",
"build": {
"context": ".",
"dockerfile": "Dockerfile"

View File

@@ -1,22 +1,22 @@
# jfx
# jsonsx
JSX factory for [json-render](https://github.com/vercel-labs/json-render). Write JSX, get Spec JSON.
## Install
```bash
bun add jfx @json-render/core
bun add jsonsx @json-render/core
```
## Setup
Configure your `tsconfig.json` to use jfx as the JSX source:
Configure your `tsconfig.json` to use jsonsx as the JSX source:
```json
{
"compilerOptions": {
"jsx": "react-jsx",
"jsxImportSource": "jfx"
"jsxImportSource": "jsonsx"
}
}
```
@@ -24,7 +24,7 @@ Configure your `tsconfig.json` to use jfx as the JSX source:
Or use a per-file pragma:
```tsx
/** @jsxImportSource jfx */
/** @jsxImportSource jsonsx */
```
## Usage
@@ -34,18 +34,18 @@ Or use a per-file pragma:
Create wrapper functions that map JSX tags to json-render component type names:
```ts
import { jsx } from "jfx/jsx-runtime";
import type { JfxNode } from "jfx";
import { jsx } from "jsonsx/jsx-runtime";
import type { JsonsxNode } from "jsonsx";
export function Stack(props: Record<string, unknown>): JfxNode {
export function Stack(props: Record<string, unknown>): JsonsxNode {
return jsx("Stack", props);
}
export function Text(props: Record<string, unknown>): JfxNode {
export function Text(props: Record<string, unknown>): JsonsxNode {
return jsx("Text", props);
}
export function Button(props: Record<string, unknown>): JfxNode {
export function Button(props: Record<string, unknown>): JsonsxNode {
return jsx("Button", props);
}
```
@@ -53,12 +53,12 @@ export function Button(props: Record<string, unknown>): JfxNode {
### Render JSX to Spec JSON
```tsx
import { render } from "jfx";
import { render } from "jsonsx";
import { Stack, Text, Button } from "./components";
const spec = render(
<Stack>
<Text content="Hello from jfx!" />
<Text content="Hello from jsonsx!" />
<Button label="Click me" />
</Stack>
);
@@ -72,7 +72,7 @@ This produces:
"elements": {
"text-1": {
"type": "Text",
"props": { "content": "Hello from jfx!" }
"props": { "content": "Hello from jsonsx!" }
},
"button-1": {
"type": "Button",
@@ -135,7 +135,7 @@ These props are extracted from JSX and mapped to Spec fields rather than passed
## Example
The `example/` directory contains a Bun HTTP server that demonstrates jfx in action. It shows JSX source, live rendered UI (via `@json-render/react`), and JSON output side by side.
The `example/` directory contains a Bun HTTP server that demonstrates jsonsx in action. It shows JSX source, live rendered UI (via `@json-render/react`), and JSON output side by side.
```bash
cd example

View File

@@ -3,11 +3,11 @@
"configVersion": 1,
"workspaces": {
"": {
"name": "jfx-example",
"name": "jsonsx-example",
"dependencies": {
"@json-render/core": "0.10.0",
"@json-render/react": "0.10.0",
"jfx": "file:..",
"jsonsx": "file:..",
"react": "19.2.4",
"react-dom": "19.2.4",
"shiki": "^4.0.0",
@@ -241,12 +241,12 @@
"html-void-elements": ["html-void-elements@3.0.0", "", {}, "sha512-bEqo66MRXsUGxWHV5IP0PUiAWwoEjba4VCzg0LjFJBpchPaTfyfCKTG6bc5F8ucKec3q5y6qOdGyYTSBEvhCrg=="],
"jfx": ["jfx@file:..", { "devDependencies": { "@json-render/core": "0.10.0", "@json-render/react": "0.10.0", "@testing-library/react": "16.3.2", "@types/bun": "^1.3.9", "@types/react": "19.2.3", "happy-dom": "18.0.1", "react": "19.2.4", "react-dom": "19.2.4", "tsup": "8.5.1", "typescript": "5.9.3" }, "peerDependencies": { "@json-render/core": ">=0.10.0" } }],
"joycon": ["joycon@3.1.1", "", {}, "sha512-34wB/Y7MW7bzjKRjUKTa46I2Z7eV62Rkhva+KkopW7Qvv/OSWBqvkSY7vusOPrNuZcUG3tApvdVgNB8POj3SPw=="],
"js-tokens": ["js-tokens@4.0.0", "", {}, "sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ=="],
"jsonsx": ["jsonsx@file:..", { "devDependencies": { "@json-render/core": "0.10.0", "@json-render/react": "0.10.0", "@testing-library/react": "16.3.2", "@types/bun": "^1.3.9", "@types/react": "19.2.3", "happy-dom": "18.0.1", "react": "19.2.4", "react-dom": "19.2.4", "tsup": "8.5.1", "typescript": "5.9.3" }, "peerDependencies": { "@json-render/core": ">=0.10.0" } }],
"lilconfig": ["lilconfig@3.1.3", "", {}, "sha512-/vlFKAoH5Cgt3Ie+JLhRbwOsCQePABiU3tJ1egGvyQ+33R/vcwM2Zl2QR/LzjsBeItPt3oSVXapn+m4nQDvpzw=="],
"lines-and-columns": ["lines-and-columns@1.2.4", "", {}, "sha512-7ylylesZQ/PV29jhEDl3Ufjo6ZX7gCqJr5F7PKrqc93v7fzSymt1BpwEU8nAUXs8qzzvqhbjhK5QZg6Mt/HkBg=="],
@@ -369,6 +369,6 @@
"zwitch": ["zwitch@2.0.4", "", {}, "sha512-bXE4cR/kVZhKZX/RjPEflHaKVhUVl85noU3v6b8apfQEc1x4A+zBxjZ4lN8LqGd6WZ3dl98pY4o717VFmoPp+A=="],
"jfx/@types/react": ["@types/react@19.2.3", "", { "dependencies": { "csstype": "^3.0.2" } }, "sha512-k5dJVszUiNr1DSe8Cs+knKR6IrqhqdhpUwzqhkS8ecQTSf3THNtbfIp/umqHMpX2bv+9dkx3fwDv/86LcSfvSg=="],
"jsonsx/@types/react": ["@types/react@19.2.3", "", { "dependencies": { "csstype": "^3.0.2" } }, "sha512-k5dJVszUiNr1DSe8Cs+knKR6IrqhqdhpUwzqhkS8ecQTSf3THNtbfIp/umqHMpX2bv+9dkx3fwDv/86LcSfvSg=="],
}
}

View File

@@ -1,23 +1,23 @@
import { jsx } from "jfx/jsx-runtime";
import type { JfxNode } from "jfx";
import { jsx } from "jsonsx/jsx-runtime";
import type { JsonsxNode } from "jsonsx";
export function Stack(props: Record<string, unknown>): JfxNode {
export function Stack(props: Record<string, unknown>): JsonsxNode {
return jsx("Stack", props);
}
export function Card(props: Record<string, unknown>): JfxNode {
export function Card(props: Record<string, unknown>): JsonsxNode {
return jsx("Card", props);
}
export function Text(props: Record<string, unknown>): JfxNode {
export function Text(props: Record<string, unknown>): JsonsxNode {
return jsx("Text", props);
}
export function Button(props: Record<string, unknown>): JfxNode {
export function Button(props: Record<string, unknown>): JsonsxNode {
return jsx("Button", props);
}
export function Input(props: Record<string, unknown>): JfxNode {
export function Input(props: Record<string, unknown>): JsonsxNode {
return jsx("Input", props);
}

View File

@@ -1,5 +1,5 @@
{
"name": "jfx-example",
"name": "jsonsx-example",
"private": true,
"type": "module",
"scripts": {
@@ -10,7 +10,7 @@
"dependencies": {
"@json-render/core": "0.10.0",
"@json-render/react": "0.10.0",
"jfx": "file:..",
"jsonsx": "file:..",
"react": "19.2.4",
"react-dom": "19.2.4",
"shiki": "^4.0.0"

View File

@@ -1,5 +1,5 @@
/** @jsxImportSource jfx */
import { render } from "jfx";
/** @jsxImportSource jsonsx */
import { render } from "jsonsx";
import { Stack, Card, Text, Button, Input } from "../components";
export const fullSpec = render(

View File

@@ -1,10 +1,10 @@
/** @jsxImportSource jfx */
import { render } from "jfx";
/** @jsxImportSource jsonsx */
import { render } from "jsonsx";
import { Stack, Text, Button } from "../components";
export const simpleSpec = render(
<Stack>
<Text content="Hello from jfx!" />
<Text content="Hello from jsonsx!" />
<Button label="Click me" />
</Stack>
);

View File

@@ -75,7 +75,7 @@ export function App() {
if (!activeSpec) {
return (
<div style={{ maxWidth: 600, margin: "60px auto", padding: "0 24px" }}>
<h1 style={{ fontSize: "24px", marginBottom: "8px" }}>jfx examples</h1>
<h1 style={{ fontSize: "24px", marginBottom: "8px" }}>jsonsx examples</h1>
<p style={{ color: "var(--text-secondary)", fontSize: "14px", marginBottom: "24px" }}>
JSX &rarr; json-render Spec. Pick a spec to see the live UI and JSON
output.

View File

@@ -3,7 +3,7 @@
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>jfx example</title>
<title>jsonsx example</title>
<style>
:root {
--bg: #f5f5f5;

View File

@@ -46,4 +46,4 @@ const server = serve({
},
});
console.log(`jfx example server running at ${server.url}`);
console.log(`jsonsx example server running at ${server.url}`);

View File

@@ -1,5 +1,5 @@
{
"name": "jfx",
"name": "jsonsx",
"version": "0.1.0",
"license": "MIT",
"description": "JSX factory for json-render. Write JSX, get Spec JSON.",

View File

@@ -1,3 +1,3 @@
export { render } from "./render";
export { isJfxNode, JFX_NODE, FRAGMENT } from "./types";
export type { JfxNode, JfxComponent, RenderOptions } from "./types";
export { isJsonsxNode, JSONSX_NODE, FRAGMENT } from "./types";
export type { JsonsxNode, JsonsxComponent, RenderOptions } from "./types";

View File

@@ -1,11 +1,11 @@
/** @jsxImportSource react */
/**
* Integration tests: verify that Specs produced by jfx are consumable
* Integration tests: verify that Specs produced by jsonsx are consumable
* by @json-render/react's Renderer.
*
* This file uses React JSX (via the pragma above) for the React component
* tree, and jfx's jsx()/jsxs() via the component wrappers for building Specs.
* tree, and jsonsx's jsx()/jsxs() via the component wrappers for building Specs.
*/
import { describe, it, expect, mock } from "bun:test";
@@ -19,7 +19,7 @@ import {
} from "@json-render/react";
import { useStateStore } from "@json-render/react";
import { jsx, jsxs } from "./jsx-runtime";
import { render as jfxRender } from "./render";
import { render as jsonsxRender } from "./render";
import {
Stack as JStack,
Card as JCard,
@@ -64,7 +64,7 @@ function StateProbe() {
const registry = { Button, Text, Stack, Card };
// ---------------------------------------------------------------------------
// Helper: render a jfx spec with @json-render/react
// Helper: render a jsonsx spec with @json-render/react
// ---------------------------------------------------------------------------
function renderSpec(spec: Spec, handlers?: Record<string, (...args: unknown[]) => void>) {
@@ -80,15 +80,15 @@ function renderSpec(spec: Spec, handlers?: Record<string, (...args: unknown[]) =
// Basic rendering
// =============================================================================
describe("jfx → @json-render/react round-trip", () => {
describe("jsonsx → @json-render/react round-trip", () => {
it("renders a single element", () => {
const spec = jfxRender(jsx(JText, { content: "Hello from jfx" }));
const spec = jsonsxRender(jsx(JText, { content: "Hello from jsonsx" }));
renderSpec(spec);
expect(screen.getByTestId("text").textContent).toBe("Hello from jfx");
expect(screen.getByTestId("text").textContent).toBe("Hello from jsonsx");
});
it("renders nested elements with children", () => {
const spec = jfxRender(
const spec = jsonsxRender(
jsxs(JCard, {
title: "My Card",
children: [jsx(JText, { content: "Inside card" })],
@@ -101,7 +101,7 @@ describe("jfx → @json-render/react round-trip", () => {
});
it("renders a tree with multiple children", () => {
const spec = jfxRender(
const spec = jsonsxRender(
jsxs(JStack, {
children: [
jsx(JText, { content: "First" }),
@@ -116,7 +116,7 @@ describe("jfx → @json-render/react round-trip", () => {
});
it("renders a deep tree", () => {
const spec = jfxRender(
const spec = jsonsxRender(
jsxs(JStack, {
children: [
jsxs(JCard, {
@@ -136,9 +136,9 @@ describe("jfx → @json-render/react round-trip", () => {
// State + actions (adapted from chained-actions.test.tsx)
// =============================================================================
describe("jfx specs with state and actions", () => {
describe("jsonsx specs with state and actions", () => {
it("renders with initial state", () => {
const spec = jfxRender(jsx(JText, { content: "Stateful" }), {
const spec = jsonsxRender(jsx(JText, { content: "Stateful" }), {
state: { count: 42 },
});
renderSpec(spec);
@@ -148,7 +148,7 @@ describe("jfx specs with state and actions", () => {
});
it("setState action updates state on button press", async () => {
const spec = jfxRender(
const spec = jsonsxRender(
jsx(JButton, {
label: "Set",
on: {
@@ -172,7 +172,7 @@ describe("jfx specs with state and actions", () => {
});
it("chained pushState + setState resolves correctly", async () => {
const spec = jfxRender(
const spec = jsonsxRender(
jsx(JButton, {
label: "Chain",
on: {
@@ -206,7 +206,7 @@ describe("jfx specs with state and actions", () => {
});
it("multiple pushState chain resolves correctly", async () => {
const spec = jfxRender(
const spec = jsonsxRender(
jsx(JButton, {
label: "Go",
on: {
@@ -242,9 +242,9 @@ describe("jfx specs with state and actions", () => {
// Spec structural validity
// =============================================================================
describe("jfx spec structural validity", () => {
describe("jsonsx spec structural validity", () => {
it("all child references resolve to existing elements", () => {
const spec = jfxRender(
const spec = jsonsxRender(
jsxs(JStack, {
children: [
jsxs(JCard, {
@@ -272,12 +272,12 @@ describe("jfx spec structural validity", () => {
});
it("root element exists in elements map", () => {
const spec = jfxRender(jsx(JCard, { title: "Root" }));
const spec = jsonsxRender(jsx(JCard, { title: "Root" }));
expect(spec.elements[spec.root]).toBeDefined();
});
it("element count matches node count", () => {
const spec = jfxRender(
const spec = jsonsxRender(
jsxs(JStack, {
children: [
jsx(JCard, { title: "A" }),
@@ -294,9 +294,9 @@ describe("jfx spec structural validity", () => {
// Dynamic features (ported from json-render's dynamic-forms.test.tsx)
// =============================================================================
describe("jfx specs with dynamic features", () => {
describe("jsonsx specs with dynamic features", () => {
it("$state prop expressions resolve at render time", () => {
const spec = jfxRender(
const spec = jsonsxRender(
jsx(JText, { content: { $state: "/message" } }),
{ state: { message: "Dynamic hello" } },
);
@@ -306,7 +306,7 @@ describe("jfx specs with dynamic features", () => {
});
it("visibility condition hides element when false", () => {
const spec = jfxRender(
const spec = jsonsxRender(
jsxs(JStack, {
children: [
jsx(JText, {
@@ -324,7 +324,7 @@ describe("jfx specs with dynamic features", () => {
it("visibility condition shows element when true", () => {
cleanup();
const spec = jfxRender(
const spec = jsonsxRender(
jsxs(JStack, {
children: [
jsx(JText, {
@@ -343,7 +343,7 @@ describe("jfx specs with dynamic features", () => {
it("watchers fire when watched state changes", async () => {
const loadCities = mock();
const spec = jfxRender(
const spec = jsonsxRender(
jsxs(JStack, {
children: [
jsx(JButton, {

View File

@@ -2,12 +2,12 @@ import type {
ActionBinding,
VisibilityCondition,
} from "@json-render/core";
import { JFX_NODE, FRAGMENT, type JfxNode } from "./types";
import type { JfxComponent } from "./types";
import { JSONSX_NODE, FRAGMENT, type JsonsxNode } from "./types";
import type { JsonsxComponent } from "./types";
export { FRAGMENT as Fragment };
/** Props reserved by jfx — extracted from JSX props and placed on the UIElement level. */
/** Props reserved by jsonsx — extracted from JSX props and placed on the UIElement level. */
const RESERVED_PROPS = new Set([
"key",
"children",
@@ -18,26 +18,26 @@ const RESERVED_PROPS = new Set([
]);
/**
* Normalize a raw `children` value from JSX props into a flat array of JfxNodes.
* Normalize a raw `children` value from JSX props into a flat array of JsonsxNodes.
* Handles: undefined, single node, nested arrays, and filters out nulls/booleans.
*/
function normalizeChildren(raw: unknown): JfxNode[] {
function normalizeChildren(raw: unknown): JsonsxNode[] {
if (raw == null || typeof raw === "boolean") return [];
if (Array.isArray(raw)) {
const result: JfxNode[] = [];
const result: JsonsxNode[] = [];
for (const child of raw) {
if (child == null || typeof child === "boolean") continue;
if (Array.isArray(child)) {
result.push(...normalizeChildren(child));
} else {
result.push(child as JfxNode);
result.push(child as JsonsxNode);
}
}
return result;
}
return [raw as JfxNode];
return [raw as JsonsxNode];
}
/**
@@ -56,20 +56,20 @@ function extractProps(
}
/** Accepted tag types: string literal, Fragment symbol, or a function component. */
type JsxType = string | typeof FRAGMENT | JfxComponent;
type JsxType = string | typeof FRAGMENT | JsonsxComponent;
/**
* Core factory — shared by `jsx` and `jsxs`.
*
* If `type` is a function, it is called with props (like React calls
* function components). The function returns a JfxNode directly.
* function components). The function returns a JsonsxNode directly.
*
* If `type` is a string or Fragment, a JfxNode is constructed inline.
* If `type` is a string or Fragment, a JsonsxNode is constructed inline.
*/
function createNode(
type: JsxType,
rawProps: Record<string, unknown> | null,
): JfxNode {
): JsonsxNode {
const p = rawProps ?? {};
// Function component — call it, just like React does.
@@ -78,7 +78,7 @@ function createNode(
}
return {
$$typeof: JFX_NODE,
$$typeof: JSONSX_NODE,
type,
props: extractProps(p),
children: normalizeChildren(p.children),
@@ -102,7 +102,7 @@ export function jsx(
type: JsxType,
props: Record<string, unknown> | null,
key?: string,
): JfxNode {
): JsonsxNode {
const node = createNode(type, props);
if (key != null) node.key = String(key);
return node;
@@ -116,7 +116,7 @@ export function jsxs(
type: JsxType,
props: Record<string, unknown> | null,
key?: string,
): JfxNode {
): JsonsxNode {
const node = createNode(type, props);
if (key != null) node.key = String(key);
return node;
@@ -133,7 +133,7 @@ export namespace JSX {
}
/** The type returned by JSX expressions. */
export type Element = JfxNode;
export type Element = JsonsxNode;
export interface ElementChildrenAttribute {
children: {};

View File

@@ -1,6 +1,6 @@
import { describe, it, expect } from "bun:test";
import { render } from "./render";
import { isJfxNode, FRAGMENT } from "./types";
import { isJsonsxNode, FRAGMENT } from "./types";
import { jsx, jsxs, Fragment } from "./jsx-runtime";
import {
Stack,
@@ -18,25 +18,25 @@ import {
// =============================================================================
describe("jsx factory", () => {
it("jsx() with string type returns a JfxNode", () => {
it("jsx() with string type returns a JsonsxNode", () => {
const node = jsx("Card", { title: "Hello" });
expect(isJfxNode(node)).toBe(true);
expect(isJsonsxNode(node)).toBe(true);
expect(node.type).toBe("Card");
expect(node.props).toEqual({ title: "Hello" });
});
it("jsx() with component function resolves typeName", () => {
const node = jsx(Card, { title: "Hello" });
expect(isJfxNode(node)).toBe(true);
expect(isJsonsxNode(node)).toBe(true);
expect(node.type).toBe("Card");
expect(node.props).toEqual({ title: "Hello" });
});
it("jsxs() returns a JfxNode with children", () => {
it("jsxs() returns a JsonsxNode with children", () => {
const node = jsxs(Stack, {
children: [jsx(Text, { content: "A" }), jsx(Text, { content: "B" })],
});
expect(isJfxNode(node)).toBe(true);
expect(isJsonsxNode(node)).toBe(true);
expect(node.children).toHaveLength(2);
});
@@ -73,7 +73,7 @@ describe("jsx factory", () => {
it("jsx() handles null props", () => {
const node = jsx("Divider", null);
expect(isJfxNode(node)).toBe(true);
expect(isJsonsxNode(node)).toBe(true);
expect(node.props).toEqual({});
expect(node.children).toEqual([]);
});

View File

@@ -1,6 +1,6 @@
import { describe, it, expect } from "bun:test";
import { render } from "./render";
import { FRAGMENT, type JfxNode } from "./types";
import { FRAGMENT, type JsonsxNode } from "./types";
import { jsx } from "./jsx-runtime";
import {
Stack,
@@ -300,7 +300,7 @@ describe("state passthrough", () => {
// =============================================================================
describe("error handling", () => {
it("throws for non-JfxNode input", () => {
expect(() => render({} as JfxNode)).toThrow(/expects a JfxNode/);
it("throws for non-JsonsxNode input", () => {
expect(() => render({} as JsonsxNode)).toThrow(/expects a JsonsxNode/);
});
});

View File

@@ -1,12 +1,12 @@
import type { Spec, UIElement } from "@json-render/core";
import { FRAGMENT, type JfxNode, type RenderOptions, isJfxNode } from "./types";
import { FRAGMENT, type JsonsxNode, type RenderOptions, isJsonsxNode } from "./types";
/**
* Flatten a JfxNode tree into a json-render `Spec`.
* Flatten a JsonsxNode tree into a json-render `Spec`.
*
* Analogous to `ReactDOM.render` but produces JSON instead of DOM mutations.
*
* @param node - Root JfxNode (produced by JSX)
* @param node - Root JsonsxNode (produced by JSX)
* @param options - Optional render configuration (e.g. initial state)
* @returns A json-render `Spec` ready for any renderer
*
@@ -20,9 +20,9 @@ import { FRAGMENT, type JfxNode, type RenderOptions, isJfxNode } from "./types";
* );
* ```
*/
export function render(node: JfxNode, options?: RenderOptions): Spec {
if (!isJfxNode(node)) {
throw new Error("render() expects a JfxNode produced by JSX.");
export function render(node: JsonsxNode, options?: RenderOptions): Spec {
if (!isJsonsxNode(node)) {
throw new Error("render() expects a JsonsxNode produced by JSX.");
}
if (node.type === FRAGMENT) {
@@ -66,12 +66,12 @@ function generateKey(
/**
* Resolve the children of a node, expanding fragments inline.
* Returns an array of concrete (non-fragment) JfxNodes.
* Returns an array of concrete (non-fragment) JsonsxNodes.
*/
function expandChildren(children: JfxNode[]): JfxNode[] {
const result: JfxNode[] = [];
function expandChildren(children: JsonsxNode[]): JsonsxNode[] {
const result: JsonsxNode[] = [];
for (const child of children) {
if (!isJfxNode(child)) continue;
if (!isJsonsxNode(child)) continue;
if (child.type === FRAGMENT) {
// Recursively expand nested fragments
result.push(...expandChildren(child.children));
@@ -83,11 +83,11 @@ function expandChildren(children: JfxNode[]): JfxNode[] {
}
/**
* Recursively flatten a JfxNode into the elements map.
* Recursively flatten a JsonsxNode into the elements map.
* Returns the key assigned to this node.
*/
function flattenNode(
node: JfxNode,
node: JsonsxNode,
elements: Record<string, UIElement>,
counters: Map<string, number>,
usedKeys: Set<string>,

View File

@@ -1,7 +1,7 @@
/**
* Ported from json-render's core/src/spec-validator.test.ts.
*
* Runs @json-render/core's validateSpec against Specs produced by jfx
* Runs @json-render/core's validateSpec against Specs produced by jsonsx
* to prove structural correctness.
*/
@@ -19,7 +19,7 @@ import {
Select,
} from "./test-components";
describe("validateSpec on jfx-produced specs", () => {
describe("validateSpec on jsonsx-produced specs", () => {
it("validates a simple single-element spec", () => {
const spec = render(<Text text="hello" />);
const result = validateSpec(spec);
@@ -90,7 +90,7 @@ describe("validateSpec on jfx-produced specs", () => {
expect(result.issues.some((i) => i.code === "watch_in_props")).toBe(false);
});
it("no orphaned elements in jfx output", () => {
it("no orphaned elements in jsonsx output", () => {
const spec = render(
<Stack>
<Text text="A" />
@@ -101,7 +101,7 @@ describe("validateSpec on jfx-produced specs", () => {
expect(result.issues.some((i) => i.code === "orphaned_element")).toBe(false);
});
it("no missing children in jfx output", () => {
it("no missing children in jsonsx output", () => {
const spec = render(
<Stack>
<Card title="X">

View File

@@ -1,42 +1,42 @@
import { jsx } from "./jsx-runtime";
import type { JfxNode } from "./types";
import type { JsonsxNode } from "./types";
export function Stack(props: Record<string, unknown>): JfxNode {
export function Stack(props: Record<string, unknown>): JsonsxNode {
return jsx("Stack", props);
}
export function Card(props: Record<string, unknown>): JfxNode {
export function Card(props: Record<string, unknown>): JsonsxNode {
return jsx("Card", props);
}
export function Text(props: Record<string, unknown>): JfxNode {
export function Text(props: Record<string, unknown>): JsonsxNode {
return jsx("Text", props);
}
export function Button(props: Record<string, unknown>): JfxNode {
export function Button(props: Record<string, unknown>): JsonsxNode {
return jsx("Button", props);
}
export function Badge(props: Record<string, unknown>): JfxNode {
export function Badge(props: Record<string, unknown>): JsonsxNode {
return jsx("Badge", props);
}
export function List(props: Record<string, unknown>): JfxNode {
export function List(props: Record<string, unknown>): JsonsxNode {
return jsx("List", props);
}
export function ListItem(props: Record<string, unknown>): JfxNode {
export function ListItem(props: Record<string, unknown>): JsonsxNode {
return jsx("ListItem", props);
}
export function Select(props: Record<string, unknown>): JfxNode {
export function Select(props: Record<string, unknown>): JsonsxNode {
return jsx("Select", props);
}
export function Input(props: Record<string, unknown>): JfxNode {
export function Input(props: Record<string, unknown>): JsonsxNode {
return jsx("Input", props);
}
export function Divider(props: Record<string, unknown>): JfxNode {
export function Divider(props: Record<string, unknown>): JsonsxNode {
return jsx("Divider", props);
}

View File

@@ -4,19 +4,19 @@ import type {
} from "@json-render/core";
// ---------------------------------------------------------------------------
// JfxNode — intermediate representation produced by the JSX factory
// JsonsxNode — intermediate representation produced by the JSX factory
// ---------------------------------------------------------------------------
/**
* Sentinel symbol identifying a JfxNode (prevents plain objects from
* Sentinel symbol identifying a JsonsxNode (prevents plain objects from
* being mistaken for nodes).
*/
export const JFX_NODE = Symbol.for("jfx.node");
export const JSONSX_NODE = Symbol.for("jsonsx.node");
/**
* Sentinel symbol for Fragment grouping.
*/
export const FRAGMENT = Symbol.for("jfx.fragment");
export const FRAGMENT = Symbol.for("jsonsx.fragment");
/**
* A node in the intermediate JSX tree.
@@ -24,9 +24,9 @@ export const FRAGMENT = Symbol.for("jfx.fragment");
* Created by the `jsx` / `jsxs` factory functions and consumed by `render()`
* which flattens the tree into a json-render `Spec`.
*/
export interface JfxNode {
/** Brand symbol — always `JFX_NODE` */
$$typeof: typeof JFX_NODE;
export interface JsonsxNode {
/** Brand symbol — always `JSONSX_NODE` */
$$typeof: typeof JSONSX_NODE;
/**
* Component type name (e.g. `"Card"`, `"Button"`).
@@ -38,7 +38,7 @@ export interface JfxNode {
props: Record<string, unknown>;
/** Child nodes */
children: JfxNode[];
children: JsonsxNode[];
// -- Reserved / meta fields (extracted from JSX props) --
@@ -59,20 +59,20 @@ export interface JfxNode {
}
// ---------------------------------------------------------------------------
// JfxComponent — a function usable as a JSX tag that maps to a type string
// JsonsxComponent — a function usable as a JSX tag that maps to a type string
// ---------------------------------------------------------------------------
/**
* A jfx component function. Works like a React function component:
* A jsonsx component function. Works like a React function component:
* when used as a JSX tag (`<Card />`), the factory calls the function
* with props and gets back a JfxNode.
* with props and gets back a JsonsxNode.
*/
export type JfxComponent = (props: Record<string, unknown>) => JfxNode;
export type JsonsxComponent = (props: Record<string, unknown>) => JsonsxNode;
/**
* Define a jfx component for use as a JSX tag.
* Define a jsonsx component for use as a JSX tag.
*
* Creates a function that, when called with props, produces a JfxNode
* Creates a function that, when called with props, produces a JsonsxNode
* with the given type name — just like a React component returns
* React elements.
*
@@ -82,12 +82,12 @@ export type JfxComponent = (props: Record<string, unknown>) => JfxNode;
* const spec = render(<Card title="Hello"><Text content="World" /></Card>);
* ```
*/
export function component(typeName: string): JfxComponent {
export function component(typeName: string): JsonsxComponent {
// Import createNodeFromString lazily to avoid circular dep
// (jsx-runtime imports types). Instead, we build the node inline.
return (props: Record<string, unknown>) => {
return {
$$typeof: JFX_NODE,
$$typeof: JSONSX_NODE,
type: typeName,
props: filterReserved(props),
children: normalizeChildrenRaw(props.children),
@@ -110,21 +110,21 @@ function filterReserved(props: Record<string, unknown>): Record<string, unknown>
return out;
}
function normalizeChildrenRaw(raw: unknown): JfxNode[] {
function normalizeChildrenRaw(raw: unknown): JsonsxNode[] {
if (raw == null || typeof raw === "boolean") return [];
if (Array.isArray(raw)) {
const result: JfxNode[] = [];
const result: JsonsxNode[] = [];
for (const child of raw) {
if (child == null || typeof child === "boolean") continue;
if (Array.isArray(child)) {
result.push(...normalizeChildrenRaw(child));
} else {
result.push(child as JfxNode);
result.push(child as JsonsxNode);
}
}
return result;
}
return [raw as JfxNode];
return [raw as JsonsxNode];
}
// ---------------------------------------------------------------------------
@@ -140,10 +140,10 @@ export interface RenderOptions {
// Type guard
// ---------------------------------------------------------------------------
export function isJfxNode(value: unknown): value is JfxNode {
export function isJsonsxNode(value: unknown): value is JsonsxNode {
return (
typeof value === "object" &&
value !== null &&
(value as JfxNode).$$typeof === JFX_NODE
(value as JsonsxNode).$$typeof === JSONSX_NODE
);
}

View File

@@ -14,11 +14,11 @@
"outDir": "dist",
"rootDir": "src",
"jsx": "react-jsx",
"jsxImportSource": "jfx",
"jsxImportSource": "jsonsx",
"baseUrl": ".",
"paths": {
"jfx/jsx-runtime": ["./src/jsx-runtime"],
"jfx/jsx-dev-runtime": ["./src/jsx-dev-runtime"]
"jsonsx/jsx-runtime": ["./src/jsx-runtime"],
"jsonsx/jsx-dev-runtime": ["./src/jsx-dev-runtime"]
}
},
"include": ["src"]