From deedc1be9bb5dca86dbfa8ad945436ead1ac2c9d Mon Sep 17 00:00:00 2001 From: Chau Tran Date: Sun, 31 Jan 2021 14:30:43 -0600 Subject: [PATCH] feat(pojos): strict mode friendly --- packages/pojos/.eslintrc.json | 3 ++- packages/pojos/src/lib/create-metadata-map.ts | 10 ++++---- packages/pojos/src/lib/pojos.ts | 4 ++-- .../src/lib/storages/pojos-mapping.storage.ts | 2 +- .../src/lib/storages/pojos-symbol.storage.ts | 2 +- .../pojos/src/lib/utils/instantiate.util.ts | 22 ++++++++++-------- .../lib/utils/specs/instantiate.util.spec.ts | 23 ++++++++----------- 7 files changed, 33 insertions(+), 33 deletions(-) diff --git a/packages/pojos/.eslintrc.json b/packages/pojos/.eslintrc.json index 46981aed6..6b7838065 100644 --- a/packages/pojos/.eslintrc.json +++ b/packages/pojos/.eslintrc.json @@ -3,6 +3,7 @@ "ignorePatterns": ["!**/*"], "rules": { "@typescript-eslint/no-non-null-assertion": 0, - "no-prototype-builtins": 0 + "no-prototype-builtins": 0, + "@typescript-eslint/no-explicit-any": 0 } } diff --git a/packages/pojos/src/lib/create-metadata-map.ts b/packages/pojos/src/lib/create-metadata-map.ts index 328375db7..44f2eb0d5 100644 --- a/packages/pojos/src/lib/create-metadata-map.ts +++ b/packages/pojos/src/lib/create-metadata-map.ts @@ -13,7 +13,7 @@ import { pojosSymbolStorage } from './storages'; * }) */ -export function createMetadataMap = unknown>( +export function createMetadataMap = any>( key: string, metadata: { [key in keyof TModel]?: @@ -24,7 +24,7 @@ export function createMetadataMap = unknown>( | string; } ): void; -export function createMetadataMap = unknown>( +export function createMetadataMap = any>( key: string, existMetadataMap: string, metadata?: { @@ -37,7 +37,7 @@ export function createMetadataMap = unknown>( | null; } ): void; -export function createMetadataMap = unknown>( +export function createMetadataMap = any>( key: string, metadataOrMetadataMap: | string @@ -81,7 +81,7 @@ export function createMetadataMap = unknown>( return; } - const result = []; + const result: unknown[] = []; for (const [existProp, existMeta] of toMergeMetadata) { if (entries.some(([entryProp]) => existProp === entryProp)) { continue; @@ -93,6 +93,6 @@ export function createMetadataMap = unknown>( result.push(...entries); pojosSymbolStorage.set( symbol, - result.filter(([, meta]) => meta !== null) + result.filter(([, meta]: any) => meta !== null) as [] ); } diff --git a/packages/pojos/src/lib/pojos.ts b/packages/pojos/src/lib/pojos.ts index c50c5a304..f53cce326 100644 --- a/packages/pojos/src/lib/pojos.ts +++ b/packages/pojos/src/lib/pojos.ts @@ -40,8 +40,8 @@ export const pojos: MapPluginInitializer = (errorHandler) => { return createInitialMapping( sourceObj, destinationObj, - sourceNestedMetadataMap, - destinationNestedMetadataMap, + sourceNestedMetadataMap as unknown[], + destinationNestedMetadataMap as unknown[], (mapping) => { mappingStorage.set(source, destination, mapping); }, diff --git a/packages/pojos/src/lib/storages/pojos-mapping.storage.ts b/packages/pojos/src/lib/storages/pojos-mapping.storage.ts index ebaa72fc2..139d07574 100644 --- a/packages/pojos/src/lib/storages/pojos-mapping.storage.ts +++ b/packages/pojos/src/lib/storages/pojos-mapping.storage.ts @@ -12,7 +12,7 @@ export class PojosMappingStorage implements MappingStorage { return this.storage.get(source)?.get(destination); } - has(source: string, destination: string): boolean { + has(source: string, destination: string): boolean | undefined { return this.storage.get(source)?.has(destination); } diff --git a/packages/pojos/src/lib/storages/pojos-symbol.storage.ts b/packages/pojos/src/lib/storages/pojos-symbol.storage.ts index 6db01b838..585c7b903 100644 --- a/packages/pojos/src/lib/storages/pojos-symbol.storage.ts +++ b/packages/pojos/src/lib/storages/pojos-symbol.storage.ts @@ -35,7 +35,7 @@ class PojosSymbolStorage { propertyKey: string, metadata: ReturnType[MetadataClassId.metadataFn]> ][] { - return this.storage.get(key); + return this.storage.get(key) || []; } has(key: symbol): boolean { diff --git a/packages/pojos/src/lib/utils/instantiate.util.ts b/packages/pojos/src/lib/utils/instantiate.util.ts index 6e74e7750..e3ec5c8ad 100644 --- a/packages/pojos/src/lib/utils/instantiate.util.ts +++ b/packages/pojos/src/lib/utils/instantiate.util.ts @@ -16,19 +16,23 @@ export function instantiate>( return [obj]; } - const nestedMetadataMap = []; + const nestedMetadataMap: unknown[] = []; let i = metadata.length; while (i--) { const [key, meta] = metadata[i]; - const valueAtKey = obj[key]; + const valueAtKey = (obj as Record)[key]; const metaResult = meta(); if (isPrimitiveConstructor(metaResult)) { - obj[key] = isDefined(valueAtKey) ? valueAtKey : undefined; + (obj as Record)[key] = isDefined(valueAtKey) + ? valueAtKey + : undefined; continue; } if (isDateConstructor(metaResult)) { - obj[key] = isDefined(valueAtKey) ? new Date(valueAtKey) : new Date(); + (obj as Record)[key] = isDefined(valueAtKey) + ? new Date(valueAtKey as number) + : new Date(); continue; } @@ -38,7 +42,7 @@ export function instantiate>( nestedMetadataMap.push([key, metaResult]); if (Array.isArray(valueAtKey)) { - obj[key] = valueAtKey.map((val) => { + (obj as Record)[key] = valueAtKey.map((val) => { const [childObj] = instantiate(metadataStorage, metaResult, val); return childObj; }); @@ -49,19 +53,19 @@ export function instantiate>( const [instantiateResult] = instantiate( metadataStorage, metaResult, - valueAtKey + valueAtKey as Dictionary ); - obj[key] = instantiateResult; + (obj as Record)[key] = instantiateResult; continue; } if (isDefined(defaultValue)) { - obj[key] = valueAtKey; + (obj as Record)[key] = valueAtKey; continue; } const [result] = instantiate(metadataStorage, metaResult); - obj[key] = result; + (obj as Record)[key] = result; } return [obj, nestedMetadataMap]; diff --git a/packages/pojos/src/lib/utils/specs/instantiate.util.spec.ts b/packages/pojos/src/lib/utils/specs/instantiate.util.spec.ts index fa5814362..0d4ad8cea 100644 --- a/packages/pojos/src/lib/utils/specs/instantiate.util.spec.ts +++ b/packages/pojos/src/lib/utils/specs/instantiate.util.spec.ts @@ -1,16 +1,13 @@ -// eslint-disable-next-line @nrwl/nx/enforce-module-boundaries -import { - createSpyObject, - resetAllWhenMocks, - when, -} from '@automapper/test-util'; +import { createSpyObj } from 'jest-createspyobj'; +import { resetAllWhenMocks, when } from 'jest-when'; import { PojosMetadataStorage } from '../../storages'; import { instantiate } from '../instantiate.util'; describe('instantiate', () => { - const mockedMetadataStorage = createSpyObject(PojosMetadataStorage, { - getMetadata: jest.fn(), - }); + const mockedMetadataStorage = createSpyObj( + PojosMetadataStorage, + ['getMetadata'] + ); interface Bar { bar: string; @@ -98,17 +95,15 @@ describe('instantiate', () => { when(mockedMetadataStorage.getMetadata) .calledWith('Foo') .mockReturnValueOnce([ - // eslint-disable-next-line @typescript-eslint/no-explicit-any - ['foo', () => String as any], + ['foo', () => (String as unknown) as string], ['bar', () => 'Bar'], ]); when(mockedMetadataStorage.getMetadata) .calledWith('Bar') .mockReturnValueOnce([ - // eslint-disable-next-line @typescript-eslint/no-explicit-any - ['bar', () => String as any], - ['date', () => Date], + ['bar', () => (String as unknown) as string], + ['date', () => (Date as unknown) as Date], ]); } });