1
1
/**
2
- * @typedef {import('unist ').Literal<number> } ExampleLiteral
3
- * @typedef {import('unist ').Parent<ExampleLiteral> } ExampleParent
2
+ * @typedef {import('mdast ').Emphasis } Emphasis
3
+ * @typedef {import('mdast ').PhrasingContent } PhrasingContent
4
4
*/
5
5
6
6
import assert from 'node:assert/strict'
@@ -32,93 +32,98 @@ test('modifyChildren', async function (t) {
32
32
} )
33
33
34
34
await t . test ( 'should call `fn` for each child in `parent`' , function ( ) {
35
+ /** @type {Array<PhrasingContent> } */
35
36
const children = [
36
- { type : 'x ' , value : 0 } ,
37
- { type : 'x ' , value : 1 } ,
38
- { type : 'x ' , value : 2 } ,
39
- { type : 'x ' , value : 3 }
37
+ { type : 'text ' , value : '0' } ,
38
+ { type : 'text ' , value : '1' } ,
39
+ { type : 'text ' , value : '2' } ,
40
+ { type : 'text ' , value : '3' }
40
41
]
41
- /** @type {ExampleParent } */
42
- const context = { type : 'y ' , children}
42
+ /** @type {Emphasis } */
43
+ const context = { type : 'emphasis ' , children}
43
44
let n = - 1
44
45
45
46
modifyChildren ( function ( child , index , parent ) {
46
47
n ++
47
48
assert . strictEqual ( child , children [ n ] )
48
49
assert . strictEqual ( index , n )
49
50
assert . strictEqual ( parent , context )
50
- return undefined
51
51
} ) ( context )
52
52
} )
53
53
54
54
await t . test ( 'should work when new children are added' , function ( ) {
55
+ /** @type {Array<PhrasingContent> } */
55
56
const children = [
56
- { type : 'x ' , value : 0 } ,
57
- { type : 'x ' , value : 1 } ,
58
- { type : 'x ' , value : 2 } ,
59
- { type : 'x ' , value : 3 } ,
60
- { type : 'x ' , value : 4 } ,
61
- { type : 'x ' , value : 5 } ,
62
- { type : 'x ' , value : 6 }
57
+ { type : 'text ' , value : '0' } ,
58
+ { type : 'text ' , value : '1' } ,
59
+ { type : 'text ' , value : '2' } ,
60
+ { type : 'text ' , value : '3' } ,
61
+ { type : 'text ' , value : '4' } ,
62
+ { type : 'text ' , value : '5' } ,
63
+ { type : 'text ' , value : '6' }
63
64
]
64
- /** @type {ExampleParent } */
65
+ /** @type {Emphasis } */
65
66
const parent = {
66
- type : 'y ' ,
67
+ type : 'emphasis ' ,
67
68
children : [
68
- { type : 'x ' , value : 0 } ,
69
- { type : 'x ' , value : 1 } ,
70
- { type : 'x ' , value : 2 } ,
71
- { type : 'x ' , value : 3 }
69
+ { type : 'text ' , value : '0' } ,
70
+ { type : 'text ' , value : '1' } ,
71
+ { type : 'text ' , value : '2' } ,
72
+ { type : 'text ' , value : '3' }
72
73
]
73
74
}
74
75
let n = - 1
75
76
76
77
modifyChildren (
77
78
/**
78
- * @param {ExampleLiteral } child
79
- * @param {ExampleParent } parent
79
+ * @param {PhrasingContent } child
80
+ * @param {Emphasis } parent
80
81
*/
81
82
function ( child , index , parent ) {
82
83
n ++
83
84
84
85
if ( index < 3 ) {
85
- parent . children . push ( { type : 'x' , value : parent . children . length } )
86
+ parent . children . push ( {
87
+ type : 'text' ,
88
+ value : String ( parent . children . length )
89
+ } )
86
90
}
87
91
88
92
assert . deepEqual ( child , children [ n ] )
89
93
assert . deepEqual ( index , n )
90
- return undefined
91
94
}
92
95
) ( parent )
93
96
} )
94
97
95
98
await t . test ( 'should skip forwards' , function ( ) {
99
+ /** @type {Array<PhrasingContent> } */
96
100
const children = [
97
- { type : 'x ' , value : 0 } ,
98
- { type : 'x ' , value : 1 } ,
99
- { type : 'x ' , value : 2 } ,
100
- { type : 'x ' , value : 3 }
101
+ { type : 'text ' , value : '0' } ,
102
+ { type : 'text ' , value : '1' } ,
103
+ { type : 'text ' , value : '2' } ,
104
+ { type : 'text ' , value : '3' }
101
105
]
106
+ /** @type {Emphasis } */
102
107
const context = {
103
- type : 'y ' ,
108
+ type : 'emphasis ' ,
104
109
children : [
105
- { type : 'x ' , value : 0 } ,
106
- { type : 'x ' , value : 1 } ,
107
- { type : 'x ' , value : 3 }
110
+ { type : 'text ' , value : '0' } ,
111
+ { type : 'text ' , value : '1' } ,
112
+ { type : 'text ' , value : '3' }
108
113
]
109
114
}
110
115
let n = - 1
111
116
112
117
modifyChildren (
113
118
/**
114
- * @param {ExampleLiteral } child
115
- * @param {ExampleParent } parent
119
+ * @param {PhrasingContent } child
120
+ * @param {Emphasis } parent
116
121
*/
117
122
function ( child , index , parent ) {
118
123
assert . deepEqual ( child , children [ ++ n ] )
119
124
120
- if ( child . value === 1 ) {
121
- parent . children . splice ( index + 1 , 0 , { type : 'x ' , value : 2 } )
125
+ if ( 'value' in child && child . value === '1' ) {
126
+ parent . children . splice ( index + 1 , 0 , { type : 'text ' , value : '2' } )
122
127
return index + 1
123
128
}
124
129
}
@@ -129,48 +134,49 @@ test('modifyChildren', async function (t) {
129
134
130
135
await t . test ( 'should skip backwards' , function ( ) {
131
136
const calls = [
132
- { type : 'x ' , value : 0 } ,
133
- { type : 'x ' , value : 1 } ,
134
- { type : 'x ' , value : - 1 } ,
135
- { type : 'x ' , value : 0 } ,
136
- { type : 'x ' , value : 1 } ,
137
- { type : 'x ' , value : 2 } ,
138
- { type : 'x ' , value : 3 }
137
+ { type : 'text ' , value : '0' } ,
138
+ { type : 'text ' , value : '1' } ,
139
+ { type : 'text ' , value : '-1' } ,
140
+ { type : 'text ' , value : '0' } ,
141
+ { type : 'text ' , value : '1' } ,
142
+ { type : 'text ' , value : '2' } ,
143
+ { type : 'text ' , value : '3' }
139
144
]
145
+ /** @type {Emphasis } */
140
146
const context = {
141
- type : 'y ' ,
147
+ type : 'emphasis ' ,
142
148
children : [
143
- { type : 'x ' , value : 0 } ,
144
- { type : 'x ' , value : 1 } ,
145
- { type : 'x ' , value : 2 } ,
146
- { type : 'x ' , value : 3 }
149
+ { type : 'text ' , value : '0' } ,
150
+ { type : 'text ' , value : '1' } ,
151
+ { type : 'text ' , value : '2' } ,
152
+ { type : 'text ' , value : '3' }
147
153
]
148
154
}
149
155
let n = - 1
150
156
let inserted = false
151
157
152
158
modifyChildren (
153
159
/**
154
- * @param {ExampleLiteral } child
155
- * @param {ExampleParent } parent
160
+ * @param {PhrasingContent } child
161
+ * @param {Emphasis } parent
156
162
*/
157
163
function ( child , _ , parent ) {
158
164
assert . deepEqual ( child , calls [ ++ n ] )
159
165
160
- if ( ! inserted && child . value === 1 ) {
166
+ if ( ! inserted && 'value' in child && child . value === '1' ) {
161
167
inserted = true
162
- parent . children . unshift ( { type : 'x ' , value : - 1 } )
168
+ parent . children . unshift ( { type : 'text ' , value : '-1' } )
163
169
return - 1
164
170
}
165
171
}
166
172
) ( context )
167
173
168
174
assert . deepEqual ( context . children , [
169
- { type : 'x ' , value : - 1 } ,
170
- { type : 'x ' , value : 0 } ,
171
- { type : 'x ' , value : 1 } ,
172
- { type : 'x ' , value : 2 } ,
173
- { type : 'x ' , value : 3 }
175
+ { type : 'text ' , value : '-1' } ,
176
+ { type : 'text ' , value : '0' } ,
177
+ { type : 'text ' , value : '1' } ,
178
+ { type : 'text ' , value : '2' } ,
179
+ { type : 'text ' , value : '3' }
174
180
] )
175
181
} )
176
182
} )
0 commit comments