Skip to content

Commit c0a3779

Browse files
committed
Add JSDoc based types
1 parent cb11dcd commit c0a3779

File tree

5 files changed

+44
-3
lines changed

5 files changed

+44
-3
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

+10
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,24 @@ import location from 'vfile-location'
22

33
var search = /\r?\n|\r/g
44

5+
/**
6+
* @param {import('unist').Node|import('unist').Position} value Value to get
7+
* @param {import('vfile').VFile|string} file File in which `value` exists
8+
* @returns {string|null}
9+
*/
510
export function source(value, file) {
611
var doc = String(file)
712
var loc = location(file)
13+
/** @type {import('unist').Position} */
14+
// @ts-ignore Looks like a node.
815
var position = (value && value.position) || value || {}
916
var startOffset = loc.toOffset(position.start)
1017
var endOffset = loc.toOffset(position.end)
18+
/** @type {Array.<string>} */
1119
var results = []
20+
/** @type {RegExpMatchArray} */
1221
var match
22+
/** @type {number} */
1323
var end
1424

1525
if (startOffset === -1 || endOffset === -1) {

package.json

+16-1
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,36 @@
2525
"sideEffects": false,
2626
"type": "module",
2727
"main": "index.js",
28+
"types": "index.d.ts",
2829
"files": [
30+
"index.d.ts",
2931
"index.js"
3032
],
3133
"dependencies": {
34+
"@types/unist": "^2.0.0",
35+
"vfile": "^4.0.0",
3236
"vfile-location": "^3.0.0"
3337
},
3438
"devDependencies": {
39+
"@types/tape": "^4.0.0",
3540
"c8": "^7.0.0",
3641
"prettier": "^2.0.0",
3742
"remark": "^13.0.0",
3843
"remark-cli": "^9.0.0",
3944
"remark-preset-wooorm": "^8.0.0",
45+
"rimraf": "^3.0.0",
4046
"tape": "^5.0.0",
47+
"type-coverage": "^2.0.0",
48+
"typescript": "^4.0.0",
4149
"xo": "^0.39.0"
4250
},
4351
"scripts": {
52+
"prepack": "npm run build && npm run format",
53+
"build": "rimraf \"*.d.ts\" && tsc && type-coverage",
4454
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
4555
"test-api": "node test.js",
4656
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js",
47-
"test": "npm run format && npm run test-coverage"
57+
"test": "npm run build && npm run format && npm run test-coverage"
4858
},
4959
"prettier": {
5060
"tabWidth": 2,
@@ -65,5 +75,10 @@
6575
"plugins": [
6676
"preset-wooorm"
6777
]
78+
},
79+
"typeCoverage": {
80+
"atLeast": 100,
81+
"detail": true,
82+
"strict": true
6883
}
6984
}

readme.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,15 @@ Now, running `node example` yields:
5555

5656
## API
5757

58-
### `source(value, doc)`
58+
### `source(value, file)`
5959

6060
This package exports the following identifiers: `source`.
6161
There is no default export.
6262

6363
###### Parameters
6464

6565
* `value` ([`Node`][node] or [`Position`][position]) — Value to get
66-
* `doc` ([`VFile`][vfile] or `string`) — [file][] in which `value` exists
66+
* `file` ([`VFile`][vfile] or `string`) — [file][] in which `value` exists
6767

6868
###### Returns
6969

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

0 commit comments

Comments
 (0)