index.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = _default;
  6. function t() {
  7. const data = _interopRequireWildcard(require("@babel/types"));
  8. t = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
  14. const visitor = {
  15. Scope(path, state) {
  16. if (state.kind === "let") path.skip();
  17. },
  18. Function(path) {
  19. path.skip();
  20. },
  21. VariableDeclaration(path, state) {
  22. if (state.kind && path.node.kind !== state.kind) return;
  23. const nodes = [];
  24. const declarations = path.get("declarations");
  25. let firstId;
  26. for (const declar of declarations) {
  27. firstId = declar.node.id;
  28. if (declar.node.init) {
  29. nodes.push(t().expressionStatement(t().assignmentExpression("=", declar.node.id, declar.node.init)));
  30. }
  31. for (const name of Object.keys(declar.getBindingIdentifiers())) {
  32. state.emit(t().identifier(name), name, declar.node.init !== null);
  33. }
  34. }
  35. if (path.parentPath.isFor({
  36. left: path.node
  37. })) {
  38. path.replaceWith(firstId);
  39. } else {
  40. path.replaceWithMultiple(nodes);
  41. }
  42. }
  43. };
  44. function _default(path, emit, kind = "var") {
  45. path.traverse(visitor, {
  46. kind,
  47. emit
  48. });
  49. }