Skip to content
This repository was archived by the owner on Aug 9, 2023. It is now read-only.

Commit 427191a

Browse files
committed
Add strict to tsconfig.json
1 parent 5bc5b47 commit 427191a

File tree

3 files changed

+52
-35
lines changed

3 files changed

+52
-35
lines changed

index.js

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,22 @@ import {html, svg, find, hastToReact} from 'property-information'
3131
import {stringify as spaces} from 'space-separated-tokens'
3232
import {stringify as commas} from 'comma-separated-tokens'
3333
import style from 'style-to-object'
34-
import {webNamespaces as ns} from 'web-namespaces'
34+
import {webNamespaces} from 'web-namespaces'
3535
import {convert} from 'unist-util-is'
3636

37+
const ns = /** @type {Record<string, string>} */ (webNamespaces)
38+
const toReact = /** @type {Record<string, string>} */ (hastToReact)
39+
3740
const own = {}.hasOwnProperty
3841

3942
/** @type {AssertRoot} */
40-
// @ts-ignore it’s correct.
43+
// @ts-expect-error it’s correct.
4144
const root = convert('root')
4245
/** @type {AssertElement} */
43-
// @ts-ignore it’s correct.
46+
// @ts-expect-error it’s correct.
4447
const element = convert('element')
4548
/** @type {AssertText} */
46-
// @ts-ignore it’s correct.
49+
// @ts-expect-error it’s correct.
4750
const text = convert('text')
4851

4952
/**
@@ -61,7 +64,7 @@ export function toH(h, tree, options) {
6164
const r = react(h)
6265
const v = vue(h)
6366
const vd = vdom(h)
64-
/** @type {string|boolean} */
67+
/** @type {string|boolean|null|undefined} */
6568
let prefix
6669
/** @type {Element} */
6770
let node
@@ -75,7 +78,7 @@ export function toH(h, tree, options) {
7578
}
7679

