Skip to content

Commit 86d8534

Browse files
ChristianMurphywooorm
authored andcommitted
Add types
Closes GH-1. Reviewed-by: Junyoung Choi <fluke8259@gmail.com> Reviewed-by: Titus Wormer <tituswormer@gmail.com>
1 parent b93dba5 commit 86d8534

File tree

6 files changed

+94
-9
lines changed

6 files changed

+94
-9
lines changed

index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ var inspect
99
try {
1010
// eslint-disable-next-line no-useless-concat
1111
inspect = require('ut' + 'il').inspect
12-
} catch (error) {}
12+
} catch (_) {}
1313

1414
exports = wrap(unist)
1515
module.exports = exports
@@ -75,7 +75,7 @@ function unist(node) {
7575
position(node.position)
7676

7777
for (key in node) {
78-
if (defined.indexOf(key) === -1) {
78+
if (!defined.includes(key)) {
7979
vanilla(key, node[key])
8080
}
8181
}
@@ -96,7 +96,7 @@ function unist(node) {
9696
function vanilla(key, value) {
9797
try {
9898
assert.deepStrictEqual(value, JSON.parse(JSON.stringify(value)))
99-
} catch (error) {
99+
} catch (_) {
100100
assert.fail('non-specced property `' + key + '` should be JSON')
101101
}
102102
}
@@ -113,7 +113,7 @@ function view(value) {
113113
return JSON.stringify(value)
114114
}
115115
/* eslint-enable no-else-return */
116-
} catch (error) {
116+
} catch (_) {
117117
/* istanbul ignore next - Cyclical. */
118118
return String(value)
119119
}

package.json

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,20 @@
2020
"x-is-object": "^0.1.0"
2121
},
2222
"files": [
23-
"index.js"
23+
"index.js",
24+
"types/index.d.ts"
2425
],
26+
"types": "types/index.d.ts",
2527
"devDependencies": {
2628
"browserify": "^16.0.0",
29+
"dtslint": "^2.0.0",
2730
"nyc": "^14.0.0",
2831
"prettier": "^1.0.0",
29-
"remark-cli": "^6.0.0",
30-
"remark-preset-wooorm": "^5.0.0",
32+
"remark-cli": "^7.0.0",
33+
"remark-preset-wooorm": "^6.0.0",
3134
"tape": "^4.0.0",
3235
"tinyify": "^2.0.0",
33-
"xo": "^0.24.0"
36+
"xo": "^0.25.0"
3437
},
3538
"scripts": {
3639
"format": "remark . -qfo && prettier --write \"**/*.js\" && xo --fix",
@@ -39,7 +42,8 @@
3942
"build": "npm run build-bundle && npm run build-mangle",
4043
"test-api": "node test",
4144
"test-coverage": "nyc --reporter lcov tape test",
42-
"test": "npm run format && npm run build && npm run test-coverage"
45+
"test-types": "dtslint types",
46+
"test": "npm run format && npm run build && npm run test-coverage && npm run test-types"
4347
},
4448
"prettier": {
4549
"tabWidth": 2,

types/index.d.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// TypeScript Version: 3.7
2+
3+
import {Node, Parent, Literal} from 'unist'
4+
5+
declare namespace unistUtilAssert {
6+
/**
7+
* A unist Node that is neither a Parent nor a Literal.
8+
*/
9+
interface Void extends Node {
10+
children: never
11+
value: never
12+
}
13+
}
14+
15+
declare const unistUtilAssert: {
16+
/**
17+
* Assert that tree is a valid unist node.
18+
* If tree is a parent, all children will be asserted as well.
19+
*/
20+
(tree: unknown): asserts tree is Node
21+
22+
/**
23+
* Assert that tree is a valid unist parent.
24+
* All children will be asserted as well.
25+
*/
26+
parent(tree: unknown): asserts tree is Parent
27+
28+
/**
29+
* Assert that node is a valid unist literal.
30+
*/
31+
text(tree: unknown): asserts tree is Literal
32+
33+
/**
34+
* Assert that node is a valid unist node, but neither parent nor literal.
35+
*/
36+
void(tree: unknown): asserts tree is unistUtilAssert.Void
37+
}
38+
39+
export = unistUtilAssert

types/tsconfig.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"compilerOptions": {
3+
"lib": ["es2015"],
4+
"strict": true,
5+
"baseUrl": ".",
6+
"paths": {
7+
"unist-util-assert": ["index.d.ts"]
8+
}
9+
}
10+
}

types/tslint.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "dtslint/dtslint.json",
3+
"rules": {
4+
"semicolon": false,
5+
"whitespace": false
6+
}
7+
}

types/unist-util-assert-test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import * as assert from 'unist-util-assert'
2+
3+
function testAssert() {
4+
const node = {}
5+
assert(node)
6+
node // $ExpectType Node
7+
}
8+
9+
function testParentAssert() {
10+
const node = {}
11+
assert.parent(node)
12+
node // $ExpectType Parent
13+
}
14+
15+
function testTextAssert() {
16+
const node = {}
17+
assert.text(node)
18+
node // $ExpectType Literal
19+
}
20+
21+
function testVoidAssert() {
22+
const node = {}
23+
assert.void(node)
24+
node // $ExpectType Void
25+
}

0 commit comments

Comments
 (0)