Skip to content

Commit 31febd8

Browse files
committed
Add strict to tsconfig.json
1 parent be3657a commit 31febd8

File tree

4 files changed

+70
-40
lines changed

4 files changed

+70
-40
lines changed

lib/index.js

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
* @property {boolean} [allowComments] Whether to allow comments
2323
* @property {boolean} [allowDoctypes] Whether to allow doctypes
2424
*
25-
* @typedef {(schema: Schema, value: unknown, node: Node, stack: Array.<string>) => unknown} Handler
25+
* @typedef {(schema: Schema, value: any, node: any, stack: Array.<string>) => unknown} Handler
2626
* @typedef {Object.<string, Handler>} NodeDefinition
27-
* @typedef {((schema: Schema, node: Node) => NodeDefinition)} NodeDefinitionGetter
27+
* @typedef {((schema: Schema, node: Node) => NodeDefinition|undefined)} NodeDefinitionGetter
2828
* @typedef {Object.<string, NodeDefinition|NodeDefinitionGetter>} NodeSchema
2929
*/
3030

@@ -68,7 +68,7 @@ export function sanitize(node, schema) {
6868
if (replace.length === 1) {
6969
ctx = replace[0]
7070
} else {
71-
// @ts-ignore Assume `root` is not a child.
71+
// @ts-expect-error Assume `root` is not a child.
7272
ctx.children = replace
7373
}
7474
} else {
@@ -86,17 +86,18 @@ export function sanitize(node, schema) {
8686
* @param {Schema} schema
8787
* @param {Node} node
8888
* @param {Array.<string>} stack
89-
* @returns {Node|Array.<Node>|null}
89+
* @returns {Node|Array.<Node>|undefined}
9090
*/
9191
function one(schema, node, stack) {
9292
const type = node && node.type
9393
/** @type {Node} */
94-
// @ts-ignore rest of props added later.
94+
// @ts-expect-error rest of props added later.
9595
const replacement = {type: node.type}
96-
/** @type {boolean} */
96+
/** @type {boolean|undefined} */
9797
let replace
9898

9999
if (own.call(nodeSchema, type)) {
100+
/** @type {NodeDefinition|NodeDefinitionGetter|undefined} */
100101
let definition = nodeSchema[type]
101102

102103
if (typeof definition === 'function') {
@@ -112,14 +113,20 @@ function one(schema, node, stack) {
112113

113114
for (key in allowed) {
114115
if (own.call(allowed, key)) {
116+
// @ts-expect-error: fine.
117+
// type-coverage:ignore-next-line
115118
const result = allowed[key](schema, node[key], node, stack)
116119

117120
// eslint-disable-next-line max-depth
118121
if (result === false) {
119-
replace = null
122+
replace = undefined
120123
// Set the non-safe value.
124+
// @ts-expect-error: fine.
125+
// type-coverage:ignore-next-line
121126
replacement[key] = node[key]
122127
} else if (result !== undefined && result !== null) {
128+
// @ts-expect-error: fine.
129+
// type-coverage:ignore-next-line
123130
replacement[key] = result
124131
}
125132
}
@@ -132,16 +139,18 @@ function one(schema, node, stack) {
132139
}
133140

134141
return replacement.type === 'element' &&
142+
schema.strip &&
135143
!schema.strip.includes(replacement.tagName)
136144
? replacement.children
137-
: null
145+
: undefined
138146
}
139147

140148
/**
141149
* Sanitize `children`.
142150
*
143151
* @type {Handler}
144152
* @param {Array.<Node>} children
153+
* @param {Node} node
145154
* @returns {Array.<Node>}
146155
*/
147156
function all(schema, children, node, stack) {
@@ -177,12 +186,12 @@ function all(schema, children, node, stack) {
177186

178187
/** @type {NodeDefinitionGetter} */
179188
function handleDoctype(schema) {
180-
return schema.allowDoctypes ? {name: handleDoctypeName} : null
189+
return schema.allowDoctypes ? {name: handleDoctypeName} : undefined
181190
}
182191

183192
/** @type {NodeDefinitionGetter} */
184193
function handleComment(schema) {
185-
return schema.allowComments ? {value: handleCommentValue} : null
194+
return schema.allowComments ? {value: handleCommentValue} : undefined
186195
}
187196

188197
/**
@@ -196,14 +205,14 @@ function handleComment(schema) {
196205
function handleProperties(schema, properties, node, stack) {
197206
const name = handleTagName(schema, node.tagName, node, stack)
198207
/* c8 ignore next */
208+
const attrs = schema.attributes || {}
209+
/* c8 ignore next */
199210
const reqs = schema.required || {}
200211
const props = properties || {}
201212
const allowed = Object.assign(
202213
{},
203-
toPropertyValueMap(schema.attributes['*']),
204-
toPropertyValueMap(
205-
name && own.call(schema.attributes, name) ? schema.attributes[name] : []
206-
)
214+
toPropertyValueMap(attrs['*']),
215+
toPropertyValueMap(name && own.call(attrs, name) ? attrs[name] : [])
207216
)
208217
/** @type {Properties} */
209218
const result = {}
@@ -259,19 +268,25 @@ function handleDoctypeName() {
259268
* Sanitize `tagName`.
260269
*
261270
* @type {Handler}
271+
* @param {unknown} tagName
272+
* @param {Node} _
262273
* @returns {string|false}
263274
*/
264275
function handleTagName(schema, tagName, _, stack) {
265276
const name = typeof tagName === 'string' ? tagName : ''
266277
let index = -1
267278

268-
if (!name || name === '*' || !schema.tagNames.includes(name)) {
279+
if (
280+
!name ||
281+
name === '*' ||
282+
(schema.tagNames && !schema.tagNames.includes(name))
283+
) {
269284
return false
270285
}
271286

272287
// Some nodes can break out of their context if they don’t have a certain
273288
// ancestor.
274-
if (own.call(schema.ancestors, name)) {
289+
if (schema.ancestors && own.call(schema.ancestors, name)) {
275290
while (++index < schema.ancestors[name].length) {
276291
if (stack.includes(schema.ancestors[name][index])) {
277292
return name
@@ -288,6 +303,7 @@ function handleTagName(schema, tagName, _, stack) {
288303
* See <https://html.spec.whatwg.org/multipage/parsing.html#serialising-html-fragments>
289304
*
290305
* @type {Handler}
306+
* @param {unknown} value
291307
* @returns {string}
292308
*/
293309
function handleCommentValue(_, value) {
@@ -301,6 +317,7 @@ function handleCommentValue(_, value) {
301317
* Sanitize `value`.
302318
*
303319
* @type {Handler}
320+
* @param {unknown} value
304321
* @returns {string}
305322
*/
306323
function handleValue(_, value) {
@@ -311,6 +328,7 @@ function handleValue(_, value) {
311328
* Allow `value`.
312329
*
313330
* @type {Handler}
331+
* @param {unknown} value
314332
*/
315333
function allow(_, value) {
316334
return value
@@ -334,7 +352,7 @@ function handlePropertyValues(schema, values, prop, definition) {
334352
const value = handlePropertyValue(schema, values[index], prop, definition)
335353

336354
if (value !== undefined && value !== null) {
337-
// @ts-ignore Assume no booleans were in arrays.
355+
// @ts-expect-error Assume no booleans were in arrays.
338356
result.push(value)
339357
}
340358
}
@@ -359,7 +377,11 @@ function handlePropertyValue(schema, value, prop, definition) {
359377
safeProtocol(schema, value, prop) &&
360378
(definition.length === 0 || definition.includes(value))
361379
) {
362-
return schema.clobber.includes(prop) ? schema.clobberPrefix + value : value
380+
return schema.clobberPrefix &&
381+
schema.clobber &&
382+
schema.clobber.includes(prop)
383+
? schema.clobberPrefix + value
384+
: value
363385
}
364386
}
365387

@@ -377,9 +399,10 @@ function safeProtocol(schema, value, prop) {
377399
const questionMark = url.indexOf('?')
378400
const numberSign = url.indexOf('#')
379401
const slash = url.indexOf('/')
380-
const protocols = own.call(schema.protocols, prop)
381-
? schema.protocols[prop].concat()
382-
: []
402+
const protocols =
403+
schema.protocols && own.call(schema.protocols, prop)
404+
? schema.protocols[prop].concat()
405+
: []
383406
let index = -1
384407

385408
if (

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@
8181
"typeCoverage": {
8282
"atLeast": 100,
8383
"detail": true,
84-
"strict": true
84+
"strict": true,
85+
"ignoreCatch": true,
86+
"#": "Couple of needed `any`s",
87+
"ignoreFiles": [
88+
"lib/index.d.ts"
89+
]
8590
}
8691
}

test.js

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,21 @@ const own = {}.hasOwnProperty
1111

1212
test('sanitize()', (t) => {
1313
t.test('non-node', (t) => {
14-
// @ts-ignore runtime.
14+
// @ts-expect-error runtime.
1515
t.equal(html(sanitize(true)), '', 'should ignore non-nodes (#1)')
16+
// @ts-expect-error runtime.
1617
t.equal(html(sanitize(null)), '', 'should ignore non-nodes (#2)')
17-
// @ts-ignore runtime.
18+
// @ts-expect-error runtime.
1819
t.equal(html(sanitize(1)), '', 'should ignore non-nodes (#3)')
19-
// @ts-ignore runtime.
20+
// @ts-expect-error runtime.
2021
t.equal(html(sanitize([])), '', 'should ignore non-nodes (#4)')
2122

2223
t.end()
2324
})
2425

2526
t.test('unknown nodes', (t) => {
2627
t.equal(
27-
// @ts-ignore runtime.
28+
// @ts-expect-error runtime.
2829
html(sanitize(u('unknown', '<xml></xml>'))),
2930
'',
3031
'should ignore unknown nodes'
@@ -34,25 +35,25 @@ test('sanitize()', (t) => {
3435
})
3536

3637
t.test('ignored nodes', (t) => {
37-
// @ts-ignore runtime.
38+
// @ts-expect-error runtime.
3839
t.equal(html(sanitize(u('raw', '<xml></xml>'))), '', 'should ignore `raw`')
3940

4041
t.equal(
41-
// @ts-ignore runtime.
42+
// @ts-expect-error runtime.
4243
html(sanitize(u('directive', {name: '!alpha'}, '!alpha bravo'))),
4344
'',
4445
'should ignore declaration `directive`s'
4546
)
4647

4748
t.equal(
48-
// @ts-ignore runtime.
49+
// @ts-expect-error runtime.
4950
html(sanitize(u('directive', {name: '?xml'}, '?xml version="1.0"'))),
5051
'',
5152
'should ignore processing instruction `directive`s'
5253
)
5354

5455
t.equal(
55-
// @ts-ignore runtime.
56+
// @ts-expect-error runtime.
5657
html(sanitize(u('characterData', 'alpha'))),
5758
'',
5859
'should ignore `characterData`s'
@@ -75,7 +76,7 @@ test('sanitize()', (t) => {
7576
)
7677

7778
t.equal(
78-
// @ts-ignore runtime.
79+
// @ts-expect-error runtime.
7980
html(sanitize(u('comment', {toString}), {allowComments: true})),
8081
'<!---->',
8182
'should ignore non-string `value`s with `allowComments: true`'
@@ -147,7 +148,7 @@ test('sanitize()', (t) => {
147148
)
148149

149150
t.equal(
150-
// @ts-ignore runtime.
151+
// @ts-expect-error runtime.
151152
html(sanitize(u('text', {toString}))),
152153
'',
153154
'should ignore non-string `value`s'
@@ -205,7 +206,7 @@ test('sanitize()', (t) => {
205206
)
206207

207208
t.deepEqual(
208-
// @ts-ignore runtime.
209+
// @ts-expect-error runtime.
209210
sanitize({
210211
type: 'element',
211212
properties: {},
@@ -216,7 +217,7 @@ test('sanitize()', (t) => {
216217
)
217218

218219
t.deepEqual(
219-
// @ts-ignore runtime.
220+
// @ts-expect-error runtime.
220221
sanitize({type: 'element', tagName: 'div'}),
221222
h(''),
222223
'should support elements without children / properties'
@@ -310,14 +311,14 @@ test('sanitize()', (t) => {
310311
)
311312

312313
t.deepEqual(
313-
// @ts-ignore runtime.
314+
// @ts-expect-error runtime.
314315
sanitize(u('element', {tagName: 'img', properties: {alt: null}})),
315316
h('img'),
316317
'should ignore `null`'
317318
)
318319

319320
t.deepEqual(
320-
// @ts-ignore runtime.
321+
// @ts-expect-error runtime.
321322
sanitize(u('element', {tagName: 'img', properties: {alt: undefined}})),
322323
h('img'),
323324
'should ignore `undefined`'
@@ -336,21 +337,21 @@ test('sanitize()', (t) => {
336337
)
337338

338339
t.deepEqual(
339-
// @ts-ignore runtime.
340+
// @ts-expect-error runtime.
340341
sanitize(u('element', {tagName: 'img', properties: {alt: {toString}}})),
341342
h('img'),
342343
'should ignore objects'
343344
)
344345

345346
t.deepEqual(
346347
sanitize(
347-
// @ts-ignore runtime.
348+
// @ts-expect-error runtime.
348349
u('element', {
349350
tagName: 'img',
350351
properties: {alt: [1, true, 'three', [4], {toString}]}
351352
})
352353
),
353-
// @ts-ignore runtime.
354+
// @ts-expect-error runtime.
354355
h('img', {alt: [1, true, 'three']}),
355356
'should supports arrays'
356357
)

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"declaration": true,
1111
"emitDeclarationOnly": true,
1212
"allowSyntheticDefaultImports": true,
13-
"skipLibCheck": true
13+
"skipLibCheck": true,
14+
"strict": true
1415
}
1516
}

0 commit comments

Comments
 (0)