/*!
* base-extract-comments <https://github.com/tunnckoCore/base-extract-comments>
*
* Copyright (c) 2016 Charlike Mike Reagent <@tunnckoCore> (http://www.tunnckocore.tk)
* Released under the MIT license.
*/
'use strict'
var utils = require('./utils')
module.exports = function baseExtractComments (opts) {
return function extractComments (app) {
// i tried both is-valid-app and is-registered here as `isValid`
if (!utils.isValid(app, 'base-extract-comments')) return
this.use(utils.plugins())
this.define('extractComments', function parse (input, options) {
if (typeof input === 'object') {
this.options = utils.extend({}, this.options, input)
input = null
}
this.cache = utils.extend({}, this.cache)
this.cache.input = input || this.cache.input
this.options = utils.extend({
preserve: false,
block: true,
line: false
}, opts, this.options, options, {
locations: true,
unwrap: true
})
this.cache.comments = utils.extract(this.cache.input, this.options).comments
var len = this.cache.comments.length
var idx = -1
while (++idx < len) {
// not so cool workaround,
// but we can't pass different than object currently
// waiting `use` PR
this.run({
current: this.cache.comments[idx],
index: idx,
next: this.cache.comments[idx + 1]
})
}
return this.cache.comments
})
}
}
test.js
/*!
* base-extract-comments <https://github.com/tunnckoCore/base-extract-comments>
*
* Copyright (c) 2016 Charlike Mike Reagent <@tunnckoCore> (http://www.tunnckocore.tk)
* Released under the MIT license.
*/
'use strict'
var fs = require('fs')
var test = require('mukla')
var Base = require('base')
var plugin = require('./index')
test('base-extract-comments:', function (done) {
var app = new Base({isApp: true}, {})
app.use(plugin())
app.use(function (app) {
return function (comment) {
comment = comment.current // workaround
console.log('actual')
}
})
app.extractComments(fs.readFileSync('./fixture.js', 'utf8'))
done()
})
index.js
test.js
utils.js
part of the package.json