Skip to content

Fix children types #2

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 30, 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
15 changes: 15 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,21 @@ test('xastscript', function (t) {
'should add children as an array'
)

t.deepEqual(
x('y', {}, [[[x('a')]], [[[[x('b')]], x('c')]]]),
{
type: 'element',
name: 'y',
attributes: {},
children: [
{type: 'element', name: 'a', attributes: {}, children: []},
{type: 'element', name: 'b', attributes: {}, children: []},
{type: 'element', name: 'c', attributes: {}, children: []}
]
},
'should add children in a deeply nested array'
)

t.deepEqual(
x('y', {}, x('a'), x('b')),
{
Expand Down
11 changes: 5 additions & 6 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
// TypeScript Version: 3.0
// TypeScript Version: 3.7

import {Attributes, Element, Node} from 'xast'

type Children = string | Node | Children[]

/**
* 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<string | Node>
): Element
declare function xastscript(name: string, ...children: Children[]): Element

/**
* Create XML trees in xast.
Expand All @@ -23,7 +22,7 @@ declare function xastscript(
declare function xastscript(
name: string,
attributes?: Attributes,
children?: string | Node | Array<string | Node>
...children: Children[]
): Element

export = xastscript
4 changes: 4 additions & 0 deletions types/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import x = require('xastscript')
x('urlset') // $ExpectType Element
x('urlset', 'string') // $ExpectType Element
x('urlset', ['string', 'string']) // $ExpectType Element
x('urlset', x('loc'), 'string') // $ExpectType Element
x('urlset', x('loc')) // $ExpectType Element
x('urlset', x('loc'), x('loc')) // $ExpectType Element
x('urlset', [x('loc'), x('loc')]) // $ExpectType Element
x('urlset', []) // $ExpectType Element

Expand All @@ -12,7 +14,9 @@ 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'), 'string') // $ExpectType Element
x('urlset', {xmlns}, x('loc')) // $ExpectType Element
x('urlset', {xmlns}, x('loc'), x('loc')) // $ExpectType Element
x('urlset', {xmlns}, [x('loc'), x('loc')]) // $ExpectType Element
x('urlset', {xmlns}, []) // $ExpectType Element

Expand Down