7 module.exports = function(nanomatch, options) {
9 if (options && typeof options.slash === 'string') {
12 if (options && typeof options.slash === 'function') {
13 return options.slash.call(nanomatch);
19 if (options && typeof options.star === 'string') {
22 if (options && typeof options.star === 'function') {
23 return options.star.call(nanomatch);
25 return '[^' + slash() + ']*?';
28 var ast = nanomatch.ast = nanomatch.parser.ast;
29 ast.state = nanomatch.parser.state;
30 nanomatch.compiler.state = ast.state;
37 .set('not', function(node) {
38 var prev = this.prev();
39 if (this.options.nonegate === true || prev.type !== 'bos') {
40 return this.emit('\\' + node.val, node);
42 return this.emit(node.val, node);
44 .set('escape', function(node) {
45 if (this.options.unescape && /^[-\w_.]/.test(node.val)) {
46 return this.emit(node.val, node);
48 return this.emit('\\' + node.val, node);
50 .set('quoted', function(node) {
51 return this.emit(node.val, node);
58 .set('dollar', function(node) {
59 if (node.parent.type === 'bracket') {
60 return this.emit(node.val, node);
62 return this.emit('\\' + node.val, node);
69 .set('dot', function(node) {
70 if (node.dotfiles === true) this.dotfiles = true;
71 return this.emit('\\' + node.val, node);
75 * Slashes: "/" and "\"
78 .set('backslash', function(node) {
79 return this.emit(node.val, node);
81 .set('slash', function(node, nodes, i) {
82 var val = '[' + slash() + ']';
83 var parent = node.parent;
84 var prev = this.prev();
86 // set "node.hasSlash" to true on all ancestor parens nodes
87 while (parent.type === 'paren' && !parent.hasSlash) {
88 parent.hasSlash = true;
89 parent = parent.parent;
97 if (node.rest.slice(0, 2) === '\\b') {
98 return this.emit(val, node);
102 if (node.parsed === '**' || node.parsed === './**') {
103 this.output = '(?:' + this.output;
104 return this.emit(val + ')?', node);
108 if (node.parsed === '!**' && this.options.nonegate !== true) {
109 return this.emit(val + '?\\b', node);
111 return this.emit(val, node);
118 .set('bracket', function(node) {
119 var close = node.close;
120 var open = !node.escaped ? '[' : '\\[';
121 var negated = node.negated;
122 var inner = node.inner;
125 if (node.escaped === true) {
126 inner = inner.replace(/\\?(\W)/g, '\\$1');
130 if (inner === ']-') {
134 if (negated && inner.indexOf('.') === -1) {
137 if (negated && inner.indexOf('/') === -1) {
141 val = open + negated + inner + close;
142 return this.emit(val, node);
146 * Square: "[.]" (only matches a single character in brackets)
149 .set('square', function(node) {
150 var val = (/^\W/.test(node.val) ? '\\' : '') + node.val;
151 return this.emit(val, node);
158 .set('qmark', function(node) {
159 var prev = this.prev();
160 // don't use "slash" variable so that we always avoid
161 // matching backslashes and slashes with a qmark
162 var val = '[^.\\\\/]';
163 if (this.options.dot || (prev.type !== 'bos' && prev.type !== 'slash')) {
167 if (node.parsed.slice(-1) === '(') {
168 var ch = node.rest.charAt(0);
169 if (ch === '!' || ch === '=' || ch === ':') {
170 return this.emit(node.val, node);
174 if (node.val.length > 1) {
175 val += '{' + node.val.length + '}';
177 return this.emit(val, node);
184 .set('plus', function(node) {
185 var prev = node.parsed.slice(-1);
186 if (prev === ']' || prev === ')') {
187 return this.emit(node.val, node);
189 if (!this.output || (/[?*+]/.test(ch) && node.parent.type !== 'bracket')) {
190 return this.emit('\\+', node);
192 var ch = this.output.slice(-1);
193 if (/\w/.test(ch) && !node.inside) {
194 return this.emit('+\\+?', node);
196 return this.emit('+', node);
203 .set('globstar', function(node, nodes, i) {
205 this.state.leadingGlobstar = true;
208 var prev = this.prev();
209 var before = this.prev(2);
210 var next = this.next();
211 var after = this.next(2);
212 var type = prev.type;
215 if (prev.type === 'slash' && next.type === 'slash') {
216 if (before.type === 'text') {
219 if (after.type !== 'text') {
220 this.output += '\\b';
225 var parsed = node.parsed;
226 if (parsed.charAt(0) === '!') {
227 parsed = parsed.slice(1);
230 var isInside = node.isInside.paren || node.isInside.brace;
231 if (parsed && type !== 'slash' && type !== 'bos' && !isInside) {
234 val = this.options.dot !== true
235 ? '(?:(?!(?:[' + slash() + ']|^)\\.).)*?'
236 : '(?:(?!(?:[' + slash() + ']|^)(?:\\.{1,2})($|[' + slash() + ']))(?!\\.{2}).)*?';
239 if ((type === 'slash' || type === 'bos') && this.options.dot !== true) {
240 val = '(?!\\.)' + val;
243 if (prev.type === 'slash' && next.type === 'slash' && before.type !== 'text') {
244 if (after.type === 'text' || after.type === 'star') {
245 node.addQmark = true;
249 if (this.options.capture) {
250 val = '(' + val + ')';
253 return this.emit(val, node);
260 .set('star', function(node, nodes, i) {
261 var prior = nodes[i - 2] || {};
262 var prev = this.prev();
263 var next = this.next();
264 var type = prev.type;
266 function isStart(n) {
267 return n.type === 'bos' || n.type === 'slash';
270 if (this.output === '' && this.options.contains !== true) {
271 this.output = '(?![' + slash() + '])';
274 if (type === 'bracket' && this.options.bash === false) {
275 var str = next && next.type === 'bracket' ? star() : '*?';
276 if (!prev.nodes || prev.nodes[1].type !== 'posix') {
277 return this.emit(str, node);
281 var prefix = !this.dotfiles && type !== 'text' && type !== 'escape'
282 ? (this.options.dot ? '(?!(?:^|[' + slash() + '])\\.{1,2}(?:$|[' + slash() + ']))' : '(?!\\.)')
285 if (isStart(prev) || (isStart(prior) && type === 'not')) {
286 if (prefix !== '(?!\\.)') {
287 prefix += '(?!(\\.{2}|\\.[' + slash() + ']))(?=.)';
291 } else if (prefix === '(?!\\.)') {
295 if (prev.type === 'not' && prior.type === 'bos' && this.options.dot === true) {
296 this.output = '(?!\\.)' + this.output;
299 var output = prefix + star();
300 if (this.options.capture) {
301 output = '(' + output + ')';
304 return this.emit(output, node);
311 .set('text', function(node) {
312 return this.emit(node.val, node);
319 .set('eos', function(node) {
320 var prev = this.prev();
323 this.output = '(?:\\.[' + slash() + '](?=.))?' + this.output;
324 if (this.state.metachar && prev.type !== 'qmark' && prev.type !== 'slash') {
325 val += (this.options.contains ? '[' + slash() + ']?' : '(?:[' + slash() + ']|$)');
328 return this.emit(val, node);
332 * Allow custom compilers to be passed on options
335 if (options && typeof options.compilers === 'function') {
336 options.compilers(nanomatch.compiler);