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 24, 2021
1 parent 095a93a commit beb9b66
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 38 deletions.
67 changes: 30 additions & 37 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const reverseTypes = [
test('unist-util-visit', (t) => {
t.throws(
() => {
// @ts-ignore runtime.
// @ts-expect-error runtime.
visit()
},
/TypeError: visitor is not a function/,
Expand All @@ -59,7 +59,7 @@ test('unist-util-visit', (t) => {

t.throws(
() => {
// @ts-ignore runtime.
// @ts-expect-error runtime.
visit(tree)
},
/TypeError: visitor is not a function/,
Expand Down Expand Up @@ -146,58 +146,47 @@ test('unist-util-visit', (t) => {
t.test('should accept any `is`-compatible test function', (t) => {
let n = 0

visit(tree, test, visitor)
visit(tree, test, (node, index, parent) => {
const info = '(' + (parent && parent.type) + ':' + index + ')'
assert.ok(test(node, index), 'should be a requested node ' + info)
n++
})

t.equal(n, 3, 'should visit all passing nodes')

t.end()

/**
* @param {Node} node
* @param {number|null} index
* @param {Parent|null} parent
*/
function visitor(node, index, parent) {
const info = '(' + (parent && parent.type) + ':' + index + ')'
assert.ok(test(node, index), 'should be a requested node ' + info)
n++
}

/**
* @param {Node} _
* @param {number|null} index
* @param {number|null|undefined} index
*/
function test(_, index) {
return index > 3
return typeof index === 'number' && index > 3
}
})

t.test('should accept an array of `is`-compatible tests', (t) => {
const expected = new Set(['root', 'paragraph', 'emphasis', 'strong'])
const tests = [test, 'paragraph', {value: '.'}, 'emphasis', 'strong']
const tests = [
/** @param {Node} node */
(node) => node.type === 'root',
'paragraph',
{value: '.'},
'emphasis',
'strong'
]
let n = 0

visit(tree, tests, visitor)

t.equal(n, 5, 'should visit all passing nodes')

t.end()

/**
* @param {Literal} node
*/
function visitor(node) {
visit(tree, tests, (node) => {
// @ts-expect-error: indexable.
const ok = expected.has(node.type) || node.value === '.'
assert.ok(ok, 'should be a requested type: ' + node.type)
n++
}
})

/**
* @param {Node} node
*/
function test(node) {
return node.type === 'root'
}
t.equal(n, 5, 'should visit all passing nodes')

t.end()
})

t.test('should stop if `visitor` stops', (t) => {
Expand Down Expand Up @@ -383,7 +372,7 @@ test('unist-util-visit', (t) => {
'should be the expected type'
)

if (again === false && node.type === 'strong') {
if (parent && again === false && node.type === 'strong') {
again = true
return parent.children.length // Skip siblings.
}
Expand Down Expand Up @@ -424,7 +413,11 @@ test('unist-util-visit', (t) => {
'should be the expected type'
)

if (again === false && node.type === 'strong') {
if (
typeof index === 'number' &&
again === false &&
node.type === 'strong'
) {
again = true
return index + 2 // Skip to `inlineCode`.
}
Expand Down Expand Up @@ -458,7 +451,7 @@ test('unist-util-visit', (t) => {
function visitor(_1, _2, parent) {
n++

if (n === 2) {
if (parent && n === 2) {
parent.children.push(other)
}
}
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 beb9b66

Please sign in to comment.