@@ -17,6 +17,7 @@ var rules = exports.rules = {}
17
17
18
18
exports . initializer = [
19
19
'var isArray = require("isarray")' ,
20
+ 'var map = require("array-map")' ,
20
21
function join ( arr ) {
21
22
return arr . join ( "" )
22
23
} ,
@@ -32,15 +33,30 @@ exports.initializer = [
32
33
function second ( arr ) {
33
34
return arr [ 1 ]
34
35
} ,
35
- function flattenConcatenations ( pieces ) {
36
- var results = [ ]
37
- pieces . forEach ( function ( piece ) {
38
- if ( piece . type == 'concatenation' )
39
- results = results . concat ( piece . pieces )
40
- else
41
- results . push ( piece )
42
- } )
43
- return results
36
+ function flattenConcatenation ( pieces ) {
37
+ var result = [ pieces [ 0 ] ]
38
+ , len = pieces . length
39
+ , prev = pieces [ 0 ]
40
+ , current
41
+
42
+ for ( var i = 1 ; i < len ; i ++ ) {
43
+ current = pieces [ i ]
44
+ if ( current . type == 'concatenation' ) {
45
+ current = flattenConcatenation ( current )
46
+ }
47
+ if ( current . type == 'literal' && prev . type == 'literal' ) {
48
+ // merge two literals
49
+ prev . value += current . value
50
+ }
51
+ else {
52
+ result . push ( current )
53
+ prev = current
54
+ }
55
+ }
56
+ return result . length == 1 ? result [ 0 ] : {
57
+ type : 'concatenation' ,
58
+ pieces : result
59
+ }
44
60
}
45
61
] . join ( '\n' )
46
62
@@ -53,7 +69,7 @@ rules.command = function (command, args, redirects, control) {
53
69
return {
54
70
type : 'command' ,
55
71
command : command ,
56
- args : args . map ( second ) ,
72
+ args : map ( args , second ) ,
57
73
redirects : redirects ,
58
74
control : control || ';' ,
59
75
env : env
@@ -91,30 +107,23 @@ rules.bareword = function (cs) { return literal(cs) }
91
107
rules . escapedMetaChar = function ( character ) { return character }
92
108
93
109
rules . concatenation = function ( pieces ) {
94
- pieces = flattenConcatenations ( pieces )
95
- return pieces . length == 1 ? pieces [ 0 ] : {
96
- type : 'concatenation' ,
97
- pieces : pieces
98
- }
110
+ return flattenConcatenation ( pieces )
99
111
}
100
112
101
113
rules . singleQuote = function ( cs ) { return literal ( cs ) }
102
114
rules . doubleQuote = function ( contents ) {
103
115
var pieces = contents . map ( function ( it ) {
104
116
return isArray ( it ) ? literal ( it ) : it
105
117
} )
106
- pieces = flattenConcatenations ( pieces )
107
- return pieces . length == 1 ? pieces [ 0 ] : {
108
- type : 'concatenation' ,
109
- pieces : pieces
110
- }
118
+ return flattenConcatenation ( pieces )
111
119
}
112
120
113
121
rules . escapedQuote = function ( character ) {
114
122
return character
115
123
}
116
124
rules . backticks = function ( commands ) {
117
- return { type : 'backticks' , commands : commands }
125
+ debugger
126
+ return { type : 'backticks' , commands : commands . map ( second ) }
118
127
}
119
128
120
129
rules . subshell = function ( commands ) {
0 commit comments