Skip to content

Commit 1609850

Browse files
authored
Fix children types
Closes GH-2. Reviewed-by: Christian Murphy <[email protected]> Reviewed-by: Titus Wormer <[email protected]>
1 parent 4c30275 commit 1609850

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,21 @@ test('xastscript', function (t) {
6363
'should add children as an array'
6464
)
6565

66+
t.deepEqual(
67+
x('y', {}, [[[x('a')]], [[[[x('b')]], x('c')]]]),
68+
{
69+
type: 'element',
70+
name: 'y',
71+
attributes: {},
72+
children: [
73+
{type: 'element', name: 'a', attributes: {}, children: []},
74+
{type: 'element', name: 'b', attributes: {}, children: []},
75+
{type: 'element', name: 'c', attributes: {}, children: []}
76+
]
77+
},
78+
'should add children in a deeply nested array'
79+
)
80+
6681
t.deepEqual(
6782
x('y', {}, x('a'), x('b')),
6883
{

types/index.d.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
// TypeScript Version: 3.0
1+
// TypeScript Version: 3.7
22

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

5+
type Children = string | Node | Children[]
6+
57
/**
68
* Create XML trees in xast.
79
*
810
* @param name Qualified name. Case sensitive and can contain a namespace prefix (such as rdf:RDF).
911
* @param children (Lists of) child nodes. When strings are encountered, they are mapped to Text nodes.
1012
*/
11-
declare function xastscript(
12-
name: string,
13-
children?: string | Node | Array<string | Node>
14-
): Element
13+
declare function xastscript(name: string, ...children: Children[]): Element
1514

1615
/**
1716
* Create XML trees in xast.
@@ -23,7 +22,7 @@ declare function xastscript(
2322
declare function xastscript(
2423
name: string,
2524
attributes?: Attributes,
26-
children?: string | Node | Array<string | Node>
25+
...children: Children[]
2726
): Element
2827

2928
export = xastscript

types/test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import x = require('xastscript')
33
x('urlset') // $ExpectType Element
44
x('urlset', 'string') // $ExpectType Element
55
x('urlset', ['string', 'string']) // $ExpectType Element
6+
x('urlset', x('loc'), 'string') // $ExpectType Element
67
x('urlset', x('loc')) // $ExpectType Element
8+
x('urlset', x('loc'), x('loc')) // $ExpectType Element
79
x('urlset', [x('loc'), x('loc')]) // $ExpectType Element
810
x('urlset', []) // $ExpectType Element
911

@@ -12,7 +14,9 @@ const xmlns = 'http://www.sitemaps.org/schemas/sitemap/0.9'
1214
x('urlset', {xmlns}) // $ExpectType Element
1315
x('urlset', {xmlns}, 'string') // $ExpectType Element
1416
x('urlset', {xmlns}, ['string', 'string']) // $ExpectType Element
17+
x('urlset', {xmlns}, x('loc'), 'string') // $ExpectType Element
1518
x('urlset', {xmlns}, x('loc')) // $ExpectType Element
19+
x('urlset', {xmlns}, x('loc'), x('loc')) // $ExpectType Element
1620
x('urlset', {xmlns}, [x('loc'), x('loc')]) // $ExpectType Element
1721
x('urlset', {xmlns}, []) // $ExpectType Element
1822

0 commit comments

Comments
 (0)