char-escape-unescape-transform.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 unnecessary escape.
  8. *
  9. * \e -> e
  10. *
  11. * [\(] -> [(]
  12. */
  13. module.exports = {
  14. _hasXFlag: false,
  15. init: function init(ast) {
  16. this._hasXFlag = ast.flags.includes('x');
  17. },
  18. Char: function Char(path) {
  19. var node = path.node;
  20. if (!node.escaped) {
  21. return;
  22. }
  23. if (shouldUnescape(path, this._hasXFlag)) {
  24. delete node.escaped;
  25. }
  26. }
  27. };
  28. function shouldUnescape(path, hasXFlag) {
  29. var value = path.node.value,
  30. index = path.index,
  31. parent = path.parent;
  32. // In char class (, etc are allowed.
  33. if (parent.type !== 'CharacterClass' && parent.type !== 'ClassRange') {
  34. return !preservesEscape(value, index, parent, hasXFlag);
  35. }
  36. return !preservesInCharClass(value, index, parent);
  37. }
  38. /**
  39. * \], \\, \^, \-
  40. */
  41. function preservesInCharClass(value, index, parent) {
  42. if (value === '^') {
  43. // Avoid [\^a] turning into [^a]
  44. return index === 0 && !parent.negative;
  45. }
  46. if (value === '-') {
  47. // Avoid [a\-z] turning into [a-z]
  48. return true;
  49. }
  50. return (/[\]\\]/.test(value)
  51. );
  52. }
  53. function preservesEscape(value, index, parent, hasXFlag) {
  54. if (value === '{') {
  55. return preservesOpeningCurlyBraceEscape(index, parent);
  56. }
  57. if (value === '}') {
  58. return preservesClosingCurlyBraceEscape(index, parent);
  59. }
  60. if (hasXFlag && /[ #]/.test(value)) {
  61. return true;
  62. }
  63. return (/[*[()+?^$./\\|]/.test(value)
  64. );
  65. }
  66. function consumeNumbers(startIndex, parent, rtl) {
  67. var i = startIndex;
  68. var siblingNode = (rtl ? i >= 0 : i < parent.expressions.length) && parent.expressions[i];
  69. while (siblingNode && siblingNode.type === 'Char' && siblingNode.kind === 'simple' && !siblingNode.escaped && /\d/.test(siblingNode.value)) {
  70. rtl ? i-- : i++;
  71. siblingNode = (rtl ? i >= 0 : i < parent.expressions.length) && parent.expressions[i];
  72. }
  73. return Math.abs(startIndex - i);
  74. }
  75. function isSimpleChar(node, value) {
  76. return node && node.type === 'Char' && node.kind === 'simple' && !node.escaped && node.value === value;
  77. }
  78. function preservesOpeningCurlyBraceEscape(index, parent) {
  79. // (?:\{) -> (?:{)
  80. if (index == null) {
  81. return false;
  82. }
  83. var nbFollowingNumbers = consumeNumbers(index + 1, parent);
  84. var i = index + nbFollowingNumbers + 1;
  85. var nextSiblingNode = i < parent.expressions.length && parent.expressions[i];
  86. if (nbFollowingNumbers) {
  87. // Avoid \{3} turning into {3}
  88. if (isSimpleChar(nextSiblingNode, '}')) {
  89. return true;
  90. }
  91. if (isSimpleChar(nextSiblingNode, ',')) {
  92. nbFollowingNumbers = consumeNumbers(i + 1, parent);
  93. i = i + nbFollowingNumbers + 1;
  94. nextSiblingNode = i < parent.expressions.length && parent.expressions[i];
  95. // Avoid \{3,} turning into {3,}
  96. return isSimpleChar(nextSiblingNode, '}');
  97. }
  98. }
  99. return false;
  100. }
  101. function preservesClosingCurlyBraceEscape(index, parent) {
  102. // (?:\{) -> (?:{)
  103. if (index == null) {
  104. return false;
  105. }
  106. var nbPrecedingNumbers = consumeNumbers(index - 1, parent, true);
  107. var i = index - nbPrecedingNumbers - 1;
  108. var previousSiblingNode = i >= 0 && parent.expressions[i];
  109. // Avoid {3\} turning into {3}
  110. if (nbPrecedingNumbers && isSimpleChar(previousSiblingNode, '{')) {
  111. return true;
  112. }
  113. if (isSimpleChar(previousSiblingNode, ',')) {
  114. nbPrecedingNumbers = consumeNumbers(i - 1, parent, true);
  115. i = i - nbPrecedingNumbers - 1;
  116. previousSiblingNode = i < parent.expressions.length && parent.expressions[i];
  117. // Avoid {3,\} turning into {3,}
  118. return nbPrecedingNumbers && isSimpleChar(previousSiblingNode, '{');
  119. }
  120. return false;
  121. }