Skip to content

Commit 4c68156

Browse files
Add types
Closes GH-13. Closes GH-14. Reviewed-by: Christian Murphy <christian.murphy.42@gmail.com> Reviewed-by: Titus Wormer <tituswormer@gmail.com>
1 parent 5465214 commit 4c68156

File tree

6 files changed

+77
-1
lines changed

6 files changed

+77
-1
lines changed

hastscript-tests.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import h = require('hastscript')
2+
import s = require('hastscript/svg')
3+
4+
h() // $ExpectType Element
5+
h('.bar', {class: 'bar'}) // $ExpectType Element
6+
h('.bar', 'child text') // $ExpectType Element
7+
h('.bar', ['child text']) // $ExpectType Element
8+
h('.foo', {class: 'bar'}, h('.baz')) // $ExpectType Element
9+
h('.foo', {class: 'bar'}, [h('.baz')]) // $ExpectType Element
10+
h('.bar', {class: 'bar'}, 'child text') // $ExpectType Element
11+
h('.bar', {class: 'bar'}, ['child text']) // $ExpectType Element
12+
h(false) // $ExpectError
13+
14+
// $ExpectType
15+
s('svg', {xmlns: 'http://www.w3.org/2000/svg', viewbox: '0 0 500 500'}, [
16+
s('title', 'SVG `<circle` element'),
17+
s('circle', {cx: 120, cy: 120, r: 100})
18+
])

index.d.ts

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// TypeScript Version: 3.5
2+
3+
import {Element, Properties, Node} from 'hast'
4+
5+
/**
6+
* DSL to create virtual hast trees for HTML or SVG
7+
*
8+
* @param selector Simple CSS selector
9+
* @param children (Lists of) child nodes
10+
*/
11+
declare function hastscript(
12+
selector?: string,
13+
children?: string | Node | Array<string | Node>
14+
): Element
15+
16+
/**
17+
* DSL to create virtual hast trees for HTML or SVG
18+
*
19+
* @param selector Simple CSS selector
20+
* @param properties Map of properties
21+
* @param children (Lists of) child nodes
22+
*/
23+
declare function hastscript(
24+
selector?: string,
25+
properties?: Properties,
26+
children?: string | Node | Array<string | Node>
27+
): Element
28+
29+
export = hastscript

package.json

+7-1
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,26 @@
2727
"contributors": [
2828
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)"
2929
],
30+
"types": "index.d.ts",
3031
"files": [
32+
"index.d.ts",
33+
"svg.d.ts",
3134
"index.js",
3235
"factory.js",
3336
"html.js",
3437
"svg.js",
3538
"svg-case-sensitive-tag-names.json"
3639
],
3740
"dependencies": {
41+
"@types/hast": "^2.3.1",
3842
"comma-separated-tokens": "^1.0.0",
3943
"hast-util-parse-selector": "^2.0.0",
4044
"property-information": "^5.0.0",
4145
"space-separated-tokens": "^1.0.0"
4246
},
4347
"devDependencies": {
4448
"browserify": "^16.0.0",
49+
"dtslint": "^3.0.0",
4550
"nyc": "^15.0.0",
4651
"prettier": "^2.0.0",
4752
"remark-cli": "^8.0.0",
@@ -59,7 +64,8 @@
5964
"build": "npm run build-bundle && npm run build-mangle",
6065
"test-api": "node test",
6166
"test-coverage": "nyc --reporter lcov tape test.js",
62-
"test": "npm run generate && npm run format && npm run build && npm run test-coverage"
67+
"test-types": "dtslint .",
68+
"test": "npm run generate && npm run format && npm run build && npm run test-coverage && npm run test-types"
6369
},
6470
"nyc": {
6571
"check-coverage": true,

svg.d.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// TypeScript Version: 3.5
2+
3+
import hastscript = require('hastscript')
4+
5+
export = hastscript

tsconfig.json

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"compilerOptions": {
3+
"lib": ["es2015"],
4+
"strict": true,
5+
"baseUrl": ".",
6+
"paths": {
7+
"hastscript": ["index.d.ts"],
8+
"hastscript/svg": ["svg.d.ts"]
9+
}
10+
}
11+
}

tslint.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "dtslint/dtslint.json",
3+
"rules": {
4+
"semicolon": false,
5+
"whitespace": false
6+
}
7+
}

0 commit comments

Comments
 (0)