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.

56 lines
2.0 KiB

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