Skip to content

Commit 8dd02c7

Browse files
authored
Merge pull request #4952 from sjrd/fix-stat-starting-object-lit-3
Fix #4949: Always wrap object literals with `()`.
2 parents f4d39fa + e8b7dd7 commit 8dd02c7

File tree

5 files changed

+57
-20
lines changed

5 files changed

+57
-20
lines changed

linker/shared/src/main/scala/org/scalajs/linker/backend/javascript/Printers.scala

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -491,15 +491,17 @@ object Printers {
491491
printSeparatorIfStat()
492492

493493
case ObjectConstr(Nil) =>
494-
if (isStat)
495-
print("({});") // force expression position for the object literal
496-
else
497-
print("{}")
494+
/* #4949 Always wrap object literals with () in case they end up at
495+
* the start of an `ExpressionStatement`.
496+
*/
497+
print("({})")
498+
printSeparatorIfStat()
498499

499500
case ObjectConstr(fields) =>
500-
if (isStat)
501-
print('(') // force expression position for the object literal
502-
print('{')
501+
/* #4949 Always wrap object literals with () in case they end up at
502+
* the start of an `ExpressionStatement`.
503+
*/
504+
print("({")
503505
indent()
504506
println()
505507
var rest = fields
@@ -517,9 +519,8 @@ object Printers {
517519
}
518520
undent()
519521
printIndent()
520-
print('}')
521-
if (isStat)
522-
print(");")
522+
print("})")
523+
printSeparatorIfStat()
523524

524525
// Literals
525526

linker/shared/src/test/scala/org/scalajs/linker/LibrarySizeTest.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ class LibrarySizeTest {
7070
)
7171

7272
testLinkedSizes(
73-
expectedFastLinkSize = 150063,
74-
expectedFullLinkSizeWithoutClosure = 92648,
73+
expectedFastLinkSize = 150205,
74+
expectedFullLinkSizeWithoutClosure = 92762,
7575
expectedFullLinkSizeWithClosure = 21325,
7676
classDefs,
7777
moduleInitializers = MainTestModuleInitializers

linker/shared/src/test/scala/org/scalajs/linker/backend/javascript/PrintersTest.scala

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,34 @@ class PrintersTest {
163163
)
164164
}
165165

166+
@Test def printObjectLiteral(): Unit = {
167+
assertPrintEquals("({});", ObjectConstr(Nil))
168+
169+
assertPrintEquals(
170+
"""
171+
|({
172+
| "foo": 1
173+
|});
174+
""",
175+
ObjectConstr(List(StringLiteral("foo") -> IntLiteral(1)))
176+
)
177+
178+
assertPrintEquals(
179+
"""
180+
|({
181+
| "foo": 1,
182+
| ["bar"]: 2,
183+
| baz: 3
184+
|});
185+
""",
186+
ObjectConstr(List(
187+
StringLiteral("foo") -> IntLiteral(1),
188+
ComputedName(StringLiteral("bar")) -> IntLiteral(2),
189+
Ident("baz") -> IntLiteral(3)
190+
))
191+
)
192+
}
193+
166194
@Test def delayedIdentPrintVersusShow(): Unit = {
167195
locally {
168196
object resolver extends DelayedIdent.Resolver {

project/Build.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1998,14 +1998,14 @@ object Build {
19981998
case `default212Version` =>
19991999
if (!useMinifySizes) {
20002000
Some(ExpectedSizes(
2001-
fastLink = 640000 to 641000,
2001+
fastLink = 641000 to 642000,
20022002
fullLink = 101000 to 102000,
20032003
fastLinkGz = 77000 to 78000,
20042004
fullLinkGz = 26000 to 27000,
20052005
))
20062006
} else {
20072007
Some(ExpectedSizes(
2008-
fastLink = 494000 to 495000,
2008+
fastLink = 495000 to 496000,
20092009
fullLink = 337000 to 338000,
20102010
fastLinkGz = 69000 to 70000,
20112011
fullLinkGz = 50000 to 51000,
@@ -2015,15 +2015,15 @@ object Build {
20152015
case `default213Version` =>
20162016
if (!useMinifySizes) {
20172017
Some(ExpectedSizes(
2018-
fastLink = 462000 to 463000,
2018+
fastLink = 463000 to 464000,
20192019
fullLink = 99000 to 100000,
20202020
fastLinkGz = 60000 to 61000,
20212021
fullLinkGz = 26000 to 27000,
20222022
))
20232023
} else {
20242024
Some(ExpectedSizes(
2025-
fastLink = 347000 to 348000,
2026-
fullLink = 307000 to 308000,
2025+
fastLink = 348000 to 349000,
2026+
fullLink = 308000 to 309000,
20272027
fastLinkGz = 54000 to 55000,
20282028
fullLinkGz = 49000 to 50000,
20292029
))

test-suite/js/src/test/scala/org/scalajs/testsuite/jsinterop/DynamicTest.scala

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,19 @@ class DynamicTest {
109109
assertJSUndefined(obj_anything)
110110
}
111111

112-
@Test def objectLiteralInStatementPosition_Issue1627(): Unit = {
113-
// Just make sure it does not cause a SyntaxError
112+
@Test def objectLiteralInStatementPosition_Issue1627_Issue4949(): Unit = {
113+
@noinline def dynProp(): String = "foo"
114+
115+
// Just make sure those statements do not cause a SyntaxError
114116
js.Dynamic.literal(foo = "bar")
115-
// and also test the case without param (different code path in Printers)
116117
js.Dynamic.literal()
118+
js.Dynamic.literal(foo = "bar").foo
119+
js.Dynamic.literal(foo = () => "bar").foo()
120+
js.Dynamic.literal(foo = "bar").foo = "babar"
121+
js.Dynamic.literal(foo = "foo").selectDynamic(dynProp())
122+
js.Dynamic.literal(foo = "foo").updateDynamic(dynProp())("babar")
123+
js.Dynamic.literal(foo = () => "bar").applyDynamic(dynProp())()
124+
js.Dynamic.literal(foo = "bar") + js.Dynamic.literal(foobar = "babar")
117125
}
118126

119127
@Test def objectLiteralConstructionWithDynamicNaming(): Unit = {

0 commit comments

Comments
 (0)