MCPcopy
hub / github.com/lebab/lebab / transformVarsToLetOrConst

Function transformVarsToLetOrConst

src/transform/let.js:145–189  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

143
144// This is where the actual transform happens
145function 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

Callers 1

leaveFunctionFunction · 0.85

Calls 11

logWarningForVarKindFunction · 0.85
hasMultiStatementBodyFunction · 0.85
multiReplaceStatementFunction · 0.85
getCommonKindMethod · 0.80
getParentNodeMethod · 0.80
getKindMethod · 0.80
getNodeMethod · 0.45
getVariablesMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…