版博士V2.0程序
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

51 строка
1.4 KiB

  1. // src/index.ts
  2. import MagicStringBase from "magic-string";
  3. export * from "magic-string";
  4. var MagicString = class extends MagicStringBase {
  5. removeNode(node, { offset = 0 } = {}) {
  6. if (isEmptyNodes(node))
  7. return this;
  8. super.remove(...getNodePos(node, offset));
  9. return this;
  10. }
  11. moveNode(node, index, { offset = 0 } = {}) {
  12. if (isEmptyNodes(node))
  13. return this;
  14. super.move(...getNodePos(node, offset), index);
  15. return this;
  16. }
  17. sliceNode(node, { offset = 0 } = {}) {
  18. if (isEmptyNodes(node))
  19. return "";
  20. return super.slice(...getNodePos(node, offset));
  21. }
  22. overwriteNode(node, content, { offset = 0, ...options } = {}) {
  23. if (isEmptyNodes(node))
  24. return this;
  25. const _content = typeof content === "string" ? content : this.sliceNode(content);
  26. super.overwrite(...getNodePos(node, offset), _content, options);
  27. return this;
  28. }
  29. snipNode(node, { offset = 0 } = {}) {
  30. if (isEmptyNodes(node))
  31. return new MagicStringBase("", {
  32. // @ts-expect-error
  33. filename: super.filename
  34. });
  35. return super.snip(...getNodePos(node, offset));
  36. }
  37. };
  38. function getNodePos(nodes, offset) {
  39. if (Array.isArray(nodes))
  40. return [offset + nodes[0].start, offset + nodes.slice(-1)[0].end];
  41. else
  42. return [offset + nodes.start, offset + nodes.end];
  43. }
  44. function isEmptyNodes(nodes) {
  45. return Array.isArray(nodes) && nodes.length === 0;
  46. }
  47. export {
  48. MagicString,
  49. MagicStringBase
  50. };