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. */

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,12 @@ class IDBObjectStore extends js.Object {
7676
def put(value: js.Any, key: js.Any = js.native): IDBRequest = js.native
7777

7878
/** The method sets the position of the cursor to the appropriate record, based on the specified direction. */
79-
def openCursor(range: js.UndefOr[IDBKeyRange | js.Any] = js.undefined,
80-
direction: js.UndefOr[IDBCursorDirection] = js.undefined): IDBRequest = js.native
79+
def openCursor(range: js.UndefOr[IDBKeyRange | js.Any] = js.native,
80+
direction: js.UndefOr[IDBCursorDirection] = js.native): IDBRequest = js.native
8181

8282
/** The method sets the position of the cursor to the appropriate key, based on the specified direction. */
83-
def openKeyCursor(range: js.UndefOr[IDBKeyRange | js.Any] = js.undefined,
84-
direction: js.UndefOr[IDBCursorDirection] = js.undefined): IDBRequest = js.native
83+
def openKeyCursor(range: js.UndefOr[IDBKeyRange | js.Any] = js.native,
84+
direction: js.UndefOr[IDBCursorDirection] = js.native): IDBRequest = js.native
8585

8686
/** Note that this method must be called only from a VersionChange transaction mode callback. Note that this method
8787
* synchronously modifies the IDBObjectStore.indexNames property.
@@ -99,14 +99,14 @@ class IDBObjectStore extends js.Object {
9999
/** If a value is successfully found, then a structured clone of it is created and set as the result of the request
100100
* object.
101101
*/
102-
def getAll(query: js.UndefOr[IDBKeyRange | js.Any] = js.undefined,
103-
count: js.UndefOr[Int] = js.undefined): IDBRequest = js.native
102+
def getAll(query: js.UndefOr[IDBKeyRange | js.Any] = js.native,
103+
count: js.UndefOr[Int] = js.native): IDBRequest = js.native
104104

105105
/** If a value is successfully found, then a structured clone of it is created and set as the result of the request
106106
* object.
107107
*/
108-
def getAllKeys(query: js.UndefOr[IDBKeyRange | js.Any] = js.undefined,
109-
count: js.UndefOr[Int] = js.undefined): IDBRequest = js.native
108+
def getAllKeys(query: js.UndefOr[IDBKeyRange | js.Any] = js.native,
109+
count: js.UndefOr[Int] = js.native): IDBRequest = js.native
110110

111111
/** If a value is successfully found, then a structured clone of it is created and set as the result of the request
112112
* object.
@@ -237,7 +237,7 @@ class IDBCursor extends js.Object {
237237
*
238238
* W3C
239239
*/
240-
def continue(key: js.Any = ???): Unit = js.native
240+
def continue(key: js.Any = js.native): Unit = js.native
241241

242242
/** Returns an IDBRequest object, and, in a separate thread, deletes the record at the cursor's position, without
243243
* changing the cursor's position.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class OffscreenCanvas(var width: Double, var height: Double) extends js.Object {
1616
def getContext(contextType: String, contextAttributes: TwoDContextAttributes): js.Dynamic = js.native
1717

1818
/** Creates a Blob object representing the image contained in the canvas. */
19-
def convertToBlob(options: ConvertToBlobOptions = ???): js.Promise[Blob] = js.native
19+
def convertToBlob(options: ConvertToBlobOptions = js.native): js.Promise[Blob] = js.native
2020

2121
/** Creates an ImageBitmap object from the most recently rendered image of the OffscreenCanvas. */
2222
def transferToImageBitmap(): ImageBitmap = js.native

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ trait WriteableStream[-T] extends js.Object {
6666
*
6767
* @param reason
6868
*/
69-
def abort(reason: js.UndefOr[Any] = js.undefined): js.Promise[Unit] = js.native
69+
def abort(reason: js.UndefOr[Any] = js.native): js.Promise[Unit] = js.native
7070

7171
/** The close method signals that the producer is done writing chunks to the stream and wishes to move the stream to a
7272
* "closed" state. This queues an action to close the stream, such that once any currently queued-up writes complete,
@@ -118,7 +118,7 @@ trait ReadableStream[+T] extends js.Object {
118118
* @return
119119
* a Promise
120120
*/
121-
def cancel(reason: js.UndefOr[Any] = js.undefined): js.Promise[Unit] = js.native
121+
def cancel(reason: js.UndefOr[Any] = js.native): js.Promise[Unit] = js.native
122122

123123
/** See [[https://streams.spec.whatwg.org/#rs-get-reader ¶3.2.4.3. getReader()]] of whatwg streams spec. Also see the
124124
* example usage there.
@@ -153,7 +153,7 @@ trait ReadableStream[+T] extends js.Object {
153153
* //todo: determine the type of options
154154
*/
155155
def pipeThrough[U](pair: Any, // TODO js.Tuple2[WriteableStream[T], ReadableStream[U]]
156-
options: Any = js.undefined): ReadableStream[U] = js.native
156+
options: Any = js.native): ReadableStream[U] = js.native
157157

158158
/** See
159159
* [[https://streams.spec.whatwg.org/#rs-pipe-to ¶3.2.4.5. pipeTo(dest, { preventClose, preventAbort, preventCancel } = {})]]
@@ -169,7 +169,7 @@ trait ReadableStream[+T] extends js.Object {
169169
*
170170
* //todo: determine the type of options
171171
*/
172-
def pipeTo(dest: WriteableStream[T], options: Any = js.undefined): Unit = js.native
172+
def pipeTo(dest: WriteableStream[T], options: Any = js.native): Unit = js.native
173173

174174
/** See [[https://streams.spec.whatwg.org/#rs-tee ¶3.2.4.6. tee()]] of whatwg streams spec.
175175
*

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ trait WindowOrWorkerGlobalScope extends WindowBase64 with WindowTimers {
3232
def origin: String = js.native //should be USVString
3333

3434
/** Starts the process of fetching a resource from the network. */
35-
def fetch(info: RequestInfo, init: RequestInit = null): js.Promise[Response] = js.native
35+
def fetch(info: RequestInfo, init: RequestInit = js.native): js.Promise[Response] = js.native
3636

3737
/** Enqueues a microtask—a short function to be executed after execution of the JavaScript code completes and control
3838
* isn't being returned to a JavaScript caller, but before handling callbacks and other tasks.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ trait BeaconNavigator extends js.Object {
2424
* @param data
2525
* The data parameter is the ArrayBufferView, Blob, DOMString, or FormData data that is to be transmitted.
2626
*/
27-
def sendBeacon(url: String, data: dom.BodyInit = null): Boolean = js.native
27+
def sendBeacon(url: String, data: dom.BodyInit = js.native): Boolean = js.native
2828
}
2929

