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