2022-01-21 08:28:41 +00:00
|
|
|
'use strict'
|
2022-04-07 07:06:43 +00:00
|
|
|
|
|
|
|
const directiveParser = require('./directive-parser.js')
|
|
|
|
|
2022-01-21 08:28:41 +00:00
|
|
|
module.exports = {
|
|
|
|
stack: [],
|
2022-04-07 07:06:43 +00:00
|
|
|
pop (current) {
|
|
|
|
const index = this.stack.indexOf(current)
|
2022-01-21 08:28:41 +00:00
|
|
|
if (index !== -1) {
|
|
|
|
this.stack.splice(index, 1)
|
|
|
|
}
|
2022-04-07 07:06:43 +00:00
|
|
|
|
2022-01-21 08:28:41 +00:00
|
|
|
if (!current.preserve) {
|
|
|
|
current.source.remove()
|
|
|
|
}
|
|
|
|
},
|
2022-04-07 07:06:43 +00:00
|
|
|
parse (node, lazyResult, callback) {
|
|
|
|
let current
|
|
|
|
const metadata = directiveParser(node)
|
2022-01-21 08:28:41 +00:00
|
|
|
if (metadata) {
|
|
|
|
if (!metadata.begin && metadata.end) {
|
2022-04-07 07:06:43 +00:00
|
|
|
this.walk((item) => {
|
2022-01-21 08:28:41 +00:00
|
|
|
if (metadata.name === item.metadata.name) {
|
|
|
|
this.pop(item)
|
2022-04-07 07:06:43 +00:00
|
|
|
current = { metadata, directive: item.directive, source: node, preserve: item.preserve }
|
2022-01-21 08:28:41 +00:00
|
|
|
return false
|
|
|
|
}
|
2022-04-07 07:06:43 +00:00
|
|
|
})
|
2022-01-21 08:28:41 +00:00
|
|
|
} else {
|
2022-04-07 07:06:43 +00:00
|
|
|
current = { metadata, directive: null, source: node, preserve: null }
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (current === undefined) {
|
2022-04-07 07:06:43 +00:00
|
|
|
lazyResult.warn(`found end "${metadata.name}" without a matching begin.`, { node: node })
|
2022-01-21 08:28:41 +00:00
|
|
|
} else if (callback(current)) {
|
|
|
|
this.stack.push(current)
|
|
|
|
} else if (!current.preserve) {
|
|
|
|
current.source.remove()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2022-04-07 07:06:43 +00:00
|
|
|
walk (callback) {
|
|
|
|
let len = this.stack.length
|
2022-01-21 08:28:41 +00:00
|
|
|
while (--len > -1) {
|
|
|
|
if (!callback(this.stack[len])) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|