-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
55 lines (45 loc) · 1.26 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*jslint node: true*/
'use strict';
var proxyquire = require('proxyquire'),
rocambole = proxyquire('rocambole', {
'esprima': require('esprima-fb')
});
function before(code) {
var ast,
ranges = [];
ast = rocambole.parse(code);
rocambole.recursive(ast, function (node) {
if (node.type === 'XJSElement' && node.parent.type !== 'XJSElement') {
ranges.push(node.range);
}
});
if (ranges.length) {
ranges = ranges.shift();
code = [
code.substring(0, ranges[0]),
'null /*@XJS__START__XJS@',
code.substring(ranges[0], ranges[1]).replace(/\*\//g, '@XJS__CONTINUE__XJS@'),
'@XJS__END__XJS@*/',
code.substring(ranges[1])
];
code = code.join('');
code = before(code);
}
return code;
}
function after(code) {
return code
.replace(/@XJS__CONTINUE__XJS@/g, '*/')
.replace(/null(|;?)\ ?\/\*@XJS__START__XJS@([^]+?)@XJS__END__XJS@\*\/\ ?/g, function (all, $1, $2) {
return $2 + ($1 || '');
});
}
// Export the plugin
module.exports = {
stringBefore: function (code) {
return before(code);
},
stringAfter: function (code) {
return after(code);
}
};