From 68e9e256f7e29de3d20c88d7f29a27a2ad8ed0ad Mon Sep 17 00:00:00 2001 From: Christian Murphy Date: Mon, 4 Nov 2019 09:05:12 -0700 Subject: [PATCH 1/3] types: add typings to unist-util-assert --- index.js | 8 ++++---- package.json | 14 +++++++++----- types/index.d.ts | 24 ++++++++++++++++++++++++ types/tsconfig.json | 10 ++++++++++ types/tslint.json | 7 +++++++ types/unist-util-assert-test.ts | 9 +++++++++ 6 files changed, 63 insertions(+), 9 deletions(-) create mode 100644 types/index.d.ts create mode 100644 types/tsconfig.json create mode 100644 types/tslint.json create mode 100644 types/unist-util-assert-test.ts 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..86d7191 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": "^1.0.2", "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..338d5b8 --- /dev/null +++ b/types/index.d.ts @@ -0,0 +1,24 @@ +// TypeScript Version: 3.7 + +import {Node, Parent, Literal} from 'unist' + +declare namespace unistUtilAssert { + interface VoidNode extends Node { + children: never + value: never + } + + function parent(tree: unknown): asserts tree is Parent + + function text(tree: unknown): asserts tree is Literal + + // FIXME: void is a reserved functio name in TS + // find a way that void can be included in the typing without errors + // function void(tree: unknown): asserts tree is VoidNode +} + +declare function unistUtilAssert( + tree: unknown +): asserts tree is Node + +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..9b0cb07 --- /dev/null +++ b/types/unist-util-assert-test.ts @@ -0,0 +1,9 @@ +import {Node, Parent, Literal} from 'unist' +import unified = require('unified') +import assert = require('unist-util-assert') + +function testAssert() { + const node = {} + assert(node) + node +} From fa8d7eb785e78ba537f5d0a1e5edbfe6f7098aaa Mon Sep 17 00:00:00 2001 From: Christian Murphy Date: Tue, 12 Nov 2019 14:52:30 -0700 Subject: [PATCH 2/3] types: upgrade dtslint to support ts 3.7, support void assertion --- package.json | 2 +- types/index.d.ts | 33 ++++++++++++++++++++++++--------- types/unist-util-assert-test.ts | 24 ++++++++++++++++++++---- 3 files changed, 45 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index 86d7191..9b2f9a1 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,7 @@ "types": "types/index.d.ts", "devDependencies": { "browserify": "^16.0.0", - "dtslint": "^1.0.2", + "dtslint": "^2.0.0", "nyc": "^14.0.0", "prettier": "^1.0.0", "remark-cli": "^7.0.0", diff --git a/types/index.d.ts b/types/index.d.ts index 338d5b8..06857ed 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -3,22 +3,37 @@ import {Node, Parent, Literal} from 'unist' declare namespace unistUtilAssert { + /** + * A unist Node that is neither a Parent nor a Literal. + */ interface VoidNode extends Node { children: never value: never } +} - function parent(tree: unknown): asserts tree is Parent +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 - function text(tree: unknown): asserts tree is Literal + /** + * Assert that tree is a valid unist parent. + * All children will be asserted as well. + */ + parent(tree: unknown): asserts tree is Parent - // FIXME: void is a reserved functio name in TS - // find a way that void can be included in the typing without errors - // function void(tree: unknown): asserts tree is VoidNode -} + /** + * Assert that node is a valid unist literal. + */ + text(tree: unknown): asserts tree is Literal -declare function unistUtilAssert( - tree: unknown -): asserts tree is Node + /** + * Assert that node is a valid unist node, but neither parent nor literal. + */ + void(tree: unknown): asserts tree is unistUtilAssert.VoidNode +} export = unistUtilAssert diff --git a/types/unist-util-assert-test.ts b/types/unist-util-assert-test.ts index 9b0cb07..dc46153 100644 --- a/types/unist-util-assert-test.ts +++ b/types/unist-util-assert-test.ts @@ -1,9 +1,25 @@ -import {Node, Parent, Literal} from 'unist' -import unified = require('unified') -import assert = require('unist-util-assert') +import * as assert from 'unist-util-assert' function testAssert() { const node = {} assert(node) - 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 VoidNode } From a73160efe72b334341d197dd1ae9cf415d355d86 Mon Sep 17 00:00:00 2001 From: Christian Murphy Date: Wed, 13 Nov 2019 08:19:41 -0700 Subject: [PATCH 3/3] refactor: rename VoidNode to Void --- types/index.d.ts | 4 ++-- types/unist-util-assert-test.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/types/index.d.ts b/types/index.d.ts index 06857ed..590b0f0 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -6,7 +6,7 @@ declare namespace unistUtilAssert { /** * A unist Node that is neither a Parent nor a Literal. */ - interface VoidNode extends Node { + interface Void extends Node { children: never value: never } @@ -33,7 +33,7 @@ declare const unistUtilAssert: { /** * Assert that node is a valid unist node, but neither parent nor literal. */ - void(tree: unknown): asserts tree is unistUtilAssert.VoidNode + void(tree: unknown): asserts tree is unistUtilAssert.Void } export = unistUtilAssert diff --git a/types/unist-util-assert-test.ts b/types/unist-util-assert-test.ts index dc46153..4ca0e8a 100644 --- a/types/unist-util-assert-test.ts +++ b/types/unist-util-assert-test.ts @@ -21,5 +21,5 @@ function testTextAssert() { function testVoidAssert() { const node = {} assert.void(node) - node // $ExpectType VoidNode + node // $ExpectType Void }