Skip to content

Commit f89c7dd

Browse files
Merge commit from fork
Co-authored-by: Rich Harris <rich.harris@vercel.com>
1 parent b8f2b86 commit f89c7dd

5 files changed

Lines changed: 17 additions & 11 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: replace usage of `for in` with `for of Object.keys`

packages/svelte/src/internal/server/index.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ export function attributes(attrs, css_hash, classes, styles, flags = 0) {
138138
const lowercase = (flags & ELEMENT_PRESERVE_ATTRIBUTE_CASE) === 0;
139139
const is_input = (flags & ELEMENT_IS_INPUT) !== 0;
140140

141-
for (name in attrs) {
141+
for (name of Object.keys(attrs)) {
142142
// omit functions, internal svelte properties and invalid attribute names
143143
if (typeof attrs[name] === 'function') continue;
144144
if (name[0] === '$' && name[1] === '$') continue; // faster than name.startsWith('$$')
@@ -174,7 +174,8 @@ export function spread_props(props) {
174174

175175
for (let i = 0; i < props.length; i++) {
176176
const obj = props[i];
177-
for (key in obj) {
177+
if (obj == null) continue;
178+
for (key of Object.keys(obj)) {
178179
const desc = Object.getOwnPropertyDescriptor(obj, key);
179180
if (desc) {
180181
Object.defineProperty(merged_props, key, desc);
@@ -302,7 +303,7 @@ export function update_store_pre(store_values, store_name, store, d = 1) {
302303

303304
/** @param {Record<string, [any, any, any]>} store_values */
304305
export function unsubscribe_stores(store_values) {
305-
for (const store_name in store_values) {
306+
for (const store_name of Object.keys(store_values)) {
306307
store_values[store_name][1]();
307308
}
308309
}
@@ -338,7 +339,7 @@ export function rest_props(props, rest) {
338339
/** @type {Record<string, unknown>} */
339340
const rest_props = {};
340341
let key;
341-
for (key in props) {
342+
for (key of Object.keys(props)) {
342343
if (!rest.includes(key)) {
343344
rest_props[key] = props[key];
344345
}
@@ -363,7 +364,7 @@ export function sanitize_slots(props) {
363364
/** @type {Record<string, boolean>} */
364365
const sanitized = {};
365366
if (props.children) sanitized.default = true;
366-
for (const key in props.$$slots) {
367+
for (const key of Object.keys(props.$$slots || {})) {
367368
sanitized[key] = true;
368369
}
369370
return sanitized;
@@ -376,7 +377,7 @@ export function sanitize_slots(props) {
376377
* @param {Record<string, unknown>} props_now
377378
*/
378379
export function bind_props(props_parent, props_now) {
379-
for (const key in props_now) {
380+
for (const key of Object.keys(props_now)) {
380381
const initial_value = props_parent[key];
381382
const value = props_now[key];
382383
if (

packages/svelte/src/internal/server/renderer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ export class Renderer {
267267
* @param {{ head?: string, body: any }} content
268268
*/
269269
const close = (renderer, value, { head, body }) => {
270-
if ('value' in attrs) {
270+
if (Object.hasOwn(attrs, 'value')) {
271271
value = attrs.value;
272272
}
273273

packages/svelte/src/internal/shared/attributes.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export function attr(name, value, is_boolean = false) {
2727
is_boolean = true;
2828
}
2929
if (value == null || (!value && is_boolean)) return '';
30-
const normalized = (name in replacements && replacements[name].get(value)) || value;
30+
const normalized = (Object.hasOwn(replacements, name) && replacements[name].get(value)) || value;
3131
const assignment = is_boolean ? `=""` : `="${escape_html(normalized, true)}"`;
3232
return ` ${name}${assignment}`;
3333
}
@@ -61,7 +61,7 @@ export function to_class(value, hash, directives) {
6161
}
6262

6363
if (directives) {
64-
for (var key in directives) {
64+
for (var key of Object.keys(directives)) {
6565
if (directives[key]) {
6666
classname = classname ? classname + ' ' + key : key;
6767
} else if (classname.length) {
@@ -96,7 +96,7 @@ function append_styles(styles, important = false) {
9696
var separator = important ? ' !important;' : ';';
9797
var css = '';
9898

99-
for (var key in styles) {
99+
for (var key of Object.keys(styles)) {
100100
var value = styles[key];
101101
if (value != null && value !== '') {
102102
css += ' ' + key + ': ' + value + separator;

packages/svelte/src/internal/shared/clone.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ function clone(value, cloned, path, paths, original = null, no_tojson = false) {
8989
cloned.set(original, copy);
9090
}
9191

92-
for (var key in value) {
92+
for (var key of Object.keys(value)) {
9393
copy[key] = clone(
9494
// @ts-expect-error
9595
value[key],

0 commit comments

Comments
 (0)