jsutils.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. __atomic_acorn = require('./acorn');
  2. __atomic_beautify = require('./beautify');
  3. exports.parseToJSON = function (source) {
  4. var start = new Date().getTime();
  5. var comments = [];
  6. var ast = __atomic_acorn.parse( source, {
  7. // collect ranges for each node
  8. ranges: true,
  9. locations: true,
  10. onComment: comments,
  11. });
  12. var end = new Date().getTime();
  13. var time = end - start;
  14. // print('Acorn parse time: ' + time);
  15. ast["comments"] = comments;
  16. start = new Date().getTime();
  17. ast = JSON.stringify(ast);
  18. end = new Date().getTime();
  19. time = end - start;
  20. // print('JSON.stringify time: ' + time);
  21. return ast;
  22. }
  23. exports.parseErrorCheck = function(source) {
  24. try {
  25. __atomic_acorn.parse( source, {
  26. ranges: true,
  27. locations: true,
  28. });
  29. } catch(e) {
  30. //if (!(e instanceof __atomic_acorn.SyntaxError)) throw e;
  31. if (!(e instanceof SyntaxError))
  32. return false; // rethrow?
  33. return e;
  34. }
  35. return false;
  36. }
  37. // TODO: options
  38. exports.jsBeautify = function (source) {
  39. return __atomic_beautify.js_beautify(source);
  40. }