Skip to content

Commit 5503921

Browse files
committed
Add strict to tsconfig.json
1 parent c60d670 commit 5503921

File tree

4 files changed

+39
-16
lines changed

4 files changed

+39
-16
lines changed

lib/index.js

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,27 @@ const DOCUMENT_FRAGMENT_NODE = 11
2121

2222
/**
2323
* @param {Node} node
24-
* @returns {HastNode|null}
24+
* @returns {HastNode|undefined}
2525
*/
2626
function transform(node) {
2727
switch (node.nodeType) {
2828
case ELEMENT_NODE:
29-
// @ts-ignore TypeScript is wrong.
29+
// @ts-expect-error TypeScript is wrong.
3030
return element(node)
3131
case DOCUMENT_NODE:
3232
case DOCUMENT_FRAGMENT_NODE:
33-
// @ts-ignore TypeScript is wrong.
33+
// @ts-expect-error TypeScript is wrong.
3434
return root(node)
3535
case TEXT_NODE:
36-
// @ts-ignore TypeScript is wrong.
36+
// @ts-expect-error TypeScript is wrong.
3737
return text(node)
3838
case COMMENT_NODE:
39-
// @ts-ignore TypeScript is wrong.
39+
// @ts-expect-error TypeScript is wrong.
4040
return comment(node)
4141
case DOCUMENT_TYPE_NODE:
4242
return doctype()
4343
default:
44-
return null
44+
return undefined
4545
}
4646
}
4747

@@ -61,7 +61,7 @@ function root(node) {
6161
* @returns {HastDoctype}
6262
*/
6363
function doctype() {
64-
// @ts-ignore hast types out of date.
64+
// @ts-expect-error hast types out of date.
6565
return {type: 'doctype'}
6666
}
6767

@@ -72,7 +72,7 @@ function doctype() {
7272
* @returns {HastText}
7373
*/
7474
function text(node) {
75-
return {type: 'text', value: node.nodeValue}
75+
return {type: 'text', value: node.nodeValue || ''}
7676
}
7777

7878
/**
@@ -82,7 +82,7 @@ function text(node) {
8282
* @returns {HastComment}
8383
*/
8484
function comment(node) {
85-
return {type: 'comment', value: node.nodeValue}
85+
return {type: 'comment', value: node.nodeValue || ''}
8686
}
8787

8888
/**
@@ -98,15 +98,15 @@ function element(node) {
9898
space === webNamespaces.html ? node.tagName.toLowerCase() : node.tagName
9999
/** @type {DocumentFragment|Element} */
100100
const content =
101-
// @ts-ignore Types are wrong.
101+
// @ts-expect-error Types are wrong.
102102
space === webNamespaces.html && tagName === 'template' ? node.content : node
103103
const attributes = node.getAttributeNames()
104104
/** @type {Object.<string, string>} */
105105
const props = {}
106106
let index = -1
107107

108108
while (++index < attributes.length) {
109-
props[attributes[index]] = node.getAttribute(attributes[index])
109+
props[attributes[index]] = node.getAttribute(attributes[index]) || ''
110110
}
111111

112112
return fn(tagName, props, all(content))
@@ -127,8 +127,8 @@ function all(node) {
127127
while (++index < nodes.length) {
128128
const child = transform(nodes[index])
129129

130-
if (child !== null) {
131-
// @ts-ignore Assume no document inside document.
130+
if (child !== undefined) {
131+
// @ts-expect-error Assume no document inside document.
132132
children.push(child)
133133
}
134134
}
@@ -141,6 +141,5 @@ function all(node) {
141141
* @returns {HastNode}
142142
*/
143143
export function fromDom(node) {
144-
// @ts-ignore Code can handle empty “node”.
145144
return transform(node || {}) || {type: 'root', children: []}
146145
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"web-namespaces": "^2.0.0"
4040
},
4141
"devDependencies": {
42+
"@types/glob": "^7.0.0",
4243
"@types/jsdom": "^16.0.0",
4344
"@types/tape": "^4.0.0",
4445
"c8": "^7.0.0",

test/index.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,34 @@ test('hast-util-from-dom', (t) => {
116116
)
117117

118118
t.deepEqual(
119-
// @ts-ignore runtime.
119+
// @ts-expect-error runtime.
120120
fromDom(),
121121
{type: 'root', children: []},
122122
'should handle a missing DOM tree'
123123
)
124124

125+
t.deepEqual(
126+
fromDom(document.createTextNode('')),
127+
{type: 'text', value: ''},
128+
'should support a text w/o value'
129+
)
130+
131+
t.deepEqual(
132+
fromDom(document.createComment('')),
133+
{type: 'comment', value: ''},
134+
'should support a comment w/o value'
135+
)
136+
137+
const attribute = document.createAttribute('title')
138+
const element = document.createElement('div')
139+
element.setAttributeNode(attribute)
140+
141+
t.deepEqual(
142+
fromDom(element),
143+
{type: 'element', tagName: 'div', properties: {title: ''}, children: []},
144+
'should support an attribute w/o value'
145+
)
146+
125147
t.end()
126148
})
127149

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)