Browse Source

fix: improve generic parse in type simplifier

pull/6537/head
Arman Ozak 6 years ago
parent
commit
40094d13f2
  1. 20
      npm/ng-packs/packages/schematics/src/utils/type.ts

20
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</.test(type) ? 'any' : type;
const regexp = new RegExp(/.*(?<=\.)(?<generic>.+)<.*(?<=[\.<])(?<genericType>.+)>/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) {

Loading…
Cancel
Save