| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.javascript.jscomp; |
| 18 |
|
| 19 |
import com.google.javascript.rhino.IR; |
| 20 |
import com.google.javascript.rhino.Node; |
| 21 |
import com.google.javascript.rhino.Token; |
| 22 |
|
| 23 |
import java.util.Iterator; |
| 24 |
|
| 25 |
import javax.annotation.Nullable; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
class ReplaceMessages extends JsMessageVisitor { |
| 33 |
private final MessageBundle bundle; |
| 34 |
private final boolean strictReplacement; |
| 35 |
|
| 36 |
static final DiagnosticType BUNDLE_DOES_NOT_HAVE_THE_MESSAGE = |
| 37 |
DiagnosticType.error("JSC_BUNDLE_DOES_NOT_HAVE_THE_MESSAGE", |
| 38 |
"Message with id = {0} could not be found in replacement bundle"); |
| 39 |
|
| 40 |
ReplaceMessages(AbstractCompiler compiler, MessageBundle bundle, |
| 41 |
boolean checkDuplicatedMessages, JsMessage.Style style, |
| 42 |
boolean strictReplacement) { |
| 43 |
|
| 44 |
super(compiler, checkDuplicatedMessages, style, bundle.idGenerator()); |
| 45 |
|
| 46 |
this.bundle = bundle; |
| 47 |
this.strictReplacement = strictReplacement; |
| 48 |
} |
| 49 |
|
| 50 |
@Override |
| 51 |
void processMessageFallback( |
| 52 |
Node callNode, JsMessage message1, JsMessage message2) { |
| 53 |
boolean isFirstMessageTranslated = |
| 54 |
(bundle.getMessage(message1.getId()) != null); |
| 55 |
boolean isSecondMessageTranslated = |
| 56 |
(bundle.getMessage(message2.getId()) != null); |
| 57 |
Node replacementNode = |
| 58 |
isSecondMessageTranslated && !isFirstMessageTranslated ? |
| 59 |
callNode.getChildAtIndex(2) : callNode.getChildAtIndex(1); |
| 60 |
callNode.getParent().replaceChild(callNode, |
| 61 |
replacementNode.detachFromParent()); |
| 62 |
} |
| 63 |
|
| 64 |
@Override |
| 65 |
void processJsMessage(JsMessage message, |
| 66 |
JsMessageDefinition definition) { |
| 67 |
|
| 68 |
|
| 69 |
JsMessage replacement = bundle.getMessage(message.getId()); |
| 70 |
if (replacement == null) { |
| 71 |
if (strictReplacement) { |
| 72 |
compiler.report(JSError.make(message.getSourceName(), |
| 73 |
definition.getMessageNode(), BUNDLE_DOES_NOT_HAVE_THE_MESSAGE, |
| 74 |
message.getId())); |
| 75 |
|
| 76 |
return; |
| 77 |
} else { |
| 78 |
|
| 79 |
|
| 80 |
replacement = message; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
Node newValue; |
| 86 |
Node msgNode = definition.getMessageNode(); |
| 87 |
try { |
| 88 |
newValue = getNewValueNode(replacement, msgNode); |
| 89 |
} catch (MalformedException e) { |
| 90 |
compiler.report(JSError.make(message.getSourceName(), |
| 91 |
e.getNode(), MESSAGE_TREE_MALFORMED, e.getMessage())); |
| 92 |
newValue = msgNode; |
| 93 |
} |
| 94 |
|
| 95 |
if (newValue != msgNode) { |
| 96 |
newValue.copyInformationFromForTree(msgNode); |
| 97 |
definition.getMessageParentNode().replaceChild(msgNode, newValue); |
| 98 |
compiler.reportCodeChange(); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
private Node getNewValueNode(JsMessage message, Node origValueNode) |
| 115 |
throws MalformedException { |
| 116 |
switch (origValueNode.getType()) { |
| 117 |
case Token.FUNCTION: |
| 118 |
|
| 119 |
updateFunctionNode(message, origValueNode); |
| 120 |
return origValueNode; |
| 121 |
case Token.STRING: |
| 122 |
|
| 123 |
String newString = message.toString(); |
| 124 |
if (!origValueNode.getString().equals(newString)) { |
| 125 |
origValueNode.setString(newString); |
| 126 |
compiler.reportCodeChange(); |
| 127 |
} |
| 128 |
return origValueNode; |
| 129 |
case Token.ADD: |
| 130 |
|
| 131 |
return IR.string(message.toString()); |
| 132 |
case Token.CALL: |
| 133 |
|
| 134 |
return replaceCallNode(message, origValueNode); |
| 135 |
default: |
| 136 |
throw new MalformedException( |
| 137 |
"Expected FUNCTION, STRING, or ADD node; found: " + |
| 138 |
origValueNode.getType(), origValueNode); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
private void updateFunctionNode(JsMessage message, Node functionNode) |
| 168 |
throws MalformedException { |
| 169 |
checkNode(functionNode, Token.FUNCTION); |
| 170 |
Node nameNode = functionNode.getFirstChild(); |
| 171 |
checkNode(nameNode, Token.NAME); |
| 172 |
Node argListNode = nameNode.getNext(); |
| 173 |
checkNode(argListNode, Token.PARAM_LIST); |
| 174 |
Node oldBlockNode = argListNode.getNext(); |
| 175 |
checkNode(oldBlockNode, Token.BLOCK); |
| 176 |
|
| 177 |
Iterator<CharSequence> iterator = message.parts().iterator(); |
| 178 |
Node valueNode = iterator.hasNext() |
| 179 |
? constructAddOrStringNode(iterator, argListNode) |
| 180 |
: IR.string(""); |
| 181 |
Node newBlockNode = IR.block(IR.returnNode(valueNode)); |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
if (newBlockNode.checkTreeEquals(oldBlockNode) != null) { |
| 186 |
newBlockNode.copyInformationFromForTree(oldBlockNode); |
| 187 |
functionNode.replaceChild(oldBlockNode, newBlockNode); |
| 188 |
compiler.reportCodeChange(); |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
private Node constructAddOrStringNode(Iterator<CharSequence> partsIterator, |
| 207 |
Node argListNode) |
| 208 |
throws MalformedException { |
| 209 |
CharSequence part = partsIterator.next(); |
| 210 |
Node partNode = null; |
| 211 |
if (part instanceof JsMessage.PlaceholderReference) { |
| 212 |
JsMessage.PlaceholderReference phRef = |
| 213 |
(JsMessage.PlaceholderReference) part; |
| 214 |
|
| 215 |
for (Node node : argListNode.children()) { |
| 216 |
if (node.isName()) { |
| 217 |
String arg = node.getString(); |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
if (arg.equalsIgnoreCase(phRef.getName())) { |
| 223 |
partNode = IR.name(arg); |
| 224 |
} |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
if (partNode == null) { |
| 229 |
throw new MalformedException( |
| 230 |
"Unrecognized message placeholder referenced: " + phRef.getName(), |
| 231 |
argListNode); |
| 232 |
} |
| 233 |
} else { |
| 234 |
|
| 235 |
partNode = IR.string(part.toString()); |
| 236 |
} |
| 237 |
|
| 238 |
if (partsIterator.hasNext()) { |
| 239 |
return IR.add(partNode, |
| 240 |
constructAddOrStringNode(partsIterator, argListNode)); |
| 241 |
} else { |
| 242 |
return partNode; |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
private Node replaceCallNode(JsMessage message, Node callNode) |
| 287 |
throws MalformedException { |
| 288 |
checkNode(callNode, Token.CALL); |
| 289 |
Node getPropNode = callNode.getFirstChild(); |
| 290 |
checkNode(getPropNode, Token.GETPROP); |
| 291 |
Node stringExprNode = getPropNode.getNext(); |
| 292 |
checkStringExprNode(stringExprNode); |
| 293 |
Node objLitNode = stringExprNode.getNext(); |
| 294 |
|
| 295 |
|
| 296 |
return constructStringExprNode(message.parts().iterator(), objLitNode); |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
private Node constructStringExprNode(Iterator<CharSequence> parts, |
| 313 |
Node objLitNode) throws MalformedException { |
| 314 |
|
| 315 |
CharSequence part = parts.next(); |
| 316 |
Node partNode = null; |
| 317 |
if (part instanceof JsMessage.PlaceholderReference) { |
| 318 |
JsMessage.PlaceholderReference phRef = |
| 319 |
(JsMessage.PlaceholderReference) part; |
| 320 |
|
| 321 |
|
| 322 |
if (objLitNode == null) { |
| 323 |
throw new MalformedException("Empty placeholder value map " + |
| 324 |
"for a translated message with placeholders.", objLitNode); |
| 325 |
} |
| 326 |
|
| 327 |
for (Node key = objLitNode.getFirstChild(); key != null; |
| 328 |
key = key.getNext()) { |
| 329 |
if (key.getString().equals(phRef.getName())) { |
| 330 |
Node valueNode = key.getFirstChild(); |
| 331 |
partNode = valueNode.cloneTree(); |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
if (partNode == null) { |
| 336 |
throw new MalformedException( |
| 337 |
"Unrecognized message placeholder referenced: " + phRef.getName(), |
| 338 |
objLitNode); |
| 339 |
} |
| 340 |
} else { |
| 341 |
|
| 342 |
partNode = IR.string(part.toString()); |
| 343 |
} |
| 344 |
|
| 345 |
if (parts.hasNext()) { |
| 346 |
return IR.add(partNode, |
| 347 |
constructStringExprNode(parts, objLitNode)); |
| 348 |
} else { |
| 349 |
return partNode; |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
private void checkStringExprNode(@Nullable Node node) { |
| 360 |
if (node == null) { |
| 361 |
throw new IllegalArgumentException("Expected a string; found: null"); |
| 362 |
} |
| 363 |
switch (node.getType()) { |
| 364 |
case Token.STRING: |
| 365 |
break; |
| 366 |
case Token.ADD: |
| 367 |
Node c = node.getFirstChild(); |
| 368 |
checkStringExprNode(c); |
| 369 |
checkStringExprNode(c.getNext()); |
| 370 |
break; |
| 371 |
default: |
| 372 |
throw new IllegalArgumentException( |
| 373 |
"Expected a string; found: " + node.getType()); |
| 374 |
} |
| 375 |
} |
| 376 |
} |