@@ -69,11 +69,6 @@ class PrepJSInterop extends MacroTransform with IdentityDenotTransformer { thisP
69
69
70
70
override def changesMembers : Boolean = true // the phase adds export forwarders
71
71
72
- override def runOn (units : List [CompilationUnit ])(using Context ): List [CompilationUnit ] = {
73
- sjsPlatform.perRunInfo.clear()
74
- super .runOn(units)
75
- }
76
-
77
72
protected def newTransformer (using Context ): Transformer =
78
73
new ScalaJSPrepJSInteropTransformer
79
74
@@ -544,29 +539,17 @@ class PrepJSInterop extends MacroTransform with IdentityDenotTransformer { thisP
544
539
report.error(i " Traits may not have an @ ${annotSym.name} annotation. " , annot.tree)
545
540
}
546
541
} else {
547
- /* Compute the loading spec now, before `flatten` destroys the name.
548
- * We store it in a global map.
549
- */
550
- val optLoadSpec = checkAndComputeJSNativeLoadSpecOf(treePos, sym)
551
- for (loadSpec <- optLoadSpec)
552
- sjsPlatform.perRunInfo.storeJSNativeLoadSpec(sym, loadSpec)
542
+ checkJSNativeLoadSpecOf(treePos, sym)
553
543
}
554
544
}
555
545
}
556
546
557
- private def checkAndComputeJSNativeLoadSpecOf (pos : SrcPos , sym : Symbol )(
558
- using Context ): Option [JSNativeLoadSpec ] = {
547
+ private def checkJSNativeLoadSpecOf (pos : SrcPos , sym : Symbol )(using Context ): Unit = {
559
548
import JSNativeLoadSpec ._
560
549
561
- def makeGlobalRefNativeLoadSpec (globalRef : String ,
562
- path : List [String ]): Global = {
563
- val validatedGlobalRef = if (! JSGlobalRef .isValidJSGlobalRefName(globalRef)) {
550
+ def checkGlobalRefName (globalRef : String ): Unit = {
551
+ if (! JSGlobalRef .isValidJSGlobalRefName(globalRef))
564
552
report.error(s " The name of a JS global variable must be a valid JS identifier (got ' $globalRef') " , pos)
565
- " erroneous"
566
- } else {
567
- globalRef
568
- }
569
- Global (validatedGlobalRef, path)
570
553
}
571
554
572
555
if (enclosingOwner is OwnerKind .JSNative ) {
@@ -593,86 +576,56 @@ class PrepJSInterop extends MacroTransform with IdentityDenotTransformer { thisP
593
576
}
594
577
}
595
578
596
- val jsName = sym.jsName match {
597
- case JSName .Literal (jsName) => jsName
598
- case JSName .Computed (_) => " <erroneous>" // compile error above
599
- }
600
-
601
- val ownerLoadSpec = sjsPlatform.perRunInfo.jsNativeLoadSpecOfOption(sym.owner)
602
- val loadSpec = ownerLoadSpec match {
603
- case None =>
604
- // The owner is a JSGlobalScope
605
- makeGlobalRefNativeLoadSpec(jsName, Nil )
606
- case Some (Global (globalRef, path)) =>
607
- Global (globalRef, path :+ jsName)
608
- case Some (Import (module, path)) =>
609
- Import (module, path :+ jsName)
610
- case Some (ImportWithGlobalFallback (
611
- Import (module, modulePath), Global (globalRef, globalPath))) =>
612
- ImportWithGlobalFallback (
613
- Import (module, modulePath :+ jsName),
614
- Global (globalRef, globalPath :+ jsName))
579
+ if (sym.owner.hasAnnotation(jsdefn.JSGlobalScopeAnnot )) {
580
+ val jsName = sym.jsName match {
581
+ case JSName .Literal (jsName) =>
582
+ checkGlobalRefName(jsName)
583
+ case JSName .Computed (_) =>
584
+ () // compile error above
585
+ }
615
586
}
616
- Some (loadSpec)
617
- } else {
618
- None
619
587
}
620
588
} else {
621
- def parsePath (pathName : String ): List [String ] =
622
- pathName.split('.' ).toList
623
-
624
- def parseGlobalPath (pathName : String ): Global = {
625
- val globalRef :: path = parsePath(pathName)
626
- makeGlobalRefNativeLoadSpec(globalRef, path)
589
+ def firstElementOfPath (pathName : String ): String = {
590
+ val dotIndex = pathName.indexOf('.' )
591
+ if (dotIndex < 0 ) pathName
592
+ else pathName.substring(0 , dotIndex)
627
593
}
628
594
595
+ def checkGlobalRefPath (pathName : String ): Unit =
596
+ checkGlobalRefName(firstElementOfPath(pathName))
597
+
629
598
checkAndGetJSNativeLoadingSpecAnnotOf(pos, sym) match {
630
599
case Some (annot) if annot.symbol == jsdefn.JSGlobalScopeAnnot =>
631
600
if (! sym.is(Module )) {
632
601
report.error(
633
602
" @JSGlobalScope can only be used on native JS objects (with @js.native)." ,
634
603
annot.tree)
635
604
}
636
- None
637
605
638
606
case Some (annot) if annot.symbol == jsdefn.JSGlobalAnnot =>
639
607
if (shouldCheckLiterals)
640
608
checkJSGlobalLiteral(annot)
641
609
val pathName = annot.argumentConstantString(0 ).getOrElse {
642
- val needsExplicitJSName = {
643
- (enclosingOwner is OwnerKind .ScalaMod ) &&
644
- ! sym.owner.isPackageObject
645
- }
646
-
647
- if (needsExplicitJSName) {
610
+ if ((enclosingOwner is OwnerKind .ScalaMod ) && ! sym.owner.isPackageObject) {
648
611
report.error(
649
612
" Native JS members inside non-native objects must have an explicit name in @JSGlobal" ,
650
613
annot.tree)
651
614
}
652
615
sym.defaultJSName
653
616
}
654
- Some (parseGlobalPath( pathName) )
617
+ checkGlobalRefPath( pathName)
655
618
656
619
case Some (annot) if annot.symbol == jsdefn.JSImportAnnot =>
657
620
if (shouldCheckLiterals)
658
621
checkJSImportLiteral(annot)
659
- val module = annot.argumentConstantString(0 ).getOrElse {
660
- " " // an error is reported by checkJSImportLiteral in this case
661
- }
662
- val path = annot.argumentConstantString(1 ).fold[List [String ]](Nil )(parsePath)
663
- val importSpec = Import (module, path)
664
- val loadSpec = annot.argumentConstantString(2 ).fold[JSNativeLoadSpec ] {
665
- importSpec
666
- } { globalPathName =>
667
- ImportWithGlobalFallback (importSpec, parseGlobalPath(globalPathName))
622
+ annot.argumentConstantString(2 ).foreach { globalPathName =>
623
+ checkGlobalRefPath(globalPathName)
668
624
}
669
- Some (loadSpec)
670
625
671
626
case _ =>
672
- /* We already emitted an error. Invent something not to cause
673
- * cascading errors.
674
- */
675
- Some (JSNativeLoadSpec .Global (" erroneous" , Nil ))
627
+ // We already emitted an error in checkAndGetJSNativeLoadingSpecAnnotOf
628
+ ()
676
629
}
677
630
}
678
631
}
0 commit comments