Skip to content

Commit bc70d9d

Browse files
authored
Add types
Closes GH-1. Reviewed-by: Christian Murphy <[email protected]> Reviewed-by: Titus Wormer <[email protected]>
1 parent 7cf15e5 commit bc70d9d

File tree

5 files changed

+81
-3
lines changed

5 files changed

+81
-3
lines changed

package.json

+9-3
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,15 @@
2626
"Titus Wormer <[email protected]> (https://wooorm.com)"
2727
],
2828
"files": [
29-
"index.js"
29+
"index.js",
30+
"types/index.d.ts"
3031
],
31-
"dependencies": {},
32+
"types": "types",
33+
"dependencies": {
34+
"@types/xast": "^1.0.0"
35+
},
3236
"devDependencies": {
37+
"dtslint": "^4.0.0",
3338
"nyc": "^15.0.0",
3439
"prettier": "^2.0.0",
3540
"remark-cli": "^8.0.0",
@@ -41,7 +46,8 @@
4146
"format": "remark . -qfo && prettier . --write && xo --fix",
4247
"test-api": "node test",
4348
"test-coverage": "nyc --reporter lcov tape test.js",
44-
"test": "npm run format && npm run test-coverage"
49+
"test-types": "dtslint types",
50+
"test": "npm run format && npm run test-coverage && npm run test-types"
4551
},
4652
"nyc": {
4753
"check-coverage": true,

types/index.d.ts

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// TypeScript Version: 3.0
2+
3+
import {Attributes, Element, Node} from 'xast'
4+
5+
/**
6+
* Create XML trees in xast.
7+
*
8+
* @param name Qualified name. Case sensitive and can contain a namespace prefix (such as rdf:RDF).
9+
* @param children (Lists of) child nodes. When strings are encountered, they are mapped to Text nodes.
10+
*/
11+
declare function xastscript(
12+
name: string,
13+
children?: string | Node | Array<string | Node>
14+
): Element
15+
16+
/**
17+
* Create XML trees in xast.
18+
*
19+
* @param name Qualified name. Case sensitive and can contain a namespace prefix (such as rdf:RDF).
20+
* @param attributes Map of attributes. Nullish (null or undefined) or NaN values are ignored, other values are turned to strings.
21+
* @param children (Lists of) child nodes. When strings are encountered, they are mapped to Text nodes.
22+
*/
23+
declare function xastscript(
24+
name: string,
25+
attributes?: Attributes,
26+
children?: string | Node | Array<string | Node>
27+
): Element
28+
29+
export = xastscript

types/test.ts

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import x = require('xastscript')
2+
3+
x('urlset') // $ExpectType Element
4+
x('urlset', 'string') // $ExpectType Element
5+
x('urlset', ['string', 'string']) // $ExpectType Element
6+
x('urlset', x('loc')) // $ExpectType Element
7+
x('urlset', [x('loc'), x('loc')]) // $ExpectType Element
8+
x('urlset', []) // $ExpectType Element
9+
10+
const xmlns = 'http://www.sitemaps.org/schemas/sitemap/0.9'
11+
12+
x('urlset', {xmlns}) // $ExpectType Element
13+
x('urlset', {xmlns}, 'string') // $ExpectType Element
14+
x('urlset', {xmlns}, ['string', 'string']) // $ExpectType Element
15+
x('urlset', {xmlns}, x('loc')) // $ExpectType Element
16+
x('urlset', {xmlns}, [x('loc'), x('loc')]) // $ExpectType Element
17+
x('urlset', {xmlns}, []) // $ExpectType Element
18+
19+
x() // $ExpectError
20+
x(false) // $ExpectError
21+
x('urlset', x('loc'), {xmlns}) // $ExpectError

types/tsconfig.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"compilerOptions": {
3+
"module": "commonjs",
4+
"lib": ["es2015"],
5+
"noImplicitAny": true,
6+
"noImplicitThis": true,
7+
"strictNullChecks": true,
8+
"strictFunctionTypes": true,
9+
"noEmit": true,
10+
"baseUrl": ".",
11+
"paths": {
12+
"xastscript": ["."]
13+
}
14+
}
15+
}

types/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)