Skip to content

Commit c7295f1

Browse files
committed
Add JSDoc based types
1 parent 9eff1e9 commit c7295f1

File tree

6 files changed

+66
-8
lines changed

6 files changed

+66
-8
lines changed

.gitignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
.DS_Store
2+
*.d.ts
3+
*.log
14
coverage/
25
node_modules/
3-
*.log
4-
.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: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,50 @@
1+
/**
2+
* @typedef {import('unist').Node} UnistNode
3+
* @typedef {import('estree-jsx').Node} EstreeNode
4+
* @typedef {import('estree-util-visit').Visitor} Visitor
5+
*
6+
* @typedef Options
7+
* @property {boolean} [dirty=false]
8+
*/
9+
110
import {positionFromEstree} from 'unist-util-position-from-estree'
211
import {visit} from 'estree-util-visit'
312

413
var own = {}.hasOwnProperty
514

15+
/**
16+
* @param {EstreeNode} estree
17+
* @param {Options} [options]
18+
* @returns {UnistNode}
19+
*/
620
export function fromEstree(estree, options = {}) {
21+
/** @type {UnistNode} */
722
var tail
823

924
visit(estree, {leave: onleave})
1025

1126
return tail
1227

28+
/**
29+
* @type {Visitor}
30+
* @param {EstreeNode} node
31+
*/
1332
// eslint-disable-next-line complexity
1433
function onleave(node, field, index, parents) {
1534
var parent = parents[parents.length - 1]
35+
/** @type {EstreeNode} */
1636
var context = index === null ? parent : parent[field]
37+
/** @type {string|number} */
1738
var prop = index === null ? field : index
39+
/** @type {UnistNode} */
1840
var copy = {}
41+
/** @type {string} */
1942
var key
43+
/** @type {RegExpMatchArray} */
2044
var match
45+
/** @type {number} */
2146
var code
47+
/** @type {unknown} */
2248
var value
2349

2450
for (key in node) {

package.json

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,30 @@
3434
"index.js"
3535
],
3636
"dependencies": {
37+
"@types/estree-jsx": "^0.0.1",
3738
"estree-util-visit": "^1.0.0",
3839
"unist-util-position-from-estree": "^1.0.0"
3940
},
4041
"devDependencies": {
42+
"@types/tape": "^4.0.0",
4143
"acorn": "^8.0.0",
4244
"c8": "^7.0.0",
4345
"prettier": "^2.0.0",
4446
"remark-cli": "^9.0.0",
4547
"remark-preset-wooorm": "^8.0.0",
48+
"rimraf": "^3.0.0",
4649
"tape": "^5.0.0",
50+
"type-coverage": "^2.0.0",
51+
"typescript": "^4.0.0",
4752
"xo": "^0.39.0"
4853
},
4954
"scripts": {
55+
"prepack": "npm run build && npm run format",
56+
"build": "rimraf \"*.d.ts\" && tsc && type-coverage",
5057
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
5158
"test-api": "node test.js",
5259
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js",
53-
"test": "npm run format && npm run test-coverage"
60+
"test": "npm run build && npm run format && npm run test-coverage"
5461
},
5562
"prettier": {
5663
"tabWidth": 2,
@@ -71,5 +78,10 @@
7178
"plugins": [
7279
"preset-wooorm"
7380
]
81+
},
82+
"typeCoverage": {
83+
"atLeast": 100,
84+
"detail": true,
85+
"strict": true
7486
}
7587
}

test.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import {fromEstree} from './index.js'
66

77
test('esast-util-from-estree', function (t) {
88
t.deepEqual(
9-
fromEstree(parse('console.log(1)', {locations: true})),
9+
// @ts-ignore Similar enough.
10+
fromEstree(parse('console.log(1)', {locations: true, ecmaVersion: 2021})),
1011
{
1112
type: 'Program',
1213
body: [
@@ -71,7 +72,10 @@ test('esast-util-from-estree', function (t) {
7172
)
7273

7374
t.deepEqual(
74-
fromEstree(parse('/(?:)/', {locations: true}).body[0].expression),
75+
fromEstree(
76+
// @ts-ignore Hush, it’s fine.
77+
parse('/(?:)/', {locations: true, ecmaVersion: 2021}).body[0].expression
78+
),
7579
{
7680
type: 'Literal',
7781
value: null,
@@ -97,8 +101,9 @@ test('esast-util-from-estree', function (t) {
97101

98102
while (++index < bigInts.length) {
99103
t.deepEqual(
100-
fromEstree(parse(bigInts[index][0], {locations: true})).body[0].expression
101-
.bigint,
104+
// @ts-ignore Hush, it’s fine.
105+
fromEstree(parse(bigInts[index][0], {locations: true, ecmaVersion: 2021}))
106+
.body[0].expression.bigint,
102107
'1',
103108
'should transform and normalize bigints (`' + bigInts[index][1] + '`)'
104109
)

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)