Skip to content

Commit

Permalink
Add strict to tsconfig.json
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jul 23, 2021
1 parent c60d670 commit 5503921
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 16 deletions.
27 changes: 13 additions & 14 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,27 @@ const DOCUMENT_FRAGMENT_NODE = 11

/**
* @param {Node} node
* @returns {HastNode|null}
* @returns {HastNode|undefined}
*/
function transform(node) {
switch (node.nodeType) {
case ELEMENT_NODE:
// @ts-ignore TypeScript is wrong.
// @ts-expect-error TypeScript is wrong.
return element(node)
case DOCUMENT_NODE:
case DOCUMENT_FRAGMENT_NODE:
// @ts-ignore TypeScript is wrong.
// @ts-expect-error TypeScript is wrong.
return root(node)
case TEXT_NODE:
// @ts-ignore TypeScript is wrong.
// @ts-expect-error TypeScript is wrong.
return text(node)
case COMMENT_NODE:
// @ts-ignore TypeScript is wrong.
// @ts-expect-error TypeScript is wrong.
return comment(node)
case DOCUMENT_TYPE_NODE:
return doctype()
default:
return null
return undefined
}
}

Expand All @@ -61,7 +61,7 @@ function root(node) {
* @returns {HastDoctype}
*/
function doctype() {
// @ts-ignore hast types out of date.
// @ts-expect-error hast types out of date.
return {type: 'doctype'}
}

Expand All @@ -72,7 +72,7 @@ function doctype() {
* @returns {HastText}
*/
function text(node) {
return {type: 'text', value: node.nodeValue}
return {type: 'text', value: node.nodeValue || ''}
}

/**
Expand All @@ -82,7 +82,7 @@ function text(node) {
* @returns {HastComment}
*/
function comment(node) {
return {type: 'comment', value: node.nodeValue}
return {type: 'comment', value: node.nodeValue || ''}
}

/**
Expand All @@ -98,15 +98,15 @@ function element(node) {
space === webNamespaces.html ? node.tagName.toLowerCase() : node.tagName
/** @type {DocumentFragment|Element} */
const content =
// @ts-ignore Types are wrong.
// @ts-expect-error Types are wrong.
space === webNamespaces.html && tagName === 'template' ? node.content : node
const attributes = node.getAttributeNames()
/** @type {Object.<string, string>} */
const props = {}
let index = -1

while (++index < attributes.length) {
props[attributes[index]] = node.getAttribute(attributes[index])
props[attributes[index]] = node.getAttribute(attributes[index]) || ''
}

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

if (child !== null) {
// @ts-ignore Assume no document inside document.
if (child !== undefined) {
// @ts-expect-error Assume no document inside document.
children.push(child)
}
}
Expand All @@ -141,6 +141,5 @@ function all(node) {
* @returns {HastNode}
*/
export function fromDom(node) {
// @ts-ignore Code can handle empty “node”.
return transform(node || {}) || {type: 'root', children: []}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"web-namespaces": "^2.0.0"
},
"devDependencies": {
"@types/glob": "^7.0.0",
"@types/jsdom": "^16.0.0",
"@types/tape": "^4.0.0",
"c8": "^7.0.0",
Expand Down
24 changes: 23 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,34 @@ test('hast-util-from-dom', (t) => {
)

t.deepEqual(
// @ts-ignore runtime.
// @ts-expect-error runtime.
fromDom(),
{type: 'root', children: []},
'should handle a missing DOM tree'
)

t.deepEqual(
fromDom(document.createTextNode('')),
{type: 'text', value: ''},
'should support a text w/o value'
)

t.deepEqual(
fromDom(document.createComment('')),
{type: 'comment', value: ''},
'should support a comment w/o value'
)

const attribute = document.createAttribute('title')
const element = document.createElement('div')
element.setAttributeNode(attribute)

t.deepEqual(
fromDom(element),
{type: 'element', tagName: 'div', properties: {title: ''}, children: []},
'should support an attribute w/o value'
)

t.end()
})

Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"declaration": true,
"emitDeclarationOnly": true,
"allowSyntheticDefaultImports": true,
"skipLibCheck": true
"skipLibCheck": true,
"strict": true
}
}

0 comments on commit 5503921

Please sign in to comment.