From 21a360aba8fc4dc2eefaa5aeffe437cc23adcb6d Mon Sep 17 00:00:00 2001 From: Stefan Probst Date: Sat, 3 Oct 2020 13:05:42 +0200 Subject: [PATCH 1/2] chore: add types --- package.json | 12 +++++++++--- types/index.d.ts | 29 +++++++++++++++++++++++++++++ types/test.ts | 21 +++++++++++++++++++++ types/tsconfig.json | 15 +++++++++++++++ types/tslint.json | 7 +++++++ 5 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 types/index.d.ts create mode 100644 types/test.ts create mode 100644 types/tsconfig.json create mode 100644 types/tslint.json diff --git a/package.json b/package.json index 03b7a36..674a8d7 100644 --- a/package.json +++ b/package.json @@ -26,10 +26,15 @@ "Titus Wormer (https://wooorm.com)" ], "files": [ - "index.js" + "index.js", + "types/index.d.ts" ], - "dependencies": {}, + "types": "types", + "dependencies": { + "@types/xast": "^1.0.0" + }, "devDependencies": { + "dtslint": "^4.0.0", "nyc": "^15.0.0", "prettier": "^2.0.0", "remark-cli": "^8.0.0", @@ -41,7 +46,8 @@ "format": "remark . -qfo && prettier . --write && xo --fix", "test-api": "node test", "test-coverage": "nyc --reporter lcov tape test.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" }, "nyc": { "check-coverage": true, diff --git a/types/index.d.ts b/types/index.d.ts new file mode 100644 index 0000000..915e821 --- /dev/null +++ b/types/index.d.ts @@ -0,0 +1,29 @@ +// TypeScript Version: 3.0 + +import {Attributes, Element, Node} from 'xast' + +/** + * Create XML trees in xast. + * + * @param name Qualified name. Case sensitive and can contain a namespace prefix (such as rdf:RDF). + * @param children (Lists of) child nodes. When strings are encountered, they are mapped to Text nodes. + */ +declare function xastscript( + name: string, + children?: string | Node | Array +): Element + +/** + * Create XML trees in xast. + * + * @param name Qualified name. Case sensitive and can contain a namespace prefix (such as rdf:RDF). + * @param attributes Map of attributes. Nullish (null or undefined) or NaN values are ignored, other values are turned to strings. + * @param children (Lists of) child nodes. When strings are encountered, they are mapped to Text nodes. + */ +declare function xastscript( + name: string, + attributes?: Attributes, + children?: string | Node | Array +): Element + +export = xastscript diff --git a/types/test.ts b/types/test.ts new file mode 100644 index 0000000..a5937bf --- /dev/null +++ b/types/test.ts @@ -0,0 +1,21 @@ +import x = require('xastscript') + +x('urlset') // $ExpectType Element +x('urlset', 'string') // $ExpectType Element +x('urlset', ['string', 'string']) // $ExpectType Element +x('urlset', x('loc')) // $ExpectType Element +x('urlset', [x('loc'), x('loc')]) // $ExpectType Element +x('urlset', []) // $ExpectType Element + +const xmlns = 'http://www.sitemaps.org/schemas/sitemap/0.9' + +x('urlset', {xmlns}) // $ExpectType Element +x('urlset', {xmlns}, 'string') // $ExpectType Element +x('urlset', {xmlns}, ['string', 'string']) // $ExpectType Element +x('urlset', {xmlns}, x('loc')) // $ExpectType Element +x('urlset', {xmlns}, [x('loc'), x('loc')]) // $ExpectType Element +x('urlset', {xmlns}, []) // $ExpectType Element + +x() // $ExpectError +x(false) // $ExpectError +x('urlset', x('loc'), {xmlns}) // $ExpectError diff --git a/types/tsconfig.json b/types/tsconfig.json new file mode 100644 index 0000000..0277d91 --- /dev/null +++ b/types/tsconfig.json @@ -0,0 +1,15 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": ["es6"], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "noEmit": true, + "baseUrl": ".", + "paths": { + "xastscript": ["."] + } + } +} diff --git a/types/tslint.json b/types/tslint.json new file mode 100644 index 0000000..70c4494 --- /dev/null +++ b/types/tslint.json @@ -0,0 +1,7 @@ +{ + "extends": "dtslint/dtslint.json", + "rules": { + "semicolon": false, + "whitespace": false + } +} From b71f60ae76c48b2c3aa579ed3e77015ed69ead18 Mon Sep 17 00:00:00 2001 From: Stefan Probst Date: Sat, 3 Oct 2020 19:51:00 +0200 Subject: [PATCH 2/2] chore: change tsconfig lib --- types/tsconfig.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/tsconfig.json b/types/tsconfig.json index 0277d91..bb008c0 100644 --- a/types/tsconfig.json +++ b/types/tsconfig.json @@ -1,7 +1,7 @@ { "compilerOptions": { "module": "commonjs", - "lib": ["es6"], + "lib": ["es2015"], "noImplicitAny": true, "noImplicitThis": true, "strictNullChecks": true,