You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

282 lines
6.7 KiB

4 years ago
  1. 'use strict';
  2. var utils = require('./utils');
  3. module.exports = function(braces, options) {
  4. braces.compiler
  5. /**
  6. * bos
  7. */
  8. .set('bos', function() {
  9. if (this.output) return;
  10. this.ast.queue = isEscaped(this.ast) ? [this.ast.val] : [];
  11. this.ast.count = 1;
  12. })
  13. /**
  14. * Square brackets
  15. */
  16. .set('bracket', function(node) {
  17. var close = node.close;
  18. var open = !node.escaped ? '[' : '\\[';
  19. var negated = node.negated;
  20. var inner = node.inner;
  21. inner = inner.replace(/\\(?=[\\\w]|$)/g, '\\\\');
  22. if (inner === ']-') {
  23. inner = '\\]\\-';
  24. }
  25. if (negated && inner.indexOf('.') === -1) {
  26. inner += '.';
  27. }
  28. if (negated && inner.indexOf('/') === -1) {
  29. inner += '/';
  30. }
  31. var val = open + negated + inner + close;
  32. var queue = node.parent.queue;
  33. var last = utils.arrayify(queue.pop());
  34. queue.push(utils.join(last, val));
  35. queue.push.apply(queue, []);
  36. })
  37. /**
  38. * Brace
  39. */
  40. .set('brace', function(node) {
  41. node.queue = isEscaped(node) ? [node.val] : [];
  42. node.count = 1;
  43. return this.mapVisit(node.nodes);
  44. })
  45. /**
  46. * Open
  47. */
  48. .set('brace.open', function(node) {
  49. node.parent.open = node.val;
  50. })
  51. /**
  52. * Inner
  53. */
  54. .set('text', function(node) {
  55. var queue = node.parent.queue;
  56. var escaped = node.escaped;
  57. var segs = [node.val];
  58. if (node.optimize === false) {
  59. options = utils.extend({}, options, {optimize: false});
  60. }
  61. if (node.multiplier > 1) {
  62. node.parent.count *= node.multiplier;
  63. }
  64. if (options.quantifiers === true && utils.isQuantifier(node.val)) {
  65. escaped = true;
  66. } else if (node.val.length > 1) {
  67. if (isType(node.parent, 'brace') && !isEscaped(node)) {
  68. var expanded = utils.expand(node.val, options);
  69. segs = expanded.segs;
  70. if (expanded.isOptimized) {
  71. node.parent.isOptimized = true;
  72. }
  73. // if nothing was expanded, we probably have a literal brace
  74. if (!segs.length) {
  75. var val = (expanded.val || node.val);
  76. if (options.unescape !== false) {
  77. // unescape unexpanded brace sequence/set separators
  78. val = val.replace(/\\([,.])/g, '$1');
  79. // strip quotes
  80. val = val.replace(/["'`]/g, '');
  81. }
  82. segs = [val];
  83. escaped = true;
  84. }
  85. }
  86. } else if (node.val === ',') {
  87. if (options.expand) {
  88. node.parent.queue.push(['']);
  89. segs = [''];
  90. } else {
  91. segs = ['|'];
  92. }
  93. } else {
  94. escaped = true;
  95. }
  96. if (escaped && isType(node.parent, 'brace')) {
  97. if (node.parent.nodes.length <= 4 && node.parent.count === 1) {
  98. node.parent.escaped = true;
  99. } else if (node.parent.length <= 3) {
  100. node.parent.escaped = true;
  101. }
  102. }
  103. if (!hasQueue(node.parent)) {
  104. node.parent.queue = segs;
  105. return;
  106. }
  107. var last = utils.arrayify(queue.pop());
  108. if (node.parent.count > 1 && options.expand) {
  109. last = multiply(last, node.parent.count);
  110. node.parent.count = 1;
  111. }
  112. queue.push(utils.join(utils.flatten(last), segs.shift()));
  113. queue.push.apply(queue, segs);
  114. })
  115. /**
  116. * Close
  117. */
  118. .set('brace.close', function(node) {
  119. var queue = node.parent.queue;
  120. var prev = node.parent.parent;
  121. var last = prev.queue.pop();
  122. var open = node.parent.open;
  123. var close = node.val;
  124. if (open && close && isOptimized(node, options)) {
  125. open = '(';
  126. close = ')';
  127. }
  128. // if a close brace exists, and the previous segment is one character
  129. // don't wrap the result in braces or parens
  130. var ele = utils.last(queue);
  131. if (node.parent.count > 1 && options.expand) {
  132. ele = multiply(queue.pop(), node.parent.count);
  133. node.parent.count = 1;
  134. queue.push(ele);
  135. }
  136. if (close && typeof ele === 'string' && ele.length === 1) {
  137. open = '';
  138. close = '';
  139. }
  140. if ((isLiteralBrace(node, options) || noInner(node)) && !node.parent.hasEmpty) {
  141. queue.push(utils.join(open, queue.pop() || ''));
  142. queue = utils.flatten(utils.join(queue, close));
  143. }
  144. if (typeof last === 'undefined') {
  145. prev.queue = [queue];
  146. } else {
  147. prev.queue.push(utils.flatten(utils.join(last, queue)));
  148. }
  149. })
  150. /**
  151. * eos
  152. */
  153. .set('eos', function(node) {
  154. if (this.input) return;
  155. if (options.optimize !== false) {
  156. this.output = utils.last(utils.flatten(this.ast.queue));
  157. } else if (Array.isArray(utils.last(this.ast.queue))) {
  158. this.output = utils.flatten(this.ast.queue.pop());
  159. } else {
  160. this.output = utils.flatten(this.ast.queue);
  161. }
  162. if (node.parent.count > 1 && options.expand) {
  163. this.output = multiply(this.output, node.parent.count);
  164. }
  165. this.output = utils.arrayify(this.output);
  166. this.ast.queue = [];
  167. });
  168. };
  169. /**
  170. * Multiply the segments in the current brace level
  171. */
  172. function multiply(queue, n, options) {
  173. return utils.flatten(utils.repeat(utils.arrayify(queue), n));
  174. }
  175. /**
  176. * Return true if `node` is escaped
  177. */
  178. function isEscaped(node) {
  179. return node.escaped === true;
  180. }
  181. /**
  182. * Returns true if regex parens should be used for sets. If the parent `type`
  183. * is not `brace`, then we're on a root node, which means we should never
  184. * expand segments and open/close braces should be `{}` (since this indicates
  185. * a brace is missing from the set)
  186. */
  187. function isOptimized(node, options) {
  188. if (node.parent.isOptimized) return true;
  189. return isType(node.parent, 'brace')
  190. && !isEscaped(node.parent)
  191. && options.expand !== true;
  192. }
  193. /**
  194. * Returns true if the value in `node` should be wrapped in a literal brace.
  195. * @return {Boolean}
  196. */
  197. function isLiteralBrace(node, options) {
  198. return isEscaped(node.parent) || options.optimize !== false;
  199. }
  200. /**
  201. * Returns true if the given `node` does not have an inner value.
  202. * @return {Boolean}
  203. */
  204. function noInner(node, type) {
  205. if (node.parent.queue.length === 1) {
  206. return true;
  207. }
  208. var nodes = node.parent.nodes;
  209. return nodes.length === 3
  210. && isType(nodes[0], 'brace.open')
  211. && !isType(nodes[1], 'text')
  212. && isType(nodes[2], 'brace.close');
  213. }
  214. /**
  215. * Returns true if the given `node` is the given `type`
  216. * @return {Boolean}
  217. */
  218. function isType(node, type) {
  219. return typeof node !== 'undefined' && node.type === type;
  220. }
  221. /**
  222. * Returns true if the given `node` has a non-empty queue.
  223. * @return {Boolean}
  224. */
  225. function hasQueue(node) {
  226. return Array.isArray(node.queue) && node.queue.length;
  227. }