1- 'use strict'
1+ import nodeAssert from 'assert'
2+ import { inspect } from './inspect.js'
23
3- var assert = require ( 'assert' )
4- var array = require ( 'x-is-array' )
5- var object = require ( 'x-is-object' )
6-
7- var inspect
8-
9- try {
10- // eslint-disable-next-line no-useless-concat
11- inspect = require ( 'ut' + 'il' ) . inspect
12- } catch ( _ ) { }
13-
14- exports = wrap ( unist )
15- module . exports = exports
16-
17- exports . parent = wrap ( parent )
18- exports . text = wrap ( text )
19- exports . void = wrap ( empty )
20- exports . wrap = wrap
4+ export var assert = wrap ( assertNode )
5+ assert . parent = wrap ( parent )
6+ assert . text = wrap ( text )
7+ assert . void = wrap ( empty )
218
229// Identifier to check if a value is seen.
2310var ID = '__unist__'
2411
2512// List of specced properties.
26- var defined = [ 'type' , 'value' , 'children' , 'position' ]
13+ var defined = new Set ( [ 'type' , 'value' , 'children' , 'position' ] )
2714
2815// Wrapper around `Node` which adds the current node (and parent, if available),
2916// to the message.
30- function wrap ( fn ) {
17+ export function wrap ( fn ) {
3118 return wrapped
3219
3320 function wrapped ( node , parent ) {
@@ -50,43 +37,43 @@ function wrap(fn) {
5037}
5138
5239// Assert.
53- function unist ( node ) {
40+ function assertNode ( node ) {
5441 var type
5542 var children
5643 var value
5744 var key
5845 var index
5946 var length
6047
61- assert . ok ( object ( node ) , 'node should be an object' )
48+ nodeAssert . ok ( node === Object ( node ) , 'node should be an object' )
6249
6350 type = node . type
6451 children = node . children
6552 value = node . value
6653
67- assert . ok ( 'type' in node , 'node should have a type' )
68- assert . strictEqual ( typeof type , 'string' , '`type` should be a string' )
69- assert . notStrictEqual ( type , '' , '`type` should not be empty' )
54+ nodeAssert . ok ( 'type' in node , 'node should have a type' )
55+ nodeAssert . strictEqual ( typeof type , 'string' , '`type` should be a string' )
56+ nodeAssert . notStrictEqual ( type , '' , '`type` should not be empty' )
7057
71- if ( value != null ) {
72- assert . strictEqual ( typeof value , 'string' , '`value` should be a string' )
58+ if ( value !== null && value !== undefined ) {
59+ nodeAssert . strictEqual ( typeof value , 'string' , '`value` should be a string' )
7360 }
7461
7562 position ( node . position )
7663
7764 for ( key in node ) {
78- if ( ! defined . includes ( key ) ) {
65+ if ( ! defined . has ( key ) ) {
7966 vanilla ( key , node [ key ] )
8067 }
8168 }
8269
83- if ( children != null ) {
84- assert . ok ( array ( children ) , '`children` should be an array' )
70+ if ( children !== null && children !== undefined ) {
71+ nodeAssert . ok ( Array . isArray ( children ) , '`children` should be an array' )
8572 index = - 1
8673 length = children . length
8774
8875 while ( ++ index < length ) {
89- exports ( children [ index ] , node )
76+ assert ( children [ index ] , node )
9077 }
9178 }
9279}
@@ -95,50 +82,53 @@ function unist(node) {
9582// same (deep) value.
9683function vanilla ( key , value ) {
9784 try {
98- assert . deepStrictEqual ( value , JSON . parse ( JSON . stringify ( value ) ) )
99- } catch ( _ ) {
100- assert . fail ( 'non-specced property `' + key + '` should be JSON' )
85+ nodeAssert . deepStrictEqual ( value , JSON . parse ( JSON . stringify ( value ) ) )
86+ } catch {
87+ nodeAssert . fail ( 'non-specced property `' + key + '` should be JSON' )
10188 }
10289}
10390
10491// Stringify a value to inspect it.
10592// Tries `JSON.stringify()`, and if that fails uses `String()` instead.
10693function view ( value ) {
10794 try {
108- /* istanbul ignore next - Browser. */
109- return inspect ? inspect ( value , { colors : false } ) : JSON . stringify ( value )
110- } catch ( _ ) {
111- /* istanbul ignore next - Cyclical. */
95+ return inspect ( value )
96+ /* c8 ignore next 3 */
97+ } catch {
11298 return String ( value )
11399 }
114100}
115101
116102// Assert `node` is a parent node.
117103function parent ( node ) {
118- unist ( node )
104+ assertNode ( node )
119105
120- assert . strictEqual ( 'value' in node , false , 'parent should not have `value`' )
121- assert . ok ( 'children' in node , 'parent should have `children`' )
106+ nodeAssert . strictEqual (
107+ 'value' in node ,
108+ false ,
109+ 'parent should not have `value`'
110+ )
111+ nodeAssert . ok ( 'children' in node , 'parent should have `children`' )
122112}
123113
124114// Assert `node` is a text node.
125115function text ( node ) {
126- unist ( node )
116+ assertNode ( node )
127117
128- assert . strictEqual (
118+ nodeAssert . strictEqual (
129119 'children' in node ,
130120 false ,
131121 'text should not have `children`'
132122 )
133- assert . ok ( 'value' in node , 'text should have `value`' )
123+ nodeAssert . ok ( 'value' in node , 'text should have `value`' )
134124}
135125
136126// Assert `node` is a unist node, but neither parent nor text.
137127function empty ( node ) {
138- unist ( node )
128+ assertNode ( node )
139129
140- assert . strictEqual ( 'value' in node , false , 'void should not have `value`' )
141- assert . strictEqual (
130+ nodeAssert . strictEqual ( 'value' in node , false , 'void should not have `value`' )
131+ nodeAssert . strictEqual (
142132 'children' in node ,
143133 false ,
144134 'void should not have `children`'
@@ -147,8 +137,11 @@ function empty(node) {
147137
148138// Assert `position` is a unist position.
149139function position ( position ) {
150- if ( position != null ) {
151- assert . ok ( object ( position ) , '`position` should be an object' )
140+ if ( position !== null && position !== undefined ) {
141+ nodeAssert . ok (
142+ position === Object ( position ) ,
143+ '`position` should be an object'
144+ )
152145
153146 point ( position . start , 'position.start' )
154147 point ( position . end , 'position.end' )
@@ -157,20 +150,26 @@ function position(position) {
157150
158151// Assert `point` is a unist point.
159152function point ( point , name ) {
160- if ( point != null ) {
161- assert . ok ( object ( point ) , '`' + name + '` should be an object' )
153+ if ( point !== null && point !== undefined ) {
154+ nodeAssert . ok ( point === Object ( point ) , '`' + name + '` should be an object' )
162155
163- if ( point . line != null ) {
164- assert . ok ( 'line' in point , '`' + name + '` should have numeric `line`' )
165- assert . ok ( point . line >= 1 , '`' + name + '.line` should be gte `1`' )
156+ if ( point . line !== null && point . line !== undefined ) {
157+ nodeAssert . ok (
158+ 'line' in point ,
159+ '`' + name + '` should have numeric `line`'
160+ )
161+ nodeAssert . ok ( point . line >= 1 , '`' + name + '.line` should be gte `1`' )
166162 }
167163
168- if ( point . column != null ) {
169- assert . ok (
164+ if ( point . column !== null && point . column !== undefined ) {
165+ nodeAssert . ok (
170166 'column' in point ,
171167 '`' + name + '` should have numeric `column`'
172168 )
173- assert . ok ( point . column >= 1 , '`' + name + '.column` should be gte `1`' )
169+ nodeAssert . ok (
170+ point . column >= 1 ,
171+ '`' + name + '.column` should be gte `1`'
172+ )
174173 }
175174 }
176175}
0 commit comments