Skip to content

Commit

Permalink
Make object literal property names emitted consistently.
Browse files Browse the repository at this point in the history
Object literal property names that are strings will always
appear in the output quoted.

Change-Id: I4198468cf9d1df3863239967712d68e4398ba0c2
  • Loading branch information
rluble committed Nov 12, 2015
1 parent 4b0b2fa commit 5d65009
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 22 deletions.
Expand Up @@ -706,24 +706,11 @@ public boolean visit(JsNumericEntry x, JsContext ctx) {
public boolean visit(JsObjectLiteral x, JsContext ctx) {
_lbrace();
boolean sep = false;
for (Object element : x.getPropertyInitializers()) {
for (JsPropertyInitializer element : x.getPropertyInitializers()) {
sep = _sepCommaOptSpace(sep);
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);
}
accept(element.getLabelExpr());
_colon();
JsExpression valueExpr = propInit.getValueExpr();
JsExpression valueExpr = element.getValueExpr();
_parenPushIfCommaExpr(valueExpr);
accept(valueExpr);
_parenPopIfCommaExpr(valueExpr);
Expand Down
Expand Up @@ -67,14 +67,14 @@ public void testSimpleIntern() throws Exception {
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};",
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));

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};",
ONES, TWOS, THREES),
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);
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);

// Strings
checkTranslation(
Expand Down
Expand Up @@ -53,20 +53,20 @@ public void testIncrement() 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 {
// 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}"));
}

public void testObjectLiteralDeclarationConcise() throws Exception {
// quotes are not necessary around many property variables in object
// literals
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'}"));
}

Expand Down

0 comments on commit 5d65009

Please sign in to comment.