jsutils.js 1.4 KB

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