esprima.js 695 B

12345678910111213141516171819202122232425262728
  1. "use strict";
  2. // This module is suitable for passing as options.parser when calling
  3. // recast.parse to process ECMAScript code with Esprima:
  4. //
  5. // const ast = recast.parse(source, {
  6. // parser: require("recast/parsers/esprima")
  7. // });
  8. //
  9. const getOption = require("../lib/util.js").getOption;
  10. exports.parse = function (source, options) {
  11. const comments = [];
  12. const ast = require("esprima").parse(source, {
  13. loc: true,
  14. locations: true,
  15. comment: true,
  16. onComment: comments,
  17. tolerant: getOption(options, "tolerant", true),
  18. tokens: getOption(options, "tokens", true)
  19. });
  20. if (! Array.isArray(ast.comments)) {
  21. ast.comments = comments;
  22. }
  23. return ast;
  24. };