1
1
/**
2
2
* @typedef {import('unist').Node } Node
3
+ * @typedef {import('mdast').Root } Root
3
4
*/
4
5
6
+ import assert from 'node:assert'
5
7
import test from 'tape'
6
8
import remark from 'remark'
7
9
import { findBefore } from './index.js'
8
10
9
- var tree = remark ( ) . parse ( 'Some *emphasis*, **importance**, and `code`.' )
10
- var paragraph = tree . children [ 0 ]
11
- var children = paragraph . children
12
-
13
11
test ( 'unist-util-find-before' , function ( t ) {
12
+ /** @type {Root } */
13
+ // @ts -expect-error: fine.
14
+ var tree = remark ( ) . parse ( 'Some *emphasis*, **importance**, and `code`.' )
15
+
16
+ assert ( tree . type === 'root' )
17
+ var paragraph = tree . children [ 0 ]
18
+ assert ( paragraph . type === 'paragraph' )
19
+ var head = paragraph . children [ 0 ]
20
+ assert ( head . type === 'text' )
21
+ var next = paragraph . children [ 1 ]
22
+ assert ( next . type === 'emphasis' )
23
+
14
24
t . throws (
15
25
function ( ) {
16
26
// @ts -ignore runtime
@@ -73,13 +83,13 @@ test('unist-util-find-before', function (t) {
73
83
)
74
84
75
85
t . strictEqual (
76
- findBefore ( paragraph , children [ 1 ] ) ,
77
- children [ 0 ] ,
86
+ findBefore ( paragraph , paragraph . children [ 1 ] ) ,
87
+ head ,
78
88
'should return the preceding node when without `test` (#1)'
79
89
)
80
90
t . strictEqual (
81
91
findBefore ( paragraph , 1 ) ,
82
- children [ 0 ] ,
92
+ head ,
83
93
'should return the preceding node when without `test` (#2)'
84
94
)
85
95
t . strictEqual (
@@ -89,39 +99,39 @@ test('unist-util-find-before', function (t) {
89
99
)
90
100
91
101
t . strictEqual (
92
- findBefore ( paragraph , 100 , children [ 0 ] ) ,
93
- children [ 0 ] ,
102
+ findBefore ( paragraph , 100 , head ) ,
103
+ head ,
94
104
'should return `node` when given a `node` and existing (#1)'
95
105
)
96
106
t . strictEqual (
97
- findBefore ( paragraph , children [ 1 ] , children [ 0 ] ) ,
98
- children [ 0 ] ,
107
+ findBefore ( paragraph , paragraph . children [ 1 ] , head ) ,
108
+ head ,
99
109
'should return `node` when given a `node` and existing (#2)'
100
110
)
101
111
t . strictEqual (
102
- findBefore ( paragraph , 1 , children [ 0 ] ) ,
103
- children [ 0 ] ,
112
+ findBefore ( paragraph , 1 , head ) ,
113
+ head ,
104
114
'should return `node` when given a `node` and existing (#3)'
105
115
)
106
116
t . strictEqual (
107
- findBefore ( paragraph , children [ 0 ] , children [ 0 ] ) ,
117
+ findBefore ( paragraph , head , head ) ,
108
118
null ,
109
119
'should return `node` when given a `node` and existing (#4)'
110
120
)
111
121
t . strictEqual (
112
- findBefore ( paragraph , 0 , children [ 0 ] ) ,
122
+ findBefore ( paragraph , 0 , head ) ,
113
123
null ,
114
124
'should return `node` when given a `node` and existing (#5)'
115
125
)
116
126
t . strictEqual (
117
- findBefore ( paragraph , 1 , children [ 1 ] ) ,
127
+ findBefore ( paragraph , 1 , next ) ,
118
128
null ,
119
129
'should return `node` when given a `node` and existing (#6)'
120
130
)
121
131
122
132
t . strictEqual (
123
133
findBefore ( paragraph , 100 , 'strong' ) ,
124
- children [ 3 ] ,
134
+ paragraph . children [ 3 ] ,
125
135
'should return a child when given a `type` and existing (#1)'
126
136
)
127
137
t . strictEqual (
@@ -130,19 +140,19 @@ test('unist-util-find-before', function (t) {
130
140
'should return a child when given a `type` and existing (#2)'
131
141
)
132
142
t . strictEqual (
133
- findBefore ( paragraph , children [ 4 ] , 'strong' ) ,
134
- children [ 3 ] ,
143
+ findBefore ( paragraph , paragraph . children [ 4 ] , 'strong' ) ,
144
+ paragraph . children [ 3 ] ,
135
145
'should return a child when given a `type` and existing (#3)'
136
146
)
137
147
t . strictEqual (
138
- findBefore ( paragraph , children [ 3 ] , 'strong' ) ,
148
+ findBefore ( paragraph , paragraph . children [ 3 ] , 'strong' ) ,
139
149
null ,
140
150
'should return a child when given a `type` and existing (#4)'
141
151
)
142
152
143
153
t . strictEqual (
144
154
findBefore ( paragraph , 100 , test ) ,
145
- children [ 3 ] ,
155
+ paragraph . children [ 3 ] ,
146
156
'should return a child when given a `test` and existing (#1)'
147
157
)
148
158
t . strictEqual (
@@ -151,12 +161,12 @@ test('unist-util-find-before', function (t) {
151
161
'should return a child when given a `test` and existing (#2)'
152
162
)
153
163
t . strictEqual (
154
- findBefore ( paragraph , children [ 4 ] , test ) ,
155
- children [ 3 ] ,
164
+ findBefore ( paragraph , paragraph . children [ 4 ] , test ) ,
165
+ paragraph . children [ 3 ] ,
156
166
'should return a child when given a `test` and existing (#3)'
157
167
)
158
168
t . strictEqual (
159
- findBefore ( paragraph , children [ 3 ] , test ) ,
169
+ findBefore ( paragraph , paragraph . children [ 3 ] , test ) ,
160
170
null ,
161
171
'should return a child when given a `test` and existing (#4)'
162
172
)
0 commit comments