You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
2.2 KiB

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