char-plus-to-star-transform.js 743 B

1234567891011121314151617181920212223242526272829303132333435
  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 replace `a+` to `aa*`, since NFA/DFA
  8. * handles Kleene-closure `a*`, and `a+` is just a syntactic sugar.
  9. */
  10. module.exports = {
  11. Repetition: function Repetition(path) {
  12. var node = path.node,
  13. parent = path.parent;
  14. if (node.quantifier.kind !== '+') {
  15. return;
  16. }
  17. if (parent.type === 'Alternative') {
  18. path.getParent().insertChildAt(node.expression, path.index);
  19. } else {
  20. path.replace({
  21. type: 'Alternative',
  22. expressions: [node.expression, node]
  23. });
  24. }
  25. // Change quantifier.
  26. node.quantifier.kind = '*';
  27. }
  28. };