Skip to content

Commit

Permalink
Merge pull request #1922 from tim-we/improve-at-rule-types
Browse files Browse the repository at this point in the history
Improve AtRule#nodes type
  • Loading branch information
ai committed Feb 5, 2024
2 parents 1a906e5 + 9dd5a93 commit bb0314a
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 8 deletions.
19 changes: 19 additions & 0 deletions lib/at-rule.d.ts
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion lib/container.d.ts
Expand Up @@ -43,7 +43,7 @@ declare abstract class Container_<Child extends Node = ChildNode> extends Node {
* root.nodes[0].nodes[0].prop //=> 'color'
* ```
*/
nodes: Child[]
nodes: Child[] | undefined

/**
* Inserts new nodes to the end of the container.
Expand Down
1 change: 1 addition & 0 deletions lib/document.d.ts
Expand Up @@ -35,6 +35,7 @@ declare namespace Document {
* ```
*/
declare class Document_ extends Container<Root> {
nodes: Root[]
parent: undefined
type: 'document'

Expand Down
1 change: 1 addition & 0 deletions lib/root.d.ts
Expand Up @@ -54,6 +54,7 @@ declare namespace Root {
* ```
*/
declare class Root_ extends Container {
nodes: NonNullable<Container['nodes']>
parent: Document | undefined
raws: Root.RootRaws
type: 'root'
Expand Down
1 change: 1 addition & 0 deletions lib/rule.d.ts
Expand Up @@ -69,6 +69,7 @@ declare namespace Rule {
* ```
*/
declare class Rule_ extends Container {
nodes: NonNullable<Container['nodes']>
parent: Container | undefined
raws: Rule.RuleRaws
/**
Expand Down
10 changes: 8 additions & 2 deletions test/at-rule.test.ts
Expand Up @@ -24,15 +24,15 @@ 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()', () => {
let rule = new AtRule()
type(rule.nodes, 'undefined')

rule.append('color: black')
is(rule.nodes.length, 1)
is(rule.nodes?.length, 1)
})

test('inserts default spaces', () => {
Expand All @@ -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()
10 changes: 5 additions & 5 deletions test/visitor.test.ts
Expand Up @@ -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[][] {
Expand Down Expand Up @@ -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()
},

Expand All @@ -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()
Expand Down

0 comments on commit bb0314a

Please sign in to comment.