From 664b864238278b7df79b92869e955fe18556ad28 Mon Sep 17 00:00:00 2001 From: Christian Murphy Date: Fri, 6 Nov 2020 10:19:45 -0700 Subject: [PATCH 1/3] types: add support for null/fragment type in typescript definition --- types/index.d.ts | 7 +++++-- types/test.ts | 9 +++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/types/index.d.ts b/types/index.d.ts index 9986a75..da80c1b 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -17,7 +17,10 @@ type Attributes = Record * @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: Children[]): Element +declare function xastscript( + name: string | null, + ...children: Children[] +): Element /** * Create XML trees in xast. @@ -27,7 +30,7 @@ declare function xastscript(name: string, ...children: Children[]): Element * @param children (Lists of) child nodes. When strings are encountered, they are mapped to Text nodes. */ declare function xastscript( - name: string, + name: string | null, attributes?: Attributes, ...children: Children[] ): Element diff --git a/types/test.ts b/types/test.ts index 855264b..4debe79 100644 --- a/types/test.ts +++ b/types/test.ts @@ -2,12 +2,17 @@ import x = require('xastscript') x('urlset') // $ExpectType Element x('urlset', 'string') // $ExpectType Element +x('urlset', 1) // $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 +x(null) // $ExpectType Element +x(null, 'string') // $ExpectType Element +x(null, 1) // $ExpectType Element +x(null, []) // $ExpectType Element const xmlns = 'http://www.sitemaps.org/schemas/sitemap/0.9' @@ -20,6 +25,10 @@ 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 +x(null, {xmlns}) // $ExpectType Element +x(null, {xmlns}, 'string') // $ExpectType Element +x(null, {xmlns}, 1) // $ExpectType Element +x(null, {xmlns}, []) // $ExpectType Element const xmlNumberAttribute = 100 x('urlset', {xmlNumberAttribute}, 'string') // $ExpectType Element From 7729efbe6c8d3f0d69d02e58419b87859cdebe38 Mon Sep 17 00:00:00 2001 From: Christian Murphy Date: Fri, 6 Nov 2020 10:40:31 -0700 Subject: [PATCH 2/3] types: overload xastscript so fragements return Root Co-authored-by: Titus Wormer --- types/index.d.ts | 26 ++++++++++++++++++++++---- types/test.ts | 16 ++++++++-------- 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/types/index.d.ts b/types/index.d.ts index da80c1b..c80471c 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -1,6 +1,6 @@ // TypeScript Version: 3.7 -import {Element, Node} from 'xast' +import {Element, Node, Root} from 'xast' type Children = string | Node | number | Children[] @@ -17,8 +17,26 @@ type Attributes = Record * @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: Children[]): Element + +/** + * 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: null, ...children: Children[]): Root + +/** + * 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 | null, + name: string, + attributes?: Attributes, ...children: Children[] ): Element @@ -30,9 +48,9 @@ declare function xastscript( * @param children (Lists of) child nodes. When strings are encountered, they are mapped to Text nodes. */ declare function xastscript( - name: string | null, + name: null, attributes?: Attributes, ...children: Children[] -): Element +): Root export = xastscript diff --git a/types/test.ts b/types/test.ts index 4debe79..c5bb086 100644 --- a/types/test.ts +++ b/types/test.ts @@ -9,10 +9,10 @@ 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 -x(null) // $ExpectType Element -x(null, 'string') // $ExpectType Element -x(null, 1) // $ExpectType Element -x(null, []) // $ExpectType Element +x(null) // $ExpectType Root +x(null, 'string') // $ExpectType Root +x(null, 1) // $ExpectType Root +x(null, []) // $ExpectType Root const xmlns = 'http://www.sitemaps.org/schemas/sitemap/0.9' @@ -25,10 +25,10 @@ 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 -x(null, {xmlns}) // $ExpectType Element -x(null, {xmlns}, 'string') // $ExpectType Element -x(null, {xmlns}, 1) // $ExpectType Element -x(null, {xmlns}, []) // $ExpectType Element +x(null, {xmlns}) // $ExpectType Root +x(null, {xmlns}, 'string') // $ExpectType Root +x(null, {xmlns}, 1) // $ExpectType Root +x(null, {xmlns}, []) // $ExpectType Root const xmlNumberAttribute = 100 x('urlset', {xmlNumberAttribute}, 'string') // $ExpectType Element From 7141b7408ded05166001361502139d1d3d88dd0b Mon Sep 17 00:00:00 2001 From: Christian Murphy Date: Fri, 6 Nov 2020 10:45:31 -0700 Subject: [PATCH 3/3] types: fragment does not support attributes Co-authored-by: Titus Wormer --- types/index.d.ts | 13 ------------- types/test.ts | 5 +---- 2 files changed, 1 insertion(+), 17 deletions(-) diff --git a/types/index.d.ts b/types/index.d.ts index c80471c..34d047c 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -40,17 +40,4 @@ declare function xastscript( ...children: Children[] ): 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: null, - attributes?: Attributes, - ...children: Children[] -): Root - export = xastscript diff --git a/types/test.ts b/types/test.ts index c5bb086..524b9f5 100644 --- a/types/test.ts +++ b/types/test.ts @@ -25,10 +25,6 @@ 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 -x(null, {xmlns}) // $ExpectType Root -x(null, {xmlns}, 'string') // $ExpectType Root -x(null, {xmlns}, 1) // $ExpectType Root -x(null, {xmlns}, []) // $ExpectType Root const xmlNumberAttribute = 100 x('urlset', {xmlNumberAttribute}, 'string') // $ExpectType Element @@ -39,3 +35,4 @@ x('urlset', {xmlNumberAttribute}, []) // $ExpectType Element x() // $ExpectError x(false) // $ExpectError x('urlset', x('loc'), {xmlns}) // $ExpectError +x(null, {xmlns}) // $ExpectError