3030
/** The Beacon interface is used to schedule an asynchronous and non-blocking request to a web server. Beacon requests
@@ -48,5 +48,5 @@ trait BeaconWorkerNavigator extends js.Object {
4848
* @param data
4949
* The data parameter is the ArrayBufferView, Blob, DOMString, or FormData data that is to be transmitted.
5050
*/
51-
def sendBeacon(url: String, data: dom.BodyInit = null): Boolean = js.native
51+
def sendBeacon(url: String, data: dom.BodyInit = js.native): Boolean = js.native
5252
}

src/main/scala/org/scalajs/dom/experimental/serviceworkers/ServiceWorkers.scala

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -184,12 +184,12 @@ trait ServiceWorkerRegistration extends EventTarget {
184184
* have many active but differently-scoped service worker registrations. Notifications created by one service worker
185185
* on the same origin will not be available to other active services workers on that same origin.
186186
*/
187-
def getNotifications(options: GetNotificationOptions = ???): js.Promise[Sequence[Notification]] = js.native
187+
def getNotifications(options: GetNotificationOptions = js.native): js.Promise[Sequence[Notification]] = js.native
188188

189189
/** The showNotification() method of the ServiceWorkerRegistration interface creates a notification on an active
190190
* service worker.
191191
*/
192-
def showNotification(title: String, options: NotificationOptions = ???): js.Promise[Unit] = js.native
192+
def showNotification(title: String, options: NotificationOptions = js.native): js.Promise[Unit] = js.native
193193
}
194194

195195
/** An object containing options to filter the notifications returned. */
@@ -212,8 +212,7 @@ trait ServiceWorkerContainer extends EventTarget {
212212
* method can't return a ServiceWorkerRegistration, it returns a Promise. You can call this method unconditionally
213213
* from the controlled page, i.e., you don't need to first check whether there's an active registration.
214214
*/
215-
def register(scriptURL: String,
216-
options: js.Object = new js.Object()): js.Promise[ServiceWorkerRegistration] = js.native
215+
def register(scriptURL: String, options: js.Object = js.native): js.Promise[ServiceWorkerRegistration] = js.native
217216

218217
/** The ServiceWorkerContainer.controller read-only property of the ServiceWorkerContainer interface returns the
219218
* ServiceWorker whose state is activated (the same object returned by ServiceWorkerRegistration.active). This
@@ -224,7 +223,7 @@ trait ServiceWorkerContainer extends EventTarget {
224223
/** Gets a ServiceWorkerRegistration object whose scope URL matches the document URL. If the method can't return a
225224
* ServiceWorkerRegistration, it returns a Promise.
226225
*/
227-
def getRegistration(scope: String = ""): js.Promise[js.UndefOr[ServiceWorkerRegistration]] = js.native
226+
def getRegistration(scope: String = js.native): js.Promise[js.UndefOr[ServiceWorkerRegistration]] = js.native
228227

229228
/** The getRegistrations() method of the ServiceWorkerContainer interface returns all ServiceWorkerRegistrations
230229
* associated with a ServiceWorkerContainer in an array. If the method can't return ServiceWorkerRegistrations, it

0 commit comments

Comments
 (0)