Skip to content

Commit 6fbe41d

Browse files
committed
Add JSDoc based types
1 parent 1407d30 commit 6fbe41d

9 files changed

+72
-63
lines changed

.gitignore

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

+28
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
var own = {}.hasOwnProperty
22

3+
/**
4+
* @typedef {import('unist').Node} Node
5+
* @typedef {import('unist').Position} Position
6+
* @typedef {import('unist').Point} Point
7+
*/
8+
9+
/**
10+
* Stringify one point, a position (start and end points), or a node’s
11+
* positional information.
12+
*
13+
* @param {Node|Position|Point} [value]
14+
* @returns {string}
15+
*/
316
export function stringifyPosition(value) {
417
// Nothing.
518
if (!value || typeof value !== 'object') {
@@ -8,31 +21,46 @@ export function stringifyPosition(value) {
821

922
// Node.
1023
if (own.call(value, 'position') || own.call(value, 'type')) {
24+
// @ts-ignore looks like a node.
1125
return position(value.position)
1226
}
1327

1428
// Position.
1529
if (own.call(value, 'start') || own.call(value, 'end')) {
30+
// @ts-ignore looks like a position.
1631
return position(value)
1732
}
1833

1934
// Point.
2035
if (own.call(value, 'line') || own.call(value, 'column')) {
36+
// @ts-ignore looks like a point.
2137
return point(value)
2238
}
2339

2440
// ?
2541
return ''
2642
}
2743

44+
/**
45+
* @param {Point} point
46+
* @returns {string}
47+
*/
2848
function point(point) {
2949
return index(point && point.line) + ':' + index(point && point.column)
3050
}
3151

52+
/**
53+
* @param {Position} pos
54+
* @returns {string}
55+
*/
3256
function position(pos) {
3357
return point(pos && pos.start) + '-' + point(pos && pos.end)
3458
}
3559

60+
/**
61+
* @param {number} value
62+
* @returns {number}
63+
*/
3664
function index(value) {
3765
return value && typeof value === 'number' ? value : 1
3866
}

package.json

+13-8
Original file line numberDiff line numberDiff line change
@@ -28,33 +28,33 @@
2828
"sideEffects": false,
2929
"type": "module",
3030
"main": "index.js",
31-
"types": "types/index.d.ts",
31+
"types": "index.d.ts",
3232
"files": [
33-
"types/index.d.ts",
33+
"index.d.ts",
3434
"index.js"
3535
],
3636
"dependencies": {
3737
"@types/unist": "^2.0.0"
3838
},
3939
"devDependencies": {
40-
"browserify": "^17.0.0",
40+
"@types/tape": "^4.0.0",
4141
"c8": "^7.0.0",
42-
"dtslint": "^4.0.0",
43-
"nyc": "^15.0.0",
4442
"prettier": "^2.0.0",
4543
"remark-cli": "^9.0.0",
4644
"remark-preset-wooorm": "^8.0.0",
45+
"rimraf": "^3.0.0",
4746
"tape": "^5.0.0",
48-
"tinyify": "^3.0.0",
47+
"type-coverage": "^2.0.0",
4948
"typescript": "^4.0.0",
5049
"xo": "^0.38.0"
5150
},
5251
"scripts": {
52+
"prepack": "npm run build && npm run format",
53+
"build": "rimraf \"*.d.ts\" && tsc && type-coverage",
5354
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
5455
"test-api": "node test.js",
5556
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js",
56-
"test-types": "dtslint types",
57-
"test": "npm run format && npm run test-coverage && npm run test-types"
57+
"test": "npm run build && npm run format && npm run test-coverage"
5858
},
5959
"prettier": {
6060
"tabWidth": 2,
@@ -75,5 +75,10 @@
7575
"plugins": [
7676
"preset-wooorm"
7777
]
78+
},
79+
"typeCoverage": {
80+
"atLeast": 100,
81+
"detail": true,
82+
"strict": true
7883
}
7984
}

test.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,23 @@ test('stringifyPosition', function (t) {
1313
'should return empty `string` with `null`'
1414
)
1515
t.equal(
16+
// @ts-ignore runtime.
1617
stringifyPosition('foo'),
1718
'',
1819
'should return empty `string` with `string`'
1920
)
2021
t.equal(
22+
// @ts-ignore runtime.
2123
stringifyPosition(5),
2224
'',
2325
'should return empty `string` with `number`'
2426
)
25-
t.equal(stringifyPosition({}), '', 'should return empty `string` with `{}`')
27+
t.equal(
28+
// @ts-ignore runtime.
29+
stringifyPosition({}),
30+
'',
31+
'should return empty `string` with `{}`'
32+
)
2633

2734
t.equal(
2835
stringifyPosition({type: 'text'}),
@@ -31,6 +38,7 @@ test('stringifyPosition', function (t) {
3138
)
3239

3340
t.equal(
41+
// @ts-ignore runtime.
3442
stringifyPosition({type: 'text', position: 3}),
3543
'1:1-1:1',
3644
'should return a range for `node` with invalid `position` #1'
@@ -39,6 +47,7 @@ test('stringifyPosition', function (t) {
3947
t.equal(
4048
stringifyPosition({
4149
type: 'text',
50+
// @ts-ignore runtime.
4251
position: {start: {}, end: {}}
4352
}),
4453
'1:1-1:1',
@@ -76,12 +85,14 @@ test('stringifyPosition', function (t) {
7685
)
7786

7887
t.equal(
88+
// @ts-ignore runtime.
7989
stringifyPosition({start: 3, end: 6}),
8090
'1:1-1:1',
8191
'should return a range for `position` with invalid `point`s #1'
8292
)
8393

8494
t.equal(
95+
// @ts-ignore runtime.
8596
stringifyPosition({start: {}, end: {}}),
8697
'1:1-1:1',
8798
'should return range for `position` with invalid `point`s #1'
@@ -112,18 +123,21 @@ test('stringifyPosition', function (t) {
112123
)
113124

114125
t.equal(
126+
// @ts-ignore runtime.
115127
stringifyPosition({line: 'foo', column: 'bar'}),
116128
'1:1',
117129
'should return a point for a `point` with invalid indices #1'
118130
)
119131

120132
t.equal(
133+
// @ts-ignore runtime.
121134
stringifyPosition({line: 4}),
122135
'4:1',
123136
'should return a point for a partially valid `point` #1'
124137
)
125138

126139
t.equal(
140+
// @ts-ignore runtime.
127141
stringifyPosition({column: 12}),
128142
'1:12',
129143
'should return a point for a partially valid `point` #1'

tsconfig.json

+15
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

-9
This file was deleted.

types/tsconfig.json

-10
This file was deleted.

types/tslint.json

-15
This file was deleted.

types/unist-util-stringify-position-tests.ts

-20
This file was deleted.

0 commit comments

Comments
 (0)