diff --git a/hastscript-tests.ts b/hastscript-tests.ts new file mode 100644 index 0000000..863a218 --- /dev/null +++ b/hastscript-tests.ts @@ -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 ` +): 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 +): Element + +export = hastscript diff --git a/package.json b/package.json index f2528b9..af00b89 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,10 @@ "contributors": [ "Titus Wormer (https://wooorm.com)" ], + "types": "index.d.ts", "files": [ + "index.d.ts", + "svg.d.ts", "index.js", "factory.js", "html.js", @@ -35,6 +38,7 @@ "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", @@ -42,6 +46,7 @@ }, "devDependencies": { "browserify": "^16.0.0", + "dtslint": "^3.0.0", "nyc": "^15.0.0", "prettier": "^2.0.0", "remark-cli": "^8.0.0", @@ -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, diff --git a/svg.d.ts b/svg.d.ts new file mode 100644 index 0000000..b39440f --- /dev/null +++ b/svg.d.ts @@ -0,0 +1,5 @@ +// TypeScript Version: 3.5 + +import hastscript = require('hastscript') + +export = hastscript diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..21dda03 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,11 @@ +{ + "compilerOptions": { + "lib": ["es2015"], + "strict": true, + "baseUrl": ".", + "paths": { + "hastscript": ["index.d.ts"], + "hastscript/svg": ["svg.d.ts"] + } + } +} diff --git a/tslint.json b/tslint.json new file mode 100644 index 0000000..70c4494 --- /dev/null +++ b/tslint.json @@ -0,0 +1,7 @@ +{ + "extends": "dtslint/dtslint.json", + "rules": { + "semicolon": false, + "whitespace": false + } +}