Use optional chaining with function calls (#9737)

This commit is contained in:
Simon Legner
2025-05-30 08:45:09 +02:00
committed by GitHub
parent a01d0a9168
commit 3380110188
12 changed files with 22 additions and 64 deletions

View File

@ -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);
}

View File

@ -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;
},

View File

@ -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;
}

View File

@ -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;
},

View File

@ -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;
},

View File

@ -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;
},

View File

@ -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;
},

View File

@ -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);
},

View File

@ -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() {

View File

@ -303,9 +303,7 @@ export const Marker = Layer.extend({
},
_removeShadow() {
if (this._shadow) {
this._shadow.remove();
}
this._shadow?.remove();
this._shadow = null;
},

View File

@ -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;
},

View File

@ -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;