char-class-classranges-to-chars-transform.js 604 B

123456789101112131415161718192021222324252627282930
  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 simplify character classes
  8. * spanning only one or two chars.
  9. *
  10. * [a-a] -> [a]
  11. * [a-b] -> [ab]
  12. */
  13. module.exports = {
  14. ClassRange: function ClassRange(path) {
  15. var node = path.node;
  16. if (node.from.codePoint === node.to.codePoint) {
  17. path.replace(node.from);
  18. } else if (node.from.codePoint === node.to.codePoint - 1) {
  19. path.getParent().insertChildAt(node.to, path.index + 1);
  20. path.replace(node.from);
  21. }
  22. }
  23. };