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.

64 lines
1.5 KiB

4 years ago
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.parseSync = parseSync;
  6. exports.parseAsync = parseAsync;
  7. exports.parse = void 0;
  8. var _config = _interopRequireDefault(require("./config"));
  9. var _normalizeFile = _interopRequireDefault(require("./transformation/normalize-file"));
  10. var _normalizeOpts = _interopRequireDefault(require("./transformation/normalize-opts"));
  11. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  12. const parse = function parse(code, opts, callback) {
  13. if (typeof opts === "function") {
  14. callback = opts;
  15. opts = undefined;
  16. }
  17. if (callback === undefined) return parseSync(code, opts);
  18. const config = (0, _config.default)(opts);
  19. if (config === null) {
  20. return null;
  21. }
  22. const cb = callback;
  23. process.nextTick(() => {
  24. let ast = null;
  25. try {
  26. const cfg = (0, _config.default)(opts);
  27. if (cfg === null) return cb(null, null);
  28. ast = (0, _normalizeFile.default)(cfg.passes, (0, _normalizeOpts.default)(cfg), code).ast;
  29. } catch (err) {
  30. return cb(err);
  31. }
  32. cb(null, ast);
  33. });
  34. };
  35. exports.parse = parse;
  36. function parseSync(code, opts) {
  37. const config = (0, _config.default)(opts);
  38. if (config === null) {
  39. return null;
  40. }
  41. return (0, _normalizeFile.default)(config.passes, (0, _normalizeOpts.default)(config), code).ast;
  42. }
  43. function parseAsync(code, opts) {
  44. return new Promise((res, rej) => {
  45. parse(code, opts, (err, result) => {
  46. if (err == null) res(result);else rej(err);
  47. });
  48. });
  49. }