8000 Allow to pass undefined to adding methods to simplify type check · postcss/postcss@65075df · GitHub
[go: up one dir, main page]

8000
Skip to content

Commit 65075df

Browse files
committed
Allow to pass undefined to adding methods to simplify type check
1 parent 477b3bb commit 65075df

File tree

4 files changed

+70
-25
lines changed

4 files changed

+70
-25
lines changed

lib/container.d.ts

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,15 @@ declare abstract class Container_<Child extends Node = ChildNode> extends Node {
6666
* @return This node for methods chain.
6767
*/
6868
append(
69-
...nodes: (ChildProps | ChildProps[] | Node | Node[] | string | string[])[]
69+
...nodes: (
70+
| ChildProps
71+
| ChildProps[]
72+
| Node
73+
| Node[]
74+
| string
75+
| string[]
76+
| undefined
77+
)[]
7078
): this
7179

7280
assign(overrides: Container.ContainerProps | object): this
@@ -146,7 +154,14 @@ declare abstract class Container_<Child extends Node = ChildNode> extends Node {
146154
*/
147155
insertAfter(
148156
oldNode: Child | number,
149-
newNode: Child | Child[] | ChildProps | ChildProps[] | string | string[]
157+
newNode:
158+
| Child
159+
| Child[]
160+
| ChildProps
161+
| ChildProps[]
162+
| string
163+
| string[]
164+
| undefined
150165
): this
151166
/**
152167
* Insert new node before old node within the container.
@@ -161,7 +176,14 @@ declare abstract class Container_<Child extends Node = ChildNode> extends Node {
161176
*/
162177
insertBefore(
163178
oldNode: Child | number,
164-
newNode: Child | Child[] | ChildProps | ChildProps[] | string | string[]
179+
newNode:
180+
| Child
181+
| Child[]
182+
| ChildProps
183+
| ChildProps[]
184+
| string
185+
| string[]
186+
| undefined
165187
): this
166188

167189
/**
@@ -202,7 +224,15 @@ declare abstract class Container_<Child extends Node = ChildNode> extends Node {
202224
* @return This node for methods chain.
203225
*/
204226
prepend(
205-
...nodes: (ChildProps | ChildProps[] | Node | Node[] | string | string[])[]
227+
...nodes: (
228+
| ChildProps
229+
| ChildProps[]
230+
| Node
231+
| Node[]
232+
| string
233+
| string[]
234+
| undefined
235+
)[]
206236
): this
207237
/**
208238
* Add child to the end of the node.
@@ -447,6 +477,8 @@ declare abstract class Container_<Child extends Node = ChildNode> extends Node {
447477
get last(): Child | undefined
448478
}
449479

450-
declare class Container<Child extends Node = ChildNode> extends Container_<Child> {}
480+
declare class Container<
481+
Child extends Node = ChildNode
482+
> extends Container_<Child> {}
451483

452484
export = Container

lib/container.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ class Container extends Node {
173173
normalize(nodes, sample) {
174174
if (typeof nodes === 'string') {
175175
nodes = cleanSource(parse(nodes).nodes)
176+
} else if (typeof nodes === 'undefined') {
177+
nodes = []
176178
} else if (Array.isArray(nodes)) {
177179
nodes = nodes.slice(0)
178180
for (let i of nodes) {

lib/node.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ declare abstract class Node_ {
246246
* @param newNode New node.
247247
* @return This node for methods chain.
248248
*/
249-
after(newNode: Node | Node.ChildProps | Node[] | string): this
249+
after(newNode: Node | Node.ChildProps | Node[] | string | undefined): this
250250

251251
/**
252252
* It assigns properties to an existing node instance.
@@ -273,7 +273,7 @@ declare abstract class Node_ {
273273
* @param newNode New node.
274274
* @return This node for methods chain.
275275
*/
276-
before(newNode: Node | Node.ChildProps | Node[] | string): this
276+
before(newNode: Node | Node.ChildProps | Node[] | string | undefined): this
277277

278278
/**
279279
* Clear the code style properties for the node and its children.
@@ -531,6 +531,6 @@ declare abstract class Node_ {
531531
warn(result: Result, message: string, options?: WarningOptions): Warning
532532
}
533533

534-
declare class Node extends Node_ { }
534+
declare class Node extends Node_ {}
535535

536536
export = Node

test/container.test.ts

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -638,18 +638,18 @@ test('insertBefore() receives array', () => {
638638

639639
test('insertBefore() receives pre-existing child node - a', () => {
640640
let a = parse('a{ align-items: start; color: red; z-index: 1 }')
641-
let declA = (a.first as Rule).nodes[0];
642-
let declC = (a.first as Rule).nodes[2];
643-
declC.before(declA);
641+
let declA = (a.first as Rule).nodes[0]
642+
let declC = (a.first as Rule).nodes[2]
643+
declC.before(declA)
644644

645645
is(a.toString(), 'a{ color: red; align-items: start; z-index: 1 }')
646646
})
647647

648648
test('insertBefore() receives pre-existing child node - b', () => {
649649
let a = parse('a{ align-items: start; color: red; z-index: 1 }')
650-
let declA = (a.first as Rule).nodes[0];
651-
let declC = (a.first as Rule).nodes[2];
652-
declA.before(declC);
650+
let declA = (a.first as Rule).nodes[0]
651+
let declC = (a.first as Rule).nodes[2]
652+
declA.before(declC)
653653

654654
is(a.toString(), 'a{ z-index: 1; align-items: start; color: red }')
655655
})
@@ -708,18 +708,18 @@ test('insertAfter() receives array', () => {
708708

709709
test('insertAfter() receives pre-existing child node - a', () => {
710710
let a = parse('a{ align-items: start; color: red; z-index: 1 }')
711-
let declA = (a.first as Rule).nodes[0];
712-
let declC = (a.first as Rule).nodes[2];
713-
declC.after(declA);
711+
let declA = (a.first as Rule).nodes[0]
712+
let declC = (a.first as Rule).nodes[2]
713+
declC.after(declA)
714714

715715
is(a.toString(), 'a{ color: red; z-index: 1; align-items: start }')
716716
})
717717

718718
test('insertAfter() receives pre-existing child node - b', () => {
719719
let a = parse('a{ align-items: start; color: red; z-index: 1 }')
720-
let declA = (a.first as Rule).nodes[0];
721-
let declC = (a.first as Rule).nodes[2];
722-
declA.after(declC);
720+
let declA = (a.first as Rule).nodes[0]
721+
let declC = (a.first as Rule).nodes[2]
722+
declA.after(declC)
723723

724724
is(a.toString(), 'a{ align-items: start; z-index: 1; color: red }')
725725
})
@@ -874,7 +874,7 @@ test('allows to clone nodes', () => {
874874

875875
test('container.nodes can be sorted', () => {
876876
let root = parse('@b; @c; @a;')
877-
let b = root.nodes[0];
877+
let b = root.nodes[0]
878878

879879
root.nodes.sort((x, y) => {
880880
return (x as AtRule).name.localeCompare((y as AtRule).name)
@@ -884,19 +884,30 @@ test('container.nodes can be sorted', () => {
884884
is(root.toString(), ' @a;@b; @c;')
885885

886886
// Sorted nodes are reflected in "walk".
887-
let result: string[] = [];
888-
root.walkAtRules((atRule) => {
887+
let result: string[] = []
888+
root.walkAtRules(atRule => {
889889
result.push(atRule.name.trim())
890-
});
890+
})
891891

892892
is(result.join(' '), 'a b c')
893893

894894
// Sorted nodes have the corect "index".
895895
is(root.index(b), 1)
896896

897897
// Inserting after a sorted node results in the correct order.
898-
b.after('@d;');
898+
b.after('@d;')
899899
is(root.toString(), ' @a;@b;@d; @c;')
900900
})
901901

902+
test('ignores undefined on adding', () => {
903+
let rule = parse('a { a: 1; b: 2 }').first as Rule
904+
rule.append({ prop: 'c', value: '3' }, undefined)
905+
rule.prepend(undefined)
906+
rule.insertAfter(0, undefined)
907+
rule.insertBefore(0, undefined)
908+
rule.after(undefined)
909+
rule.before(undefined)
910+
is(rule.parent!.toString(), 'a { a: 1; b: 2; c: 3 }')
911+
})
912+
902913
test.run()

0 commit comments

Comments
 (0)
0