-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
50 lines (48 loc) · 1.87 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
const traverse = require('bash-ast-traverser');
module.exports = (ast) => {
return traverse(ast, {
Word: (node) => {
Object.defineProperty(node, 'source', {
get: () => node.text
})
return node;
},
AssignmentWord: (node) => {
Object.defineProperty(node, 'source', {
get: () => node.text
})
return node;
},
Command: (node) => {
Object.defineProperty(node, 'source', {
get: () => {
const suffix = node.suffix ? node.suffix.map(s => s.source).join(' ') : '';
const prefix = node.prefix ? node.prefix.map(s => s.source).join(' ') : '';
const name = node.name ? node.name.source : '';
return prefix + ' ' + name + ' ' + suffix;
}
});
return node;
},
If: (node) => {
Object.defineProperty(node, 'source', {
get: () => {
const _clause = node.clause ? node.clause.commands.map(c => {
return c.source;
}).join(' ') : '';
const _then = node.then ? node.then.commands.map(c => {
return c.source;
}).join('\n') : '';
const _else = node.else ? node.else.commands.map(c => {
return c.source;
}).join(' ') : '';
const _elseif = node.elseif ? node.elseif.commands.map(c => {
return c.source;
}).join(' ') : '';
return `if ${_clause}\n${_then && `then\n${_then}\n`}${_else && `else\n${_else}\n`}${_elseif && `elseif\n${_elseif}\n`}\nfi`
}
});
return node;
}
});
}