Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add types #14

Merged
merged 8 commits into from
Jul 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions hastscript-tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import h = require('hastscript')
import s = require('hastscript/svg')

h() // $ExpectType Element
h('.bar', {class: 'bar'}) // $ExpectType Element
h('.bar', 'child text') // $ExpectType Element
h('.bar', ['child text']) // $ExpectType Element
h('.foo', {class: 'bar'}, h('.baz')) // $ExpectType Element
h('.foo', {class: 'bar'}, [h('.baz')]) // $ExpectType Element
h('.bar', {class: 'bar'}, 'child text') // $ExpectType Element
h('.bar', {class: 'bar'}, ['child text']) // $ExpectType Element
h(false) // $ExpectError

// $ExpectType
s('svg', {xmlns: 'http://www.w3.org/2000/svg', viewbox: '0 0 500 500'}, [
s('title', 'SVG `<circle` element'),
s('circle', {cx: 120, cy: 120, r: 100})
])
29 changes: 29 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// TypeScript Version: 3.5

import {Element, Properties, Node} from 'hast'

/**
* DSL to create virtual hast trees for HTML or SVG
*
* @param selector Simple CSS selector
* @param children (Lists of) child nodes
*/
declare function hastscript(
selector?: string,
children?: string | Node | Array<string | Node>
): Element

/**
* DSL to create virtual hast trees for HTML or SVG
*
* @param selector Simple CSS selector
* @param properties Map of properties
* @param children (Lists of) child nodes
*/
declare function hastscript(
selector?: string,
properties?: Properties,
children?: string | Node | Array<string | Node>
): Element

export = hastscript
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,26 @@
"contributors": [
"Titus Wormer <[email protected]> (https://wooorm.com)"
],
"types": "index.d.ts",
"files": [
"index.d.ts",
"svg.d.ts",
"index.js",
"factory.js",
"html.js",
"svg.js",
"svg-case-sensitive-tag-names.json"
],
"dependencies": {
"@types/hast": "^2.3.1",
"comma-separated-tokens": "^1.0.0",
"hast-util-parse-selector": "^2.0.0",
"property-information": "^5.0.0",
"space-separated-tokens": "^1.0.0"
},
"devDependencies": {
"browserify": "^16.0.0",
"dtslint": "^3.0.0",
"nyc": "^15.0.0",
"prettier": "^2.0.0",
"remark-cli": "^8.0.0",
Expand All @@ -59,7 +64,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 generate && npm run format && npm run build && npm run test-coverage"
"test-types": "dtslint .",
"test": "npm run generate && npm run format && npm run build && npm run test-coverage && npm run test-types"
},
"nyc": {
"check-coverage": true,
Expand Down
5 changes: 5 additions & 0 deletions svg.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// TypeScript Version: 3.5

import hastscript = require('hastscript')

export = hastscript
11 changes: 11 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"lib": ["es2015"],
"strict": true,
"baseUrl": ".",
"paths": {
"hastscript": ["index.d.ts"],
"hastscript/svg": ["svg.d.ts"]
}
}
}
7 changes: 7 additions & 0 deletions tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "dtslint/dtslint.json",
"rules": {
"semicolon": false,
"whitespace": false
}
}