2222import { embedded } from 'hast-util-embedded'
2323import { convertElement } from 'hast-util-is-element'
2424import { phrasing } from 'hast-util-phrasing'
25+ // @ts -expect-error: to do type.
2526import toString from 'hast-util-to-string'
2627import { whitespace } from 'hast-util-whitespace'
2728import { toString as nlcstToString } from 'nlcst-to-string'
2829import { pointStart } from 'unist-util-position'
2930import vfileLocation from 'vfile-location'
3031
31- const push = [ ] . push
32-
3332const source = convertElement ( [ 'code' , dataNlcstSourced ] )
3433const ignore = convertElement ( [
3534 'script' ,
@@ -181,7 +180,7 @@ export function toNlcst(tree, file, Parser) {
181180 function implicit ( children ) {
182181 let index = - 1
183182 let start = - 1
184- /** @type {boolean } */
183+ /** @type {boolean|undefined } */
185184 let viable
186185
187186 while ( ++ index <= children . length ) {
@@ -203,7 +202,7 @@ export function toNlcst(tree, file, Parser) {
203202 find ( child )
204203 }
205204
206- viable = null
205+ viable = undefined
207206 start = - 1
208207 }
209208 }
@@ -213,12 +212,12 @@ export function toNlcst(tree, file, Parser) {
213212 * Convert `node` (hast) to nlcst.
214213 *
215214 * @param {Node } node
216- * @returns {Array.<UnistNode> }
215+ * @returns {Array.<UnistNode>|undefined }
217216 */
218217 function one ( node ) {
219- /** @type {Array.<UnistNode> } */
218+ /** @type {Array.<UnistNode>|undefined } */
220219 let replacement
221- /** @type {boolean } */
220+ /** @type {boolean|undefined } */
222221 let change
223222
224223 if ( node . type === 'text' ) {
@@ -239,7 +238,7 @@ export function toNlcst(tree, file, Parser) {
239238 }
240239 }
241240
242- return change
241+ return change && replacement
243242 ? patch ( replacement , location , location . toOffset ( pointStart ( node ) ) )
244243 : replacement
245244 }
@@ -256,7 +255,7 @@ export function toNlcst(tree, file, Parser) {
256255 let index = - 1
257256
258257 while ( ++ index < children . length ) {
259- push . apply ( results , one ( children [ index ] ) || [ ] )
258+ results . push ( ... ( one ( children [ index ] ) || [ ] ) )
260259 }
261260
262261 return results
@@ -306,13 +305,13 @@ export function toNlcst(tree, file, Parser) {
306305 * @returns {boolean }
307306 */
308307function dataNlcstSourced ( node ) {
309- return node . properties . dataNlcst === 'source'
308+ return Boolean ( node . properties && node . properties . dataNlcst === 'source' )
310309}
311310
312311/**
313312 * @param {Element } node
314313 * @returns {boolean }
315314 */
316315function dataNlcstIgnore ( node ) {
317- return node . properties . dataNlcst === 'ignore'
316+ return Boolean ( node . properties && node . properties . dataNlcst === 'ignore' )
318317}
0 commit comments