Skip to content

Commit

Permalink
Revert "Make object literal property names emitted consistently."
Browse files Browse the repository at this point in the history
This reverts commit 5d65009.

Change-Id: I68795d5a28836abc8db1438aa5907761964199da
  • Loading branch information
rluble authored and Gerrit Code Review committed Nov 14, 2015
1 parent 184e750 commit 538ff9e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
Expand Up @@ -706,11 +706,24 @@ public boolean visit(JsNumericEntry x, JsContext ctx) {
public boolean visit(JsObjectLiteral x, JsContext ctx) { public boolean visit(JsObjectLiteral x, JsContext ctx) {
_lbrace(); _lbrace();
boolean sep = false; boolean sep = false;
for (JsPropertyInitializer element : x.getPropertyInitializers()) { for (Object element : x.getPropertyInitializers()) {
sep = _sepCommaOptSpace(sep); sep = _sepCommaOptSpace(sep);
accept(element.getLabelExpr()); JsPropertyInitializer propInit = (JsPropertyInitializer) element;
printLabel : {
JsExpression labelExpr = propInit.getLabelExpr();
// labels can be either string, integral, or decimal literals
if (labelExpr instanceof JsStringLiteral) {
String propName = ((JsStringLiteral) labelExpr).getValue();
if (JsUtils.isValidJsIdentifier(propName) && !JsProtectedNames.isKeyword(propName)) {
// Print unquoted if the property name is a valid identifier.
p.print(propName);
break printLabel;
}
}
accept(labelExpr);
}
_colon(); _colon();
JsExpression valueExpr = element.getValueExpr(); JsExpression valueExpr = propInit.getValueExpr();
_parenPushIfCommaExpr(valueExpr); _parenPushIfCommaExpr(valueExpr);
accept(valueExpr); accept(valueExpr);
_parenPopIfCommaExpr(valueExpr); _parenPopIfCommaExpr(valueExpr);
Expand Down
Expand Up @@ -67,14 +67,14 @@ public void testSimpleIntern() throws Exception {
checkTranslation( checkTranslation(
String.format("var x={a:%1$s,b:%2$s}, y={a:%1$s,b:%2$s}, z={a:%1$s,b:%3$s};", String.format("var x={a:%1$s,b:%2$s}, y={a:%1$s,b:%2$s}, z={a:%1$s,b:%3$s};",
ONES, TWOS, THREES), ONES, TWOS, THREES),
String.format("var $intern_0={'a':%1$s,'b':%2$s};var x=$intern_0,y=$intern_0,z={'a':%1$s,'b':%3$s};", String.format("var $intern_0={a:%1$s,b:%2$s};var x=$intern_0,y=$intern_0,z={a:%1$s,b:%3$s};",
ONES, TWOS, THREES)); ONES, TWOS, THREES));


checkTranslation( checkTranslation(
String.format("var x={a:%1$s,b:%2$s}, y={a:%1$s,b:%2$s}, z={a:%1$s,b:%3$s};", String.format("var x={a:%1$s,b:%2$s}, y={a:%1$s,b:%2$s}, z={a:%1$s,b:%3$s};",
ONES, TWOS, THREES), ONES, TWOS, THREES),
String.format("var $intern_0=%1$s,$intern_1=%2$s;var x={'a':$intern_0,'b':$intern_1}," + String.format("var $intern_0=%1$s,$intern_1=%2$s;var x={a:$intern_0,b:$intern_1}," +
"y={'a':$intern_0,'b':$intern_1},z={'a':$intern_0,'b':%3$s};", ONES, TWOS, THREES), false); "y={a:$intern_0,b:$intern_1},z={a:$intern_0,b:%3$s};", ONES, TWOS, THREES), false);


// Strings // Strings
checkTranslation( checkTranslation(
Expand Down
Expand Up @@ -53,20 +53,20 @@ public void testIncrement() throws Exception {
} }


public void testObjectLiteralAssignment() throws Exception { public void testObjectLiteralAssignment() throws Exception {
assertEquals("var x={'a':b=2,'c':d}", parse("var x = {a : (b = 2), c : d}")); assertEquals("var x={a:b=2,c:d}", parse("var x = {a : (b = 2), c : d}"));
} }


public void testObjectLiteralConditional() throws Exception { public void testObjectLiteralConditional() throws Exception {
// the parentheses are not required around the conditional // the parentheses are not required around the conditional
assertEquals("var x={'a':b?c:d,'e':f}", assertEquals("var x={a:b?c:d,e:f}",
parse("var x = {a : (b ? c : d), e : f}")); parse("var x = {a : (b ? c : d), e : f}"));
} }


public void testObjectLiteralDeclarationConcise() throws Exception { public void testObjectLiteralDeclarationConcise() throws Exception {
// quotes are not necessary around many property variables in object // quotes are not necessary around many property variables in object
// literals // literals
assertEquals("var x={1:'b'}", parse("var x = {1 : 'b'}")); assertEquals("var x={1:'b'}", parse("var x = {1 : 'b'}"));
assertEquals("var x={'$a_':'b'}", parse("var x = {'$a_' : 'b'}")); assertEquals("var x={$a_:'b'}", parse("var x = {'$a_' : 'b'}"));
assertEquals("var x={1.2:'b'}", parse("var x = {1.2 : 'b'}")); assertEquals("var x={1.2:'b'}", parse("var x = {1.2 : 'b'}"));
} }


Expand Down

0 comments on commit 538ff9e

Please sign in to comment.