main.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.run = exports.prettyPrint = exports.print = exports.visit = exports.types = exports.parse = void 0;
  4. var tslib_1 = require("tslib");
  5. var fs_1 = tslib_1.__importDefault(require("fs"));
  6. var types = tslib_1.__importStar(require("ast-types"));
  7. exports.types = types;
  8. var parser_1 = require("./lib/parser");
  9. Object.defineProperty(exports, "parse", { enumerable: true, get: function () { return parser_1.parse; } });
  10. var printer_1 = require("./lib/printer");
  11. /**
  12. * Traverse and potentially modify an abstract syntax tree using a
  13. * convenient visitor syntax:
  14. *
  15. * recast.visit(ast, {
  16. * names: [],
  17. * visitIdentifier: function(path) {
  18. * var node = path.value;
  19. * this.visitor.names.push(node.name);
  20. * this.traverse(path);
  21. * }
  22. * });
  23. */
  24. var ast_types_1 = require("ast-types");
  25. Object.defineProperty(exports, "visit", { enumerable: true, get: function () { return ast_types_1.visit; } });
  26. /**
  27. * Reprint a modified syntax tree using as much of the original source
  28. * code as possible.
  29. */
  30. function print(node, options) {
  31. return new printer_1.Printer(options).print(node);
  32. }
  33. exports.print = print;
  34. /**
  35. * Print without attempting to reuse any original source code.
  36. */
  37. function prettyPrint(node, options) {
  38. return new printer_1.Printer(options).printGenerically(node);
  39. }
  40. exports.prettyPrint = prettyPrint;
  41. /**
  42. * Convenient command-line interface (see e.g. example/add-braces).
  43. */
  44. function run(transformer, options) {
  45. return runFile(process.argv[2], transformer, options);
  46. }
  47. exports.run = run;
  48. function runFile(path, transformer, options) {
  49. fs_1.default.readFile(path, "utf-8", function (err, code) {
  50. if (err) {
  51. console.error(err);
  52. return;
  53. }
  54. runString(code, transformer, options);
  55. });
  56. }
  57. function defaultWriteback(output) {
  58. process.stdout.write(output);
  59. }
  60. function runString(code, transformer, options) {
  61. var writeback = (options && options.writeback) || defaultWriteback;
  62. transformer((0, parser_1.parse)(code, options), function (node) {
  63. writeback(print(node, options).code);
  64. });
  65. }