| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 package com.google.dart.compiler.backend.js; | |
| 6 | |
| 7 import com.google.dart.compiler.backend.js.ast.JsBlock; | |
| 8 import com.google.dart.compiler.backend.js.ast.JsBreak; | |
| 9 import com.google.dart.compiler.backend.js.ast.JsContext; | |
| 10 import com.google.dart.compiler.backend.js.ast.JsDebugger; | |
| 11 import com.google.dart.compiler.backend.js.ast.JsDoWhile; | |
| 12 import com.google.dart.compiler.backend.js.ast.JsEmpty; | |
| 13 import com.google.dart.compiler.backend.js.ast.JsExprStmt; | |
| 14 import com.google.dart.compiler.backend.js.ast.JsFor; | |
| 15 import com.google.dart.compiler.backend.js.ast.JsForIn; | |
| 16 import com.google.dart.compiler.backend.js.ast.JsIf; | |
| 17 import com.google.dart.compiler.backend.js.ast.JsLabel; | |
| 18 import com.google.dart.compiler.backend.js.ast.JsReturn; | |
| 19 import com.google.dart.compiler.backend.js.ast.JsStatement; | |
| 20 import com.google.dart.compiler.backend.js.ast.JsSwitch; | |
| 21 import com.google.dart.compiler.backend.js.ast.JsThrow; | |
| 22 import com.google.dart.compiler.backend.js.ast.JsTry; | |
| 23 import com.google.dart.compiler.backend.js.ast.JsVars; | |
| 24 import com.google.dart.compiler.backend.js.ast.JsVisitor; | |
| 25 import com.google.dart.compiler.backend.js.ast.JsWhile; | |
| 26 | |
| 27 /** | |
| 28 * Determines if a statement at the end of a block requires a semicolon. | |
| 29 * | |
| 30 * For example, the following statements require semicolons:<br> | |
| 31 * <ul> | |
| 32 * <li>if (cond);</li> | |
| 33 * <li>while (cond);</li> | |
| 34 * </ul> | |
| 35 * | |
| 36 * The following do not require semicolons:<br> | |
| 37 * <ul> | |
| 38 * <li>return 1</li> | |
| 39 * <li>do {} while(true)</li> | |
| 40 * </ul> | |
| 41 */ | |
| 42 public class JsRequiresSemiVisitor extends JsVisitor { | |
| 43 | |
| 44 public static boolean exec(JsStatement lastStatement) { | |
| 45 JsRequiresSemiVisitor visitor = new JsRequiresSemiVisitor(); | |
| 46 visitor.accept(lastStatement); | |
| 47 return visitor.needsSemicolon; | |
| 48 } | |
| 49 | |
| 50 private boolean needsSemicolon = false; | |
| 51 | |
| 52 private JsRequiresSemiVisitor() { | |
| 53 } | |
| 54 | |
| 55 @Override | |
| 56 public boolean visit(JsBlock x, JsContext ctx) { | |
| 57 return false; | |
| 58 } | |
| 59 | |
| 60 @Override | |
| 61 public boolean visit(JsBreak x, JsContext ctx) { | |
| 62 return false; | |
| 63 } | |
| 64 | |
| 65 @Override | |
| 66 public boolean visit(JsDebugger x, JsContext ctx) { | |
| 67 return false; | |
| 68 } | |
| 69 | |
| 70 @Override | |
| 71 public boolean visit(JsDoWhile x, JsContext ctx) { | |
| 72 return false; | |
| 73 } | |
| 74 | |
| 75 @Override | |
| 76 public boolean visit(JsEmpty x, JsContext ctx) { | |
| 77 return false; | |
| 78 } | |
| 79 | |
| 80 @Override | |
| 81 public boolean visit(JsExprStmt x, JsContext ctx) { | |
| 82 return false; | |
| 83 } | |
| 84 | |
| 85 @Override | |
| 86 public boolean visit(JsFor x, JsContext ctx) { | |
| 87 if (x.getBody() instanceof JsEmpty) { | |
| 88 needsSemicolon = true; | |
| 89 } | |
| 90 return false; | |
| 91 } | |
| 92 | |
| 93 @Override | |
| 94 public boolean visit(JsForIn x, JsContext ctx) { | |
| 95 if (x.getBody() instanceof JsEmpty) { | |
| 96 needsSemicolon = true; | |
| 97 } | |
| 98 return false; | |
| 99 } | |
| 100 | |
| 101 @Override | |
| 102 public boolean visit(JsIf x, JsContext ctx) { | |
| 103 JsStatement thenStmt = x.getThenStmt(); | |
| 104 JsStatement elseStmt = x.getElseStmt(); | |
| 105 JsStatement toCheck = thenStmt; | |
| 106 if (elseStmt != null) { | |
| 107 toCheck = elseStmt; | |
| 108 } | |
| 109 if (toCheck instanceof JsEmpty) { | |
| 110 needsSemicolon = true; | |
| 111 } else { | |
| 112 // Must recurse to determine last statement (possible if-else chain). | |
| 113 accept(toCheck); | |
| 114 } | |
| 115 return false; | |
| 116 } | |
| 117 | |
| 118 @Override | |
| 119 public boolean visit(JsLabel x, JsContext ctx) { | |
| 120 if (x.getStmt() instanceof JsEmpty) { | |
| 121 needsSemicolon = true; | |
| 122 } | |
| 123 return false; | |
| 124 } | |
| 125 | |
| 126 @Override | |
| 127 public boolean visit(JsReturn x, JsContext ctx) { | |
| 128 return false; | |
| 129 } | |
| 130 | |
| 131 @Override | |
| 132 public boolean visit(JsSwitch x, JsContext ctx) { | |
| 133 return false; | |
| 134 } | |
| 135 | |
| 136 @Override | |
| 137 public boolean visit(JsThrow x, JsContext ctx) { | |
| 138 return false; | |
| 139 } | |
| 140 | |
| 141 @Override | |
| 142 public boolean visit(JsTry x, JsContext ctx) { | |
| 143 return false; | |
| 144 } | |
| 145 | |
| 146 @Override | |
| 147 public boolean visit(JsVars x, JsContext ctx) { | |
| 148 return false; | |
| 149 } | |
| 150 | |
| 151 @Override | |
| 152 public boolean visit(JsWhile x, JsContext ctx) { | |
| 153 if (x.getBody() instanceof JsEmpty) { | |
| 154 needsSemicolon = true; | |
| 155 } | |
| 156 return false; | |
| 157 } | |
| 158 } | |
| OLD | NEW |