1
- 'use strict'
1
+ import nodeAssert from 'assert'
2
+ import { inspect } from './inspect.js'
2
3
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 )
21
8
22
9
// Identifier to check if a value is seen.
23
10
var ID = '__unist__'
24
11
25
12
// List of specced properties.
26
- var defined = [ 'type' , 'value' , 'children' , 'position' ]
13
+ var defined = new Set ( [ 'type' , 'value' , 'children' , 'position' ] )
27
14
28
15
// Wrapper around `Node` which adds the current node (and parent, if available),
29
16
// to the message.
30
- function wrap ( fn ) {
17
+ export function wrap ( fn ) {
31
18
return wrapped
32
19
33
20
function wrapped ( node , parent ) {
@@ -50,43 +37,43 @@ function wrap(fn) {
50
37
}
51
38
52
39
// Assert.
53
- function unist ( node ) {
40
+ function assertNode ( node ) {
54
41
var type
55
42
var children
56
43
var value
57
44
var key
58
45
var index
59
46
var length
60
47
61
- assert . ok ( object ( node ) , 'node should be an object' )
48
+ nodeAssert . ok ( node === Object ( node ) , 'node should be an object' )
62
49
63
50
type = node . type
64
51
children = node . children
65
52
value = node . value
66
53
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' )
70
57
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' )
73
60
}
74
61
75
62
position ( node . position )
76
63
77
64
for ( key in node ) {
78
- if ( ! defined . includes ( key ) ) {
65
+ if ( ! defined . has ( key ) ) {
79
66
vanilla ( key , node [ key ] )
80
67
}
81
68
}
82
69
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' )
85
72
index = - 1
86
73
length = children . length
87
74
88
75
while ( ++ index < length ) {
89
- exports ( children [ index ] , node )
76
+ assert ( children [ index ] , node )
90
77
}
91
78
}
92
79
}
@@ -95,50 +82,53 @@ function unist(node) {
95
82
// same (deep) value.
96
83
function vanilla ( key , value ) {
97
84
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' )
101
88
}
102
89
}
103
90
104
91
// Stringify a value to inspect it.
105
92
// Tries `JSON.stringify()`, and if that fails uses `String()` instead.
106
93
function view ( value ) {
107
94
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 {
112
98
return String ( value )
113
99
}
114
100
}
115
101
116
102
// Assert `node` is a parent node.
117
103
function parent ( node ) {
118
- unist ( node )
104
+ assertNode ( node )
119
105
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`' )
122
112
}
123
113
124
114
// Assert `node` is a text node.
125
115
function text ( node ) {
126
- unist ( node )
116
+ assertNode ( node )
127
117
128
- assert . strictEqual (
118
+ nodeAssert . strictEqual (
129
119
'children' in node ,
130
120
false ,
131
121
'text should not have `children`'
132
122
)
133
- assert . ok ( 'value' in node , 'text should have `value`' )
123
+ nodeAssert . ok ( 'value' in node , 'text should have `value`' )
134
124
}
135
125
136
126
// Assert `node` is a unist node, but neither parent nor text.
137
127
function empty ( node ) {
138
- unist ( node )
128
+ assertNode ( node )
139
129
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 (
142
132
'children' in node ,
143
133
false ,
144
134
'void should not have `children`'
@@ -147,8 +137,11 @@ function empty(node) {
147
137
148
138
// Assert `position` is a unist position.
149
139
function 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
+ )
152
145
153
146
point ( position . start , 'position.start' )
154
147
point ( position . end , 'position.end' )
@@ -157,20 +150,26 @@ function position(position) {
157
150
158
151
// Assert `point` is a unist point.
159
152
function 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' )
162
155
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`' )
166
162
}
167
163
168
- if ( point . column != null ) {
169
- assert . ok (
164
+ if ( point . column !== null && point . column !== undefined ) {
165
+ nodeAssert . ok (
170
166
'column' in point ,
171
167
'`' + name + '` should have numeric `column`'
172
168
)
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
+ )
174
173
}
175
174
}
176
175
}
0 commit comments