compat-dotall-s-transform.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 translate `/./s` to `/[\0-\uFFFF]/`.
  8. */
  9. module.exports = {
  10. // Whether `u` flag present. In which case we transform to
  11. // \u{10FFFF} instead of \uFFFF.
  12. _hasUFlag: false,
  13. // Only run this plugin if we have `s` flag.
  14. shouldRun: function shouldRun(ast) {
  15. var shouldRun = ast.flags.includes('s');
  16. if (!shouldRun) {
  17. return false;
  18. }
  19. // Strip the `s` flag.
  20. ast.flags = ast.flags.replace('s', '');
  21. // Whether we have also `u`.
  22. this._hasUFlag = ast.flags.includes('u');
  23. return true;
  24. },
  25. Char: function Char(path) {
  26. var node = path.node;
  27. if (node.kind !== 'meta' || node.value !== '.') {
  28. return;
  29. }
  30. var toValue = '\\uFFFF';
  31. var toSymbol = '\uFFFF';
  32. if (this._hasUFlag) {
  33. toValue = '\\u{10FFFF}';
  34. toSymbol = '\uDBFF\uDFFF';
  35. }
  36. path.replace({
  37. type: 'CharacterClass',
  38. expressions: [{
  39. type: 'ClassRange',
  40. from: {
  41. type: 'Char',
  42. value: '\\0',
  43. kind: 'decimal',
  44. symbol: '\0'
  45. },
  46. to: {
  47. type: 'Char',
  48. value: toValue,
  49. kind: 'unicode',
  50. symbol: toSymbol
  51. }
  52. }]
  53. });
  54. }
  55. };