Skip to content

Add types #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var inspect
try {
// eslint-disable-next-line no-useless-concat
inspect = require('ut' + 'il').inspect
} catch (error) {}
} catch (_) {}

exports = wrap(unist)
module.exports = exports
Expand Down Expand Up @@ -75,7 +75,7 @@ function unist(node) {
position(node.position)

for (key in node) {
if (defined.indexOf(key) === -1) {
if (!defined.includes(key)) {
vanilla(key, node[key])
}
}
Expand All @@ -96,7 +96,7 @@ function unist(node) {
function vanilla(key, value) {
try {
assert.deepStrictEqual(value, JSON.parse(JSON.stringify(value)))
} catch (error) {
} catch (_) {
assert.fail('non-specced property `' + key + '` should be JSON')
}
}
Expand All @@ -113,7 +113,7 @@ function view(value) {
return JSON.stringify(value)
}
/* eslint-enable no-else-return */
} catch (error) {
} catch (_) {
/* istanbul ignore next - Cyclical. */
return String(value)
}
Expand Down
14 changes: 9 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,20 @@
"x-is-object": "^0.1.0"
},
"files": [
"index.js"
"index.js",
"types/index.d.ts"
],
"types": "types/index.d.ts",
"devDependencies": {
"browserify": "^16.0.0",
"dtslint": "^2.0.0",
"nyc": "^14.0.0",
"prettier": "^1.0.0",
"remark-cli": "^6.0.0",
"remark-preset-wooorm": "^5.0.0",
"remark-cli": "^7.0.0",
"remark-preset-wooorm": "^6.0.0",
"tape": "^4.0.0",
"tinyify": "^2.0.0",
"xo": "^0.24.0"
"xo": "^0.25.0"
},
"scripts": {
"format": "remark . -qfo && prettier --write \"**/*.js\" && xo --fix",
Expand All @@ -39,7 +42,8 @@
"build": "npm run build-bundle && npm run build-mangle",
"test-api": "node test",
"test-coverage": "nyc --reporter lcov tape test",
"test": "npm run format && npm run build && npm run test-coverage"
"test-types": "dtslint types",
"test": "npm run format && npm run build && npm run test-coverage && npm run test-types"
},
"prettier": {
"tabWidth": 2,
Expand Down
39 changes: 39 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// TypeScript Version: 3.7

import {Node, Parent, Literal} from 'unist'

declare namespace unistUtilAssert {
/**
* A unist Node that is neither a Parent nor a Literal.
*/
interface Void extends Node {
children: never
value: never
}
}

declare const unistUtilAssert: {
/**
* Assert that tree is a valid unist node.
* If tree is a parent, all children will be asserted as well.
*/
(tree: unknown): asserts tree is Node

/**
* Assert that tree is a valid unist parent.
* All children will be asserted as well.
*/
parent(tree: unknown): asserts tree is Parent

/**
* Assert that node is a valid unist literal.
*/
text(tree: unknown): asserts tree is Literal

/**
* Assert that node is a valid unist node, but neither parent nor literal.
*/
void(tree: unknown): asserts tree is unistUtilAssert.Void
}

export = unistUtilAssert
10 changes: 10 additions & 0 deletions types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"lib": ["es2015"],
"strict": true,
"baseUrl": ".",
"paths": {
"unist-util-assert": ["index.d.ts"]
}
}
}
7 changes: 7 additions & 0 deletions types/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "dtslint/dtslint.json",
"rules": {
"semicolon": false,
"whitespace": false
}
}
25 changes: 25 additions & 0 deletions types/unist-util-assert-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import * as assert from 'unist-util-assert'

function testAssert() {
const node = {}
assert(node)
node // $ExpectType Node
}

function testParentAssert() {
const node = {}
assert.parent(node)
node // $ExpectType Parent
}

function testTextAssert() {
const node = {}
assert.text(node)
node // $ExpectType Literal
}

function testVoidAssert() {
const node = {}
assert.void(node)
node // $ExpectType Void
}