From 5bfc5dcf945148e97fc2de36dcf99baded2b98c3 Mon Sep 17 00:00:00 2001 From: Arman Ozak Date: Thu, 20 Aug 2020 12:57:41 +0300 Subject: [PATCH] fix: add imports for referred types in model schematics --- .../packages/schematics/src/utils/model.ts | 5 +++-- .../packages/schematics/src/utils/type.ts | 15 ++++++++++----- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/npm/ng-packs/packages/schematics/src/utils/model.ts b/npm/ng-packs/packages/schematics/src/utils/model.ts index d02ec1db04..ec454aad6f 100644 --- a/npm/ng-packs/packages/schematics/src/utils/model.ts +++ b/npm/ng-packs/packages/schematics/src/utils/model.ts @@ -8,7 +8,7 @@ import { createTypeSimplifier, createTypesToImportsReducer, flattenUnionTypes, - removeTypeModifiers, + normalizeTypeAnnotations, } from './type'; export function createImportRefsToModelMapper(solution: string, types: Record) { @@ -37,6 +37,7 @@ export function createImportRefsToModelMapper(solution: string, types: Record removeTypeModifiers(removeGenerics(type)); + const clearTypes = (type: string) => normalizeTypeAnnotations(removeGenerics(type)); const baseTypes = baseType ? [baseType] : []; const propTypes = (properties ?? []).map(({ type }) => type); diff --git a/npm/ng-packs/packages/schematics/src/utils/type.ts b/npm/ng-packs/packages/schematics/src/utils/type.ts index 74e116c746..6157275fc2 100644 --- a/npm/ng-packs/packages/schematics/src/utils/type.ts +++ b/npm/ng-packs/packages/schematics/src/utils/type.ts @@ -24,7 +24,7 @@ export function createTypeParser(replacerFn = (t: string) => t) { return (originalType: string) => flattenUnionTypes([], originalType) .map(type => { - type = removeTypeModifiers(type); + type = removeTypeModifiers(normalizeTypeAnnotations(type)); type = type.replace( /System\.([0-9A-Za-z]+)/g, (_, match) => SYSTEM_TYPES.get(match) ?? strings.camelize(match), @@ -45,11 +45,15 @@ export function flattenUnionTypes(types: string[], type: string) { return types; } -export function removeTypeModifiers(type: string) { - type = type.startsWith('[') ? type.slice(1, -1) + '[]' : type; +export function normalizeTypeAnnotations(type: string) { + type = type.replace(/\[(.+)+\]/g, '$1[]'); return type.replace(/\?/g, ''); } +export function removeTypeModifiers(type: string) { + return type.replace(/\[\]/g, ''); +} + export function createTypesToImportsReducer(solution: string, namespace: string) { const mapTypeToImport = createTypeToImportMapper(solution, namespace); @@ -75,13 +79,14 @@ export function createTypesToImportsReducer(solution: string, namespace: string) export function createTypeToImportMapper(solution: string, namespace: string) { const adaptType = createTypeAdapter(solution); + const simplifyType = createTypeSimplifier(solution); return (type: string, isEnum: boolean) => { if (!type || type.startsWith('System')) return; const modelNamespace = parseNamespace(solution, type); - const refs = [type]; - const specifiers = [adaptType(type.split('<')[0])]; + const refs = [removeTypeModifiers(type)]; + const specifiers = [adaptType(simplifyType(type).split('<')[0])]; const path = /^Volo\.Abp\.(Application\.Dtos|ObjectExtending)/.test(type) ? '@abp/ng.core' : isEnum