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, createTypeSimplifier,
createTypesToImportsReducer, createTypesToImportsReducer,
flattenUnionTypes, flattenUnionTypes,
removeTypeModifiers, normalizeTypeAnnotations,
} from './type'; } from './type';
export function createImportRefsToModelMapper(solution: string, types: Record<string, 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); const interfaceIndirect = mapImportRefToInterface(ref);
if (interfaceIndirect) model.interfaces.push(interfaceIndirect); if (interfaceIndirect) model.interfaces.push(interfaceIndirect);
reduceImportRefToImport(imports, ref);
}); });
model.imports.push(_import); model.imports.push(_import);
@ -102,7 +103,7 @@ export function createImportRefToImportReducerCreator(
export function mergeBaseTypeWithProperties({ baseType, genericArguments, properties }: Type) { export function mergeBaseTypeWithProperties({ baseType, genericArguments, properties }: Type) {
const removeGenerics = createGenericRemover(genericArguments); const removeGenerics = createGenericRemover(genericArguments);
const clearTypes = (type: string) => removeTypeModifiers(removeGenerics(type)); const clearTypes = (type: string) => normalizeTypeAnnotations(removeGenerics(type));
const baseTypes = baseType ? [baseType] : []; const baseTypes = baseType ? [baseType] : [];
const propTypes = (properties ?? []).map(({ type }) => type); 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) => return (originalType: string) =>
flattenUnionTypes([], originalType) flattenUnionTypes([], originalType)
.map(type => { .map(type => {
type = removeTypeModifiers(type); type = removeTypeModifiers(normalizeTypeAnnotations(type));
type = type.replace( type = type.replace(
/System\.([0-9A-Za-z]+)/g, /System\.([0-9A-Za-z]+)/g,
(_, match) => SYSTEM_TYPES.get(match) ?? strings.camelize(match), (_, match) => SYSTEM_TYPES.get(match) ?? strings.camelize(match),
@ -45,11 +45,15 @@ export function flattenUnionTypes(types: string[], type: string) {
return types; return types;
} }
export function removeTypeModifiers(type: string) { export function normalizeTypeAnnotations(type: string) {
type = type.startsWith('[') ? type.slice(1, -1) + '[]' : type; type = type.replace(/\[(.+)+\]/g, '$1[]');
return type.replace(/\?/g, ''); return type.replace(/\?/g, '');
} }
export function removeTypeModifiers(type: string) {
return type.replace(/\[\]/g, '');
}
export function createTypesToImportsReducer(solution: string, namespace: string) { export function createTypesToImportsReducer(solution: string, namespace: string) {
const mapTypeToImport = createTypeToImportMapper(solution, namespace); const mapTypeToImport = createTypeToImportMapper(solution, namespace);
@ -75,13 +79,14 @@ export function createTypesToImportsReducer(solution: string, namespace: string)
export function createTypeToImportMapper(solution: string, namespace: string) { export function createTypeToImportMapper(solution: string, namespace: string) {
const adaptType = createTypeAdapter(solution); const adaptType = createTypeAdapter(solution);
const simplifyType = createTypeSimplifier(solution);
return (type: string, isEnum: boolean) => { return (type: string, isEnum: boolean) => {
if (!type || type.startsWith('System')) return; if (!type || type.startsWith('System')) return;
const modelNamespace = parseNamespace(solution, type); const modelNamespace = parseNamespace(solution, type);
const refs = [type]; const refs = [removeTypeModifiers(type)];
const specifiers = [adaptType(type.split('<')[0])]; const specifiers = [adaptType(simplifyType(type).split('<')[0])];
const path = /^Volo\.Abp\.(Application\.Dtos|ObjectExtending)/.test(type) const path = /^Volo\.Abp\.(Application\.Dtos|ObjectExtending)/.test(type)
? '@abp/ng.core' ? '@abp/ng.core'
: isEnum : isEnum

Loading…
Cancel
Save