2
2
* @typedef {import('unist').Parent } UnistParent
3
3
* @typedef {import('nlcst').Root } Root
4
4
* @typedef {import('nlcst').Content } Content
5
- * @typedef {Root|Content } Node
5
+ */
6
+
7
+ /**
8
+ * @typedef {Root | Content } Node
6
9
* @typedef {Extract<Node, UnistParent> } Parent
7
10
*/
8
11
@@ -50,19 +53,25 @@ const pairs = {
50
53
const open = Object . keys ( pairs )
51
54
52
55
/**
53
- * Check if the node in `parent` at `position ` is enclosed by matching
56
+ * Check if the child in `parent` at `index ` is enclosed by matching
54
57
* delimiters.
55
58
*
56
- * @param {Parent } parent
57
- * @param {number } index
59
+ * @template {Parent} ParentType
60
+ * Parent node.
61
+ * @param {ParentType } parent
62
+ * Parent node.
63
+ * @param {number | ParentType['children'][number] } index
64
+ * Child node of parent or index of child in parent.
58
65
* @returns {boolean }
66
+ * Whether the child is a literal.
59
67
*/
60
68
export function isLiteral ( parent , index ) {
61
69
if ( ! ( parent && parent . children ) ) {
62
70
throw new Error ( 'Parent must be a node' )
63
71
}
64
72
65
73
if ( index !== null && typeof index === 'object' && 'type' in index ) {
74
+ // @ts -expect-error: assume child of `parent`.
66
75
index = parent . children . indexOf ( index )
67
76
68
77
if ( index === - 1 ) {
@@ -86,27 +95,41 @@ export function isLiteral(parent, index) {
86
95
/**
87
96
* Check if the node in `parent` at `position` is enclosed by matching
88
97
* delimiters.
98
+ *
89
99
* @param {Parent } parent
100
+ * Parent node.
90
101
* @param {number } position
91
- * @returns {Node|void }
102
+ * Position to look around.
103
+ * @returns {boolean }
104
+ * Whether a child is wrapped.
92
105
*/
93
106
function isWrapped ( parent , position ) {
94
107
const previous = siblingDelimiter ( parent , position , - 1 , open )
95
108
96
109
if ( previous ) {
97
- return siblingDelimiter ( parent , position , 1 , pairs [ toString ( previous ) ] )
110
+ return (
111
+ siblingDelimiter ( parent , position , 1 , pairs [ toString ( previous ) ] ) !==
112
+ undefined
113
+ )
98
114
}
115
+
116
+ return false
99
117
}
100
118
101
119
/**
102
120
* Find the previous or next delimiter before or after `position` in `parent`.
103
121
* Returns the delimiter node when found.
104
122
*
105
123
* @param {Parent } parent
124
+ * Parent node.
106
125
* @param {number } position
126
+ * Start position in `parent`.
107
127
* @param {number } step
128
+ * Step (`-1` to move back, `1` to move forward).
108
129
* @param {Array<string> } delimiters
109
- * @returns {Node|void }
130
+ * Delimiters to look for.
131
+ * @returns {Node | void }
132
+ * Delimiter, if found.
110
133
*/
111
134
function siblingDelimiter ( parent , position , step , delimiters ) {
112
135
let index = position + step
@@ -115,7 +138,7 @@ function siblingDelimiter(parent, position, step, delimiters) {
115
138
const sibling = parent . children [ index ]
116
139
117
140
if ( sibling . type === 'WordNode' || sibling . type === 'SourceNode' ) {
118
- return
141
+ break
119
142
}
120
143
121
144
if ( sibling . type !== 'WhiteSpaceNode' ) {
@@ -127,17 +150,24 @@ function siblingDelimiter(parent, position, step, delimiters) {
127
150
}
128
151
129
152
/**
130
- * Check if parent contains word- nodes between `start` and `end` (both
153
+ * Check if parent contains word nodes between `start` and `end` (both
131
154
* excluding).
155
+ *
132
156
* @param {Parent } parent
157
+ * Parent node.
133
158
* @param {number } start
159
+ * Start index in `parent` (excluding).
134
160
* @param {number } end
135
- * @returns {boolean|void }
161
+ * End index in `parent` (excluding).
162
+ * @returns {boolean }
163
+ * Whether a child contains a word.
136
164
*/
137
165
function containsWord ( parent , start , end ) {
138
166
while ( ++ start < end ) {
139
167
if ( parent . children [ start ] . type === 'WordNode' ) {
140
168
return true
141
169
}
142
170
}
171
+
172
+ return false
143
173
}
0 commit comments