From 53d0dfe19ee71ca40a57072191ef04bcbd2fc1f9 Mon Sep 17 00:00:00 2001 From: Malav Shah Date: Mon, 2 Nov 2020 09:42:17 -0700 Subject: [PATCH 1/4] Added Support for Primitive Types for Attributes --- types/index.d.ts | 9 +++++++++ types/test.ts | 1 + 2 files changed, 10 insertions(+) diff --git a/types/index.d.ts b/types/index.d.ts index 9bfa1ed..57383c8 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -4,6 +4,15 @@ import {Attributes, Element, Node} from 'xast' type Children = string | Node | Children[] +type Primitive = null | undefined | string | number | boolean | symbol | bigint; + +/** + * Extending Attributes to Support JS Primitive Types + */ +interface Attributes extends xast.Attributes { + [name: string]: Primitive +} + /** * Create XML trees in xast. * diff --git a/types/test.ts b/types/test.ts index 2965c93..aa993a8 100644 --- a/types/test.ts +++ b/types/test.ts @@ -15,6 +15,7 @@ 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'), 100) // $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 From e2f4f125001a971c723f13a700eb33e3fbfc7921 Mon Sep 17 00:00:00 2001 From: Malav Shah Date: Mon, 2 Nov 2020 09:54:11 -0700 Subject: [PATCH 2/4] Added Testcases for Primitive Attributes --- types/test.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/types/test.ts b/types/test.ts index aa993a8..9831b8c 100644 --- a/types/test.ts +++ b/types/test.ts @@ -21,6 +21,12 @@ x('urlset', {xmlns}, x('loc'), x('loc')) // $ExpectType Element x('urlset', {xmlns}, [x('loc'), x('loc')]) // $ExpectType Element x('urlset', {xmlns}, []) // $ExpectType Element +const xmlNumberAttribute = 100 +x('urlset', {xmlNumberAttribute}, 'string') //$ExpectType Element +x('urlset', {xmlNumberAttribute}, 100) //$ExpectType Element +x('urlset', {xmlNumberAttribute}, x('loc'), 100) //$ExpectType Element +x('urlset', {xmlNumberAttribute}, []) //$ExpectType Element + x() // $ExpectError x(false) // $ExpectError x('urlset', x('loc'), {xmlns}) // $ExpectError From b2db779dff214f29bbbb9eb0536e1b8b4180380e Mon Sep 17 00:00:00 2001 From: malav2110 Date: Mon, 2 Nov 2020 10:27:55 -0700 Subject: [PATCH 3/4] Remove boolean, symbol and bigint as primitive data type Co-authored-by: Titus --- types/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/index.d.ts b/types/index.d.ts index 57383c8..fd0dbfe 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -4,7 +4,7 @@ import {Attributes, Element, Node} from 'xast' type Children = string | Node | Children[] -type Primitive = null | undefined | string | number | boolean | symbol | bigint; +type Primitive = null | undefined | string | number; /** * Extending Attributes to Support JS Primitive Types From 03d8f1ff8c81661f0b422b0c4ddd071c0577fb88 Mon Sep 17 00:00:00 2001 From: Malav Shah Date: Thu, 5 Nov 2020 16:26:33 -0700 Subject: [PATCH 4/4] Added number as supported type for Children --- types/index.d.ts | 10 ++++------ types/test.ts | 8 ++++---- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/types/index.d.ts b/types/index.d.ts index fd0dbfe..9986a75 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -1,17 +1,15 @@ // TypeScript Version: 3.7 -import {Attributes, Element, Node} from 'xast' +import {Element, Node} from 'xast' -type Children = string | Node | Children[] +type Children = string | Node | number | Children[] -type Primitive = null | undefined | string | number; +type Primitive = null | undefined | string | number /** * Extending Attributes to Support JS Primitive Types */ -interface Attributes extends xast.Attributes { - [name: string]: Primitive -} +type Attributes = Record /** * Create XML trees in xast. diff --git a/types/test.ts b/types/test.ts index 9831b8c..855264b 100644 --- a/types/test.ts +++ b/types/test.ts @@ -22,10 +22,10 @@ x('urlset', {xmlns}, [x('loc'), x('loc')]) // $ExpectType Element x('urlset', {xmlns}, []) // $ExpectType Element const xmlNumberAttribute = 100 -x('urlset', {xmlNumberAttribute}, 'string') //$ExpectType Element -x('urlset', {xmlNumberAttribute}, 100) //$ExpectType Element -x('urlset', {xmlNumberAttribute}, x('loc'), 100) //$ExpectType Element -x('urlset', {xmlNumberAttribute}, []) //$ExpectType Element +x('urlset', {xmlNumberAttribute}, 'string') // $ExpectType Element +x('urlset', {xmlNumberAttribute}, 100) // $ExpectType Element +x('urlset', {xmlNumberAttribute}, x('loc'), 100) // $ExpectType Element +x('urlset', {xmlNumberAttribute}, []) // $ExpectType Element x() // $ExpectError x(false) // $ExpectError