|
1 |
| -/** |
2 |
| - * @typedef {import('unist').Node} UnistNode |
3 |
| - * @typedef {import('unist').Parent} UnistParent |
4 |
| - * @typedef {import('unist').Literal} UnistLiteral |
5 |
| - * @typedef {import('hast').Root} Root |
6 |
| - * @typedef {import('hast').Content} Content |
7 |
| - * @typedef {import('hast').Element} Element |
8 |
| - */ |
9 |
| - |
10 |
| -/** |
11 |
| - * @typedef {Root | Content} Node |
12 |
| - * @typedef {Extract<Node, UnistParent>} Parent |
13 |
| - * @typedef {Extract<Node, UnistLiteral>} Literal |
14 |
| - */ |
15 |
| - |
16 |
| -import nodeAssert from 'node:assert' |
17 |
| -import {zwitch} from 'zwitch' |
18 |
| -import {mapz} from 'mapz' |
19 |
| -import { |
20 |
| - assert as unistAssert, |
21 |
| - parent as unistParent, |
22 |
| - literal as unistLiteral, |
23 |
| - wrap, |
24 |
| - _void |
25 |
| -} from 'unist-util-assert' |
26 |
| - |
27 |
| -/** |
28 |
| - * Assert that `tree` is a valid hast node. |
29 |
| - * |
30 |
| - * If `tree` is a parent, all children will be asserted too. |
31 |
| - * |
32 |
| - * Supports unknown hast nodes. |
33 |
| - * |
34 |
| - * @param {unknown} [tree] |
35 |
| - * Thing to assert. |
36 |
| - * @param {UnistParent | null | undefined} [parent] |
37 |
| - * Optional, valid parent. |
38 |
| - * @returns {asserts tree is Node} |
39 |
| - * Nothing. |
40 |
| - * @throws {AssertionError} |
41 |
| - * When `tree` (or its descendants) is not a hast node. |
42 |
| - */ |
43 |
| -export function assert(tree, parent) { |
44 |
| - return wrap(hast)(tree, parent) |
45 |
| -} |
46 |
| - |
47 |
| -/** |
48 |
| - * Assert that `tree` is a valid hast parent. |
49 |
| - * |
50 |
| - * All children will be asserted too. |
51 |
| - * |
52 |
| - * Supports unknown hast nodes. |
53 |
| - * |
54 |
| - * @param {unknown} [tree] |
55 |
| - * Thing to assert. |
56 |
| - * @param {UnistParent | null | undefined} [parent] |
57 |
| - * Optional, valid parent. |
58 |
| - * @returns {asserts tree is Parent} |
59 |
| - * Nothing. |
60 |
| - * @throws {AssertionError} |
61 |
| - * When `tree` is not a parent or its descendants are not nodes. |
62 |
| - */ |
63 |
| -export function parent(tree, parent) { |
64 |
| - return wrap(assertParent)(tree, parent) |
65 |
| -} |
66 |
| - |
67 |
| -/** |
68 |
| - * Assert that `node` is a valid hast literal. |
69 |
| - * |
70 |
| - * Supports unknown hast nodes. |
71 |
| - * |
72 |
| - * @param {unknown} [node] |
73 |
| - * Thing to assert. |
74 |
| - * @param {UnistParent | null | undefined} [parent] |
75 |
| - * Optional, valid parent. |
76 |
| - * @returns {asserts node is Literal} |
77 |
| - * Nothing. |
78 |
| - * @throws {AssertionError} |
79 |
| - * When `node` is not a hast literal. |
80 |
| - */ |
81 |
| -export function literal(node, parent) { |
82 |
| - return wrap(assertLiteral)(node, parent) |
83 |
| -} |
84 |
| - |
85 |
| -const hast = zwitch('type', { |
86 |
| - // Core interface. |
87 |
| - unknown, |
88 |
| - invalid: unknown, |
89 |
| - // Per-type handling. |
90 |
| - handlers: { |
91 |
| - root: wrap(assertRoot), |
92 |
| - element: wrap(assertElement), |
93 |
| - doctype: _void, |
94 |
| - comment: literal, |
95 |
| - text: literal |
96 |
| - } |
97 |
| -}) |
98 |
| - |
99 |
| -const all = mapz(hast, {key: 'children'}) |
100 |
| - |
101 |
| -/** |
102 |
| - * Assert that `node` (which is not a known hast node) is a valid unist node. |
103 |
| - * |
104 |
| - * @param {unknown} [node] |
105 |
| - * Thing to assert. |
106 |
| - * @param {UnistParent | null | undefined} [parent] |
107 |
| - * Optional, valid parent. |
108 |
| - * @returns {asserts node is UnistNode} |
109 |
| - * Nothing. |
110 |
| - * @throws {AssertionError} |
111 |
| - * When `node` is not a unist node. |
112 |
| - */ |
113 |
| -function unknown(node, parent) { |
114 |
| - unistAssert(node, parent) |
115 |
| -} |
116 |
| - |
117 |
| -/** |
118 |
| - * Assert that `tree` is a valid hast parent, with valid children. |
119 |
| - * |
120 |
| - * All children will be asserted too. |
121 |
| - * |
122 |
| - * Supports unknown hast nodes. |
123 |
| - * |
124 |
| - * @param {unknown} [tree] |
125 |
| - * Thing to assert. |
126 |
| - * @returns {asserts tree is Parent} |
127 |
| - * Nothing. |
128 |
| - * @throws {AssertionError} |
129 |
| - * When `tree` is not a parent or its descendants are not nodes. |
130 |
| - */ |
131 |
| -function assertParent(tree) { |
132 |
| - unistParent(tree) |
133 |
| - all(tree) |
134 |
| -} |
135 |
| - |
136 |
| -/** |
137 |
| - * Assert that `node` is a valid hast literal. |
138 |
| - * |
139 |
| - * Supports unknown hast nodes. |
140 |
| - * |
141 |
| - * @param {unknown} [node] |
142 |
| - * Thing to assert. |
143 |
| - * @returns {asserts node is Literal} |
144 |
| - * Nothing. |
145 |
| - * @throws {AssertionError} |
146 |
| - * When `node` is not a hast literal. |
147 |
| - */ |
148 |
| -function assertLiteral(node) { |
149 |
| - unistLiteral(node) |
150 |
| - nodeAssert.strictEqual( |
151 |
| - // `value` in unist literals is `any`. |
152 |
| - // type-coverage:ignore-next-line |
153 |
| - typeof node.value, |
154 |
| - 'string', |
155 |
| - 'literal should have a string `value`' |
156 |
| - ) |
157 |
| -} |
158 |
| - |
159 |
| -/** |
160 |
| - * Assert that `tree` is a hast root with valid children. |
161 |
| - * |
162 |
| - * Supports unknown hast descendants. |
163 |
| - * |
164 |
| - * @param {unknown} [tree] |
165 |
| - * Thing to assert. |
166 |
| - * @param {UnistParent | null | undefined} [parent] |
167 |
| - * Optional, valid parent. |
168 |
| - * @returns {asserts tree is Root} |
169 |
| - * Nothing. |
170 |
| - * @throws {AssertionError} |
171 |
| - * When `tree` is not a root or its descendants are not valid. |
172 |
| - */ |
173 |
| -function assertRoot(tree, parent) { |
174 |
| - assertParent(tree) |
175 |
| - nodeAssert.strictEqual(parent, undefined, '`root` should not have a parent') |
176 |
| -} |
177 |
| - |
178 |
| -/** |
179 |
| - * Assert that `tree` is a hast element with valid children. |
180 |
| - * |
181 |
| - * Supports unknown hast descendants. |
182 |
| - * |
183 |
| - * @param {unknown} [tree] |
184 |
| - * Thing to assert. |
185 |
| - * @returns {asserts tree is Element} |
186 |
| - * Nothing. |
187 |
| - * @throws {AssertionError} |
188 |
| - * When `tree` is not an element or its descendants are not valid. |
189 |
| - */ |
190 |
| -function assertElement(tree) { |
191 |
| - assertParent(tree) |
192 |
| - |
193 |
| - nodeAssert.strictEqual( |
194 |
| - // @ts-expect-error: hush. |
195 |
| - typeof tree.tagName, |
196 |
| - 'string', |
197 |
| - '`element` should have a `tagName`' |
198 |
| - ) |
199 |
| - nodeAssert.notStrictEqual( |
200 |
| - // @ts-expect-error: hush. |
201 |
| - tree.tagName, |
202 |
| - '', |
203 |
| - '`element.tagName` should not be empty' |
204 |
| - ) |
205 |
| -} |
206 |
| - |
207 |
| -export {_void, wrap} from 'unist-util-assert' |
| 1 | +export {_void, assert, parent, literal, wrap} from './lib/index.js' |
0 commit comments