index.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. var acorn = require('acorn')
  2. var xtend = require('xtend')
  3. var CJSParser = acorn.Parser
  4. .extend(require('./lib/bigint'))
  5. .extend(require('./lib/class-fields'))
  6. .extend(require('./lib/static-class-features'))
  7. .extend(require('./lib/numeric-separator'))
  8. .extend(require('./lib/dynamic-import').default)
  9. var ESModulesParser = CJSParser
  10. .extend(require('./lib/export-ns-from'))
  11. .extend(require('./lib/import-meta'))
  12. function mapOptions (opts) {
  13. if (!opts) opts = {}
  14. return xtend({
  15. ecmaVersion: 2020,
  16. allowHashBang: true,
  17. allowReturnOutsideFunction: true
  18. }, opts)
  19. }
  20. function getParser (opts) {
  21. if (!opts) opts = {}
  22. return opts.sourceType === 'module' ? ESModulesParser : CJSParser
  23. }
  24. module.exports = exports = xtend(acorn, {
  25. parse: function parse (src, opts) {
  26. return getParser(opts).parse(src, mapOptions(opts))
  27. },
  28. parseExpressionAt: function parseExpressionAt (src, offset, opts) {
  29. return getParser(opts).parseExpressionAt(src, offset, mapOptions(opts))
  30. },
  31. tokenizer: function tokenizer (src, opts) {
  32. return getParser(opts).tokenizer(src, mapOptions(opts))
  33. }
  34. })