From 9139d089d4ccaafd25622ffc12eb57b7354ba484 Mon Sep 17 00:00:00 2001 From: Murderlon Date: Fri, 13 Mar 2020 17:28:53 +0100 Subject: [PATCH 1/3] install tsd, add types, add type tests --- package.json | 11 ++++++++--- types/index.d.ts | 19 +++++++++++++++++++ types/index.test-d.ts | 33 +++++++++++++++++++++++++++++++++ types/tsconfig.json | 10 ++++++++++ 4 files changed, 70 insertions(+), 3 deletions(-) create mode 100644 types/index.d.ts create mode 100644 types/index.test-d.ts create mode 100644 types/tsconfig.json diff --git a/package.json b/package.json index 800e901..28141dd 100644 --- a/package.json +++ b/package.json @@ -21,11 +21,14 @@ }, "author": "Titus Wormer (https://wooorm.com)", "contributors": [ - "Titus Wormer (https://wooorm.com)" + "Titus Wormer (https://wooorm.com)", + "Merlijn Vos " ], "files": [ - "index.js" + "index.js", + "types/index.d.ts" ], + "types": "types/index.d.ts", "dependencies": { "array-iterate": "^1.0.0" }, @@ -37,6 +40,7 @@ "remark-preset-wooorm": "^6.0.0", "tape": "^4.0.0", "tinyify": "^2.0.0", + "tsd": "^0.11.0", "xo": "^0.26.0" }, "scripts": { @@ -46,7 +50,8 @@ "build": "npm run build-bundle && npm run build-mangle", "test-api": "node test", "test-coverage": "nyc --reporter lcov tape test.js", - "test": "npm run format && npm run build && npm run test-coverage" + "test-types": "tsd", + "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..88ae1a3 --- /dev/null +++ b/types/index.d.ts @@ -0,0 +1,19 @@ +// TypeScript Version: 3.5 + +import {Node, Parent} from 'unist' + +declare namespace unistUtilModifyChildren { + type Modifier = ( + node: Node, + index: number, + parent: Parent + ) => number | void + + type Modify = (tree: Node) => undefined +} + +declare function unistUtilModifyChildren( + modifier: unistUtilModifyChildren.Modifier +): unistUtilModifyChildren.Modify + +export = unistUtilModifyChildren diff --git a/types/index.test-d.ts b/types/index.test-d.ts new file mode 100644 index 0000000..bfec2b9 --- /dev/null +++ b/types/index.test-d.ts @@ -0,0 +1,33 @@ +import {expectError, expectType} from 'tsd' +import {Node} from 'unist' + +import * as modifyChildren from '.' + +const node: Node = { + type: 'root', + children: [ + {type: 'leaf', value: '1'}, + {type: 'leaf', children: [{type: 'leaf', value: '2'}]}, + {type: 'leaf', value: '3'} + ] +} + +expectType( + modifyChildren(function(node, index) { + return index + 1 + }) +) + +expectType(modifyChildren(function() {})) + +expectError( + modifyChildren(function() { + return '' + }) +) + +expectType( + modifyChildren(function(node, index) { + return index + 1 + })(node) +) diff --git a/types/tsconfig.json b/types/tsconfig.json new file mode 100644 index 0000000..c09a33a --- /dev/null +++ b/types/tsconfig.json @@ -0,0 +1,10 @@ +{ + "compilerOptions": { + "lib": ["es2015"], + "strict": true, + "baseUrl": ".", + "paths": { + "unist-util-modify-children": ["index.d.ts"] + } + } +} From 6d2cf89df1bdc732506e1a4ec5ab78ba7966d6c2 Mon Sep 17 00:00:00 2001 From: Murderlon Date: Sat, 14 Mar 2020 15:24:13 +0100 Subject: [PATCH 2/3] switch to dtslint, tweak type, add test --- index.js | 2 +- package.json | 5 +-- types/index.d.ts | 2 +- types/index.test-d.ts | 33 ------------------ types/tslint.json | 8 +++++ types/unist-util-modify-children-test.ts | 43 ++++++++++++++++++++++++ 6 files changed, 56 insertions(+), 37 deletions(-) delete mode 100644 types/index.test-d.ts create mode 100644 types/tslint.json create mode 100644 types/unist-util-modify-children-test.ts diff --git a/index.js b/index.js index 6926d44..1718b93 100644 --- a/index.js +++ b/index.js @@ -21,7 +21,7 @@ function iteratorFactory(callback) { throw new Error('Missing children in `parent` for `modifier`') } - return iterate(children, callback, parent) + iterate(children, callback, parent) } } diff --git a/package.json b/package.json index 28141dd..e8b05d8 100644 --- a/package.json +++ b/package.json @@ -34,13 +34,14 @@ }, "devDependencies": { "browserify": "^16.0.0", + "dtslint": "^3.3.0", "nyc": "^15.0.0", "prettier": "^1.0.0", "remark-cli": "^7.0.0", "remark-preset-wooorm": "^6.0.0", "tape": "^4.0.0", "tinyify": "^2.0.0", - "tsd": "^0.11.0", + "unified": "^8.4.2", "xo": "^0.26.0" }, "scripts": { @@ -50,7 +51,7 @@ "build": "npm run build-bundle && npm run build-mangle", "test-api": "node test", "test-coverage": "nyc --reporter lcov tape test.js", - "test-types": "tsd", + "test-types": "dtslint types", "test": "npm run format && npm run build && npm run test-coverage && npm run test-types" }, "prettier": { diff --git a/types/index.d.ts b/types/index.d.ts index 88ae1a3..f188483 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -9,7 +9,7 @@ declare namespace unistUtilModifyChildren { parent: Parent ) => number | void - type Modify = (tree: Node) => undefined + type Modify = (tree: Node) => void } declare function unistUtilModifyChildren( diff --git a/types/index.test-d.ts b/types/index.test-d.ts deleted file mode 100644 index bfec2b9..0000000 --- a/types/index.test-d.ts +++ /dev/null @@ -1,33 +0,0 @@ -import {expectError, expectType} from 'tsd' -import {Node} from 'unist' - -import * as modifyChildren from '.' - -const node: Node = { - type: 'root', - children: [ - {type: 'leaf', value: '1'}, - {type: 'leaf', children: [{type: 'leaf', value: '2'}]}, - {type: 'leaf', value: '3'} - ] -} - -expectType( - modifyChildren(function(node, index) { - return index + 1 - }) -) - -expectType(modifyChildren(function() {})) - -expectError( - modifyChildren(function() { - return '' - }) -) - -expectType( - modifyChildren(function(node, index) { - return index + 1 - })(node) -) diff --git a/types/tslint.json b/types/tslint.json new file mode 100644 index 0000000..9d87fc0 --- /dev/null +++ b/types/tslint.json @@ -0,0 +1,8 @@ + +{ + "extends": "dtslint/dtslint.json", + "rules": { + "whitespace": false, + "semicolon": false + } +} diff --git a/types/unist-util-modify-children-test.ts b/types/unist-util-modify-children-test.ts new file mode 100644 index 0000000..bd39114 --- /dev/null +++ b/types/unist-util-modify-children-test.ts @@ -0,0 +1,43 @@ +import {Node} from 'unist' + +import unified = require('unified') + +import * as modifyChildren from 'unist-util-modify-children' + +const node: Node = { + type: 'root', + children: [ + {type: 'leaf', value: '1'}, + {type: 'leaf', children: [{type: 'leaf', value: '2'}]}, + {type: 'leaf', value: '3'} + ] +} + +// $ExpectType Modify +modifyChildren((node, index) => index + 1) + +// $ExpectType Modify +modifyChildren(() => {}) + +// $ExpectError +modifyChildren(() => '') + +// $ExpectType void +modifyChildren((node, index) => index + 1)(node) + +// Usable in unified transform +unified().use(() => tree => { + const modify = modifyChildren((node, index, parent) => { + if (node.type === 'node') { + parent.children.splice(index, 1, { + type: 'subtree', + children: node.children + }) + return index + 1 + } + }) + + modify(tree) + + return tree +}) From 01d7d637899dc4d8c51ad20a01a42abfc2bc21e5 Mon Sep 17 00:00:00 2001 From: Murderlon Date: Sat, 14 Mar 2020 15:29:02 +0100 Subject: [PATCH 3/3] add docs to index.d.ts --- types/index.d.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/types/index.d.ts b/types/index.d.ts index f188483..ec6473a 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -12,6 +12,12 @@ declare namespace unistUtilModifyChildren { type Modify = (tree: Node) => void } +/** + * unist utility to modify direct children of a parent. + * + * @param callback modifier function that (optionally) returns a next position (number) to iterate. + * @returns callback to be used on the tree. + */ declare function unistUtilModifyChildren( modifier: unistUtilModifyChildren.Modifier ): unistUtilModifyChildren.Modify