From 9d0298b1d14f4dca871b4ed10d1ba38d4b71cf35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Wei=C3=9Fenfels?= <10491533+tim-we@users.noreply.github.com> Date: Sat, 3 Feb 2024 13:37:36 +0100 Subject: [PATCH 1/9] Allow Container#nodes to be undefined --- lib/container.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/container.d.ts b/lib/container.d.ts index bd6b1ba0a..92961f496 100644 --- a/lib/container.d.ts +++ b/lib/container.d.ts @@ -43,7 +43,7 @@ declare abstract class Container_ extends Node { * root.nodes[0].nodes[0].prop //=> 'color' * ``` */ - nodes: Child[] + nodes: Child[] | undefined /** * Inserts new nodes to the end of the container. From d8a3309635078a7dc9191c77f50ceb8a70a46bff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Wei=C3=9Fenfels?= <10491533+tim-we@users.noreply.github.com> Date: Sat, 3 Feb 2024 13:38:02 +0100 Subject: [PATCH 2/9] Make Root#nodes and Rule#nodes not undefined --- lib/root.d.ts | 1 + lib/rule.d.ts | 1 + 2 files changed, 2 insertions(+) diff --git a/lib/root.d.ts b/lib/root.d.ts index 98fb4bc91..9046aacdf 100644 --- a/lib/root.d.ts +++ b/lib/root.d.ts @@ -54,6 +54,7 @@ declare namespace Root { * ``` */ declare class Root_ extends Container { + nodes: NonNullable parent: Document | undefined raws: Root.RootRaws type: 'root' diff --git a/lib/rule.d.ts b/lib/rule.d.ts index 04be5d6c0..8b3db5f1d 100644 --- a/lib/rule.d.ts +++ b/lib/rule.d.ts @@ -69,6 +69,7 @@ declare namespace Rule { * ``` */ declare class Rule_ extends Container { + nodes: NonNullable parent: Container | undefined raws: Rule.RuleRaws /** From e288c8de2105203ccffccf1c3c77e7b8e8524507 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Wei=C3=9Fenfels?= <10491533+tim-we@users.noreply.github.com> Date: Sat, 3 Feb 2024 13:49:56 +0100 Subject: [PATCH 3/9] Update AtRule#nodes documentation --- lib/at-rule.d.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/lib/at-rule.d.ts b/lib/at-rule.d.ts index fa95ed742..378fe4d7b 100644 --- a/lib/at-rule.d.ts +++ b/lib/at-rule.d.ts @@ -86,6 +86,25 @@ declare class AtRule_ extends Container { * ``` */ name: string + /** + * An array containing the layer’s children. + * + * ```js + * const root = postcss.parse('@layer example { a { color: black } }') + * const layer = root.first + * layer.nodes.length //=> 1 + * layer.nodes[0].selector //=> 'a' + * ``` + * + * Can be `undefinded` if the at-rule has no body. + * + * ```js + * const root = postcss.parse('@layer a, b, c;') + * const layer = root.first + * layer.nodes //=> undefined + * ``` + */ + nodes: Container['nodes'] /** * The at-rule’s parameters, the values that follow the at-rule’s name * but precede any `{}` block. From b787a649d81fe8641415b8b9e4c5080a05ebdbdf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Wei=C3=9Fenfels?= <10491533+tim-we@users.noreply.github.com> Date: Sat, 3 Feb 2024 13:50:40 +0100 Subject: [PATCH 4/9] Remove whitespaces --- lib/at-rule.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/at-rule.d.ts b/lib/at-rule.d.ts index 378fe4d7b..88f37a086 100644 --- a/lib/at-rule.d.ts +++ b/lib/at-rule.d.ts @@ -101,7 +101,7 @@ declare class AtRule_ extends Container { * ```js * const root = postcss.parse('@layer a, b, c;') * const layer = root.first - * layer.nodes //=> undefined + * layer.nodes //=> undefined * ``` */ nodes: Container['nodes'] From 8fda920523898afa288c6c08f153ee09e087b8f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Wei=C3=9Fenfels?= <10491533+tim-we@users.noreply.github.com> Date: Sat, 3 Feb 2024 14:20:41 +0100 Subject: [PATCH 5/9] Add unit test --- test/at-rule.test.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/at-rule.test.ts b/test/at-rule.test.ts index 10a23a871..acffeb952 100755 --- a/test/at-rule.test.ts +++ b/test/at-rule.test.ts @@ -48,4 +48,10 @@ test('clone spaces from another at-rule', () => { is(rule.toString(), '@page 1{}') }) +test('at-rule without body has no nodes property', () => { + let root = parse('@layer a, b, c;'); + let layer = root.first as AtRule + type(layer.nodes, 'undefined') +}); + test.run() From 5e7dde77f8d1334970a032f27d11c362cb5c9b9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Wei=C3=9Fenfels?= <10491533+tim-we@users.noreply.github.com> Date: Sat, 3 Feb 2024 14:28:45 +0100 Subject: [PATCH 6/9] Remove whitespaces --- lib/at-rule.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/at-rule.d.ts b/lib/at-rule.d.ts index 88f37a086..3315d1ec5 100644 --- a/lib/at-rule.d.ts +++ b/lib/at-rule.d.ts @@ -95,9 +95,9 @@ declare class AtRule_ extends Container { * layer.nodes.length //=> 1 * layer.nodes[0].selector //=> 'a' * ``` - * + * * Can be `undefinded` if the at-rule has no body. - * + * * ```js * const root = postcss.parse('@layer a, b, c;') * const layer = root.first From ee7fcd42757f32791647674273be20b2ac85cc71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Wei=C3=9Fenfels?= <10491533+tim-we@users.noreply.github.com> Date: Sat, 3 Feb 2024 16:29:24 +0100 Subject: [PATCH 7/9] Fix Document#nodes --- lib/document.d.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/document.d.ts b/lib/document.d.ts index f018f6e67..a368f166a 100644 --- a/lib/document.d.ts +++ b/lib/document.d.ts @@ -35,6 +35,7 @@ declare namespace Document { * ``` */ declare class Document_ extends Container { + nodes: Root[] parent: undefined type: 'document' From 8322d112e860d9e9bc9521d47f94cdbd54143449 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Wei=C3=9Fenfels?= <10491533+tim-we@users.noreply.github.com> Date: Sat, 3 Feb 2024 16:32:11 +0100 Subject: [PATCH 8/9] Fix visitor test --- test/visitor.test.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/visitor.test.ts b/test/visitor.test.ts index 251a64ca2..e2a663872 100755 --- a/test/visitor.test.ts +++ b/test/visitor.test.ts @@ -18,9 +18,9 @@ import postcss, { function hasAlready(parent: Container | undefined, selector: string): boolean { if (typeof parent === 'undefined') return false - return parent.nodes.some(i => { + return parent.nodes?.some(i => { return i.type === 'rule' && i.selectors.includes(selector) - }) + }) ?? false } function addIndex(array: any[][]): any[][] { @@ -1559,9 +1559,9 @@ test('append works after reassigning nodes through .parent', async () => { OnceExit(root) { let firstNode = root.nodes[0] as AtRule let secondNode = root.nodes[1] as AtRule - let rule2 = secondNode.nodes[0] + let rule2 = secondNode.nodes![0] rule2.parent!.nodes = rule2.parent!.nodes - firstNode.append(...secondNode.nodes) + firstNode.append(...secondNode.nodes!) secondNode.remove() }, @@ -1580,7 +1580,7 @@ test('append works after reassigning nodes through .parent', async () => { let atrule = rule.nodes[0] - atrule.append(rule.clone({ nodes: [] }).append(...atrule.nodes)) + atrule.append(rule.clone({ nodes: [] }).append(...atrule.nodes!)) rule.after(atrule) rule.remove() From 9dd5a9337edfbfcb55722f13f56a961396cc4c5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Wei=C3=9Fenfels?= <10491533+tim-we@users.noreply.github.com> Date: Sat, 3 Feb 2024 16:32:35 +0100 Subject: [PATCH 9/9] Fix at-rule test --- test/at-rule.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/at-rule.test.ts b/test/at-rule.test.ts index acffeb952..fff3a13c8 100755 --- a/test/at-rule.test.ts +++ b/test/at-rule.test.ts @@ -24,7 +24,7 @@ test('creates nodes property on prepend()', () => { type(rule.nodes, 'undefined') rule.prepend('color: black') - is(rule.nodes.length, 1) + is(rule.nodes?.length, 1) }) test('creates nodes property on append()', () => { @@ -32,7 +32,7 @@ test('creates nodes property on append()', () => { type(rule.nodes, 'undefined') rule.append('color: black') - is(rule.nodes.length, 1) + is(rule.nodes?.length, 1) }) test('inserts default spaces', () => {