Skip to content

Commit 606e7e8

Browse files
committed
Add JSDoc based types
1 parent 9249ae4 commit 606e7e8

File tree

6 files changed

+68
-7
lines changed

6 files changed

+68
-7
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
coverage/
22
node_modules/
3+
*.d.ts
34
*.log
45
.DS_Store
56
yarn.lock

.prettierignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
coverage/
2-
*.json
32
*.md

index.js

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,37 @@
1+
/**
2+
* @typedef {import('unist').Position} Position
3+
*
4+
* @typedef {[number, number]} RangeLike
5+
*
6+
* @typedef {Object} PointLike
7+
* @property {number} [line]
8+
* @property {number} [column]
9+
*
10+
* @typedef {Object} LocLike
11+
* @property {PointLike} [start]
12+
* @property {PointLike} [end]
13+
*
14+
* @typedef {Object} NodeLike
15+
* @property {LocLike} [loc]
16+
* @property {RangeLike} [range]
17+
* @property {number} [start]
18+
* @property {number} [end]
19+
*/
20+
21+
/**
22+
* Given an estree `node`, returns a unist `position`.
23+
* @param {NodeLike} [value]
24+
* @returns {Position}
25+
*/
126
export function positionFromEstree(value) {
27+
/** @type {NodeLike} */
228
var node = value || {}
29+
/** @type {LocLike} */
330
var loc = node.loc || {}
4-
var range = node.range || []
31+
/** @type {RangeLike} */
32+
var range = node.range || [0, 0]
533
var startOffset = range[0] || node.start
34+
var endOffset = range[1] || node.end
635

736
return {
837
start: {
@@ -13,7 +42,7 @@ export function positionFromEstree(value) {
1342
end: {
1443
line: loc.end && loc.end.line > -1 ? loc.end.line : null,
1544
column: loc.end && loc.end.column > -1 ? loc.end.column + 1 : null,
16-
offset: range[1] || node.end || null
45+
offset: endOffset > -1 ? endOffset : null
1746
}
1847
}
1948
}

package.json

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,35 @@
2929
"sideEffects": false,
3030
"type": "module",
3131
"main": "index.js",
32+
"types": "index.d.ts",
3233
"files": [
34+
"index.d.ts",
3335
"index.js"
3436
],
37+
"dependencies": {
38+
"@types/unist": "^2.0.0"
39+
},
3540
"devDependencies": {
41+
"@types/acorn": "^4.0.0",
42+
"@types/tape": "^4.0.0",
3643
"acorn": "^8.0.0",
3744
"c8": "^7.0.0",
3845
"prettier": "^2.0.0",
3946
"remark-cli": "^9.0.0",
4047
"remark-preset-wooorm": "^8.0.0",
48+
"rimraf": "^3.0.0",
4149
"tape": "^5.0.0",
50+
"type-coverage": "^2.0.0",
51+
"typescript": "^4.0.0",
4252
"xo": "^0.38.0"
4353
},
4454
"scripts": {
55+
"prepack": "npm run build && npm run format",
56+
"build": "rimraf \"*.d.ts\" && tsc && type-coverage",
4557
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
4658
"test-api": "node test",
4759
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node --experimental-modules test.js",
48-
"test": "npm run format && npm run test-coverage"
60+
"test": "npm run build && npm run format && npm run test-coverage"
4961
},
5062
"prettier": {
5163
"tabWidth": 2,
@@ -66,5 +78,10 @@
6678
"plugins": [
6779
"preset-wooorm"
6880
]
81+
},
82+
"typeCoverage": {
83+
"atLeast": 100,
84+
"detail": true,
85+
"strict": true
6986
}
7087
}

test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ test('unist-util-position-from-estree', function (t) {
1515
)
1616

1717
t.deepEqual(
18-
positionFromEstree(parse('x')),
18+
positionFromEstree(parse('x', {ecmaVersion: 2020})),
1919
{
2020
start: {line: null, column: null, offset: 0},
2121
end: {line: null, column: null, offset: 1}
@@ -24,7 +24,7 @@ test('unist-util-position-from-estree', function (t) {
2424
)
2525

2626
t.deepEqual(
27-
positionFromEstree(parse('x', {locations: true})),
27+
positionFromEstree(parse('x', {ecmaVersion: 2020, locations: true})),
2828
{
2929
start: {line: 1, column: 1, offset: 0},
3030
end: {line: 1, column: 2, offset: 1}
@@ -33,7 +33,7 @@ test('unist-util-position-from-estree', function (t) {
3333
)
3434

3535
t.deepEqual(
36-
positionFromEstree(parse('x', {ranges: true})),
36+
positionFromEstree(parse('x', {ecmaVersion: 2020, ranges: true})),
3737
{
3838
start: {line: null, column: null, offset: 0},
3939
end: {line: null, column: null, offset: 1}

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

0 commit comments

Comments
 (0)