Skip to content

Commit 1afad42

Browse files
committed
Fix #4949: Always wrap object literals with ().
1 parent f4d39fa commit 1afad42

File tree

4 files changed

+29
-20
lines changed

4 files changed

+29
-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

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)