diff --git a/index.js b/index.js index a458a86..6b53947 100644 --- a/index.js +++ b/index.js @@ -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 @@ -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]) } } @@ -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') } } @@ -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) } diff --git a/package.json b/package.json index 5686397..9b2f9a1 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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, diff --git a/types/index.d.ts b/types/index.d.ts new file mode 100644 index 0000000..590b0f0 --- /dev/null +++ b/types/index.d.ts @@ -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 diff --git a/types/tsconfig.json b/types/tsconfig.json new file mode 100644 index 0000000..6c5f15b --- /dev/null +++ b/types/tsconfig.json @@ -0,0 +1,10 @@ +{ + "compilerOptions": { + "lib": ["es2015"], + "strict": true, + "baseUrl": ".", + "paths": { + "unist-util-assert": ["index.d.ts"] + } + } +} diff --git a/types/tslint.json b/types/tslint.json new file mode 100644 index 0000000..70c4494 --- /dev/null +++ b/types/tslint.json @@ -0,0 +1,7 @@ +{ + "extends": "dtslint/dtslint.json", + "rules": { + "semicolon": false, + "whitespace": false + } +} diff --git a/types/unist-util-assert-test.ts b/types/unist-util-assert-test.ts new file mode 100644 index 0000000..4ca0e8a --- /dev/null +++ b/types/unist-util-assert-test.ts @@ -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 +}