Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/dom/src/Render/DOMRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ export const DOMRenderer: Renderer<Node> = {
Effect.sync(() => document.createTextNode(text)),

appendChild: (parent: Node, child: Node) =>
Effect.sync(() => {
warnIfInvalidNesting(tagNameOf(parent), tagNameOf(child));
Effect.gen(function* () {
yield* warnIfInvalidNesting(tagNameOf(parent), tagNameOf(child));
parent.appendChild(child);
}),

Expand Down
4 changes: 2 additions & 2 deletions packages/dom/src/Render/server/StringRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ export const StringRenderer: Renderer<VNode> = {
createTextNode: (text: string) => Effect.sync(() => vText(text)),

appendChild: (parent: VNode, child: VNode) =>
Effect.sync(() => {
Effect.gen(function* () {
if (parent._tag === "VElement") {
if (child._tag === "VElement") {
warnIfInvalidNesting(parent.type, child.type);
yield* warnIfInvalidNesting(parent.type, child.type);
}
parent.children.push(child);
// Track parent reference for slot markers
Expand Down
26 changes: 16 additions & 10 deletions packages/dom/src/Render/validateNesting.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { Effect } from "effect";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";

import { checkNesting, warnIfInvalidNesting } from "./validateNesting.js";

/** Helper: warnIfInvalidNesting returns Effect<void>; run it synchronously. */
const warn = (parent: string | undefined, child: string | undefined): void => {
Effect.runSync(warnIfInvalidNesting(parent, child));
};

describe("checkNesting", () => {
it("flags a paragraph nested inside a paragraph", () => {
expect(checkNesting("p", "p")).toMatch(/<p> inside <p>/);
Expand Down Expand Up @@ -56,36 +62,36 @@ describe("warnIfInvalidNesting", () => {
});

it("emits a console.warn on invalid nesting", () => {
warnIfInvalidNesting("p", "div");
warn("p", "div");
expect(warnSpy).toHaveBeenCalledTimes(1);
expect(warnSpy.mock.calls[0][0]).toMatch(/\[@effex\/dom\]/);
expect(warnSpy.mock.calls[0][0]).toMatch(/<div> inside <p>/);
});

it("is silent on valid nesting", () => {
warnIfInvalidNesting("div", "p");
warnIfInvalidNesting("ul", "li");
warn("div", "p");
warn("ul", "li");
expect(warnSpy).not.toHaveBeenCalled();
});

it("is silent when either arg is missing (text nodes, comments, etc.)", () => {
warnIfInvalidNesting(undefined, "div");
warnIfInvalidNesting("p", undefined);
warnIfInvalidNesting(undefined, undefined);
warn(undefined, "div");
warn("p", undefined);
warn(undefined, undefined);
expect(warnSpy).not.toHaveBeenCalled();
});

it("only warns once per parent-child pair (across calls)", () => {
// The module-level Set caches across tests, so use a distinct pair that
// no other test in this file exercises for warning-count assertions.
warnIfInvalidNesting("form", "form");
warnIfInvalidNesting("form", "form");
warnIfInvalidNesting("form", "form");
warn("form", "form");
warn("form", "form");
warn("form", "form");
expect(warnSpy).toHaveBeenCalledTimes(1);
});

it("normalizes tag case", () => {
warnIfInvalidNesting("BUTTON", "INPUT");
warn("BUTTON", "INPUT");
expect(warnSpy).toHaveBeenCalledTimes(1);
expect(warnSpy.mock.calls[0][0]).toMatch(/<input> inside <button>/);
});
Expand Down
25 changes: 14 additions & 11 deletions packages/dom/src/Render/validateNesting.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Console, Effect } from "effect";

/**
* Compile-time HTML nesting validator.
*
Expand Down Expand Up @@ -102,24 +104,25 @@ export const checkNesting = (parent: string, child: string): string | null => {
const warned = new Set<string>();

/**
* Report an invalid nesting via `console.warn`, once per parent-child pair
* per process. Safe to call unconditionally — the check is cheap and the
* warning only fires on real bugs. No-ops in environments without `console`.
* Report an invalid nesting via Effect's {@link Console.warn}, once per
* parent-child pair per process. Returns `Effect.void` when the pair is
* valid or has already been warned about, so callers can `yield*` it
* unconditionally without branching.
*
* Uses Effect's Console service (not raw `console.warn`) to stay idiomatic
* with the rest of the codebase and to let tests swap the sink via Layer.
*/
export const warnIfInvalidNesting = (
parent: string | undefined,
child: string | undefined,
): void => {
if (!parent || !child) return;
): Effect.Effect<void> => {
if (!parent || !child) return Effect.void;
const p = parent.toLowerCase();
const c = child.toLowerCase();
const key = `${p}>${c}`;
if (warned.has(key)) return;
if (warned.has(key)) return Effect.void;
const message = checkNesting(p, c);
if (!message) return;
if (!message) return Effect.void;
warned.add(key);
if (typeof console !== "undefined") {
// eslint-disable-next-line no-console
console.warn(`[@effex/dom] ${message}`);
}
return Console.warn(`[@effex/dom] ${message}`);
};
Loading