()
| 143 | |
| 144 | // This is where the actual transform happens |
| 145 | function transformVarsToLetOrConst() { |
| 146 | getFunctionVariableGroups().forEach(group => { |
| 147 | // Do not modify existing let & const |
| 148 | if (group.getNode().kind !== 'var') { |
| 149 | return; |
| 150 | } |
| 151 | |
| 152 | if (isLexicalVariableProhibited(group)) { |
| 153 | logWarningForVarKind(group.getNode()); |
| 154 | return; |
| 155 | } |
| 156 | |
| 157 | const commonKind = group.getCommonKind(); |
| 158 | if (commonKind) { |
| 159 | // When all variables in group are of the same kind, |
| 160 | // just set appropriate `kind` value for the existing |
| 161 | // VariableDeclaration node. |
| 162 | group.getNode().kind = commonKind; |
| 163 | logWarningForVarKind(group.getNode()); |
| 164 | } |
| 165 | else if (hasMultiStatementBody(group.getParentNode())) { |
| 166 | // When some variables are of a different kind, |
| 167 | // create separate VariableDeclaration nodes for each |
| 168 | // VariableDeclarator and set their `kind` value appropriately. |
| 169 | const varNodes = group.getVariables().map(v => { |
| 170 | return new VariableDeclaration(v.getKind(), [v.getNode()]); |
| 171 | }); |
| 172 | |
| 173 | multiReplaceStatement({ |
| 174 | parent: group.getParentNode(), |
| 175 | node: group.getNode(), |
| 176 | replacements: varNodes, |
| 177 | preserveComments: true, |
| 178 | }); |
| 179 | |
| 180 | logWarningForVarKind(group.getNode()); |
| 181 | } |
| 182 | else { |
| 183 | // When parent node restricts breaking VariableDeclaration to multiple ones |
| 184 | // just change the kind of the declaration to the most restrictive possible |
| 185 | group.getNode().kind = group.getMostRestrictiveKind(); |
| 186 | logWarningForVarKind(group.getNode()); |
| 187 | } |
| 188 | }); |
| 189 | } |
| 190 | |
| 191 | // let and const declarations aren't allowed in all the same places where |
| 192 | // var declarations are allowed. Notably, only var-declaration can occur |
no test coverage detected
searching dependent graphs…