diff --git a/spec/suites/SpecHelper.js b/spec/suites/SpecHelper.js index e99f44dff..31468c3b9 100644 --- a/spec/suites/SpecHelper.js +++ b/spec/suites/SpecHelper.js @@ -51,9 +51,7 @@ export function createContainer(width, height) { } export function removeMapContainer(map, container) { - if (map) { - map.remove(); - } + map?.remove(); if (container) { document.body.removeChild(container); } diff --git a/src/control/Control.js b/src/control/Control.js index f4209e83b..94307c01e 100644 --- a/src/control/Control.js +++ b/src/control/Control.js @@ -41,15 +41,11 @@ export const Control = Class.extend({ setPosition(position) { const map = this._map; - if (map) { - map.removeControl(this); - } + map?.removeControl(this); this.options.position = position; - if (map) { - map.addControl(this); - } + map?.addControl(this); return this; }, diff --git a/src/dom/DomUtil.js b/src/dom/DomUtil.js index 283189841..811ac25a0 100644 --- a/src/dom/DomUtil.js +++ b/src/dom/DomUtil.js @@ -25,9 +25,7 @@ export function create(tagName, className, container) { const el = document.createElement(tagName); el.className = className ?? ''; - if (container) { - container.appendChild(el); - } + container?.appendChild(el); return el; } diff --git a/src/layer/DivOverlay.js b/src/layer/DivOverlay.js index d945d3ba9..459f9ad70 100644 --- a/src/layer/DivOverlay.js +++ b/src/layer/DivOverlay.js @@ -69,9 +69,7 @@ export const DivOverlay = Layer.extend({ // Alternative to `map.closePopup(popup)`/`.closeTooltip(tooltip)` // and `layer.closePopup()`/`.closeTooltip()`. close() { - if (this._map) { - this._map.removeLayer(this); - } + this._map?.removeLayer(this); return this; }, diff --git a/src/layer/Layer.js b/src/layer/Layer.js index d5dd85a85..090bf36d1 100644 --- a/src/layer/Layer.js +++ b/src/layer/Layer.js @@ -66,9 +66,7 @@ export const Layer = Evented.extend({ // @method removeFrom(group: LayerGroup): this // Removes the layer from the given `LayerGroup` removeFrom(obj) { - if (obj) { - obj.removeLayer(this); - } + obj?.removeLayer(this); return this; }, diff --git a/src/layer/LayerGroup.js b/src/layer/LayerGroup.js index 82594d0ba..3af7b96c8 100644 --- a/src/layer/LayerGroup.js +++ b/src/layer/LayerGroup.js @@ -40,9 +40,7 @@ export const LayerGroup = Layer.extend({ this._layers[id] = layer; - if (this._map) { - this._map.addLayer(layer); - } + this._map?.addLayer(layer); return this; }, @@ -86,9 +84,7 @@ export const LayerGroup = Layer.extend({ // implement `methodName`. invoke(methodName, ...args) { for (const layer of Object.values(this._layers)) { - if (layer[methodName]) { - layer[methodName].apply(layer, args); - } + layer[methodName]?.apply(layer, args); } return this; }, diff --git a/src/layer/Popup.js b/src/layer/Popup.js index b1d881343..4dae0fe88 100644 --- a/src/layer/Popup.js +++ b/src/layer/Popup.js @@ -282,7 +282,7 @@ export const Popup = DivOverlay.extend({ _adjustPan() { if (!this.options.autoPan) { return; } - if (this._map._panAnim) { this._map._panAnim.stop(); } + this._map._panAnim?.stop(); // We can endlessly recurse if keepInView is set and the view resets. // Let's guard against that by exiting early if we're responding to our own autopan. @@ -373,9 +373,7 @@ Map.include({ // Closes the popup previously opened with [openPopup](#map-openpopup) (or the given one). closePopup(popup) { popup = arguments.length ? popup : this._popup; - if (popup) { - popup.close(); - } + popup?.close(); return this; } }); @@ -451,18 +449,14 @@ Layer.include({ // @method closePopup(): this // Closes the popup bound to this layer if it is open. closePopup() { - if (this._popup) { - this._popup.close(); - } + this._popup?.close(); return this; }, // @method togglePopup(): this // Opens or closes the popup bound to this layer depending on its current state. togglePopup() { - if (this._popup) { - this._popup.toggle(this); - } + this._popup?.toggle(this); return this; }, @@ -475,9 +469,7 @@ Layer.include({ // @method setPopupContent(content: String|HTMLElement|Popup): this // Sets the content of the popup bound to this layer. setPopupContent(content) { - if (this._popup) { - this._popup.setContent(content); - } + this._popup?.setContent(content); return this; }, diff --git a/src/layer/Tooltip.js b/src/layer/Tooltip.js index 2673614ee..cec6b5a5d 100644 --- a/src/layer/Tooltip.js +++ b/src/layer/Tooltip.js @@ -352,9 +352,7 @@ Layer.include({ // @method toggleTooltip(): this // Opens or closes the tooltip bound to this layer depending on its current state. toggleTooltip() { - if (this._tooltip) { - this._tooltip.toggle(this); - } + this._tooltip?.toggle(this); return this; }, @@ -367,9 +365,7 @@ Layer.include({ // @method setTooltipContent(content: String|HTMLElement|Tooltip): this // Sets the content of the tooltip bound to this layer. setTooltipContent(content) { - if (this._tooltip) { - this._tooltip.setContent(content); - } + this._tooltip?.setContent(content); return this; }, @@ -415,9 +411,7 @@ Layer.include({ _setAriaDescribedByOnLayer(layer) { const el = typeof layer.getElement === 'function' && layer.getElement(); - if (el) { - el.setAttribute('aria-describedby', this._tooltip._container.id); - } + el?.setAttribute?.('aria-describedby', this._tooltip._container.id); }, diff --git a/src/layer/marker/Marker.Drag.js b/src/layer/marker/Marker.Drag.js index 71eb717d8..87b950cf3 100644 --- a/src/layer/marker/Marker.Drag.js +++ b/src/layer/marker/Marker.Drag.js @@ -52,9 +52,7 @@ export const MarkerDrag = Handler.extend({ dragend: this._onDragEnd }, this).disable(); - if (this._marker._icon) { - this._marker._icon.classList.remove('leaflet-marker-draggable'); - } + this._marker._icon?.classList.remove('leaflet-marker-draggable'); }, moved() { diff --git a/src/layer/marker/Marker.js b/src/layer/marker/Marker.js index 84dff0e0f..ea36967c9 100644 --- a/src/layer/marker/Marker.js +++ b/src/layer/marker/Marker.js @@ -303,9 +303,7 @@ export const Marker = Layer.extend({ }, _removeShadow() { - if (this._shadow) { - this._shadow.remove(); - } + this._shadow?.remove(); this._shadow = null; }, diff --git a/src/layer/vector/Path.js b/src/layer/vector/Path.js index 126e6eac1..1d1bf50ee 100644 --- a/src/layer/vector/Path.js +++ b/src/layer/vector/Path.js @@ -114,18 +114,14 @@ export const Path = Layer.extend({ // @method bringToFront(): this // Brings the layer to the top of all path layers. bringToFront() { - if (this._renderer) { - this._renderer._bringToFront(this); - } + this._renderer?._bringToFront(this); return this; }, // @method bringToBack(): this // Brings the layer to the bottom of all path layers. bringToBack() { - if (this._renderer) { - this._renderer._bringToBack(this); - } + this._renderer?._bringToBack(this); return this; }, diff --git a/src/map/Map.js b/src/map/Map.js index d003de5f4..8350ab0e3 100644 --- a/src/map/Map.js +++ b/src/map/Map.js @@ -1266,9 +1266,7 @@ export const Map = Evented.extend({ _stop() { cancelAnimationFrame(this._flyToFrame); - if (this._panAnim) { - this._panAnim.stop(); - } + this._panAnim?.stop(); return this; }, @@ -1745,9 +1743,7 @@ export const Map = Evented.extend({ _onZoomTransitionEnd() { if (!this._animatingZoom) { return; } - if (this._mapPane) { - this._mapPane.classList.remove('leaflet-zoom-anim'); - } + this._mapPane?.classList.remove('leaflet-zoom-anim'); this._animatingZoom = false;