Skip to content

Commit

Permalink
Merge pull request #26269 from Tcharl/typesafe
Browse files Browse the repository at this point in the history
More type checking
  • Loading branch information
mshima authored May 26, 2024
2 parents 69365db + 1341a70 commit 949ded3
Show file tree
Hide file tree
Showing 38 changed files with 282 additions and 149 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default {
* @param jdlEntities - the JDL entities to convert.
* @return a map having for keys entity names and for values the corresponding JSON entities.
*/
export function convert(jdlEntities: JDLEntity[] | null): Map<string, JSONEntity> {
export function convert(jdlEntities: JDLEntity[]): Map<string, JSONEntity> {
if (!jdlEntities) {
throw new Error('JDL entities must be passed to get the basic entity information.');
}
Expand All @@ -41,7 +41,7 @@ export function convert(jdlEntities: JDLEntity[] | null): Map<string, JSONEntity
function createJSONEntities(jdlEntities: JDLEntity[]): Map<string, JSONEntity> {
const convertedEntities = new Map<string, JSONEntity>();

jdlEntities.forEach(jdlEntity => {
jdlEntities.forEach((jdlEntity: JDLEntity) => {
const entityName = jdlEntity.name;
convertedEntities.set(
entityName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('jdl - JDLToJSONBasicEntityConverter', () => {
describe('convert', () => {
describe('when not passing JDL entities', () => {
it('should fail', () => {
// @ts-expect-error
// @ts-expect-error empty parameter not allowed
expect(() => convert()).to.throw(/^JDL entities must be passed to get the basic entity information\.$/);
});
});
Expand Down
2 changes: 1 addition & 1 deletion jdl/converters/jdl-to-json/jdl-to-json-field-converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export function convert(jdlObject: JDLObject): Map<string, JSONField[]> {

function getConvertedFieldsForEntity(jdlEntity: JDLEntity, jdlObject: JDLObject): JSONField[] {
const convertedEntityFields: JSONField[] = [];
jdlEntity.forEachField(jdlField => {
jdlEntity.forEachField((jdlField: JDLField) => {
let fieldData: JSONField = {
fieldName: camelCase(jdlField.name),
fieldType: jdlField.type,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('jdl - JDLToJSONOptionConverter', () => {
describe('convert', () => {
describe('when not passing a JDL option holder', () => {
it('should fail', () => {
// @ts-expect-error
// @ts-expect-error empty parameter not allowed
expect(() => convert()).to.throw(/^A JDL object or application must be passed to convert JDL options to JSON\.$/);
});
});
Expand Down
13 changes: 8 additions & 5 deletions jdl/converters/jdl-to-json/jdl-to-json-relationship-converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ type RelationshipsRelatedToEntity = {
to: JDLRelationship[];
};

function getRelatedRelationships(relationships: JDLRelationship[], entityNames: string[]) {
function getRelatedRelationships(relationships: JDLRelationship[], entityNames: string[]): Map<string, RelationshipsRelatedToEntity> {
const relatedRelationships = new Map();
entityNames.forEach(entityName => {
const relationshipsRelatedToEntity: RelationshipsRelatedToEntity = {
Expand All @@ -80,7 +80,7 @@ function getRelatedRelationships(relationships: JDLRelationship[], entityNames:
return relatedRelationships;
}

function setRelationshipsFromEntity(relatedRelationships: RelationshipsRelatedToEntity, entityName: string) {
function setRelationshipsFromEntity(relatedRelationships: RelationshipsRelatedToEntity, entityName: string): void {
relatedRelationships.from.forEach(relationshipToConvert => {
const otherSplitField: any = extractField(relationshipToConvert.injectedFieldInTo);
const convertedRelationship: Partial<JSONRelationship> = {
Expand Down Expand Up @@ -110,7 +110,7 @@ function setRelationshipsFromEntity(relatedRelationships: RelationshipsRelatedTo

export const otherRelationshipType = relationshipType => relationshipType.split('-').reverse().join('-');

function setRelationshipsToEntity(relatedRelationships: RelationshipsRelatedToEntity, entityName: string) {
function setRelationshipsToEntity(relatedRelationships: RelationshipsRelatedToEntity, entityName: string): void {
relatedRelationships.to.forEach(relationshipToConvert => {
const otherSplitField = extractField(relationshipToConvert.injectedFieldInFrom);
const convertedRelationship: any = {
Expand Down Expand Up @@ -141,7 +141,10 @@ function setRelationshipsToEntity(relatedRelationships: RelationshipsRelatedToEn
});
}

function setOptionsForRelationshipSourceSide(relationshipToConvert: JDLRelationship, convertedRelationship: Partial<JSONRelationship>) {
function setOptionsForRelationshipSourceSide(
relationshipToConvert: JDLRelationship,
convertedRelationship: Partial<JSONRelationship>,
): void {
convertedRelationship.options = convertedRelationship.options || {};
relationshipToConvert.forEachGlobalOption((optionName, optionValue) => {
if (optionName === BUILT_IN_ENTITY) {
Expand All @@ -158,7 +161,7 @@ function setOptionsForRelationshipSourceSide(relationshipToConvert: JDLRelations
}
}

function setOptionsForRelationshipDestinationSide(relationshipToConvert: JDLRelationship, convertedRelationship: JSONRelationship) {
function setOptionsForRelationshipDestinationSide(relationshipToConvert: JDLRelationship, convertedRelationship: JSONRelationship): void {
convertedRelationship.options = convertedRelationship.options || {};
relationshipToConvert.forEachGlobalOption((optionName, optionValue) => {
convertedRelationship.options![optionName] = optionValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ function setOptions(entitiesForEachApplication) {
});
}

function getEntitiesForEachApplicationMap() {
function getEntitiesForEachApplicationMap(): Map<string, any> {
const entitiesForEachApplication = new Map();
entitiesPerApplication.forEach((entityNames, applicationName) => {
const entitiesInObject = entityNames
Expand All @@ -159,7 +159,7 @@ function formatEntitiesForEachApplication(entitiesForEachApplication) {
});
}

function addApplicationsWithoutEntities(entitiesForEachApplication) {
function addApplicationsWithoutEntities(entitiesForEachApplication: Map<string, string[]>) {
jdlObject?.forEachApplication(jdlApplication => {
if (jdlApplication.getEntityNames().length === 0) {
entitiesForEachApplication.set(jdlApplication.getConfigurationOptionValue('baseName'), []);
Expand Down
18 changes: 12 additions & 6 deletions jdl/converters/json-to-jdl-application-converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,25 @@

import JDLObject from '../models/jdl-object.js';
import createJDLApplication from '../models/jdl-application-factory.js';
import { JHipsterYoRcContent, JHipsterYoRcContentWrapper } from './types.js';

const GENERATOR_NAME = 'generator-jhipster';

export function convertApplicationsToJDL({ applications, jdl }: any = {}) {
const jsonApplications = applications || [];
const jdlObject = jdl || new JDLObject();
jsonApplications.forEach(application => {
type JHipsterYoRcContentAndJDLWrapper = {
applications?: JHipsterYoRcContent[];
jdl?: JDLObject;
};

export function convertApplicationsToJDL({ applications, jdl }: JHipsterYoRcContentAndJDLWrapper = {}) {
const jsonApplications: JHipsterYoRcContent[] = applications || [];
const jdlObject: JDLObject = jdl || new JDLObject();
jsonApplications.forEach((application: JHipsterYoRcContent) => {
const convertedApplication = convertApplicationToJDL({ application });
jdlObject.addApplication(convertedApplication);
});
return jdlObject;
}

export function convertApplicationToJDL({ application }: any = {}) {
return createJDLApplication(application[GENERATOR_NAME]);
export function convertApplicationToJDL({ application }: JHipsterYoRcContentWrapper = {}) {
return createJDLApplication(application![GENERATOR_NAME]);
}
14 changes: 7 additions & 7 deletions jdl/converters/json-to-jdl-converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { readJSONFile } from '../readers/json-file-reader.js';
import { convertApplicationToJDL } from './json-to-jdl-application-converter.js';
import { convertEntitiesToJDL } from './json-to-jdl-entity-converter.js';
import exportJDLObject from '../exporters/jdl-exporter.js';
import { JSONEntity, JSONRootObject, PostProcessedJSONGeneratorJhipsterContent, PostProcessedJSONRootObject } from './types.js';
import { JSONEntity, JHipsterYoRcContent, PostProcessedJSONRootObject } from './types.js';
import { removeFieldsWithNullishValues } from '../../generators/base/support/config.js';
import { GENERATOR_JHIPSTER } from '../../generators/generator-constants.js';
import JDLApplication from '../models/jdl-application.js';
Expand All @@ -44,7 +44,7 @@ export default {
export function convertToJDL(directory = '.', output: string | false = 'app.jdl'): JDLObject | undefined {
let jdlObject: JDLObject;
if (doesFileExist(path.join(directory, '.yo-rc.json'))) {
const yoRcFileContent: JSONRootObject = readJSONFile(path.join(directory, '.yo-rc.json'));
const yoRcFileContent: JHipsterYoRcContent = readJSONFile(path.join(directory, '.yo-rc.json'));
let entities: Map<string, JSONEntity> | undefined;
if (doesDirectoryExist(path.join(directory, '.jhipster'))) {
entities = getJSONEntityFiles(directory);
Expand All @@ -64,7 +64,7 @@ export function convertToJDL(directory = '.', output: string | false = 'app.jdl'
return jdlObject;
}

export function convertSingleContentToJDL(yoRcFileContent: JSONRootObject, entities?: Map<string, JSONEntity>): string {
export function convertSingleContentToJDL(yoRcFileContent: JHipsterYoRcContent, entities?: Map<string, JSONEntity>): string {
return getJDLObjectFromSingleApplication(yoRcFileContent, entities).toString();
}

Expand All @@ -76,7 +76,7 @@ function getJDLObjectFromMultipleApplications(directory: string): JDLObject {
let jdlObject = new JDLObject();
subDirectories.forEach(subDirectory => {
const applicationDirectory = path.join(directory, subDirectory);
const yoRcFileContent: JSONRootObject = readJSONFile(path.join(applicationDirectory, '.yo-rc.json'));
const yoRcFileContent: JHipsterYoRcContent = readJSONFile(path.join(applicationDirectory, '.yo-rc.json'));
let entities: Map<string, JSONEntity> = new Map();
if (doesDirectoryExist(path.join(applicationDirectory, '.jhipster'))) {
entities = getJSONEntityFiles(applicationDirectory);
Expand All @@ -87,11 +87,11 @@ function getJDLObjectFromMultipleApplications(directory: string): JDLObject {
}

export function getJDLObjectFromSingleApplication(
yoRcFileContent: JSONRootObject,
yoRcFileContent: JHipsterYoRcContent,
entities?: Map<string, JSONEntity>,
existingJDLObject = new JDLObject(),
): JDLObject {
const cleanedYoRcFileContent: PostProcessedJSONGeneratorJhipsterContent = cleanYoRcFileContent(yoRcFileContent);
const cleanedYoRcFileContent: PostProcessedJSONRootObject = cleanYoRcFileContent(yoRcFileContent);
const jdlApplication: JDLApplication = convertApplicationToJDL({ application: cleanedYoRcFileContent });
if (!entities) {
existingJDLObject.addApplication(jdlApplication);
Expand All @@ -103,7 +103,7 @@ export function getJDLObjectFromSingleApplication(
return mergeJDLObjects(existingJDLObject, jdlObject);
}

function cleanYoRcFileContent(yoRcFileContent: JSONRootObject): PostProcessedJSONRootObject {
function cleanYoRcFileContent(yoRcFileContent: JHipsterYoRcContent): PostProcessedJSONRootObject {
for (const key of Object.keys(yoRcFileContent)) {
yoRcFileContent[key] = removeFieldsWithNullishValues(yoRcFileContent[key]);
}
Expand Down
4 changes: 2 additions & 2 deletions jdl/converters/json-to-jdl-entity-converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ function convertJSONToJDLValidation(rule: string, field: JSONField): JDLValidati
}

function addEnumsToJDL(entity: JSONEntity): void {
entity?.fields?.forEach(field => {
entity?.fields?.forEach((field: JSONField) => {
if (field.fieldValues !== undefined) {
jdlObject.addEnum(
new JDLEnum({
Expand All @@ -145,7 +145,7 @@ function addEnumsToJDL(entity: JSONEntity): void {
});
}

function getEnumValuesFromString(valuesAsString: string): any {
function getEnumValuesFromString(valuesAsString: string): any[] {
return valuesAsString.split(',').map(fieldValue => {
// if fieldValue looks like ENUM_VALUE (something)
if (fieldValue.includes('(')) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ describe('jdl - DeploymentConverter', () => {
"directoryPath": "../",
"dockerPushCommand": "docker push",
"dockerRepositoryName": "test",
"gatewayType": undefined,
"ingressDomain": undefined,
"ingressType": undefined,
"istio": undefined,
"kubernetesServiceType": undefined,
"monitoring": "no",
"openshiftNamespace": "default",
"registryReplicas": 2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('jdl - EnumConverter', () => {
describe('convertEnums', () => {
describe('when not passing enumerations', () => {
it('should fail', () => {
// @ts-expect-error
// @ts-expect-error empty parameter not authorized
expect(() => convertEnums()).toThrow(/^Enumerations have to be passed so as to be converted.$/);
});
});
Expand Down
2 changes: 1 addition & 1 deletion jdl/converters/parsed-jdl-to-jdl-object/enum-converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default { convertEnums };
* @param {Array} enumerations - parsed JDL enumerations.
* @return the converted JDLEnums.
*/
export function convertEnums(enumerations): JDLEnum[] {
export function convertEnums(enumerations: ParsedJDLEnum[]): JDLEnum[] {
if (!enumerations) {
throw new Error('Enumerations have to be passed so as to be converted.');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import { before, it, describe, expect } from 'esmocha';
import { convertField } from './field-converter.js';
import JDLField from '../../models/jdl-field.js';

describe('jdl - FieldConverter', () => {
describe('convertField', () => {
Expand All @@ -34,9 +35,9 @@ describe('jdl - FieldConverter', () => {

before(() => {
convertedField = convertField({
validations: [],
name: 'anAwesomeField',
type: 'String',
comment: 'An awesome comment!',
});
});

Expand All @@ -56,7 +57,8 @@ JDLField {
let nameFromConvertedField;

before(() => {
const convertedField = convertField({
const convertedField: JDLField = convertField({
validations: [],
name: 'AnAwesomeField',
type: 'String',
});
Expand All @@ -73,6 +75,7 @@ JDLField {
before(() => {
const convertedField = convertField({
name: 'AnAwesomeField',
validations: [],
type: 'String',
documentation: 'An awesome comment!',
});
Expand Down
3 changes: 2 additions & 1 deletion jdl/converters/parsed-jdl-to-jdl-object/field-converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import JDLField from '../../models/jdl-field.js';
import { formatComment } from '../../utils/format-utils.js';
import { lowerFirst } from '../../utils/string-utils.js';
import { ParsedJDLEntityField } from './types.js';

export default { convertField };

Expand All @@ -28,7 +29,7 @@ export default { convertField };
* @param {Object} field - a parsed JDL field.
* @return the converted JDLField.
*/
export function convertField(field): JDLField {
export function convertField(field: ParsedJDLEntityField): JDLField {
if (!field) {
throw new Error('A field has to be passed so as to be converted.');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -581,8 +581,13 @@ JDLDeployment {
"directoryPath": "../",
"dockerRepositoryName": "test",
"gatewayType": "SpringCloudGateway",
"ingressDomain": undefined,
"ingressType": undefined,
"istio": undefined,
"kubernetesServiceType": undefined,
"monitoring": "no",
"serviceDiscoveryType": "consul",
"storageType": undefined,
}
`);
});
Expand Down
Loading

0 comments on commit 949ded3

Please sign in to comment.