Skip to content

Commit d34ff6f

Browse files
authored
Add types
Closes GH-1. Reviewed-by: Christian Murphy <christian.murphy.42@gmail.com> Reviewed-by: Titus Wormer <tituswormer@gmail.com>
1 parent fdac125 commit d34ff6f

File tree

5 files changed

+109
-2
lines changed

5 files changed

+109
-2
lines changed

package.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,18 @@
2323
],
2424
"files": [
2525
"lib",
26-
"index.js"
26+
"index.js",
27+
"types/index.d.ts"
2728
],
29+
"types": "types",
2830
"dependencies": {
31+
"@types/xast": "^1.0.0",
2932
"ccount": "^1.0.0",
3033
"stringify-entities": "^2.0.0"
3134
},
3235
"devDependencies": {
3336
"browserify": "^16.0.0",
37+
"dtslint": "^4.0.0",
3438
"nyc": "^15.0.0",
3539
"prettier": "^2.0.0",
3640
"remark-cli": "^8.0.0",
@@ -45,7 +49,8 @@
4549
"format": "remark . -qfo && prettier . --write && xo --fix",
4650
"test-api": "node test",
4751
"test-coverage": "nyc --reporter lcov tape test/index.js",
48-
"test": "npm run format && npm run test-coverage"
52+
"test-types": "dtslint types",
53+
"test": "npm run format && npm run test-coverage && npm run test-types"
4954
},
5055
"prettier": {
5156
"tabWidth": 2,

types/index.d.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// TypeScript Version: 3.0
2+
3+
import {Node} from 'xast'
4+
5+
declare namespace toXml {
6+
interface Options {
7+
/**
8+
* Preferred quote to use.
9+
*
10+
* @default '"'
11+
*/
12+
quote?: '"' | "'"
13+
14+
/**
15+
* Use the other quote if that results in less bytes.
16+
*
17+
* @default false
18+
*/
19+
quoteSmart?: boolean
20+
21+
/**
22+
* Close elements without any content with slash (/) on the opening tag
23+
* instead of an end tag: `<circle />` instead of `<circle></circle>`.
24+
* See `tightClose` to control whether a space is used before the slash.
25+
*
26+
* @default false
27+
*/
28+
closeEmptyElements?: boolean
29+
30+
/**
31+
* Do not use an extra space when closing self-closing elements:
32+
* `<circle/>` instead of `<circle />`.
33+
*
34+
* @default false
35+
*/
36+
tightClose?: boolean
37+
38+
/**
39+
* Allow `raw` nodes and insert them as raw XML. When falsey, encodes `raw` nodes.
40+
* Only set this if you completely trust the content!
41+
*
42+
* @default false
43+
*/
44+
allowDangerousXml?: boolean
45+
}
46+
}
47+
48+
/**
49+
* Serialize the given xast tree to xml.
50+
*
51+
* @param tree Tree or list of nodes
52+
* @param options Options
53+
*/
54+
declare function toXml(tree: Node | Node[], options?: toXml.Options): string
55+
56+
export = toXml

types/test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import toXml = require('xast-util-to-xml')
2+
import {Element} from 'xast'
3+
4+
const xmlns = 'http://www.sitemaps.org/schemas/sitemap/0.9'
5+
const element: Element = {
6+
type: 'element',
7+
name: 'urlset',
8+
attributes: {xmlns},
9+
children: []
10+
}
11+
12+
toXml(element) // $ExpectType string
13+
toXml([element]) // $ExpectType string
14+
toXml(element, {allowDangerousXml: true}) // $ExpectType string
15+
toXml(element, {quote: "'"}) // $ExpectType string
16+
toXml(element, {quoteSmart: true}) // $ExpectType string
17+
toXml(element, {closeEmptyElements: true}) // $ExpectType string
18+
toXml(element, {closeEmptyElements: true, tightClose: true}) // $ExpectType string
19+
20+
toXml() // $ExpectError
21+
toXml(false) // $ExpectError
22+
toXml(element, element) // $ExpectError

types/tsconfig.json

Lines changed: 15 additions & 0 deletions
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+
"xast-util-to-xml": ["."]
13+
}
14+
}
15+
}

types/tslint.json

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

0 commit comments

Comments
 (0)