Skip to content

Commit 5fe3cd3

Browse files
committed
Update @types/hast, utilities
1 parent 66aa7c8 commit 5fe3cd3

File tree

3 files changed

+32
-22
lines changed

3 files changed

+32
-22
lines changed

lib/index.js

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
/**
2-
* @typedef {import('hast').Root} Root
3-
* @typedef {import('hast').Content} Content
2+
* @typedef {import('hast').Nodes} Nodes
43
* @typedef {import('hast').Element} Element
54
* @typedef {import('hast').Properties} Properties
6-
* @typedef {Content | Root} Node
75
*
86
* @typedef {Properties[string]} PropertyValue
97
* Possible property values.
@@ -218,7 +216,7 @@
218216
*
219217
* @typedef {(schema: Schema, value: any, node: any, stack: Array<string>) => unknown} Handler
220218
* @typedef {Record<string, Handler>} NodeDefinition
221-
* @typedef {((schema: Schema, node: Node) => NodeDefinition | undefined)} NodeDefinitionGetter
219+
* @typedef {((schema: Schema, node: Nodes) => NodeDefinition | undefined)} NodeDefinitionGetter
222220
* @typedef {Record<string, NodeDefinition | NodeDefinitionGetter>} NodeSchema
223221
*/
224222

@@ -243,15 +241,15 @@ const nodeSchema = {
243241
/**
244242
* Sanitize a tree.
245243
*
246-
* @param {Node} node
244+
* @param {Nodes} node
247245
* Tree to clean.
248246
* @param {Schema | null | undefined} [schema]
249247
* Schema defining how to sanitize.
250-
* @returns {Node}
248+
* @returns {Nodes}
251249
* New, sanitized, tree.
252250
*/
253251
export function sanitize(node, schema) {
254-
/** @type {Node} */
252+
/** @type {Nodes} */
255253
let ctx = {type: 'root', children: []}
256254

257255
if (node && typeof node === 'object' && node.type) {
@@ -282,13 +280,13 @@ export function sanitize(node, schema) {
282280
* Sanitize `node`.
283281
*
284282
* @param {Schema} schema
285-
* @param {Node} node
283+
* @param {Nodes} node
286284
* @param {Array<string>} stack
287-
* @returns {Node | Array<Node> | undefined}
285+
* @returns {Nodes | Array<Nodes> | undefined}
288286
*/
289287
function one(schema, node, stack) {
290288
const type = node && node.type
291-
/** @type {Node} */
289+
/** @type {Nodes} */
292290
// @ts-expect-error rest of props added later.
293291
const replacement = {type: node.type}
294292
/** @type {boolean | undefined} */
@@ -347,12 +345,12 @@ function one(schema, node, stack) {
347345
* Sanitize `children`.
348346
*
349347
* @type {Handler}
350-
* @param {Array<Node>} children
351-
* @param {Node} node
352-
* @returns {Array<Node>}
348+
* @param {Array<Nodes>} children
349+
* @param {Nodes} node
350+
* @returns {Array<Nodes>}
353351
*/
354352
function all(schema, children, node, stack) {
355-
/** @type {Array<Node>} */
353+
/** @type {Array<Nodes>} */
356354
const results = []
357355

358356
if (Array.isArray(children)) {
@@ -467,7 +465,7 @@ function handleDoctypeName() {
467465
*
468466
* @param {Schema} schema
469467
* @param {string} tagName
470-
* @param {Node} _
468+
* @param {Nodes} _
471469
* @param {Array<string>} stack
472470
* @returns {string | false}
473471
*/

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,20 @@
3636
"index.js"
3737
],
3838
"dependencies": {
39-
"@types/hast": "^2.0.0"
39+
"@types/hast": "^3.0.0"
4040
},
4141
"devDependencies": {
4242
"@types/node": "^20.0.0",
4343
"c8": "^8.0.0",
4444
"deepmerge": "^4.0.0",
4545
"hast-util-to-html": "^8.0.0",
46-
"hastscript": "^7.0.0",
46+
"hastscript": "^8.0.0",
4747
"prettier": "^3.0.0",
4848
"remark-cli": "^11.0.0",
4949
"remark-preset-wooorm": "^9.0.0",
5050
"type-coverage": "^2.0.0",
5151
"typescript": "^5.0.0",
52-
"unist-builder": "^3.0.0",
52+
"unist-builder": "^4.0.0",
5353
"xo": "^0.55.0"
5454
},
5555
"scripts": {

test.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ test('sanitize()', async (t) => {
2929

3030
await t.test('unknown nodes', () => {
3131
assert.equal(
32-
// @ts-expect-error runtime.
33-
toHtml(sanitize(u('unknown', '<xml></xml>'))),
32+
toHtml(
33+
// @ts-expect-error runtime.
34+
sanitize(u('unknown', '<xml></xml>'))
35+
),
3436
'',
3537
'should ignore unknown nodes'
3638
)
@@ -47,8 +49,10 @@ test('sanitize()', async (t) => {
4749
)
4850

4951
assert.equal(
50-
// @ts-expect-error runtime.
51-
toHtml(sanitize(u('directive', {name: '!alpha'}, '!alpha bravo'))),
52+
toHtml(
53+
// @ts-expect-error: no longer a hast node.
54+
sanitize(u('directive', {name: '!alpha'}, '!alpha bravo'))
55+
),
5256
'',
5357
'should ignore declaration `directive`s'
5458
)
@@ -70,12 +74,14 @@ test('sanitize()', async (t) => {
7074

7175
await t.test('`comment`', () => {
7276
assert.equal(
77+
// @ts-expect-error: remove when `hast-util-to-html` updates.
7378
toHtml(sanitize(u('comment', 'alpha'))),
7479
'',
7580
'should ignore `comment`s by default'
7681
)
7782

7883
assert.equal(
84+
// @ts-expect-error: remove when `hast-util-to-html` updates.
7985
toHtml(sanitize(u('comment', 'alpha'), {allowComments: true})),
8086
'<!--alpha-->',
8187
'should allow `comment`s with `allowComments: true`'
@@ -90,6 +96,7 @@ test('sanitize()', async (t) => {
9096

9197
assert.equal(
9298
toHtml(
99+
// @ts-expect-error: remove when `hast-util-to-html` updates.
93100
sanitize(u('comment', 'alpha--><script>alert(1)</script><!--bravo'), {
94101
allowComments: true
95102
})
@@ -101,13 +108,15 @@ test('sanitize()', async (t) => {
101108

102109
await t.test('`doctype`', () => {
103110
assert.equal(
111+
// @ts-expect-error: remove when `hast-util-to-html` updates.
104112
toHtml(sanitize(u('doctype', {name: 'html'}, 'alpha'))),
105113
'',
106114
'should ignore `doctype`s by default'
107115
)
108116

109117
assert.equal(
110118
toHtml(
119+
// @ts-expect-error: remove when `hast-util-to-html` updates.
111120
sanitize(u('doctype', {name: 'html'}, 'alpha'), {allowDoctypes: true})
112121
),
113122
'<!doctype html>',
@@ -144,6 +153,7 @@ test('sanitize()', async (t) => {
144153
)
145154

146155
assert.equal(
156+
// @ts-expect-error: remove when `hast-util-to-html` updates.
147157
toHtml(sanitize(u('text', 'alert(1)'))),
148158
'alert(1)',
149159
'should allow `text`'
@@ -157,12 +167,14 @@ test('sanitize()', async (t) => {
157167
)
158168

159169
assert.equal(
170+
// @ts-expect-error: remove when `hast-util-to-html` updates.
160171
toHtml(sanitize(h('script', u('text', 'alert(1)')))),
161172
'',
162173
'should ignore `text` in `script` elements'
163174
)
164175

165176
assert.equal(
177+
// @ts-expect-error: remove when `hast-util-to-html` updates.
166178
toHtml(sanitize(h('style', u('text', 'alert(1)')))),
167179
'alert(1)',
168180
'should show `text` in `style` elements'

0 commit comments

Comments
 (0)