Browse Source

refactor: move schematics type simplifier to separate file

pull/5137/head
Arman Ozak 6 years ago
parent
commit
dc60ff57ff
  1. 1
      npm/ng-packs/packages/schematics/src/utils/index.ts
  2. 19
      npm/ng-packs/packages/schematics/src/utils/service.ts
  3. 21
      npm/ng-packs/packages/schematics/src/utils/type.ts

1
npm/ng-packs/packages/schematics/src/utils/index.ts

@ -9,4 +9,5 @@ export * from './service';
export * from './source';
export * from './text';
export * from './tree';
export * from './type';
export * from './workspace';

19
npm/ng-packs/packages/schematics/src/utils/service.ts

@ -3,6 +3,7 @@ import { Action, Body, Controller, Import, Method, Property, Service, Signature
import { parseNamespace } from './namespace';
import { relativePathFromServiceToModel } from './path';
import { parseGenerics } from './tree';
import { createTypeSimplifier } from './type';
export function serializeParameters(parameters: Property[]) {
return parameters.map(p => p.name + p.optional + ': ' + p.type + p.default, '').join(', ');
@ -128,26 +129,12 @@ function getMethodNameFromAction(action: Action): string {
}
function createTypeAdapter(solution: string) {
const removeNamespace = createNamespaceRemover(solution);
const simplifyType = createTypeSimplifier(solution);
return (typeSimple: string) => {
if (typeSimple === 'System.Void') return 'void';
return parseGenerics(typeSimple, node => removeNamespace(node.data)).toString();
};
}
function createNamespaceRemover(solution: string) {
const optionalRegex = /\?/g;
const solutionRegex = new RegExp(solution.replace(/\./g, `\.`) + `\.`);
const voloRegex = /^Volo\.(Abp\.?)(Application\.?)/;
return (type: string) => {
type = type.replace(voloRegex, '');
type = type.replace(solutionRegex, '');
type = type.replace(optionalRegex, '');
type = type.split('.').pop()!;
return type;
return parseGenerics(typeSimple, node => simplifyType(node.data)).toString();
};
}

21
npm/ng-packs/packages/schematics/src/utils/type.ts

@ -0,0 +1,21 @@
import { strings } from '@angular-devkit/core';
import { SYSTEM_TYPES } from '../constants';
export function createTypeSimplifier(solution: string) {
const optionalRegex = /\?/g;
const solutionRegex = new RegExp(solution.replace(/\./g, `\.`) + `\.`);
const voloRegex = /^Volo\.(Abp\.?)(Application\.?)/;
return (type: string) => {
type = type.replace(voloRegex, '');
type = type.replace(solutionRegex, '');
type = type.replace(optionalRegex, '');
type = type.replace(
/System\.([0-9A-Za-z]+)/g,
(_, match) => SYSTEM_TYPES.get(match) ?? strings.camelize(match),
);
type = type.split('.').pop()!;
type = type.startsWith('[') ? type.slice(1, -1) + '[]' : type;
return type;
};
}
Loading…
Cancel
Save