89 lines
2.6 KiB
JavaScript
89 lines
2.6 KiB
JavaScript
var acorn = require('acorn')
|
|
var xtend = require('xtend')
|
|
|
|
// acorn-node@1.x expects acorn@7+ which provides a static `Parser.extend`.
|
|
// Some dependency trees (like this Pulsar package) end up with an older acorn
|
|
// that only exposes the Parser constructor without the `extend` helper.
|
|
//
|
|
// Provide a small polyfill so acorn-node can load and operate.
|
|
if (acorn && acorn.Parser && typeof acorn.Parser.extend !== 'function') {
|
|
var ensureParserStatics = function (ParserCtor) {
|
|
if (!ParserCtor) return
|
|
|
|
if (typeof ParserCtor.parse !== 'function') {
|
|
ParserCtor.parse = function (input, options) {
|
|
return new ParserCtor(options, input).parse()
|
|
}
|
|
}
|
|
|
|
if (typeof ParserCtor.parseExpressionAt !== 'function') {
|
|
ParserCtor.parseExpressionAt = function (input, pos, options) {
|
|
var p = new ParserCtor(options, input, pos)
|
|
p.nextToken()
|
|
return p.parseExpression()
|
|
}
|
|
}
|
|
|
|
if (typeof ParserCtor.tokenizer !== 'function') {
|
|
ParserCtor.tokenizer = function (input, options) {
|
|
return new ParserCtor(options, input)
|
|
}
|
|
}
|
|
}
|
|
|
|
var extend = function () {
|
|
var Parser = this
|
|
for (var i = 0; i < arguments.length; i++) {
|
|
var plugin = arguments[i]
|
|
if (plugin && typeof plugin !== 'function' && typeof plugin.default === 'function') {
|
|
plugin = plugin.default
|
|
}
|
|
if (typeof plugin === 'function') {
|
|
Parser = plugin(Parser)
|
|
Parser.extend = extend
|
|
ensureParserStatics(Parser)
|
|
}
|
|
}
|
|
return Parser
|
|
}
|
|
|
|
acorn.Parser.extend = extend
|
|
ensureParserStatics(acorn.Parser)
|
|
}
|
|
|
|
var CJSParser = acorn.Parser
|
|
.extend(require('./lib/bigint'))
|
|
.extend(require('./lib/class-fields'))
|
|
.extend(require('./lib/static-class-features'))
|
|
.extend(require('./lib/numeric-separator'))
|
|
.extend(require('./lib/dynamic-import').default)
|
|
var ESModulesParser = CJSParser
|
|
.extend(require('./lib/export-ns-from'))
|
|
.extend(require('./lib/import-meta'))
|
|
|
|
function mapOptions (opts) {
|
|
if (!opts) opts = {}
|
|
return xtend({
|
|
ecmaVersion: 2020,
|
|
allowHashBang: true,
|
|
allowReturnOutsideFunction: true
|
|
}, opts)
|
|
}
|
|
|
|
function getParser (opts) {
|
|
if (!opts) opts = {}
|
|
return opts.sourceType === 'module' ? ESModulesParser : CJSParser
|
|
}
|
|
|
|
module.exports = exports = xtend(acorn, {
|
|
parse: function parse (src, opts) {
|
|
return getParser(opts).parse(src, mapOptions(opts))
|
|
},
|
|
parseExpressionAt: function parseExpressionAt (src, offset, opts) {
|
|
return getParser(opts).parseExpressionAt(src, offset, mapOptions(opts))
|
|
},
|
|
tokenizer: function tokenizer (src, opts) {
|
|
return getParser(opts).tokenizer(src, mapOptions(opts))
|
|
}
|
|
})
|