private Node transform(JsObjectLiteral x) { Node n = IR.objectlit(); for (Object element : x.getPropertyInitializers()) { JsPropertyInitializer propInit = (JsPropertyInitializer) element; Node key; if (propInit.getLabelExpr().getKind() == NodeKind.NUMBER) { key = transformNumberAsString((JsNumberLiteral) propInit.getLabelExpr()); key.putBooleanProp(Node.QUOTED_PROP, true); } else if (propInit.getLabelExpr().getKind() == NodeKind.NAME_REF) { key = transformNameAsString( ((JsNameRef) propInit.getLabelExpr()).getShortIdent(), propInit.getLabelExpr()); } else { key = transform(propInit.getLabelExpr()); } Preconditions.checkState(key.isString(), key); key.setType(Token.STRING_KEY); // Set as quoted as the rhino version we use does not distinguish one from the other. // Closure assumes unquoted property names are obfuscatable, but since there is no way to // distinguish between them at this point they have to be assumed quoted, hence not // obfuscatable. // TODO(rluble): Make sure this is handled correctly once rhino is upgraded. key.putBooleanProp(Node.QUOTED_PROP, true); n.addChildToBack(IR.propdef(key, transform(propInit.getValueExpr()))); } return applySourceInfo(n, x); }
@Override JsLiteral translate(JExpression literal) { SourceInfo sourceInfo = literal.getSourceInfo(); long[] values = LongLib.getAsLongArray(((JLongLiteral) literal).getValue()); if (values.length == 1) { return new JsNumberLiteral(literal.getSourceInfo(), ((JLongLiteral) literal).getValue()); } JsObjectLiteral objectLiteral = new JsObjectLiteral(sourceInfo); objectLiteral.setInternable(); assert values.length == names.length; for (int i = 0; i < names.length; i++) { addPropertyToObject(sourceInfo, names[i], values[i], objectLiteral); } return objectLiteral; }
private static void addPropertyToObject( SourceInfo sourceInfo, JsName propertyName, long propertyValue, JsObjectLiteral objectLiteral) { JsExpression label = propertyName.makeRef(sourceInfo); JsExpression value = new JsNumberLiteral(sourceInfo, propertyValue); objectLiteral.addProperty(sourceInfo, label, value); }