Skip to content

Commit 2120252

Browse files
Replace hasOwnProperty with Object.hasOwn (#1920)
## PR Summary This PR replaces all occurrences of `hasOwnProperty()` with `Object.hasOwn()` in `controllers.js` (no other places). This change uses the modern ES2022 method that's more robust and doesn't require eslint-disable comments. The replacement is functionally identical but safer since `Object.hasOwn()` can't be overridden on the object being checked. Closes #1893. <a data-ca-tag href="https://codeapprove.com/pr/tiny-pilot/tinypilot/1920"><img src="https://codeapprove.com/external/github-tag-allbg.png" alt="Review on CodeApprove" /></a> Signed-off-by: Emmanuel Ferdman <[email protected]>
1 parent e6dae68 commit 2120252

File tree

1 file changed

+14
-28
lines changed

1 file changed

+14
-28
lines changed

app/static/js/controllers.js

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ export async function getLatestRelease() {
6969
.then(processJsonResponse)
7070
.then((updateInfo) => {
7171
["version", "kind", "data"].forEach((field) => {
72-
// eslint-disable-next-line no-prototype-builtins
73-
if (!updateInfo.hasOwnProperty(field)) {
72+
if (!Object.hasOwn(updateInfo, field)) {
7473
throw new ControllerError(`Missing expected ${field} field`);
7574
}
7675
});
@@ -87,8 +86,7 @@ export async function getVersion() {
8786
})
8887
.then(processJsonResponse)
8988
.then((versionResponse) => {
90-
// eslint-disable-next-line no-prototype-builtins
91-
if (!versionResponse.hasOwnProperty("version")) {
89+
if (!Object.hasOwn(versionResponse, "version")) {
9290
throw new ControllerError("Missing expected version field");
9391
}
9492
return versionResponse;
@@ -149,12 +147,10 @@ export async function getUpdateStatus() {
149147
})
150148
.then(processJsonResponse)
151149
.then((data) => {
152-
// eslint-disable-next-line no-prototype-builtins
153-
if (!data.hasOwnProperty("status")) {
150+
if (!Object.hasOwn(data, "status")) {
154151
throw new ControllerError("Missing expected status field");
155152
}
156-
// eslint-disable-next-line no-prototype-builtins
157-
if (!data.hasOwnProperty("updateError")) {
153+
if (!Object.hasOwn(data, "updateError")) {
158154
throw new ControllerError("Missing expected updateError field");
159155
}
160156
return { status: data.status, updateError: data.updateError };
@@ -170,8 +166,7 @@ export async function determineHostname() {
170166
})
171167
.then(processJsonResponse)
172168
.then((hostnameResponse) => {
173-
// eslint-disable-next-line no-prototype-builtins
174-
if (!hostnameResponse.hasOwnProperty("hostname")) {
169+
if (!Object.hasOwn(hostnameResponse, "hostname")) {
175170
throw new ControllerError("Missing expected hostname field");
176171
}
177172
return hostnameResponse.hostname;
@@ -182,12 +177,10 @@ export async function getUsers() {
182177
return fetch("/api/users")
183178
.then(processJsonResponse)
184179
.then((data) => {
185-
// eslint-disable-next-line no-prototype-builtins
186-
if (!data.hasOwnProperty("users")) {
180+
if (!Object.hasOwn(data, "users")) {
187181
throw new ControllerError("Missing expected users field");
188182
}
189-
// eslint-disable-next-line no-prototype-builtins
190-
if (!data.hasOwnProperty("currentUsername")) {
183+
if (!Object.hasOwn(data, "currentUsername")) {
191184
throw new ControllerError("Missing expected currentUsername field");
192185
}
193186
return { users: data.users, currentUsername: data.currentUsername };
@@ -222,8 +215,7 @@ export async function addUser(username, password, role) {
222215
})
223216
.then(processJsonResponse)
224217
.then((data) => {
225-
// eslint-disable-next-line no-prototype-builtins
226-
if (!data.hasOwnProperty("username")) {
218+
if (!Object.hasOwn(data, "username")) {
227219
throw new ControllerError("Missing expected username field");
228220
}
229221
return { username: data.username };
@@ -259,8 +251,7 @@ export async function deleteUser(username) {
259251
})
260252
.then(processJsonResponse)
261253
.then((data) => {
262-
// eslint-disable-next-line no-prototype-builtins
263-
if (!data.hasOwnProperty("username")) {
254+
if (!Object.hasOwn(data, "username")) {
264255
throw new ControllerError("Missing expected username field");
265256
}
266257
return { username: data.username };
@@ -304,8 +295,7 @@ export async function requiresHttps() {
304295
})
305296
.then(processJsonResponse)
306297
.then((data) => {
307-
// eslint-disable-next-line no-prototype-builtins
308-
if (!data.hasOwnProperty("requiresHttps")) {
298+
if (!Object.hasOwn(data, "requiresHttps")) {
309299
throw new ControllerError("Missing expected requiresHttps field");
310300
}
311301
return data.requiresHttps;
@@ -351,8 +341,7 @@ export async function getNetworkStatus() {
351341
})
352342
.then(processJsonResponse)
353343
.then((response) => {
354-
// eslint-disable-next-line no-prototype-builtins
355-
if (!response.hasOwnProperty("interfaces")) {
344+
if (!Object.hasOwn(response, "interfaces")) {
356345
throw new ControllerError("Missing expected interfaces field");
357346
}
358347
return response.interfaces;
@@ -369,8 +358,7 @@ export async function getWifiSettings() {
369358
.then(processJsonResponse)
370359
.then((response) => {
371360
["countryCode", "ssid"].forEach((field) => {
372-
// eslint-disable-next-line no-prototype-builtins
373-
if (!response.hasOwnProperty(field)) {
361+
if (!Object.hasOwn(response, field)) {
374362
throw new ControllerError(`Missing expected ${field} field`);
375363
}
376364
});
@@ -455,8 +443,7 @@ export async function textToShareableUrl(text) {
455443
})
456444
.then(processJsonResponse)
457445
.then((data) => {
458-
// eslint-disable-next-line no-prototype-builtins
459-
if (!data.hasOwnProperty("id")) {
446+
if (!Object.hasOwn(data, "id")) {
460447
throw new ControllerError("Missing expected id field");
461448
}
462449
return data;
@@ -486,8 +473,7 @@ export async function getVideoSettings() {
486473
"h264StunPort",
487474
"defaultH264StunPort",
488475
].forEach((field) => {
489-
// eslint-disable-next-line no-prototype-builtins
490-
if (!data.hasOwnProperty(field)) {
476+
if (!Object.hasOwn(data, field)) {
491477
throw new ControllerError(`Missing expected ${field} field`);
492478
}
493479
});

0 commit comments

Comments
 (0)