char-class-remove-duplicates-transform.js 702 B

123456789101112131415161718192021222324252627282930313233
  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 duplicates from character classes.
  8. */
  9. module.exports = {
  10. CharacterClass: function CharacterClass(path) {
  11. var node = path.node;
  12. var sources = {};
  13. for (var i = 0; i < node.expressions.length; i++) {
  14. var childPath = path.getChild(i);
  15. var source = childPath.jsonEncode();
  16. if (sources.hasOwnProperty(source)) {
  17. childPath.remove();
  18. // Since we remove the current node.
  19. // TODO: make it simpler for users with a method.
  20. i--;
  21. }
  22. sources[source] = true;
  23. }
  24. }
  25. };