Skip to content

Commit

Permalink
Merge pull request #26177 from Tcharl/typesafety
Browse files Browse the repository at this point in the history
More type checking
  • Loading branch information
Tcharl authored May 20, 2024
2 parents 3264fef + 0ed8ae8 commit f4142f5
Show file tree
Hide file tree
Showing 26 changed files with 253 additions and 82 deletions.
3 changes: 1 addition & 2 deletions generators/java/support/checks/check-java.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { after, before, it, describe, expect, resetAllMocks, esmocha } from 'esmocha';
import { SyncResult } from 'execa';
import quibble from 'quibble';

const execa = { execa: esmocha.fn(), execaSync: esmocha.fn(), execaCommandSync: esmocha.fn(), execaCommand: esmocha.fn() };

const baseResult: SyncResult = {
const baseResult = {
cwd: '',
command: 'java',
escapedCommand: 'java',
Expand Down
14 changes: 9 additions & 5 deletions generators/spring-boot/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ import chalk from 'chalk';
import { JHipsterCommandDefinition } from '../base/api.js';
import { GENERATOR_JAVA, GENERATOR_LIQUIBASE, GENERATOR_SPRING_DATA_RELATIONAL } from '../generator-list.js';
import { createBase64Secret, createSecret } from '../base/support/secret.js';
import { authenticationTypes, applicationTypes } from '../../jdl/index.js';

const { OAUTH2, SESSION, JWT } = authenticationTypes;
const { GATEWAY, MICROSERVICE } = applicationTypes;

const command: JHipsterCommandDefinition = {
options: {
Expand Down Expand Up @@ -97,13 +101,13 @@ const command: JHipsterCommandDefinition = {
],
configure: gen => {
const { jwtSecretKey, rememberMeKey, authenticationType, applicationType } = gen.jhipsterConfigWithDefaults;
if (authenticationType === 'session' && !rememberMeKey) {
if (authenticationType === SESSION && !rememberMeKey) {
gen.jhipsterConfig.rememberMeKey = createSecret();
} else if (authenticationType === 'oauth2' && gen.jhipsterConfig.skipUserManagement === undefined) {
} else if (authenticationType === OAUTH2 && gen.jhipsterConfig.skipUserManagement === undefined) {
gen.jhipsterConfig.skipUserManagement = true;
} else if (
jwtSecretKey === undefined &&
(authenticationType === 'jwt' || applicationType === 'microservice' || applicationType === 'gateway')
(authenticationType === JWT || applicationType === MICROSERVICE || applicationType === GATEWAY)
) {
gen.jhipsterConfig.jwtSecretKey = createBase64Secret(64, gen.options.reproducibleTests);
}
Expand All @@ -118,7 +122,7 @@ const command: JHipsterCommandDefinition = {
type: 'confirm',
message: 'Do you want to generate a feign client?',
when: ({ reactive }) =>
['microservice'].includes(gen.jhipsterConfigWithDefaults.applicationType) &&
[MICROSERVICE].includes(gen.jhipsterConfigWithDefaults.applicationType) &&
(reactive ?? gen.jhipsterConfigWithDefaults.reactive) === false,
}),
default: false,
Expand All @@ -138,7 +142,7 @@ const command: JHipsterCommandDefinition = {
if (gen.isJhipsterVersionLessThan('8.1.1')) {
gen.jhipsterConfig.syncUserWithIdp = true;
}
} else if (gen.jhipsterConfig.syncUserWithIdp && gen.jhipsterConfig.authenticationType !== 'oauth2') {
} else if (gen.jhipsterConfig.syncUserWithIdp && gen.jhipsterConfig.authenticationType !== OAUTH2) {
throw new Error('syncUserWithIdp is only supported with authenticationType oauth2');
}
},
Expand Down
11 changes: 7 additions & 4 deletions jdl/converters/parsed-jdl-to-jdl-object/application-converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ import { convertOptions } from './option-converter.js';
import ApplicationOptions from '../../jhipster/application-options.js';
import JDLUnaryOption from '../../models/jdl-unary-option.js';
import JDLBinaryOption from '../../models/jdl-binary-option.js';
import { ParsedJDLApplication } from './types.js';
import AbstractJDLOption from '../../models/abstract-jdl-option.js';
import JDLApplication from '../../models/jdl-application.js';

const {
OptionNames: { BASE_NAME },
Expand All @@ -34,7 +37,7 @@ export default { convertApplications };
* @param {Array<Object>} parsedApplications - the parsed applications.
* @return {Array} the converted JDL applications.
*/
export function convertApplications(parsedApplications) {
export function convertApplications(parsedApplications: ParsedJDLApplication[]): JDLApplication[] {
if (!parsedApplications) {
throw new Error('Applications have to be passed so as to be converted.');
}
Expand All @@ -48,8 +51,8 @@ export function convertApplications(parsedApplications) {
});
}

function getEntityOptionsInApplication(parsedApplication) {
return convertOptions(parsedApplication.options, parsedApplication.useOptions);
function getEntityOptionsInApplication(parsedApplication: ParsedJDLApplication): AbstractJDLOption[] {
return convertOptions(parsedApplication.options, parsedApplication.useOptions || []);
}

/**
Expand All @@ -61,7 +64,7 @@ function getEntityOptionsInApplication(parsedApplication) {
function checkEntityNamesInOptions(
applicationName: string,
entityOptions: (JDLUnaryOption | JDLBinaryOption)[],
entityNamesInApplication: string[],
entityNamesInApplication: string[] | undefined,
) {
const entityNamesInApplicationSet = new Set<string>(entityNamesInApplication);
entityOptions.forEach(option => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/

import JDLDeployment from '../../models/jdl-deployment.js';
import { ParsedJDLDeployment } from './types.js';

export default { convertDeployments };

Expand All @@ -26,7 +27,7 @@ export default { convertDeployments };
* @param {Array} parsedDeployments - parsed JDL deployments.
* @return the converted JDLDeployment objects.
*/
export function convertDeployments(parsedDeployments): JDLDeployment[] {
export function convertDeployments(parsedDeployments: ParsedJDLDeployment[]): JDLDeployment[] {
if (!parsedDeployments) {
throw new Error('Deployments have to be passed so as to be converted.');
}
Expand Down
7 changes: 6 additions & 1 deletion jdl/converters/parsed-jdl-to-jdl-object/entity-converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import { lowerFirst } from 'lodash-es';
import { JDLEntity } from '../../models/index.js';
import { formatComment } from '../../utils/format-utils.js';
import { ParsedJDLEntity } from './types.js';
import JDLField from '../../models/jdl-field.js';

export default { convertEntities };

Expand All @@ -29,7 +31,10 @@ export default { convertEntities };
* @param {Function} jdlFieldGetterFunction - the function called to retrieve JDL fields for an entity.
* @returns converted JDLEntity objects.
*/
export function convertEntities(parsedEntities, jdlFieldGetterFunction): JDLEntity[] {
export function convertEntities(
parsedEntities: ParsedJDLEntity[],
jdlFieldGetterFunction: (entity: ParsedJDLEntity) => JDLField[],
): JDLEntity[] {
if (!parsedEntities) {
throw new Error('Entities have to be passed so as to be converted.');
}
Expand Down
3 changes: 2 additions & 1 deletion jdl/converters/parsed-jdl-to-jdl-object/enum-converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import { JDLEnum } from '../../models/index.js';
import { formatComment } from '../../utils/format-utils.js';
import { ParsedJDLEnum } from './types.js';

export default { convertEnums };

Expand All @@ -39,7 +40,7 @@ export function convertEnums(enumerations): JDLEnum[] {
* @param {Object} enumeration - a parsed JDL enumeration.
* @return the converted JDLEnum.
*/
function convertEnum(enumeration): JDLEnum {
function convertEnum(enumeration: ParsedJDLEnum): JDLEnum {
return new JDLEnum({
name: enumeration.name,
values: enumeration.values,
Expand Down
21 changes: 13 additions & 8 deletions jdl/converters/parsed-jdl-to-jdl-object/option-converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import JDLUnaryOption from '../../models/jdl-unary-option.js';
import JDLBinaryOption from '../../models/jdl-binary-option.js';
import { unaryOptions, binaryOptions } from '../../jhipster/index.js';
import { ParsedJDLOption, ParsedJDLUseOption } from './types.js';
import AbstractJDLOption from '../../models/abstract-jdl-option.js';

const { OptionValues, getOptionName } = binaryOptions;
export default { convertOptions };
Expand All @@ -30,19 +32,22 @@ export default { convertOptions };
* @param {Array<Object>} useOptions - the parsed option object, using the use form.
* @returns {Array<JDLUnaryOption|JDLBinaryOption>} the converted JDLUnaryOption & JDLBinaryOption objects.
*/
export function convertOptions(parsedOptions, useOptions) {
export function convertOptions(
parsedOptions: Record<string, ParsedJDLOption | Record<string, ParsedJDLOption>> | undefined,
useOptions: ParsedJDLUseOption[],
): AbstractJDLOption[] {
if (!parsedOptions) {
throw new Error('Options have to be passed so as to be converted.');
}
const convertedUnaryOptions = convertUnaryOptions(parsedOptions);
const convertedBinaryOptions = convertBinaryOptions(parsedOptions);
const convertedUnaryOptions = convertUnaryOptions(parsedOptions as Record<string, ParsedJDLOption>);
const convertedBinaryOptions = convertBinaryOptions(parsedOptions as Record<string, Record<string, ParsedJDLOption>>);
const convertedUseOptions = convertUseOptions(useOptions);
return [...convertedUnaryOptions, ...convertedBinaryOptions, ...convertedUseOptions];
}

function convertUnaryOptions(parsedOptions) {
function convertUnaryOptions(parsedOptions: Record<string, ParsedJDLOption>): JDLUnaryOption[] {
const convertedUnaryOptions: JDLUnaryOption[] = [];
unaryOptions.forEach(unaryOptionName => {
unaryOptions.forEach((unaryOptionName: string) => {
const parsedUnaryOption = parsedOptions[unaryOptionName];
if (!parsedUnaryOption || !parsedUnaryOption.list || parsedUnaryOption.list.length === 0) {
return;
Expand All @@ -58,9 +63,9 @@ function convertUnaryOptions(parsedOptions) {
return convertedUnaryOptions;
}

function convertBinaryOptions(parsedOptions) {
function convertBinaryOptions(parsedOptions: Record<string, Record<string, ParsedJDLOption>>): JDLBinaryOption[] {
const convertedBinaryOptions: JDLBinaryOption[] = [];
binaryOptions.forEach(binaryOptionName => {
binaryOptions.forEach((binaryOptionName: string) => {
if (!parsedOptions[binaryOptionName]) {
return;
}
Expand All @@ -80,7 +85,7 @@ function convertBinaryOptions(parsedOptions) {
return convertedBinaryOptions;
}

function convertUseOptions(useOptions) {
function convertUseOptions(useOptions: ParsedJDLUseOption[]): JDLBinaryOption[] {
const convertedUseOptions: JDLBinaryOption[] = [];

useOptions.forEach(useValue => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ describe('jdl - ParsedJDLToJDLObjectConverter', () => {
describe('because there is no parsedContent', () => {
it('should fail', () => {
expect(() => {
// @ts-expect-error
ParsedJDLToJDLObjectConverter.parseFromConfigurationObject({});
}).to.throw(/^The parsed JDL content must be passed\.$/);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,22 @@ import { convertValidations } from './validation-converter.js';
import { convertOptions } from './option-converter.js';
import { convertRelationships } from './relationship-converter.js';
import { convertDeployments } from './deployment-converter.js';

let parsedContent;
let configuration;
import {
ParsedJDLAnnotation,
ParsedJDLApplication,
ParsedJDLApplications,
ParsedJDLEntity,
ParsedJDLEntityField,
ParsedJDLRoot,
} from './types.js';
import JDLApplication from '../../models/jdl-application.js';
import JDLField from '../../models/jdl-field.js';

let parsedContent: ParsedJDLApplications;
let configuration: ParsedJDLRoot;
let jdlObject: JDLObject;
let entityNames;
let applicationsPerEntityName;
let entityNames: string[];
let applicationsPerEntityName: Map<string, ParsedJDLApplication>;

/**
* Converts the intermediate parsedContent to a JDLObject from a configuration object.
Expand All @@ -46,7 +56,7 @@ let applicationsPerEntityName;
* @param {String} configurationObject.databaseType - The application's database type
* @return the built JDL object.
*/
export function parseFromConfigurationObject(configurationObject): JDLObject {
export function parseFromConfigurationObject(configurationObject: ParsedJDLRoot): JDLObject {
parsedContent = configurationObject.parsedContent || configurationObject.document;
if (!parsedContent) {
throw new Error('The parsed JDL content must be passed.');
Expand All @@ -61,32 +71,32 @@ export function parseFromConfigurationObject(configurationObject): JDLObject {
return jdlObject;
}

function init(passedConfiguration) {
function init(passedConfiguration: ParsedJDLRoot) {
configuration = passedConfiguration;
jdlObject = new JDLObject();
entityNames = parsedContent.entities.map(entity => entity.name);
applicationsPerEntityName = {};
applicationsPerEntityName = new Map();
}

function fillApplications() {
function fillApplications(): void {
// TODO: Function which expects two arguments is called with three.
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
const jdlApplications = convertApplications(parsedContent.applications, configuration, entityNames);
jdlApplications.forEach(jdlApplication => {
const jdlApplications: JDLApplication[] = convertApplications(parsedContent.applications, configuration, entityNames);
jdlApplications.forEach((jdlApplication: JDLApplication) => {
jdlObject.addApplication(jdlApplication);
fillApplicationsPerEntityName(jdlApplication);
});
}

function fillApplicationsPerEntityName(application) {
application.forEachEntityName(entityName => {
function fillApplicationsPerEntityName(application: JDLApplication): void {
application.forEachEntityName((entityName: string) => {
applicationsPerEntityName[entityName] = applicationsPerEntityName[entityName] || [];
applicationsPerEntityName[entityName].push(application);
});
}

function fillDeployments() {
function fillDeployments(): void {
const jdlDeployments = convertDeployments(parsedContent.deployments);
jdlDeployments.forEach(jdlDeployment => {
jdlObject.addDeployment(jdlDeployment);
Expand All @@ -107,10 +117,11 @@ function fillClassesAndFields() {
});
}

function getJDLFieldsFromParsedEntity(entity) {
function getJDLFieldsFromParsedEntity(entity: ParsedJDLEntity): JDLField[] {
const fields: any[] = [];
for (let i = 0; i < entity.body.length; i++) {
const field = entity.body[i];
const arr = entity.body || [];
for (let i = 0; i < arr.length; i++) {
const field = arr[i];
const jdlField = convertField(field);
jdlField.validations = getValidations(field);
jdlField.options = convertAnnotationsToOptions(field.annotations);
Expand All @@ -119,14 +130,14 @@ function getJDLFieldsFromParsedEntity(entity) {
return fields;
}

function getValidations(field) {
function getValidations(field: ParsedJDLEntityField) {
return convertValidations(field.validations, getConstantValueFromConstantName).reduce((jdlValidations, jdlValidation) => {
jdlValidations[jdlValidation.name] = jdlValidation;
return jdlValidations;
}, {});
}

function getConstantValueFromConstantName(constantName) {
function getConstantValueFromConstantName(constantName: string) {
return parsedContent.constants[constantName];
}

Expand All @@ -139,7 +150,9 @@ function fillAssociations() {
});
}

function convertAnnotationsToOptions(annotations) {
function convertAnnotationsToOptions(
annotations: ParsedJDLAnnotation[],
): Record<string, boolean | string | number | string[] | boolean[] | number[]> {
const result = {};
annotations.forEach(annotation => {
const annotationName = lowerFirst(annotation.optionName);
Expand Down Expand Up @@ -184,7 +197,7 @@ function fillUnaryAndBinaryOptions() {
jdlObject.addOption(
new JDLBinaryOption({
name: binaryOptions.Options.CLIENT_ROOT_FOLDER,
value: configuration.applicationName,
value: configuration.applicationName!,
entityNames,
}),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import JDLRelationship from '../../models/jdl-relationship.js';
import { lowerFirst } from '../../utils/string-utils.js';
import { formatComment } from '../../utils/format-utils.js';
import { asJdlRelationshipType } from '../../jhipster/relationship-types.js';
import { ParsedJDLRelationship } from './types.js';

export default { convertRelationships };

Expand All @@ -30,7 +31,7 @@ export default { convertRelationships };
* @param {Function} annotationToOptionConverter - the function that can convert annotations to options.
* @return the converted JDL relationships.
*/
export function convertRelationships(parsedRelationships, annotationToOptionConverter): JDLRelationship[] {
export function convertRelationships(parsedRelationships: ParsedJDLRelationship[], annotationToOptionConverter): JDLRelationship[] {
if (!parsedRelationships) {
throw new Error('Relationships have to be passed so as to be converted.');
}
Expand Down
Loading

0 comments on commit f4142f5

Please sign in to comment.