Skip to content

Commit 42f3a6d

Browse files
committed
chore: use guarded for-in
1 parent 9e44329 commit 42f3a6d

29 files changed

Lines changed: 67 additions & 51 deletions

File tree

.changeset/nasty-walls-tap.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"@smithy/undici-http-handler": patch
3+
"@smithy/node-http-handler": patch
4+
"@smithy/server-common": patch
5+
"@smithy/core": patch
6+
---
7+
8+
switch for-in loops to guarded

.oxlintrc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"rules": {
1111
"@typescript-eslint/consistent-type-imports": "error",
1212
"@typescript-eslint/no-import-type-side-effects": "error",
13+
"guard-for-in": "error",
1314
"no-sparse-arrays": "off",
1415
"no-unused-vars": ["error", { "args": "none", "caughtErrorsIgnorePattern": "^ignored", "varsIgnorePattern": "^_" }],
1516
"unicorn/no-new-array": "off",

packages/core/src/legacy-root-exports/util-identity-and-auth/DefaultIdentityProviderConfig.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export class DefaultIdentityProviderConfig implements IdentityProviderConfig {
1313
* @param config scheme IDs and identity providers to configure
1414
*/
1515
constructor(config: Record<HttpAuthSchemeId, IdentityProvider<Identity> | undefined>) {
16-
for (const key in config) {
16+
for (const key of Object.keys(config)) {
1717
const value = config[key];
1818
if (value !== undefined) {
1919
this.authSchemes.set(key, value);

packages/core/src/submodules/cbor/codec-v1/CborShapeDeserializer.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export class CborShapeDeserializer extends SerdeContext implements ShapeDeserial
7979
if (ns.isMapSchema()) {
8080
const targetSchema = ns.getValueSchema();
8181

82-
for (const key in value) {
82+
for (const key of Object.keys(value)) {
8383
const itemValue = this.readValue(targetSchema, value[key]);
8484
newObject[key] = itemValue;
8585
}
@@ -88,7 +88,7 @@ export class CborShapeDeserializer extends SerdeContext implements ShapeDeserial
8888
let keys: Set<string> | undefined;
8989
if (isUnion) {
9090
keys = new Set<string>();
91-
for (const k in value) {
91+
for (const k of Object.keys(value)) {
9292
if (k !== "__type") {
9393
keys.add(k);
9494
}
@@ -104,7 +104,7 @@ export class CborShapeDeserializer extends SerdeContext implements ShapeDeserial
104104
}
105105
if (isUnion && keys?.size === 1) {
106106
let newObjectEmpty = true;
107-
for (const _ in newObject) {
107+
for (const _ of Object.keys(newObject)) {
108108
newObjectEmpty = false;
109109
break;
110110
}
@@ -115,7 +115,7 @@ export class CborShapeDeserializer extends SerdeContext implements ShapeDeserial
115115
} else if (typeof value.__type === "string") {
116116
// This if-block is for backwards compatibility support and should not be copied
117117
// to other implementations.
118-
for (const k in value) {
118+
for (const k of Object.keys(value)) {
119119
if (!(k in newObject)) {
120120
// we have no type information, so copy as-is from CBOR-derived object.
121121
newObject[k] = value[k];

packages/core/src/submodules/cbor/codec-v1/CborShapeSerializer.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export class CborShapeSerializer extends SerdeContext implements ShapeSerializer
6565
const newObject = {} as any;
6666
if (ns.isMapSchema()) {
6767
const sparse = !!ns.getMergedTraits().sparse;
68-
for (const key in sourceObject) {
68+
for (const key of Object.keys(sourceObject)) {
6969
const value = this.serialize(ns.getValueSchema(), sourceObject[key]);
7070
if (value != null || sparse) {
7171
newObject[key] = value;
@@ -85,7 +85,7 @@ export class CborShapeSerializer extends SerdeContext implements ShapeSerializer
8585
} else if (typeof sourceObject.__type === "string") {
8686
// This if-block is for backwards compatibility support and should not be copied
8787
// to other implementations.
88-
for (const k in sourceObject) {
88+
for (const k of Object.keys(sourceObject)) {
8989
if (!(k in newObject)) {
9090
// we have no type information, so serialize with Document rules.
9191
newObject[k] = this.serialize(15 satisfies DocumentSchema, sourceObject[k]);
@@ -101,7 +101,7 @@ export class CborShapeSerializer extends SerdeContext implements ShapeSerializer
101101
}
102102
return newArray;
103103
}
104-
for (const key in sourceObject) {
104+
for (const key of Object.keys(sourceObject)) {
105105
newObject[key] = this.serialize(ns.getValueSchema(), sourceObject[key]);
106106
}
107107
} else if (ns.isBigDecimalSchema()) {

packages/core/src/submodules/cbor/codec-v2/CborShapeDeserializer2.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ function readStruct(ns: NormalizedSchema, count: number, startPos: number): any
172172

173173
if (isUnion) {
174174
let resultEmpty = true;
175-
for (const _ in result) {
175+
for (const _ of Object.keys(result)) {
176176
resultEmpty = false;
177177
break;
178178
}
@@ -347,7 +347,7 @@ function readMapIndefinite(ns: NormalizedSchema): any {
347347
pos += 1;
348348
if (isUnion) {
349349
let resultEmpty = true;
350-
for (const _ in result) {
350+
for (const _ of Object.keys(result)) {
351351
resultEmpty = false;
352352
break;
353353
}
@@ -693,15 +693,15 @@ function transformObject(ns: NormalizedSchema, value: any): any {
693693

694694
if (ns.isMapSchema()) {
695695
const targetSchema = ns.getValueSchema();
696-
for (const key in value) {
696+
for (const key of Object.keys(value)) {
697697
newObject[key] = transformObject(targetSchema, value[key]);
698698
}
699699
} else if (ns.isStructSchema()) {
700700
const isUnion = ns.isUnionSchema();
701701
let keys: Set<string> | undefined;
702702
if (isUnion) {
703703
keys = new Set<string>();
704-
for (const k in value) {
704+
for (const k of Object.keys(value)) {
705705
if (k !== "__type") {
706706
keys.add(k);
707707
}
@@ -717,7 +717,7 @@ function transformObject(ns: NormalizedSchema, value: any): any {
717717
}
718718
if (isUnion && keys?.size === 1) {
719719
let newObjectEmpty = true;
720-
for (const _ in newObject) {
720+
for (const _ of Object.keys(newObject)) {
721721
newObjectEmpty = false;
722722
break;
723723
}
@@ -726,7 +726,7 @@ function transformObject(ns: NormalizedSchema, value: any): any {
726726
newObject.$unknown = [k, value[k]];
727727
}
728728
} else if (typeof value.__type === "string") {
729-
for (const k in value) {
729+
for (const k of Object.keys(value)) {
730730
if (!(k in newObject)) {
731731
newObject[k] = value[k];
732732
}

packages/core/src/submodules/cbor/codec-v2/CborShapeSerializer2.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ function writeStruct(ns: NormalizedSchema, value: Record<string, unknown>, serde
352352
}
353353

354354
if (typeof value.__type === "string") {
355-
for (const k in value) {
355+
for (const k of Object.keys(value)) {
356356
if (!memberNames.includes(k)) {
357357
writeString(k);
358358
writeUntypedValue(value[k]);
@@ -418,7 +418,7 @@ function writeMap(ns: NormalizedSchema, value: Record<string, unknown>, isDocume
418418
const valueSchema = ns.getValueSchema();
419419

420420
const keys: string[] = [];
421-
for (const k in value) {
421+
for (const k of Object.keys(value)) {
422422
const v = value[k];
423423
if (isDocument ? v !== undefined : v != null || sparse) {
424424
keys.push(k);

packages/core/src/submodules/cbor/parseCborBody.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export const loadSmithyRpcV2CborErrorCode = (output: HttpResponse, data: any): s
7474
}
7575

7676
let codeKey: string | undefined;
77-
for (const key in data) {
77+
for (const key of Object.keys(data)) {
7878
if (key.toLowerCase() === "code") {
7979
codeKey = key;
8080
break;
@@ -121,7 +121,7 @@ export const buildHttpRpcRequest = async (
121121
contents.hostname = resolvedHostname;
122122
}
123123
if (endpoint.headers) {
124-
for (const name in endpoint.headers) {
124+
for (const name of Object.keys(endpoint.headers)) {
125125
contents.headers[name] = endpoint.headers[name];
126126
}
127127
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* @internal
3+
*/
4+
export function hasOwn(o: object, k: string) {
5+
return Object.prototype.hasOwnProperty.call(o, k);
6+
}

packages/core/src/submodules/client/smithy-client/extensions/checksum.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export type PartialChecksumRuntimeConfigType = {
3232
export const getChecksumConfiguration = (runtimeConfig: PartialChecksumRuntimeConfigType) => {
3333
const checksumAlgorithms: ChecksumAlgorithm[] = [];
3434

35-
for (const id in AlgorithmId) {
35+
for (const id of Object.keys(AlgorithmId)) {
3636
const algorithmId = AlgorithmId[id as keyof typeof AlgorithmId];
3737
if (runtimeConfig[algorithmId] === undefined) {
3838
continue;

0 commit comments

Comments
 (0)