…lines 1–274 unchanged (274 lines)
275 n.addChildBefore(new Node(Token.EMPTY), expr);
276 n.addChildAfter(new Node(Token.EMPTY), expr);
277 reportCodeChange("WHILE node");
278 }
279 break;
280
281 case Token.FUNCTION:
282 normalizeFunctionDeclaration(n);
283 break;
284 }
285 }
286
287 /**
288 * Rewrite named unhoisted functions declarations to a known
289 * consistent behavior so we don't to different logic paths for the same
290 * code. From:
291 * function f() {}
292 * to:
293 * var f = function () {};
294 */
295 private void normalizeFunctionDeclaration(Node n) {
296 Preconditions.checkState(n.getType() == Token.FUNCTION);
297 if (!NodeUtil.isFunctionAnonymous(n)
298 && !NodeUtil.isHoistedFunctionDeclaration(n)) {
299 rewriteFunctionDeclaration(n);
300 }
301 }
302
303 /**
304 * Rewrite the function declaration from:
305 * function x() {}
306 * FUNCTION
307 * NAME
…lines 308–312 unchanged (5 lines)
313 * NAME
314 * FUNCTION
315 * NAME (w/ empty string)
316 * LP
317 * BLOCK
318 */
319 private void rewriteFunctionDeclaration(Node n) {
320 // Prepare a spot for the function.
321 Node oldNameNode = n.getFirstChild();
322 Node fnNameNode = oldNameNode.cloneNode();
323 Node var = new Node(Token.VAR, fnNameNode, n.getLineno(), n.getCharno());
324 var.copyInformationFrom(n);
325
326 // Prepare the function
327 oldNameNode.setString("");
328
329 // Move the function
330 Node parent = n.getParent();
331 parent.replaceChild(n, var);
332 fnNameNode.addChildToFront(n);
333
334 reportCodeChange("Function declaration");
335 }
336
337 /**
338 * Do normalizations that introduce new siblings or parents.
339 */
340 private void doStatementNormalizations(
341 NodeTraversal t, Node n, Node parent) {
…lines 342–535 unchanged (194 lines)
536 */
537 @Override
538 public void onRedeclaration(
539 Scope s, String name, Node n, Node parent, Node gramps,
540 Node nodeWithLineNumber) {
541 Preconditions.checkState(n.getType() == Token.NAME);
542 Var v = s.getVar(name);
543 // If name is "arguments", Var maybe null.
544 Preconditions.checkState(
545 v == null || v.getParentNode().getType() != Token.CATCH);
546 if (v != null && parent.getType() == Token.FUNCTION) {
547 if (v.getParentNode().getType() == Token.VAR) {
548 s.undeclare(v);
549 s.declare(name, n, n.getJSType(), v.input);
550 replaceVarWithAssignment(v.getNameNode(), v.getParentNode(),
551 v.getParentNode().getParent());
552 }
553 } else if (parent.getType() == Token.VAR) {
554 Preconditions.checkState(parent.hasOneChild());
555
556 replaceVarWithAssignment(n, parent, gramps);
557 }
558 }
559
…lines 560–634 unchanged (75 lines)