Skip to content
This repository was archived by the owner on Aug 9, 2023. It is now read-only.

Commit 22c7855

Browse files
Add types
Closes GH-24. Reviewed-by: Junyoung Choi <fluke8259@gmail.com> Reviewed-by: Titus Wormer <tituswormer@gmail.com>
1 parent 89e9e08 commit 22c7855

File tree

5 files changed

+126
-4
lines changed

5 files changed

+126
-4
lines changed

package.json

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,16 @@
2929
"contributors": [
3030
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)",
3131
"Jannis Redmann <mail@jannisr.de>",
32-
"Koto Hajime <toxictoxer@gmail.com>"
32+
"Koto Hajime <toxictoxer@gmail.com>",
33+
"Christian Murphy <christian.murphy.42@gmail.com>"
3334
],
3435
"files": [
35-
"index.js"
36+
"index.js",
37+
"types/index.d.ts"
3638
],
39+
"types": "types/index.d.ts",
3740
"dependencies": {
41+
"@types/unist": "^2.0.3",
3842
"comma-separated-tokens": "^1.0.0",
3943
"property-information": "^5.3.0",
4044
"space-separated-tokens": "^1.0.0",
@@ -43,7 +47,11 @@
4347
"web-namespaces": "^1.0.0"
4448
},
4549
"devDependencies": {
50+
"@types/hyperscript": "0.0.4",
51+
"@types/react": "^16.0.0",
52+
"@types/virtual-dom": "^2.0.0",
4653
"browserify": "^16.0.0",
54+
"dtslint": "^3.6.2",
4755
"hyperscript": "^2.0.0",
4856
"nyc": "^15.0.0",
4957
"prettier": "^2.0.0",
@@ -62,13 +70,14 @@
6270
"xo": "^0.30.0"
6371
},
6472
"scripts": {
65-
"format": "remark . -qfo && prettier --write \"**/*.js\" && xo --fix",
73+
"format": "remark . -qfo && prettier --write \"**/*.{js,ts}\" && xo --fix",
6674
"build-bundle": "browserify index.js -s hastToHyperscript > hast-to-hyperscript.js",
6775
"build-mangle": "browserify index.js -s hastToHyperscript -p tinyify > hast-to-hyperscript.min.js",
6876
"build": "npm run build-bundle && npm run build-mangle",
6977
"test-api": "node test",
7078
"test-coverage": "nyc --reporter lcov tape test.js",
71-
"test": "npm run format && npm run build && npm run test-coverage"
79+
"test-types": "dtslint types",
80+
"test": "npm run format && npm run build && npm run test-coverage && npm run test-types"
7281
},
7382
"nyc": {
7483
"check-coverage": true,
@@ -90,6 +99,7 @@
9099
"rules": {
91100
"unicorn/no-fn-reference-in-iterator": "off",
92101
"unicorn/prefer-type-error": "off",
102+
"@typescript-eslint/prefer-readonly-parameter-types": "off",
93103
"guard-for-in": "off",
94104
"no-self-compare": "off",
95105
"complexity": "off"

types/hast-to-hyperscript-test.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import * as hastToHyperScript from 'hast-to-hyperscript'
2+
import {createElement as reactCreateElement} from 'react'
3+
import {h as virtualDomCreateElement} from 'virtual-dom'
4+
import * as hyperscriptCreateElement from 'hyperscript'
5+
import Vue from 'vue'
6+
7+
const element = {
8+
type: 'element',
9+
tagName: 'a',
10+
properties: {
11+
href: 'https://alpha.com',
12+
className: ['bravo'],
13+
download: true
14+
},
15+
children: []
16+
}
17+
18+
// Different options
19+
hastToHyperScript(hyperscriptCreateElement, element, 'h')
20+
hastToHyperScript(hyperscriptCreateElement, element, false)
21+
hastToHyperScript(hyperscriptCreateElement, element, {})
22+
hastToHyperScript(hyperscriptCreateElement, element, {
23+
prefix: false
24+
})
25+
hastToHyperScript(hyperscriptCreateElement, element, {
26+
space: 'svg'
27+
})
28+
hastToHyperScript(hyperscriptCreateElement, element, {
29+
prefix: false,
30+
space: 'svg'
31+
})
32+
hastToHyperScript(hyperscriptCreateElement, element, {
33+
// $ExpectError
34+
unknown: 'does not exist'
35+
})
36+
37+
// Try different types of renderers
38+
hastToHyperScript(hyperscriptCreateElement, element) // $ExpectType Element
39+
hastToHyperScript(reactCreateElement, element) // $ExpectType ReactElement<{}, string | ((props: any) => ReactElement<any, string | any | (new (props: any) => Component<any, any, any>)> | null) | (new (props: any) => Component<any, any, any>)>
40+
hastToHyperScript(virtualDomCreateElement, element) // $ExpectType VNode
41+
hastToHyperScript(new Vue().$createElement, element) // $ExpectType VNode
42+
hastToHyperScript((name: number) => name, element) // $ExpectError

types/index.d.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Minimum TypeScript Version: 3.2
2+
import {Node} from 'unist'
3+
4+
declare namespace hastToHyperScript {
5+
/**
6+
* Basic shape of a create element function,
7+
* this should be extended by a concrete type.
8+
*
9+
* @remark
10+
* This exists to avoid needing to make all supported renders' typings as dependencies
11+
*/
12+
type CreateElementLike = (
13+
name: string,
14+
attributes: {[key: string]: any},
15+
children: any[]
16+
) => any
17+
18+
/**
19+
* Prefix to use as a prefix for keys passed in attrs to h(), this behaviour is turned off by passing false, turned on by passing a string.
20+
*/
21+
type Prefix = string | boolean
22+
23+
interface Options {
24+
/**
25+
* Prefix to use as a prefix for keys passed in attrs to h(), this behaviour is turned off by passing false, turned on by passing a string.
26+
*/
27+
prefix?: Prefix
28+
29+
/**
30+
* Whether node is in the 'html' or 'svg' space
31+
*/
32+
space?: 'svg' | 'html'
33+
}
34+
}
35+
36+
/**
37+
* Hast utility to transform a tree to something else through a hyperscript DSL.
38+
*
39+
* @param h Hyperscript function
40+
* @param tree Tree to transform
41+
* @param options Options or prefix
42+
* @typeParam H a Hyperscript like function type, can be inferred
43+
*/
44+
declare function hastToHyperScript<
45+
H extends hastToHyperScript.CreateElementLike
46+
>(
47+
h: H,
48+
tree: Node,
49+
options?: hastToHyperScript.Prefix | hastToHyperScript.Options
50+
): ReturnType<H>
51+
52+
export = hastToHyperScript

types/tsconfig.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"compilerOptions": {
3+
"lib": ["es2015", "DOM"],
4+
"strict": true,
5+
"baseUrl": ".",
6+
"paths": {
7+
"hast-to-hyperscript": ["index.d.ts"]
8+
}
9+
}
10+
}

types/tslint.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"extends": "dtslint/dtslint.json",
3+
"rules": {
4+
"semicolon": false,
5+
"whitespace": false,
6+
"max-line-length": false
7+
}
8+
}

0 commit comments

Comments
 (0)