Skip to content

Commit c0f73da

Browse files
committed
Use JSDoc based types
1 parent a8d04b6 commit c0f73da

9 files changed

+137
-117
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.DS_Store
2+
*.d.ts
23
*.log
34
coverage/
45
node_modules/

index.js

Lines changed: 100 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
import {color} from './color.js'
22

3-
export var inspect = color
4-
? inspectColor
5-
: /* istanbul ignore next */ inspectNoColor
3+
/**
4+
* @typedef {import('unist').Node} Node
5+
* @typedef {import('unist').Position} Position
6+
* @typedef {import('unist').Point} Point
7+
*
8+
* @typedef {Object} InspectOptions
9+
* @property {boolean} [showPositions=true]
10+
*/
11+
12+
/* c8 ignore next */
13+
export var inspect = color ? inspectColor : inspectNoColor
614

715
var own = {}.hasOwnProperty
816

@@ -15,12 +23,24 @@ var green = ansiColor(32, 39)
1523
/* eslint-disable-next-line no-control-regex */
1624
var colorExpression = /(?:(?:\u001B\[)|\u009B)(?:\d{1,3})?(?:(?:;\d{0,3})*)?[A-M|f-m]|\u001B[A-M]/g
1725

18-
// Inspects a node, without using color.
19-
export function inspectNoColor(node) {
20-
return inspectColor(node).replace(colorExpression, '')
26+
/**
27+
* Inspects a node, without using color.
28+
*
29+
* @param {unknown} node
30+
* @param {InspectOptions} [options]
31+
* @returns {string}
32+
*/
33+
export function inspectNoColor(node, options) {
34+
return inspectColor(node, options).replace(colorExpression, '')
2135
}
2236

23-
// Inspects a node.
37+
/**
38+
* Inspects a node, using color.
39+
*
40+
* @param {unknown} tree
41+
* @param {InspectOptions} [options]
42+
* @returns {string}
43+
*/
2444
export function inspectColor(tree, options) {
2545
var positions =
2646
!options ||
@@ -31,23 +51,39 @@ export function inspectColor(tree, options) {
3151

3252
return inspectValue(tree)
3353

54+
/**
55+
* @param {unknown} node
56+
* @returns {string}
57+
*/
3458
function inspectValue(node) {
3559
if (node && typeof node === 'object' && 'length' in node) {
60+
// @ts-ignore looks like a list of nodes.
3661
return inspectNodes(node)
3762
}
3863

64+
// @ts-ignore looks like a single node.
3965
if (node && node.type) {
66+
// @ts-ignore looks like a single node.
4067
return inspectTree(node)
4168
}
4269

4370
return inspectNonTree(node)
4471
}
4572

73+
/**
74+
* @param {unknown} value
75+
* @returns {string}
76+
*/
4677
function inspectNonTree(value) {
4778
return JSON.stringify(value)
4879
}
4980

81+
/**
82+
* @param {Node[]} nodes
83+
* @returns {string}
84+
*/
5085
function inspectNodes(nodes) {
86+
/** @type {Array.<string>} */
5187
var result = []
5288
var index = -1
5389

@@ -66,13 +102,22 @@ export function inspectColor(tree, options) {
66102
return result.join('\n')
67103
}
68104

105+
/**
106+
* @param {Object.<string, unknown>} object
107+
* @returns {string}
108+
*/
69109
function inspectFields(object) {
110+
/** @type {Array.<string>} */
70111
var result = []
112+
/** @type {string} */
71113
var key
114+
/** @type {unknown} */
72115
var value
116+
/** @type {string} */
73117
var formatted
74118

75119
for (key in object) {
120+
/* c8 ignore next 1 */
76121
if (!own.call(object, key)) continue
77122

78123
value = object[key]
@@ -95,11 +140,13 @@ export function inspectColor(tree, options) {
95140
if (
96141
value &&
97142
typeof value === 'object' &&
143+
// @ts-ignore looks like a node.
98144
value.type &&
99145
key !== 'data' &&
100146
key !== 'attributes' &&
101147
key !== 'properties'
102148
) {
149+
// @ts-ignore looks like a node.
103150
formatted = inspectTree(value)
104151
}
105152
// A list of nodes.
@@ -110,6 +157,7 @@ export function inspectColor(tree, options) {
110157
value[0] &&
111158
value[0].type
112159
) {
160+
// @ts-ignore looks like a list of nodes.
113161
formatted = '\n' + inspectNodes(value)
114162
} else {
115163
formatted = inspectNonTree(value)
@@ -122,20 +170,31 @@ export function inspectColor(tree, options) {
122170

123171
return indent(
124172
result.join('\n'),
173+
// @ts-ignore looks like a parent node.
125174
(object.children && object.children.length > 0 ? dim('│') : ' ') + ' '
126175
)
127176
}
128177

129-
function inspectTree(node, pad) {
130-
var result = [formatNode(node, pad)]
178+
/**
179+
* @param {Node} node
180+
* @returns {string}
181+
*/
182+
function inspectTree(node) {
183+
var result = [formatNode(node)]
131184
var fields = inspectFields(node)
185+
// @ts-ignore looks like a parent.
132186
var content = inspectNodes(node.children || [])
133187
if (fields) result.push(fields)
134188
if (content) result.push(content)
135189
return result.join('\n')
136190
}
137191

138-
// Colored node formatter.
192+
/**
193+
* Colored node formatter.
194+
*
195+
* @param {Node} node
196+
* @returns {string}
197+
*/
139198
function formatNode(node) {
140199
var result = [bold(node.type)]
141200
var kind = node.tagName || node.name
@@ -146,9 +205,10 @@ export function inspectColor(tree, options) {
146205
}
147206

148207
if (node.children) {
208+
// @ts-ignore looks like a parent.
149209
result.push(dim('['), yellow(node.children.length), dim(']'))
150210
} else if (typeof node.value === 'string') {
151-
result.push(' ', green(inspectNonTree(node.value, '')))
211+
result.push(' ', green(inspectNonTree(node.value)))
152212
}
153213

154214
if (position) {
@@ -159,6 +219,12 @@ export function inspectColor(tree, options) {
159219
}
160220
}
161221

222+
/**
223+
* @param {string} value
224+
* @param {string} indentation
225+
* @param {boolean} [ignoreFirst=false]
226+
* @returns {string}
227+
*/
162228
function indent(value, indentation, ignoreFirst) {
163229
var lines = value.split('\n')
164230
var index = ignoreFirst ? 0 : -1
@@ -172,11 +238,19 @@ function indent(value, indentation, ignoreFirst) {
172238
return lines.join('\n')
173239
}
174240

175-
// Compile a position.
241+
/**
242+
* @param {Position} value
243+
* @returns {string}
244+
*/
176245
function stringifyPosition(value) {
246+
/** @type {Position} */
247+
// @ts-ignore
177248
var position = value || {}
249+
/** @type {Array.<string>} */
178250
var result = []
251+
/** @type {Array.<string>} */
179252
var positions = []
253+
/** @type {Array.<string>} */
180254
var offsets = []
181255

182256
point(position.start)
@@ -187,6 +261,9 @@ function stringifyPosition(value) {
187261

188262
return result.join(', ')
189263

264+
/**
265+
* @param {Point} value
266+
*/
190267
function point(value) {
191268
if (value) {
192269
positions.push((value.line || 1) + ':' + (value.column || 1))
@@ -198,10 +275,20 @@ function stringifyPosition(value) {
198275
}
199276
}
200277

201-
// Factory to wrap values in ANSI colours.
278+
/**
279+
* Factory to wrap values in ANSI colours.
280+
*
281+
* @param {number} open
282+
* @param {number} close
283+
* @returns {function(string): string}
284+
*/
202285
function ansiColor(open, close) {
203286
return color
204287

288+
/**
289+
* @param {string} value
290+
* @returns {string}
291+
*/
205292
function color(value) {
206293
return '\u001B[' + open + 'm' + value + '\u001B[' + close + 'm'
207294
}

package.json

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,42 +26,50 @@
2626
"sideEffects": false,
2727
"type": "module",
2828
"main": "index.js",
29-
"types": "types/index.d.ts",
3029
"browser": {
3130
"./color.js": "./color-browser.js"
3231
},
3332
"react-native": {
3433
"./color.js": "./color-browser.js"
3534
},
35+
"types": "index.d.ts",
3636
"files": [
37-
"types/index.d.ts",
37+
"color.d.ts",
3838
"color.js",
39+
"color-browser.d.ts",
3940
"color-browser.js",
41+
"index.d.ts",
4042
"index.js"
4143
],
44+
"dependencies": {
45+
"@types/unist": "^2.0.0"
46+
},
4247
"devDependencies": {
43-
"@types/unist": "^2.0.0",
48+
"@types/tape": "^4.0.0",
4449
"c8": "^7.0.0",
4550
"chalk": "^4.0.0",
46-
"dtslint": "^4.0.0",
4751
"hastscript": "^6.0.0",
4852
"prettier": "^2.0.0",
4953
"remark-cli": "^9.0.0",
5054
"remark-preset-wooorm": "^8.0.0",
5155
"retext": "^7.0.0",
56+
"rimraf": "^3.0.0",
5257
"strip-ansi": "^6.0.0",
5358
"tape": "^5.0.0",
59+
"type-coverage": "^2.0.0",
60+
"typescript": "^4.0.0",
5461
"unist-builder": "^2.0.0",
5562
"xast-util-from-xml": "^1.0.0",
5663
"xastscript": "^2.0.0",
5764
"xo": "^0.38.0"
5865
},
5966
"scripts": {
67+
"prepack": "npm run build && npm run format",
68+
"build": "rimraf \"*.d.ts\" && tsc && type-coverage",
6069
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
6170
"test-api": "node test.js",
6271
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js",
63-
"test-types": "dtslint types",
64-
"test": "npm run format && npm run test-coverage && npm run test-types"
72+
"test": "npm run build && npm run format && npm run test-coverage"
6573
},
6674
"prettier": {
6775
"tabWidth": 2,
@@ -84,5 +92,10 @@
8492
"plugins": [
8593
"preset-wooorm"
8694
]
95+
},
96+
"typeCoverage": {
97+
"atLeast": 100,
98+
"detail": true,
99+
"strict": true
87100
}
88101
}

test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import strip from 'strip-ansi'
44
import u from 'unist-builder'
55
import h from 'hastscript'
66
import x from 'xastscript'
7+
// @ts-ignore remove when typed.
78
import retext from 'retext'
9+
// @ts-ignore remove when typed.
810
import fromXml from 'xast-util-from-xml'
911
import {inspect, inspectColor, inspectNoColor} from './index.js'
1012

tsconfig.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"include": ["*.js"],
3+
"compilerOptions": {
4+
"target": "ES2020",
5+
"lib": ["ES2020"],
6+
"module": "ES2020",
7+
"moduleResolution": "node",
8+
"allowJs": true,
9+
"checkJs": true,
10+
"declaration": true,
11+
"emitDeclarationOnly": true,
12+
"allowSyntheticDefaultImports": true,
13+
"skipLibCheck": true
14+
}
15+
}

types/index.d.ts

Lines changed: 0 additions & 45 deletions
This file was deleted.

0 commit comments

Comments
 (0)