Browse Source

fix: add imports for referred types in model schematics

pull/5137/head
Arman Ozak 6 years ago
parent
commit
5bfc5dcf94
  1. 5
      npm/ng-packs/packages/schematics/src/utils/model.ts
  2. 15
      npm/ng-packs/packages/schematics/src/utils/type.ts

5
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<string, Type>) {
@ -37,6 +37,7 @@ export function createImportRefsToModelMapper(solution: string, types: Record<st
const interfaceIndirect = mapImportRefToInterface(ref);
if (interfaceIndirect) model.interfaces.push(interfaceIndirect);
reduceImportRefToImport(imports, ref);
});
model.imports.push(_import);
@ -102,7 +103,7 @@ export function createImportRefToImportReducerCreator(
export function mergeBaseTypeWithProperties({ baseType, genericArguments, properties }: Type) {
const removeGenerics = createGenericRemover(genericArguments);
const clearTypes = (type: string) => removeTypeModifiers(removeGenerics(type));
const clearTypes = (type: string) => normalizeTypeAnnotations(removeGenerics(type));
const baseTypes = baseType ? [baseType] : [];
const propTypes = (properties ?? []).map(({ type }) => type);

15
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

Loading…
Cancel
Save