From 40094d13f267f08fc11f616422f6ca4dc650ff57 Mon Sep 17 00:00:00 2001 From: Arman Ozak Date: Tue, 8 Dec 2020 17:30:02 +0300 Subject: [PATCH] fix: improve generic parse in type simplifier --- .../packages/schematics/src/utils/type.ts | 20 ++++++------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/npm/ng-packs/packages/schematics/src/utils/type.ts b/npm/ng-packs/packages/schematics/src/utils/type.ts index 781794c463..1c7eda81bb 100644 --- a/npm/ng-packs/packages/schematics/src/utils/type.ts +++ b/npm/ng-packs/packages/schematics/src/utils/type.ts @@ -1,7 +1,7 @@ -import { SYSTEM_TYPES } from '../constants'; -import { VOLO_REGEX } from '../constants/volo'; +import { SYSTEM_TYPES, VOLO_REGEX } from '../constants'; import { eImportKeyword } from '../enums'; import { Import, TypeWithEnum } from '../models'; +import { extractSimpleGenerics } from './generics'; import { parseNamespace } from './namespace'; import { relativePathToEnum, relativePathToModel } from './path'; import { parseGenerics } from './tree'; @@ -15,14 +15,9 @@ export function createTypeSimplifier() { type = /any.+)<.*(?<=[\.<])(?.+)>/gm); - const { generic, genericType } = regexp.exec(type)?.groups ?? {}; + const { identifier, generics } = extractSimpleGenerics(type); - return generic - ? generic === 'any' - ? 'any' - : `${generic}<${genericType}>` - : type.split('.').pop()!; + return generics.length ? `${identifier}<${generics.join(', ')}>` : identifier; }); return (type: string) => { @@ -40,9 +35,7 @@ export function createTypeParser(replacerFn = (t: string) => t) { export function createTypeNormalizer(replacerFn = (t: string) => t) { return (type: string) => { - type = normalizeTypeAnnotations(type); - - return replacerFn(type); + return replacerFn(normalizeTypeAnnotations(type)); }; } @@ -56,8 +49,7 @@ export function flattenDictionaryTypes(types: string[], type: string) { } export function normalizeTypeAnnotations(type: string) { - type = type.replace(/\[(.+)+\]/g, '$1[]'); - return type.replace(/\?/g, ''); + return type.replace(/\[(.+)+\]/g, '$1[]').replace(/\?/g, ''); } export function removeGenerics(type: string) {