Skip to content

Commit 1232790

Browse files
committed
Add strict to tsconfig.json
1 parent e02b8b7 commit 1232790

File tree

4 files changed

+26
-15
lines changed

4 files changed

+26
-15
lines changed

index.js

+20-14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
/**
2+
* @typedef {import('mdast').Root|import('mdast').Content} Node
3+
*
24
* @typedef Options
35
* @property {boolean} [includeImageAlt=true]
46
*/
@@ -8,31 +10,27 @@
810
* Prefer the node’s plain-text fields, otherwise serialize its children,
911
* and if the given value is an array, serialize the nodes in it.
1012
*
11-
* @param {unknown} node
13+
* @param {unknown} value
1214
* @param {Options} [options]
1315
* @returns {string}
1416
*/
15-
export function toString(node, options = {}) {
17+
export function toString(value, options = {}) {
1618
const {includeImageAlt = true} = options
17-
return one(node, includeImageAlt)
19+
return one(value, includeImageAlt)
1820
}
1921

2022
/**
21-
* @param {unknown} node
23+
* @param {unknown} value
2224
* @param {boolean} includeImageAlt
2325
* @returns {string}
2426
*/
25-
function one(node, includeImageAlt) {
27+
function one(value, includeImageAlt) {
2628
return (
27-
(node &&
28-
typeof node === 'object' &&
29-
// @ts-ignore looks like a literal.
30-
(node.value ||
31-
// @ts-ignore looks like an image.
32-
(includeImageAlt ? node.alt : '') ||
33-
// @ts-ignore looks like a parent.
34-
('children' in node && all(node.children, includeImageAlt)) ||
35-
(Array.isArray(node) && all(node, includeImageAlt)))) ||
29+
(node(value) &&
30+
(('value' in value && value.value) ||
31+
(includeImageAlt && 'alt' in value && value.alt) ||
32+
('children' in value && all(value.children, includeImageAlt)))) ||
33+
(Array.isArray(value) && all(value, includeImageAlt)) ||
3634
''
3735
)
3836
}
@@ -53,3 +51,11 @@ function all(values, includeImageAlt) {
5351

5452
return result.join('')
5553
}
54+
55+
/**
56+
* @param {unknown} value
57+
* @returns {value is Node}
58+
*/
59+
function node(value) {
60+
return Boolean(value && typeof value === 'object')
61+
}

package.json

+3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
"index.d.ts",
3333
"index.js"
3434
],
35+
"dependencies": {
36+
"@types/mdast": "^3.0.0"
37+
},
3538
"devDependencies": {
3639
"@types/tape": "^4.0.0",
3740
"c8": "^7.0.0",

test.js

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import test from 'tape'
22
import {toString} from './index.js'
33

44
test('toString', (t) => {
5+
// @ts-expect-error: runtime.
56
t.equal(toString(), '', 'should not fail on a missing node')
67
t.equal(toString(null), '', 'should not fail on `null` missing node')
78

tsconfig.json

+2-1
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)