File tree Expand file tree Collapse file tree 3 files changed +29
-4
lines changed Expand file tree Collapse file tree 3 files changed +29
-4
lines changed Original file line number Diff line number Diff line change @@ -89,6 +89,14 @@ function isLiteral(parent, index) {
89
89
throw new Error ( 'Parent must be a node' )
90
90
}
91
91
92
+ if ( index !== null && typeof index === 'object' && 'type' in index ) {
93
+ index = parent . children . indexOf ( index )
94
+
95
+ if ( index === - 1 ) {
96
+ throw new Error ( 'Node must be a child of `parent`' )
97
+ }
98
+ }
99
+
92
100
if ( isNaN ( index ) ) {
93
101
throw new Error ( 'Index must be a number' )
94
102
}
Original file line number Diff line number Diff line change 75
75
76
76
## API
77
77
78
- ### ` isLiteral(parent, index) `
78
+ ### ` isLiteral(parent, index|child ) `
79
79
80
- Check if the node in ` parent ` at ` position ` is enclosed
81
- by matching delimiters .
80
+ Check if the ` child ` in ` parent ` is enclosed by matching delimiters.
81
+ If ` index ` is given, the [ child ] [ ] of ` parent ` at that [ index ] [ ] is checked .
82
82
83
83
For example, ` foo ` is literal in the following samples:
84
84
@@ -141,3 +141,7 @@ abide by its terms.
141
141
[ coc ] : https://github.com/syntax-tree/.github/blob/master/code-of-conduct.md
142
142
143
143
[ nlcst ] : https://github.com/syntax-tree/nlcst
144
+
145
+ [ index ] : https://github.com/syntax-tree/unist#index
146
+
147
+ [ child ] : https://github.com/syntax-tree/unist#child
Original file line number Diff line number Diff line change @@ -28,9 +28,22 @@ test('isLiteral()', function(t) {
28
28
isLiteral ( { children : [ ] } )
29
29
} ,
30
30
/ I n d e x m u s t b e a n u m b e r / ,
31
- 'should throw without index '
31
+ 'should throw without node '
32
32
)
33
33
34
+ t . throws (
35
+ function ( ) {
36
+ isLiteral ( { children : [ ] } , { type : 'a' } )
37
+ } ,
38
+ / N o d e m u s t b e a c h i l d o f ` p a r e n t ` / ,
39
+ 'should throw if `node` is not in `parent`'
40
+ )
41
+
42
+ t . doesNotThrow ( function ( ) {
43
+ var n = { type : 'a' }
44
+ isLiteral ( { children : [ n ] } , n )
45
+ } , 'should not throw if `node` is in `parent`' )
46
+
34
47
t . doesNotThrow ( function ( ) {
35
48
process ( 'Well? Ha! Funky' , function ( node , index , parent ) {
36
49
assert . strictEqual ( isLiteral ( parent , index ) , false )
You can’t perform that action at this time.
0 commit comments