Skip to content

Commit 48a3231

Browse files
authored
Merge pull request #565 from scala-js/topic/defaultArgs
Default args for facades should be `js.native`
2 parents 432af50 + f8a4b74 commit 48a3231

File tree

17 files changed

+188
-140
lines changed

17 files changed

+188
-140
lines changed

.scalafix.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
rules = [
2+
DefaultFacadeArgs,
23
ExplicitResultTypes,
34
OrganizeImports,
45
RemoveUnused,

api-reports/2_12.txt

Lines changed: 29 additions & 29 deletions
Large diffs are not rendered by default.

api-reports/2_13.txt

Lines changed: 65 additions & 65 deletions
Large diffs are not rendered by default.

prePR.sbt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import Build._
2+
import Dependencies.Ver._
23

34
addCommandAlias("prePR", "+prePR_nonCross")
45

6+
addCommandAlias("quickPrePR", s"++$scala212; prePR_nonCross; ++$scala213; prePR_nonCross")
7+
58
val prePR_nonCross = taskKey[Unit]("Performs all necessary work required before submitting a PR, for a single version of Scala.")
69

710
// Unfortunately we can't just call `root/Test/compile` because it doesn't take aggregation into account :(
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
org.scalajs.dom.scalafix.DefaultFacadeArgs
12
org.scalajs.dom.scalafix.GenerateApiReport
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.scalajs.dom.scalafix
2+
3+
import scala.meta._
4+
import scalafix.v1._
5+
6+
class DefaultFacadeArgs extends SemanticRule("DefaultFacadeArgs") {
7+
8+
override def fix(implicit doc: SemanticDocument): Patch =
9+
doc.tree.collect {
10+
11+
case Defn.Def(mods, _, _, paramss, _, body) if Util.isJsNative(body) =>
12+
13+
// https://github.com/scala-js/scala-js/issues/4553
14+
// `js.native` as default arg doesn't compile for top-level method facades.
15+
if (Util.isJSGlobal(mods)) {
16+
// Ignore for now
17+
Patch.empty
18+
19+
} else {
20+
// Replace default argument with js.native
21+
lazy val jsNative = body.toString
22+
paramss.iterator.flatten.foldLeft(Patch.empty) { (patch, param) =>
23+
param.default match {
24+
case Some(d) if d.toString != jsNative => patch + Patch.replaceTree(d, jsNative)
25+
case _ => patch
26+
}
27+
}
28+
}
29+
}.asPatch
30+
31+
}

scalafix/src/main/scala/org/scalajs/dom/scalafix/GenerateApiReport.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class GenerateApiReport extends SemanticRule("GenerateApiReport") {
9595
.replaceAll(" {2,}", " ")
9696
.trim
9797
.stripSuffix(" = js.native")
98-
.replaceAll(" = js.native(?=[^\n])", "?")
98+
.replaceAll(" = js\\.(native|undefined)(?=[^\n])", "?")
9999

100100
// "?" means that type aliases come before everything else
101101
val name = Util.termName(t2).fold("?")(_.value)

scalafix/src/main/scala/org/scalajs/dom/scalafix/Util.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,19 @@ object Util {
2424

2525
// ===================================================================================================================
2626

27+
def isJSGlobal(mods: List[Mod]): Boolean =
28+
mods.exists {
29+
case Mod.Annot(Init(Type.Name("JSGlobal"), _, _)) => true
30+
case _ => false
31+
}
32+
33+
def isJsNative(t: Term): Boolean = {
34+
val s = t.toString
35+
s == "js.native" || s == "native"
36+
}
37+
38+
// ===================================================================================================================
39+
2740
def parents(sym: Symbol)(implicit doc: SemanticDocument): List[SemanticType] =
2841
dealias(sym).info match {
2942
case Some(i) => parents(i.signature)

src/main/scala/org/scalajs/dom/Audio.scala

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class AudioContext extends EventTarget {
8181
* The number of channels in the input audio streams, which the output stream will contain; the default is 6 is
8282
* this parameter is not specified.
8383
*/
84-
def createChannelMerger(numberOfInputs: Int = 6): ChannelMergerNode = js.native
84+
def createChannelMerger(numberOfInputs: Int = js.native): ChannelMergerNode = js.native
8585

8686
/** Creates a ChannelSplitterNode, which is used to access the individual channels of an audio stream and process them
8787
* separately.
@@ -90,7 +90,7 @@ class AudioContext extends EventTarget {
9090
* The number of channels in the input audio stream that you want to output separately; the default is 6 is this
9191
* parameter is not specified.
9292
*/
93-
def createChannelSplitter(numberOfOutputs: Int = 6): ChannelSplitterNode = js.native
93+
def createChannelSplitter(numberOfOutputs: Int = js.native): ChannelSplitterNode = js.native
9494

9595
/** Creates a ConvolverNode, which can be used to apply convolution effects to your audio graph, for example a
9696
* reverberation effect.
@@ -414,15 +414,15 @@ trait AudioBufferSourceNode extends AudioNode {
414414
* The duration parameter, which defaults to the length of the asset minus the value of offset, defines the length
415415
* of the portion of the asset to be played.
416416
*/
417-
def start(when: Double = 0.0, offset: Double = 0.0, duration: Double = js.native): Unit = js.native
417+
def start(when: Double = js.native, offset: Double = js.native, duration: Double = js.native): Unit = js.native
418418

419419
/** Schedules the end of the playback of an audio asset.
420420
*
421421
* @param when
422422
* The when parameter defines when the playback will stop. If it represents a time in the past, the playback will
423423
* end immediately. If this method is called twice or more, an exception is raised.
424424
*/
425-
def stop(when: Double = 0.0): Unit = js.native
425+
def stop(when: Double = js.native): Unit = js.native
426426

427427
/** Is an EventHandler containing the callback associated with the ended event. */
428428
var onended: js.Function1[Event, _] = js.native
@@ -481,7 +481,7 @@ trait AudioListener extends AudioNode {
481481
* @param z
482482
* The z position of the listener in 3D space.
483483
*/
484-
def setPosition(x: Double = 0.0, y: Double = 0.0, z: Double = 0.0): Unit = js.native
484+
def setPosition(x: Double = js.native, y: Double = js.native, z: Double = js.native): Unit = js.native
485485

486486
/** Defines the orientation of the listener.
487487
*
@@ -508,8 +508,8 @@ trait AudioListener extends AudioNode {
508508
* @param zUp
509509
* The z value of the up vector of the listener.
510510
*/
511-
def setOrientation(x: Double = 0.0, y: Double = 0.0, z: Double = -1.0, xUp: Double = 0.0, yUp: Double = 1.0,
512-
zUp: Double = 0.0): Unit = js.native
511+
def setOrientation(x: Double = js.native, y: Double = js.native, z: Double = js.native, xUp: Double = js.native,
512+
yUp: Double = js.native, zUp: Double = js.native): Unit = js.native
513513
}
514514

515515
/** The AudioParam interface represents an audio-related parameter, usually a parameter of an AudioNode (such as
@@ -891,10 +891,10 @@ trait OscillatorNode extends AudioNode {
891891
var `type`: String = js.native // Not sure if this is correct ...
892892

893893
/** This method specifies the exact time to start playing the tone. */
894-
def start(when: Double = 0.0): Unit = js.native
894+
def start(when: Double = js.native): Unit = js.native
895895

896896
/** This method specifies the exact time to stop playing the tone. */
897-
def stop(when: Double = 0.0): Unit = js.native
897+
def stop(when: Double = js.native): Unit = js.native
898898

899899
/** Used to point to a PeriodicWave defining a periodic waveform that can be used to shape the oscillator's output,
900900
* when type = "custom" is used.
@@ -977,7 +977,7 @@ trait PannerNode extends AudioNode {
977977
* @param z
978978
* The z position of the panner in 3D space.
979979
*/
980-
def setPosition(x: Double = 0.0, y: Double = 0.0, z: Double = 0.0): Unit = js.native
980+
def setPosition(x: Double = js.native, y: Double = js.native, z: Double = js.native): Unit = js.native
981981

982982
/** Defines the direction the audio source is playing in. This can have a big effect if the sound is very directional
983983
* — controlled by the three cone-related attributes PannerNode.coneInnerAngle, PannerNode.coneOuterAngle, and
@@ -996,7 +996,7 @@ trait PannerNode extends AudioNode {
996996
* @param z
997997
* The z value of the panner's direction vector in 3D space.
998998
*/
999-
def setOrientation(x: Double = 1.0, y: Double = 0.0, z: Double = 0.0): Unit = js.native
999+
def setOrientation(x: Double = js.native, y: Double = js.native, z: Double = js.native): Unit = js.native
10001000

10011001
/** Defines the velocity vector of the audio source — how fast it is moving and in what direction.
10021002
*
@@ -1015,7 +1015,7 @@ trait PannerNode extends AudioNode {
10151015
* @param z
10161016
* The z value of the panner's velocity vector.
10171017
*/
1018-
def setVelocity(x: Double = 0.0, y: Double = 0.0, z: Double = 0.0): Unit = js.native
1018+
def setVelocity(x: Double = js.native, y: Double = js.native, z: Double = js.native): Unit = js.native
10191019
}
10201020

10211021
/** The StereoPannerNode interface of the Web Audio API represents a simple stereo panner node that can be used to pan

src/main/scala/org/scalajs/dom/Fetch.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ object Fetch extends js.Object {
1414
* @param init
1515
* @return
1616
*/
17-
def fetch(info: RequestInfo, init: RequestInit = null): js.Promise[Response] = js.native
17+
def fetch(info: RequestInfo, init: RequestInit = js.native): js.Promise[Response] = js.native
1818
}
1919

2020
/** The Request interface of the Fetch API represents a resource request.
@@ -151,7 +151,7 @@ object Response extends js.Object {
151151
* @return
152152
* a new Response
153153
*/
154-
def redirect(url: String, status: Int = 302): Response = js.native
154+
def redirect(url: String, status: Int = js.native): Response = js.native
155155
}
156156

157157
/** See [[https://fetch.spec.whatwg.org/#response-class ¶6.4 Response class]] definition in whatwg Fetch spec. */

0 commit comments

Comments
 (0)