extglob.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. 'use strict';
  2. /**
  3. * Module dependencies
  4. */
  5. var Snapdragon = require('snapdragon');
  6. var define = require('define-property');
  7. var extend = require('extend-shallow');
  8. /**
  9. * Local dependencies
  10. */
  11. var compilers = require('./compilers');
  12. var parsers = require('./parsers');
  13. /**
  14. * Customize Snapdragon parser and renderer
  15. */
  16. function Extglob(options) {
  17. this.options = extend({source: 'extglob'}, options);
  18. this.snapdragon = this.options.snapdragon || new Snapdragon(this.options);
  19. this.snapdragon.patterns = this.snapdragon.patterns || {};
  20. this.compiler = this.snapdragon.compiler;
  21. this.parser = this.snapdragon.parser;
  22. compilers(this.snapdragon);
  23. parsers(this.snapdragon);
  24. /**
  25. * Override Snapdragon `.parse` method
  26. */
  27. define(this.snapdragon, 'parse', function(str, options) {
  28. var parsed = Snapdragon.prototype.parse.apply(this, arguments);
  29. parsed.input = str;
  30. // escape unmatched brace/bracket/parens
  31. var last = this.parser.stack.pop();
  32. if (last && this.options.strict !== true) {
  33. var node = last.nodes[0];
  34. node.val = '\\' + node.val;
  35. var sibling = node.parent.nodes[1];
  36. if (sibling.type === 'star') {
  37. sibling.loose = true;
  38. }
  39. }
  40. // add non-enumerable parser reference
  41. define(parsed, 'parser', this.parser);
  42. return parsed;
  43. });
  44. /**
  45. * Decorate `.parse` method
  46. */
  47. define(this, 'parse', function(ast, options) {
  48. return this.snapdragon.parse.apply(this.snapdragon, arguments);
  49. });
  50. /**
  51. * Decorate `.compile` method
  52. */
  53. define(this, 'compile', function(ast, options) {
  54. return this.snapdragon.compile.apply(this.snapdragon, arguments);
  55. });
  56. }
  57. /**
  58. * Expose `Extglob`
  59. */
  60. module.exports = Extglob;