Skip to content

Commit d223d79

Browse files
committed
fix(core): clean up optional chaining
1 parent 65304a7 commit d223d79

5 files changed

Lines changed: 34 additions & 12 deletions

File tree

packages/core/src/lib/create-mapper/create-mapper.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ export function createMapper<TKey = unknown>({
6060
const { preMap } = plugin;
6161

6262
// run preMap if available
63-
const [sourceInstance] = preMap?.(source, destination, sourceObj) ?? [];
63+
const [sourceInstance] = preMap
64+
? preMap(source, destination, sourceObj)
65+
: [];
6466

6567
// get mapping between Source and Destination
6668
const mapping = this.getMapping(source, destination);
@@ -152,7 +154,9 @@ export function createMapper<TKey = unknown>({
152154
);
153155
},
154156
dispose() {
155-
plugin.dispose?.();
157+
if (plugin.dispose) {
158+
plugin.dispose();
159+
}
156160
},
157161
};
158162
}

packages/core/src/lib/initialize-utils/create-initial-mapping.util.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ export function createInitialMapping(
6666
undefined,
6767
];
6868

69-
prePropertiesLoop?.(mapping);
69+
if (prePropertiesLoop) {
70+
prePropertiesLoop(mapping);
71+
}
7072

7173
const destinationPaths = getPathRecursive(destinationObj) || [];
7274
const namingConventions = mapping[MappingClassId.namingConventions];

packages/core/src/lib/initialize-utils/extend-mappings.util.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ export function extendMappings(bases: any[], mapping: Mapping) {
1919
]);
2020
}
2121
}
22-
mapping[MappingClassId.bases] ??= [];
22+
23+
if (mapping[MappingClassId.bases] == null) {
24+
mapping[MappingClassId.bases] = [];
25+
}
2326
mapping[MappingClassId.bases]!.push(
2427
mappingToExtend[MappingClassId.mappings]
2528
);

packages/core/src/lib/map/map.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,9 @@ function map<
216216
// Do not run before map when in Map Array mode
217217
if (!isMapArray) {
218218
const beforeMap = mapBeforeAction ?? mappingBeforeAction;
219-
beforeMap?.(sourceObj, destination);
219+
if (beforeMap) {
220+
beforeMap(sourceObj, destination);
221+
}
220222
}
221223

222224
// map
@@ -358,7 +360,9 @@ Original error: ${originalError}`;
358360
// Do not map for when in Map Array mode
359361
if (!isMapArray) {
360362
const afterMap = mapAfterAction ?? mappingAfterAction;
361-
afterMap?.(sourceObj, destination);
363+
if (afterMap) {
364+
afterMap(sourceObj, destination);
365+
}
362366
}
363367

364368
// Check unmapped properties
@@ -394,15 +398,19 @@ export function mapArray<
394398
const { beforeMap, afterMap, extraArguments } = options ?? {};
395399

396400
// run beforeMap for the whole map operation
397-
beforeMap?.(sourceArray, []);
401+
if (beforeMap) {
402+
beforeMap(sourceArray, destinationArray);
403+
}
398404

399405
// loop through each item and run map() for each
400406
for (let i = 0, len = sourceArray.length; i < len; i++) {
401-
const mapping = mapper.getMapping(source, destination);
402407
destinationArray.push(
403408
mapReturn(
404409
sourceArray[i],
405-
mapping as Mapping<TSource, TDestination>,
410+
mapper.getMapping(source, destination) as Mapping<
411+
TSource,
412+
TDestination
413+
>,
406414
{ extraArguments },
407415
mapper,
408416
errorHandler,
@@ -412,7 +420,9 @@ export function mapArray<
412420
}
413421

414422
// run afterMap for the whole map operation
415-
afterMap?.(sourceArray, destinationArray);
423+
if (afterMap) {
424+
afterMap(sourceArray, destinationArray);
425+
}
416426

417427
return destinationArray;
418428
}

packages/core/src/lib/member-map-functions/convert-using.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@ export function convertUsing<
1919
return [
2020
TransformationType.ConvertUsing,
2121
(source) => {
22-
const valueToConvert =
23-
value?.(source) ?? ((source as unknown) as TConvertSource);
22+
let valueToConvert = (source as unknown) as TConvertSource;
23+
24+
if (value) {
25+
valueToConvert = value(source);
26+
}
2427
return (converter.convert(valueToConvert) as unknown) as TConvertSource;
2528
},
2629
];

0 commit comments

Comments
 (0)