remove-empty-group-transform.js 599 B

12345678910111213141516171819202122232425262728293031323334
  1. /**
  2. * The MIT License (MIT)
  3. * Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
  4. */
  5. 'use strict';
  6. /**
  7. * A regexp-tree plugin to remove non-capturing empty groups.
  8. *
  9. * /(?:)a/ -> /a/
  10. * /a|(?:)/ -> /a|/
  11. */
  12. module.exports = {
  13. Group: function Group(path) {
  14. var node = path.node,
  15. parent = path.parent;
  16. var childPath = path.getChild();
  17. if (node.capturing || childPath) {
  18. return;
  19. }
  20. if (parent.type === 'Repetition') {
  21. path.getParent().replace(node);
  22. } else if (parent.type !== 'RegExp') {
  23. path.remove();
  24. }
  25. }
  26. };