Skip to content

Commit 405d968

Browse files
Update JS syntax to Baseline 2025 using esupgrade (#2346)
Co-authored-by: Tim Schilling <schillingt@better-simple.com>
1 parent 0e5c976 commit 405d968

4 files changed

Lines changed: 56 additions & 54 deletions

File tree

.pre-commit-config.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ repos:
1919
hooks:
2020
- id: django-upgrade
2121
args: [--target-version, "5.2"]
22+
- repo: https://github.com/codingjoe/esupgrade
23+
rev: 2025.15.0
24+
hooks:
25+
- id: esupgrade
2226
- repo: https://github.com/adamchainz/djade-pre-commit
2327
rev: "1.9.0"
2428
hooks:

debug_toolbar/static/debug_toolbar/js/toolbar.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const djdt = {
4040
if (requestId && inner.children.length === 0) {
4141
const url = new URL(
4242
djDebug.dataset.renderPanelUrl,
43-
window.location
43+
globalThis.location
4444
);
4545
url.searchParams.append("request_id", requestId);
4646
url.searchParams.append("panel_id", panelId);
@@ -163,8 +163,8 @@ const djdt = {
163163

164164
if (top < 0) {
165165
top = 0;
166-
} else if (top + handle.offsetHeight > window.innerHeight) {
167-
top = window.innerHeight - handle.offsetHeight;
166+
} else if (top + handle.offsetHeight > globalThis.innerHeight) {
167+
top = globalThis.innerHeight - handle.offsetHeight;
168168
}
169169

170170
handle.style.top = `${top}px`;
@@ -209,19 +209,20 @@ const djdt = {
209209
djdt.updateOnAjax();
210210
}
211211

212-
const prefersDark = window.matchMedia(
212+
const prefersDark = globalThis.matchMedia(
213213
"(prefers-color-scheme: dark)"
214214
).matches;
215215
const themeList = prefersDark
216216
? ["auto", "light", "dark"]
217217
: ["auto", "dark", "light"];
218-
const setTheme = (theme) => {
218+
219+
function setTheme(theme) {
219220
djDebug.setAttribute(
220221
"data-theme",
221222
theme === "auto" ? (prefersDark ? "dark" : "light") : theme
222223
);
223224
djDebug.setAttribute("data-user-theme", theme);
224-
};
225+
}
225226

226227
// Updates the theme using user settings
227228
let userTheme = localStorage.getItem("djdt.user-theme") || "auto";
@@ -251,7 +252,7 @@ const djdt = {
251252
// set handle position
252253
const handleTop = Math.min(
253254
localStorage.getItem("djdt.top") || 265,
254-
window.innerHeight - handle.offsetWidth
255+
globalThis.innerHeight - handle.offsetWidth
255256
);
256257
handle.style.top = `${handleTop}px`;
257258
},
@@ -264,7 +265,7 @@ const djdt = {
264265
const handle = djDebug.querySelector("#djDebugToolbarHandle");
265266
$$.show(handle);
266267
djdt.ensureHandleVisibility();
267-
window.addEventListener("resize", djdt.ensureHandleVisibility);
268+
globalThis.addEventListener("resize", djdt.ensureHandleVisibility);
268269
document.removeEventListener("keydown", onKeyDown);
269270

270271
localStorage.setItem("djdt.show", "false");
@@ -290,7 +291,7 @@ const djdt = {
290291
$$.hide(djDebug.querySelector("#djDebugToolbarHandle"));
291292
$$.show(djDebug.querySelector("#djDebugToolbar"));
292293
localStorage.setItem("djdt.show", "true");
293-
window.removeEventListener("resize", djdt.ensureHandleVisibility);
294+
globalThis.removeEventListener("resize", djdt.ensureHandleVisibility);
294295
},
295296
updateOnAjax() {
296297
const handleAjaxResponse = debounce(async (requestId) => {
@@ -328,8 +329,8 @@ const djdt = {
328329
origOpen.apply(this, args);
329330
};
330331

331-
const origFetch = window.fetch;
332-
window.fetch = function (...args) {
332+
const origFetch = globalThis.fetch;
333+
globalThis.fetch = function (...args) {
333334
// Heads up! Before modifying this code, please be aware of the
334335
// possible unhandled errors that might arise from changing this.
335336
// For details, see
@@ -384,7 +385,7 @@ const djdt = {
384385
},
385386
},
386387
};
387-
window.djdt = {
388+
globalThis.djdt = {
388389
show_toolbar: djdt.showToolbar,
389390
hide_toolbar: djdt.hideToolbar,
390391
init: djdt.init,

debug_toolbar/static/debug_toolbar/js/utils.js

Lines changed: 37 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const $$ = {
1+
export const $$ = {
22
on(root, eventName, selector, fn) {
33
root.removeEventListener(eventName, fn);
44
root.addEventListener(eventName, (event) => {
@@ -8,15 +8,15 @@ const $$ = {
88
}
99
});
1010
},
11+
/**
12+
* This is a helper function to attach a handler for a `djdt.panel.render`
13+
* event of a specific panel.
14+
*
15+
* root: The container element that the listener should be attached to.
16+
* panelId: The Id of the panel.
17+
* fn: A function to execute when the event is triggered.
18+
*/
1119
onPanelRender(root, panelId, fn) {
12-
/*
13-
This is a helper function to attach a handler for a `djdt.panel.render`
14-
event of a specific panel.
15-
16-
root: The container element that the listener should be attached to.
17-
panelId: The Id of the panel.
18-
fn: A function to execute when the event is triggered.
19-
*/
2020
root.addEventListener("djdt.panel.render", (event) => {
2121
if (event.detail.panelId === panelId) {
2222
fn.call(event);
@@ -48,12 +48,12 @@ const $$ = {
4848
document.head.appendChild(el);
4949
}
5050
},
51+
/**
52+
* Given a container element, apply styles set via data-djdt-styles attribute.
53+
* The format is data-djdt-styles="styleName1:value;styleName2:value2"
54+
* The style names should use the CSSStyleDeclaration camel cased names.
55+
*/
5156
applyStyles(container) {
52-
/*
53-
* Given a container element, apply styles set via data-djdt-styles attribute.
54-
* The format is data-djdt-styles="styleName1:value;styleName2:value2"
55-
* The style names should use the CSSStyleDeclaration camel cased names.
56-
*/
5757
for (const element of container.querySelectorAll(
5858
"[data-djdt-styles]"
5959
)) {
@@ -85,34 +85,31 @@ function getDebugElement() {
8585
return root.querySelector("#djDebug");
8686
}
8787

88-
function ajax(url, init) {
89-
return fetch(url, Object.assign({ credentials: "same-origin" }, init))
90-
.then((response) => {
91-
if (response.ok) {
92-
return response
93-
.json()
94-
.catch((error) =>
95-
Promise.reject(
96-
new Error(
97-
`The response is a invalid Json object : ${error}`
98-
)
99-
)
100-
);
101-
}
102-
return Promise.reject(
103-
new Error(`${response.status}: ${response.statusText}`)
104-
);
105-
})
106-
.catch((error) => {
107-
const djDebug = getDebugElement();
108-
const win = djDebug.querySelector("#djDebugWindow");
109-
win.innerHTML = `<div class="djDebugPanelTitle"><h3>${error.message}</h3><button type="button" class="djDebugClose">»</button></div>`;
110-
$$.show(win);
111-
throw error;
88+
export async function ajax(url, init) {
89+
try {
90+
const response = await fetch(url, {
91+
credentials: "same-origin",
92+
...init,
11293
});
94+
if (response.ok) {
95+
try {
96+
return response.json();
97+
} catch (error) {
98+
throw new Error(
99+
`The response is a invalid Json object : ${error}`
100+
);
101+
}
102+
}
103+
throw new Error(`${response.status}: ${response.statusText}`);
104+
} catch (error) {
105+
const win = document.getElementById("djDebugWindow");
106+
win.innerHTML = `<div class="djDebugPanelTitle"><h3>${error.message}</h3><button type="button" class="djDebugClose">»</button></div>`;
107+
$$.show(win);
108+
throw error;
109+
}
113110
}
114111

115-
function ajaxForm(element) {
112+
export function ajaxForm(element) {
116113
const form = element.closest("form");
117114
const url = new URL(form.action);
118115
const formData = new FormData(form);
@@ -125,7 +122,7 @@ function ajaxForm(element) {
125122
return ajax(url, ajaxData);
126123
}
127124

128-
function replaceToolbarState(newRequestId, data) {
125+
export function replaceToolbarState(newRequestId, data) {
129126
const djDebug = getDebugElement();
130127
djDebug.setAttribute("data-request-id", newRequestId);
131128
// Check if response is empty, it could be due to an expired requestId.
@@ -155,5 +152,3 @@ export function debounce(func, timeout) {
155152
timer = setTimeout(() => func(...args), timeout);
156153
};
157154
}
158-
159-
export { $$, ajax, ajaxForm, getDebugElement, replaceToolbarState };

docs/changes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ Pending
2121
limits. When ``SQLParseError`` is raised, the SQL panel now automatically
2222
disables grouping and retries formatting, preventing crashes with large
2323
queries.
24+
* Upgraded the JavaScript code to use modern ECMAScript features using
25+
``esupgrade``.
2426

2527
6.3.0 (2026-04-01)
2628
------------------

0 commit comments

Comments
 (0)