Skip to content

Add types #1

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

Merged
merged 2 commits into from
Oct 4, 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
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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,
Expand Down
56 changes: 56 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -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: `<circle />` instead of `<circle></circle>`.
* 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:
* `<circle/>` instead of `<circle />`.
*
* @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
22 changes: 22 additions & 0 deletions types/test.ts
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -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": ["."]
}
}
}
9 changes: 9 additions & 0 deletions types/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "dtslint/dtslint.json",
"rules": {
"no-redundant-jsdoc": false,
"no-redundant-jsdoc-2": true,
"semicolon": false,
"whitespace": false
}
}