11/**
22 * @typedef {import('unist').Node } Node
3+ * @typedef {import('mdast').Root } Root
34 */
45
6+ import assert from 'node:assert'
57import test from 'tape'
68import remark from 'remark'
79import { findBefore } from './index.js'
810
9- var tree = remark ( ) . parse ( 'Some *emphasis*, **importance**, and `code`.' )
10- var paragraph = tree . children [ 0 ]
11- var children = paragraph . children
12-
1311test ( '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+
1424 t . throws (
1525 function ( ) {
1626 // @ts -ignore runtime
@@ -73,13 +83,13 @@ test('unist-util-find-before', function (t) {
7383 )
7484
7585 t . strictEqual (
76- findBefore ( paragraph , children [ 1 ] ) ,
77- children [ 0 ] ,
86+ findBefore ( paragraph , paragraph . children [ 1 ] ) ,
87+ head ,
7888 'should return the preceding node when without `test` (#1)'
7989 )
8090 t . strictEqual (
8191 findBefore ( paragraph , 1 ) ,
82- children [ 0 ] ,
92+ head ,
8393 'should return the preceding node when without `test` (#2)'
8494 )
8595 t . strictEqual (
@@ -89,39 +99,39 @@ test('unist-util-find-before', function (t) {
8999 )
90100
91101 t . strictEqual (
92- findBefore ( paragraph , 100 , children [ 0 ] ) ,
93- children [ 0 ] ,
102+ findBefore ( paragraph , 100 , head ) ,
103+ head ,
94104 'should return `node` when given a `node` and existing (#1)'
95105 )
96106 t . strictEqual (
97- findBefore ( paragraph , children [ 1 ] , children [ 0 ] ) ,
98- children [ 0 ] ,
107+ findBefore ( paragraph , paragraph . children [ 1 ] , head ) ,
108+ head ,
99109 'should return `node` when given a `node` and existing (#2)'
100110 )
101111 t . strictEqual (
102- findBefore ( paragraph , 1 , children [ 0 ] ) ,
103- children [ 0 ] ,
112+ findBefore ( paragraph , 1 , head ) ,
113+ head ,
104114 'should return `node` when given a `node` and existing (#3)'
105115 )
106116 t . strictEqual (
107- findBefore ( paragraph , children [ 0 ] , children [ 0 ] ) ,
117+ findBefore ( paragraph , head , head ) ,
108118 null ,
109119 'should return `node` when given a `node` and existing (#4)'
110120 )
111121 t . strictEqual (
112- findBefore ( paragraph , 0 , children [ 0 ] ) ,
122+ findBefore ( paragraph , 0 , head ) ,
113123 null ,
114124 'should return `node` when given a `node` and existing (#5)'
115125 )
116126 t . strictEqual (
117- findBefore ( paragraph , 1 , children [ 1 ] ) ,
127+ findBefore ( paragraph , 1 , next ) ,
118128 null ,
119129 'should return `node` when given a `node` and existing (#6)'
120130 )
121131
122132 t . strictEqual (
123133 findBefore ( paragraph , 100 , 'strong' ) ,
124- children [ 3 ] ,
134+ paragraph . children [ 3 ] ,
125135 'should return a child when given a `type` and existing (#1)'
126136 )
127137 t . strictEqual (
@@ -130,19 +140,19 @@ test('unist-util-find-before', function (t) {
130140 'should return a child when given a `type` and existing (#2)'
131141 )
132142 t . strictEqual (
133- findBefore ( paragraph , children [ 4 ] , 'strong' ) ,
134- children [ 3 ] ,
143+ findBefore ( paragraph , paragraph . children [ 4 ] , 'strong' ) ,
144+ paragraph . children [ 3 ] ,
135145 'should return a child when given a `type` and existing (#3)'
136146 )
137147 t . strictEqual (
138- findBefore ( paragraph , children [ 3 ] , 'strong' ) ,
148+ findBefore ( paragraph , paragraph . children [ 3 ] , 'strong' ) ,
139149 null ,
140150 'should return a child when given a `type` and existing (#4)'
141151 )
142152
143153 t . strictEqual (
144154 findBefore ( paragraph , 100 , test ) ,
145- children [ 3 ] ,
155+ paragraph . children [ 3 ] ,
146156 'should return a child when given a `test` and existing (#1)'
147157 )
148158 t . strictEqual (
@@ -151,12 +161,12 @@ test('unist-util-find-before', function (t) {
151161 'should return a child when given a `test` and existing (#2)'
152162 )
153163 t . strictEqual (
154- findBefore ( paragraph , children [ 4 ] , test ) ,
155- children [ 3 ] ,
164+ findBefore ( paragraph , paragraph . children [ 4 ] , test ) ,
165+ paragraph . children [ 3 ] ,
156166 'should return a child when given a `test` and existing (#3)'
157167 )
158168 t . strictEqual (
159- findBefore ( paragraph , children [ 3 ] , test ) ,
169+ findBefore ( paragraph , paragraph . children [ 3 ] , test ) ,
160170 null ,
161171 'should return a child when given a `test` and existing (#4)'
162172 )
0 commit comments