7780
if (root(tree)) {
78-
// @ts-ignore Allow `doctypes` in there, we’ll filter them out later.
81+
// @ts-expect-error Allow `doctypes` in there, we’ll filter them out later.
7982
node =
8083
tree.children.length === 1 && element(tree.children[0])
8184
? tree.children[0]
@@ -89,7 +92,7 @@ export function toH(h, tree, options) {
8992
node = tree
9093
} else {
9194
throw new Error(
92-
// @ts-ignore runtime.
95+
// @ts-expect-error runtime.
9396
'Expected root or element, not `' + ((tree && tree.type) || tree) + '`'
9497
)
9598
}
@@ -140,15 +143,15 @@ function transform(h, node, ctx) {
140143
}
141144

142145
for (key in node.properties) {
143-
if (own.call(node.properties, key)) {
146+
if (node.properties && own.call(node.properties, key)) {
144147
addAttribute(attributes, key, node.properties[key], ctx, name)
145148
}
146149
}
147150

148151
if (ctx.vdom) {
149152
if (schema.space === 'html') {
150153
name = name.toUpperCase()
151-
} else {
154+
} else if (schema.space) {
152155
attributes.namespace = ns[schema.space]
153156
}
154157
}
@@ -190,7 +193,7 @@ function transform(h, node, ctx) {
190193
// eslint-disable-next-line complexity, max-params
191194
function addAttribute(props, prop, value, ctx, name) {
192195
const info = find(ctx.schema, prop)
193-
/** @type {string} */
196+
/** @type {string|undefined} */
194197
let subprop
195198

196199
// Ignore nullish and `NaN` values.
@@ -236,10 +239,11 @@ function addAttribute(props, prop, value, ctx, name) {
236239
}
237240

238241
if (subprop) {
239-
if (!props[subprop]) props[subprop] = {}
240-
props[subprop][info.attribute] = value
242+
props[subprop] = Object.assign(props[subprop] || {}, {
243+
[info.attribute]: value
244+
})
241245
} else if (info.space && ctx.react) {
242-
props[hastToReact[info.property] || info.property] = value
246+
props[toReact[info.property] || info.property] = value
243247
} else {
244248
props[info.attribute] = value
245249
}
@@ -256,9 +260,9 @@ function react(h) {
256260
const node = h('div')
257261
return Boolean(
258262
node &&
259-
// @ts-ignore Looks like a React node.
263+
// @ts-expect-error Looks like a React node.
260264
('_owner' in node || '_store' in node) &&
261-
// @ts-ignore Looks like a React node.
265+
// @ts-expect-error Looks like a React node.
262266
(node.key === undefined || node.key === null)
263267
)
264268
}
@@ -282,7 +286,7 @@ function hyperscript(h) {
282286
function vdom(h) {
283287
/** @type {unknown} */
284288
const node = h('div')
285-
// @ts-ignore Looks like a vnode.
289+
// @ts-expect-error Looks like a vnode.
286290
return node.type === 'VirtualNode'
287291
}
288292

@@ -295,7 +299,7 @@ function vdom(h) {
295299
function vue(h) {
296300
/** @type {unknown} */
297301
const node = h('div')
298-
// @ts-ignore Looks like a Vue node.
302+
// @ts-expect-error Looks like a Vue node.
299303
return Boolean(node && node.context && node.context._isVue)
300304
}
301305

test.js

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ import {webNamespaces as ns} from 'web-namespaces'
1010
import {u} from 'unist-builder'
1111
import h from 'hyperscript'
1212
import {h as v} from 'virtual-dom'
13+
// @ts-expect-error: hush
1314
import vs from 'virtual-dom/virtual-hyperscript/svg.js'
1415
import rehype from 'rehype'
16+
// @ts-expect-error: hush
1517
import vToString from 'vdom-to-html'
1618
import {createElement as r} from 'react'
1719
import {renderToStaticMarkup as rToString} from 'react-dom/server.js'
@@ -26,6 +28,7 @@ test('hast-to-hyperscript', (t) => {
2628

2729
t.test('should throw if not given h', (t) => {
2830
t.throws(() => {
31+
// @ts-expect-error: runtime
2932
toH(null, u('element', {tagName: ''}, []))
3033
}, /h is not a function/)
3134

@@ -34,17 +37,17 @@ test('hast-to-hyperscript', (t) => {
3437

3538
t.test('should throw if not given a node', (t) => {
3639
t.throws(() => {
37-
// @ts-ignore runtime.
40+
// @ts-expect-error runtime.
3841
toH(h)
3942
}, /Expected root or element, not `undefined`/)
4043

4144
t.throws(() => {
42-
// @ts-ignore runtime.
45+
// @ts-expect-error runtime.
4346
toH(h, u('text', 'Alpha'))
4447
}, /Error: Expected root or element, not `text`/)
4548

4649
t.throws(() => {
47-
// @ts-ignore runtime.
50+
// @ts-expect-error runtime.
4851
toH(h, u('text', 'value'))
4952
}, /Expected root or element/)
5053

@@ -180,7 +183,7 @@ test('hast-to-hyperscript', (t) => {
180183
)
181184
])
182185

183-
// @ts-ignore `outerHTML` definitely does exist.
186+
// @ts-expect-error `outerHTML` definitely does exist.
184187
t.deepEqual(html(actual.outerHTML), html(baseline), 'equal output')
185188

186189
t.deepEqual(
@@ -194,13 +197,15 @@ test('hast-to-hyperscript', (t) => {
194197

195198
t.test('should support `virtual-dom/h`', (t) => {
196199
const baseline = doc.replace(/color:red;/, 'color: red;')
200+
/** @type {ReturnType<v>} */
201+
// @ts-expect-error Vue is too strict.
197202
const actual = toH(v, hast)
198203
const expected = v('div', {key: 'h-1'}, [
199204
v(
200205
'h1',
201206
{
202207
key: 'h-2',
203-
// @ts-ignore Works fine.
208+
// @ts-expect-error Works fine.
204209
attributes: {id: 'a', class: 'b c', hidden: true, height: 2}
205210
},
206211
[
@@ -521,42 +526,42 @@ test('hast-to-hyperscript', (t) => {
521526

522527
t.test('should support keys', (t) => {
523528
t.equal(
524-
// @ts-ignore Types are wrong.
529+
// @ts-expect-error Types are wrong.
525530
toH(h, u('element', {tagName: 'div'}, [])).key,
526531
undefined,
527532
'should not patch `keys` normally'
528533
)
529534

530535
t.equal(
531-
// @ts-ignore Types are wrong.
536+
// @ts-expect-error Types are wrong.
532537
toH(h, u('element', {tagName: 'div'}, []), 'prefix-').key,
533538
'prefix-1',
534539
'should patch `keys` when given'
535540
)
536541

537542
t.equal(
538-
// @ts-ignore Types are wrong.
543+
// @ts-expect-error Types are wrong.
539544
toH(h, u('element', {tagName: 'div'}, []), true).key,
540545
'h-1',
541546
'should patch `keys` when `true`'
542547
)
543548

544549
t.equal(
545-
// @ts-ignore Types are wrong.
550+
// @ts-expect-error Types are wrong.
546551
toH(h, u('element', {tagName: 'div'}, []), false).key,
547552
undefined,
548553
'should not patch `keys` when `false`'
549554
)
550555

551556
t.equal(
552-
// @ts-ignore Types are wrong.
557+
// @ts-expect-error Types are wrong.
558+
// type-coverage:ignore-next-line
553559
toH(v, u('element', {tagName: 'div'}, [])).key,
554560
'h-1',
555561
'should patch `keys` on vdom'
556562
)
557563

558564
t.equal(
559-
// @ts-ignore Types are wrong.
560565
toH(r, u('element', {tagName: 'div'}, [])).key,
561566
'h-1',
562567
'should patch `keys` on react'
@@ -569,6 +574,7 @@ test('hast-to-hyperscript', (t) => {
569574
t.deepEqual(
570575
vToString(
571576
toH(
577+
// @ts-expect-error Vue is too strict.
572578
v,
573579
u('element', {tagName: 'div', properties: {style: 'color:red'}}, [])
574580
)
@@ -581,7 +587,7 @@ test('hast-to-hyperscript', (t) => {
581587
toH(
582588
h,
583589
u('element', {tagName: 'div', properties: {style: 'color: red'}}, [])
584-
// @ts-ignore Types are wrong.
590+
// @ts-expect-error Types are wrong.
585591
).outerHTML,
586592
'<div style="color:red;"></div>',
587593
'hyperscript: should parse a style declaration'
@@ -670,18 +676,24 @@ test('hast-to-hyperscript', (t) => {
670676

671677
t.test('should support space', (t) => {
672678
t.equal(
679+
// @ts-expect-error Vue is too strict.
680+
// type-coverage:ignore-next-line
673681
toH(v, u('element', {tagName: 'div'}, [])).namespace,
674682
null,
675683
'should start in HTML'
676684
)
677685

678686
t.equal(
687+
// @ts-expect-error Vue is too strict.
688+
// type-coverage:ignore-next-line
679689
toH(v, u('element', {tagName: 'div'}, []), {space: 'svg'}).namespace,
680690
ns.svg,
681691
'should support `space: "svg"`'
682692
)
683693

684694
t.equal(
695+
// @ts-expect-error Vue is too strict.
696+
// type-coverage:ignore-next-line
685697
toH(v, u('element', {tagName: 'svg'}, [])).namespace,
686698
ns.svg,
687699
'should infer `space: "svg"`'
@@ -698,7 +710,7 @@ test('hast-to-hyperscript', (t) => {
698710
const expected = h('h1#a')
699711
const doc = '<h1 id="a"></h1>'
700712

701-
// @ts-ignore seems to exist fine 🤷‍♂️
713+
// @ts-expect-error seems to exist fine 🤷‍♂️
702714
t.deepEqual(html(actual.outerHTML), html(doc), 'equal output')
703715
t.deepEqual(html(expected.outerHTML), html(doc), 'equal output baseline')
704716
t.end()
@@ -709,7 +721,7 @@ test('hast-to-hyperscript', (t) => {
709721
const expected = h('div')
710722
const doc = '<div></div>'
711723

712-
// @ts-ignore Types are wrong.
724+
// @ts-expect-error Types are wrong.
713725
t.deepEqual(html(actual.outerHTML), html(doc), 'equal output')
714726
t.deepEqual(html(expected.outerHTML), html(doc), 'equal output baseline')
715727
t.end()
@@ -720,7 +732,7 @@ test('hast-to-hyperscript', (t) => {
720732
const expected = h('div', 'Alpha')
721733
const doc = '<div>Alpha</div>'
722734

723-
// @ts-ignore Types are wrong.
735+
// @ts-expect-error Types are wrong.
724736
t.deepEqual(html(actual.outerHTML), html(doc), 'equal output')
725737
t.deepEqual(html(expected.outerHTML), html(doc), 'equal output baseline')
726738
t.end()
@@ -737,7 +749,7 @@ test('hast-to-hyperscript', (t) => {
737749
const expected = h('div', [h('h1', 'Alpha'), h('p', 'Bravo')])
738750
const doc = '<div><h1>Alpha</h1><p>Bravo</p></div>'
739751

740-
// @ts-ignore Types are wrong.
752+
// @ts-expect-error Types are wrong.
741753
t.deepEqual(html(actual.outerHTML), html(doc), 'equal output')
742754
t.deepEqual(html(expected.outerHTML), html(doc), 'equal output baseline')
743755
t.end()
@@ -803,7 +815,7 @@ test('hast-to-hyperscript', (t) => {
803815
* @returns {HastRoot}
804816
*/
805817
function html(doc) {
806-
// @ts-ignore it’s a root!
818+
// @ts-expect-error it’s a root!
807819
return processor.parse(doc)
808820
}
809821

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)