(ast)
| 134 | // Finds multiple consecutive VariableDeclaration nodes, and fuses them together. |
| 135 | // 'var a = 1; var b = 2;' -> 'var a = 1, b = 2;' |
| 136 | function optPassMergeVarDeclarations(ast) { |
| 137 | let progress = false; |
| 138 | |
| 139 | visitNodes(ast, ['BlockStatement', 'Program'], (node) => { |
| 140 | const nodeArray = node.body; |
| 141 | for (let i = 0; i < nodeArray.length; ++i) { |
| 142 | const n = nodeArray[i]; |
| 143 | if (n.type != 'VariableDeclaration') continue; |
| 144 | // Look back to find if there is a preceding VariableDeclaration that this declaration could be fused to. |
| 145 | for (let j = i - 1; j >= 0; --j) { |
| 146 | const p = nodeArray[j]; |
| 147 | if (p.type == 'VariableDeclaration') { |
| 148 | p.declarations = p.declarations.concat(n.declarations); |
| 149 | nodeArray.splice(i--, 1); |
| 150 | progress = true; |
| 151 | break; |
| 152 | } else if (!['FunctionDeclaration'].includes(p.type)) { |
| 153 | break; |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | }); |
| 158 | return progress; |
| 159 | } |
| 160 | |
| 161 | // Merges "var a,b;a = ...;" to "var b, a = ...;" |
| 162 | function optPassMergeVarInitializationAssignments(ast) { |
no test coverage detected