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.

compilers.js 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. 'use strict';
  2. var nanomatch = require('nanomatch');
  3. var extglob = require('extglob');
  4. module.exports = function(snapdragon) {
  5. var compilers = snapdragon.compiler.compilers;
  6. var opts = snapdragon.options;
  7. // register nanomatch compilers
  8. snapdragon.use(nanomatch.compilers);
  9. // get references to some specific nanomatch compilers before they
  10. // are overridden by the extglob and/or custom compilers
  11. var escape = compilers.escape;
  12. var qmark = compilers.qmark;
  13. var slash = compilers.slash;
  14. var star = compilers.star;
  15. var text = compilers.text;
  16. var plus = compilers.plus;
  17. var dot = compilers.dot;
  18. // register extglob compilers or escape exglobs if disabled
  19. if (opts.extglob === false || opts.noext === true) {
  20. snapdragon.compiler.use(escapeExtglobs);
  21. } else {
  22. snapdragon.use(extglob.compilers);
  23. }
  24. snapdragon.use(function() {
  25. this.options.star = this.options.star || function(/*node*/) {
  26. return '[^\\\\/]*?';
  27. };
  28. });
  29. // custom micromatch compilers
  30. snapdragon.compiler
  31. // reset referenced compiler
  32. .set('dot', dot)
  33. .set('escape', escape)
  34. .set('plus', plus)
  35. .set('slash', slash)
  36. .set('qmark', qmark)
  37. .set('star', star)
  38. .set('text', text);
  39. };
  40. function escapeExtglobs(compiler) {
  41. compiler.set('paren', function(node) {
  42. var val = '';
  43. visit(node, function(tok) {
  44. if (tok.val) val += (/^\W/.test(tok.val) ? '\\' : '') + tok.val;
  45. });
  46. return this.emit(val, node);
  47. });
  48. /**
  49. * Visit `node` with the given `fn`
  50. */
  51. function visit(node, fn) {
  52. return node.nodes ? mapVisit(node.nodes, fn) : fn(node);
  53. }
  54. /**
  55. * Map visit over array of `nodes`.
  56. */
  57. function mapVisit(nodes, fn) {
  58. var len = nodes.length;
  59. var idx = -1;
  60. while (++idx < len) {
  61. visit(nodes[idx], fn);
  62. }
  63. }
  64. }