From 66e36eaf79cf87539069ca10ced2c2d4fed53ed5 Mon Sep 17 00:00:00 2001 From: Christian Murphy Date: Mon, 28 Oct 2019 01:02:58 -0700 Subject: [PATCH] Add types Closes GH-1. Reviewed-by: Titus Wormer --- package.json | 13 +++++++++--- types/index.d.ts | 19 ++++++++++++++++++ types/tsconfig.json | 10 ++++++++++ types/tslint.json | 7 +++++++ types/unist-util-map-tests.ts | 37 +++++++++++++++++++++++++++++++++++ 5 files changed, 83 insertions(+), 3 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-map-tests.ts diff --git a/package.json b/package.json index e5923c1..addee2d 100644 --- a/package.json +++ b/package.json @@ -15,28 +15,35 @@ "author": "azu ", "contributors": [ "azu ", - "Titus Wormer (https://wooorm.com)" + "Titus Wormer (https://wooorm.com)", + "Christian Murphy " ], "files": [ - "index.js" + "index.js", + "types/index.d.ts" ], + "types": "types/index.d.ts", "dependencies": { + "@types/mdast": "^3.0.3", "object-assign": "^4.0.1" }, "devDependencies": { + "dtslint": "^0.9.9", "nyc": "^14.0.0", "prettier": "^1.0.0", "remark-cli": "^6.0.0", "remark-preset-wooorm": "^5.0.0", "tape": "^4.10.1", "unist-builder": "^1.0.3", + "unist-util-is": "^4.0.0", "xo": "^0.24.0" }, "scripts": { "format": "remark . -qfo && prettier --write \"**/*.js\" && xo --fix", "test-api": "node test", "test-coverage": "nyc --reporter lcov tape test.js", - "test": "npm run format && npm run test-coverage" + "test-types": "dtslint types", + "test": "npm run format && npm run test-coverage && npm run test-types" }, "nyc": { "check-coverage": true, diff --git a/types/index.d.ts b/types/index.d.ts new file mode 100644 index 0000000..45a0593 --- /dev/null +++ b/types/index.d.ts @@ -0,0 +1,19 @@ +// TypeScript Version: 3.5 + +import {Node} from 'unist' + +declare namespace unistUtilMap { + type MapFunction = (node: Node, index: number | null, parent: Node | null) => Node +} + +/** + * unist utility to create a new tree by mapping all nodes with the given function. + * + * @param tree Tree to map + * @param callback Function that returns a new node + * @returns Node to be used in the new tree. + * Its children are not used: if the original node has children, those are mapped. + */ +declare function unistUtilMap(tree: Node, callback: unistUtilMap.MapFunction): Node + +export = unistUtilMap diff --git a/types/tsconfig.json b/types/tsconfig.json new file mode 100644 index 0000000..8644302 --- /dev/null +++ b/types/tsconfig.json @@ -0,0 +1,10 @@ +{ + "compilerOptions": { + "lib": ["es2015"], + "strict": true, + "baseUrl": ".", + "paths": { + "unist-util-map": ["index.d.ts"] + } + } +} diff --git a/types/tslint.json b/types/tslint.json new file mode 100644 index 0000000..6978386 --- /dev/null +++ b/types/tslint.json @@ -0,0 +1,7 @@ +{ + "extends": "dtslint/dtslint.json", + "rules": { + "whitespace": false, + "semicolon": false + } +} diff --git a/types/unist-util-map-tests.ts b/types/unist-util-map-tests.ts new file mode 100644 index 0000000..2f3e1a2 --- /dev/null +++ b/types/unist-util-map-tests.ts @@ -0,0 +1,37 @@ +import * as map from 'unist-util-map' +import * as is from 'unist-util-is' +import {Node} from 'unist' +import {Heading, Paragraph} from 'mdast' + +// $ExpectType Node +map({type: 'root'}, (node: Node) => ({type: node.type + 'test'})) +// $ExpectType Node +map({type: 'root'}, (node: Node, index: number | null) => ({ + type: node.type + 'test' +})) +// $ExpectType Node +map( + {type: 'root'}, + (node: Node, index: number | null, parent: Node | null) => ({ + type: node.type + 'test' + }) +) +// $ExpectType Node +map({type: 'root'}, (node: Node) => { + if (is(node, 'heading')) { + const depth = node.depth < 5 ? node.depth + 1 : 6 + return { + ...node, + depth + } + } + + if (is(node, 'paragraph')) { + return { + type: 'strong', + children: node.children + } + } + + return node +})