diff --git a/package.json b/package.json index 62977c5..314bfe1 100644 --- a/package.json +++ b/package.json @@ -23,14 +23,18 @@ ], "files": [ "lib", - "index.js" + "index.js", + "types/index.d.ts" ], + "types": "types", "dependencies": { + "@types/xast": "^1.0.0", "ccount": "^1.0.0", "stringify-entities": "^2.0.0" }, "devDependencies": { "browserify": "^16.0.0", + "dtslint": "^4.0.0", "nyc": "^15.0.0", "prettier": "^2.0.0", "remark-cli": "^8.0.0", @@ -45,7 +49,8 @@ "format": "remark . -qfo && prettier . --write && xo --fix", "test-api": "node test", "test-coverage": "nyc --reporter lcov tape test/index.js", - "test": "npm run format && npm run test-coverage" + "test-types": "dtslint types", + "test": "npm run format && npm run test-coverage && npm run test-types" }, "prettier": { "tabWidth": 2, diff --git a/types/index.d.ts b/types/index.d.ts new file mode 100644 index 0000000..4cb5b00 --- /dev/null +++ b/types/index.d.ts @@ -0,0 +1,56 @@ +// TypeScript Version: 3.0 + +import {Node} from 'xast' + +declare namespace toXml { + interface Options { + /** + * Preferred quote to use. + * + * @default '"' + */ + quote?: '"' | "'" + + /** + * Use the other quote if that results in less bytes. + * + * @default false + */ + quoteSmart?: boolean + + /** + * Close elements without any content with slash (/) on the opening tag + * instead of an end tag: `` instead of ``. + * See `tightClose` to control whether a space is used before the slash. + * + * @default false + */ + closeEmptyElements?: boolean + + /** + * Do not use an extra space when closing self-closing elements: + * `` instead of ``. + * + * @default false + */ + tightClose?: boolean + + /** + * Allow `raw` nodes and insert them as raw XML. When falsey, encodes `raw` nodes. + * Only set this if you completely trust the content! + * + * @default false + */ + allowDangerousXml?: boolean + } +} + +/** + * Serialize the given xast tree to xml. + * + * @param tree Tree or list of nodes + * @param options Options + */ +declare function toXml(tree: Node | Node[], options?: toXml.Options): string + +export = toXml diff --git a/types/test.ts b/types/test.ts new file mode 100644 index 0000000..83c7d67 --- /dev/null +++ b/types/test.ts @@ -0,0 +1,22 @@ +import toXml = require('xast-util-to-xml') +import {Element} from 'xast' + +const xmlns = 'http://www.sitemaps.org/schemas/sitemap/0.9' +const element: Element = { + type: 'element', + name: 'urlset', + attributes: {xmlns}, + children: [] +} + +toXml(element) // $ExpectType string +toXml([element]) // $ExpectType string +toXml(element, {allowDangerousXml: true}) // $ExpectType string +toXml(element, {quote: "'"}) // $ExpectType string +toXml(element, {quoteSmart: true}) // $ExpectType string +toXml(element, {closeEmptyElements: true}) // $ExpectType string +toXml(element, {closeEmptyElements: true, tightClose: true}) // $ExpectType string + +toXml() // $ExpectError +toXml(false) // $ExpectError +toXml(element, element) // $ExpectError diff --git a/types/tsconfig.json b/types/tsconfig.json new file mode 100644 index 0000000..5915e12 --- /dev/null +++ b/types/tsconfig.json @@ -0,0 +1,15 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": ["es2015"], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "noEmit": true, + "baseUrl": ".", + "paths": { + "xast-util-to-xml": ["."] + } + } +} diff --git a/types/tslint.json b/types/tslint.json new file mode 100644 index 0000000..4e872e2 --- /dev/null +++ b/types/tslint.json @@ -0,0 +1,9 @@ +{ + "extends": "dtslint/dtslint.json", + "rules": { + "no-redundant-jsdoc": false, + "no-redundant-jsdoc-2": true, + "semicolon": false, + "whitespace": false + } +}