index.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /**
  2. * The MIT License (MIT)
  3. * Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
  4. */
  5. 'use strict';
  6. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  7. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  8. var generator = require('../generator');
  9. var parser = require('../parser');
  10. var traverse = require('../traverse');
  11. /**
  12. * Transform result.
  13. */
  14. var TransformResult = function () {
  15. /**
  16. * Initializes a transform result for an AST.
  17. *
  18. * @param Object ast - an AST node
  19. * @param mixed extra - any extra data a transform may return
  20. */
  21. function TransformResult(ast) {
  22. var extra = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
  23. _classCallCheck(this, TransformResult);
  24. this._ast = ast;
  25. this._source = null;
  26. this._string = null;
  27. this._regexp = null;
  28. this._extra = extra;
  29. }
  30. _createClass(TransformResult, [{
  31. key: 'getAST',
  32. value: function getAST() {
  33. return this._ast;
  34. }
  35. }, {
  36. key: 'setExtra',
  37. value: function setExtra(extra) {
  38. this._extra = extra;
  39. }
  40. }, {
  41. key: 'getExtra',
  42. value: function getExtra() {
  43. return this._extra;
  44. }
  45. }, {
  46. key: 'toRegExp',
  47. value: function toRegExp() {
  48. if (!this._regexp) {
  49. this._regexp = new RegExp(this.getSource(), this._ast.flags);
  50. }
  51. return this._regexp;
  52. }
  53. }, {
  54. key: 'getSource',
  55. value: function getSource() {
  56. if (!this._source) {
  57. this._source = generator.generate(this._ast.body);
  58. }
  59. return this._source;
  60. }
  61. }, {
  62. key: 'getFlags',
  63. value: function getFlags() {
  64. return this._ast.flags;
  65. }
  66. }, {
  67. key: 'toString',
  68. value: function toString() {
  69. if (!this._string) {
  70. this._string = generator.generate(this._ast);
  71. }
  72. return this._string;
  73. }
  74. }]);
  75. return TransformResult;
  76. }();
  77. module.exports = {
  78. /**
  79. * Expose `TransformResult`.
  80. */
  81. TransformResult: TransformResult,
  82. /**
  83. * Transforms a regular expression applying a set of
  84. * transformation handlers.
  85. *
  86. * @param string | AST | RegExp:
  87. *
  88. * a regular expression in different representations: a string,
  89. * a RegExp object, or an AST.
  90. *
  91. * @param Object | Array<Object>:
  92. *
  93. * a handler (or a list of handlers) from `traverse` API.
  94. *
  95. * @return TransformResult instance.
  96. *
  97. * Example:
  98. *
  99. * transform(/[a-z]/i, {
  100. * onChar(path) {
  101. * const {node} = path;
  102. *
  103. * if (...) {
  104. * path.remove();
  105. * }
  106. * }
  107. * });
  108. */
  109. transform: function transform(regexp, handlers) {
  110. var ast = regexp;
  111. if (regexp instanceof RegExp) {
  112. regexp = '' + regexp;
  113. }
  114. if (typeof regexp === 'string') {
  115. ast = parser.parse(regexp, {
  116. captureLocations: true
  117. });
  118. }
  119. traverse.traverse(ast, handlers);
  120. return new TransformResult(ast);
  121. }
  122. };