Skip to content

Commit c729d2b

Browse files
authored
String overrides all other types in this union type (#31610)
1 parent 2b907f3 commit c729d2b

File tree

6 files changed

+24
-18
lines changed

6 files changed

+24
-18
lines changed

lib/jdl/converters/json-to-jdl-entity-converter.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import { lowerFirst, upperFirst } from 'lodash-es';
2121

2222
import { asJdlRelationshipType } from '../core/basic-types/relationship-types.ts';
23+
import type { JDLOptionName } from '../core/built-in-options/binary-options.ts';
2324
import { binaryOptions, relationshipOptions, unaryOptions } from '../core/built-in-options/index.ts';
2425
import { JDLEntity, JDLEnum } from '../core/models/index.ts';
2526
import JDLBinaryOption from '../core/models/jdl-binary-option.ts';
@@ -307,7 +308,7 @@ function addEntityOptionsToJDL(entity: JSONEntity, entityName: string): void {
307308
}
308309
}
309310

310-
function addUnaryOptionToJDL(unaryOption: string, entityName: string): void {
311+
function addUnaryOptionToJDL(unaryOption: JDLOptionName, entityName: string): void {
311312
jdlObject.addOption(
312313
new JDLUnaryOption({
313314
name: unaryOption,
@@ -316,7 +317,7 @@ function addUnaryOptionToJDL(unaryOption: string, entityName: string): void {
316317
);
317318
}
318319

319-
function addBinaryOptionToJDL(binaryOption: string, value: string, entityName: string): void {
320+
function addBinaryOptionToJDL(binaryOption: JDLOptionName, value: string, entityName: string): void {
320321
jdlObject.addOption(
321322
new JDLBinaryOption({
322323
name: binaryOption,

lib/jdl/converters/parsed-jdl-to-jdl-object/option-converter.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
* limitations under the License.
1818
*/
1919

20+
import type { BinaryOptionType } from '../../core/built-in-options/binary-options.ts';
2021
import { binaryOptions, unaryOptions } from '../../core/built-in-options/index.ts';
22+
import type { UnaryOptionType } from '../../core/built-in-options/unary-options.ts';
2123
import type AbstractJDLOption from '../../core/models/abstract-jdl-option.ts';
2224
import JDLBinaryOption from '../../core/models/jdl-binary-option.ts';
2325
import JDLUnaryOption from '../../core/models/jdl-unary-option.ts';
@@ -47,7 +49,7 @@ export function convertOptions(
4749

4850
function convertUnaryOptions(parsedOptions: Record<string, ParsedJDLOption>): JDLUnaryOption[] {
4951
const convertedUnaryOptions: JDLUnaryOption[] = [];
50-
unaryOptions.forEach((unaryOptionName: string) => {
52+
unaryOptions.forEach((unaryOptionName: UnaryOptionType) => {
5153
const parsedUnaryOption = parsedOptions[unaryOptionName];
5254
if (!parsedUnaryOption?.list || parsedUnaryOption.list.length === 0) {
5355
return;
@@ -65,7 +67,7 @@ function convertUnaryOptions(parsedOptions: Record<string, ParsedJDLOption>): JD
6567

6668
function convertBinaryOptions(parsedOptions: Record<string, Record<string, ParsedJDLOption>>): JDLBinaryOption[] {
6769
const convertedBinaryOptions: JDLBinaryOption[] = [];
68-
binaryOptions.forEach((binaryOptionName: string) => {
70+
binaryOptions.forEach((binaryOptionName: BinaryOptionType) => {
6971
if (!parsedOptions[binaryOptionName]) {
7072
return;
7173
}

lib/jdl/core/built-in-options/binary-options.spec.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,6 @@ describe('jdl - BinaryOptions', () => {
5555
expect(binaryOptions.exists(binaryOptions.Options.DTO, binaryOptions.Values.dto.MAPSTRUCT)).to.be.true;
5656
});
5757
});
58-
describe('when checking for a custom binary option', () => {
59-
it('should return true', () => {
60-
expect(binaryOptions.exists('customOption')).to.be.true;
61-
expect(binaryOptions.exists('customOption', 'customValue')).to.be.true;
62-
});
63-
});
6458
});
6559
describe('forEach', () => {
6660
describe('when not passing a function', () => {

lib/jdl/core/built-in-options/binary-options.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
*/
1919
import entityOptions from '../../../jhipster/entity-options.ts';
2020

21+
import type { UnaryOptionType } from './unary-options.ts';
22+
2123
const { MapperTypes, PaginationTypes, SearchTypes, ServiceTypes } = entityOptions;
2224
const { MAPSTRUCT } = MapperTypes;
2325
const NO_MAPPER = MapperTypes.NO;
@@ -52,13 +54,17 @@ const Values = {
5254
search: searchValues,
5355
} as const;
5456

57+
export type BinaryOptionType = (typeof Options)[keyof typeof Options];
58+
59+
export type JDLOptionName = BinaryOptionType | UnaryOptionType | 'paginate';
60+
5561
const DefaultValues = {
5662
[Options.DTO]: Values[Options.DTO].NO,
5763
[Options.SERVICE]: Values[Options.SERVICE].NO,
5864
[Options.PAGINATION]: Values[Options.PAGINATION].NO,
5965
};
6066

61-
function getOptionName(optionValue: string): string | undefined {
67+
function getOptionName(optionValue: string): JDLOptionName | undefined {
6268
return optionNames.find(optionName => (Values as Record<string, Record<string, string>>)[optionName]?.[optionValue]);
6369
}
6470

@@ -72,14 +78,14 @@ const OptionValues = {
7278
couchbase: 'COUCHBASE',
7379
} as const;
7480

75-
function forEach(passedFunction: (optionName: string) => void): void {
81+
function forEach(passedFunction: (optionName: BinaryOptionType) => void): void {
7682
if (!passedFunction) {
7783
throw new Error('A function has to be passed to loop over the binary options.');
7884
}
79-
optionNames.forEach(passedFunction);
85+
optionNames.forEach(optionName => passedFunction(optionName as BinaryOptionType));
8086
}
8187

82-
function exists(passedOption: string | keyof typeof Values | 'microservice' | 'angularSuffix' | 'clientRootFolder', passedValue?: any) {
88+
function exists(passedOption: JDLOptionName, passedValue?: any) {
8389
return (
8490
!(optionNames as string[]).includes(passedOption) ||
8591
optionNames.some(

lib/jdl/core/built-in-options/unary-options.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,16 @@ const Options = {
2727

2828
const optionNames = Object.values(Options);
2929

30+
export type UnaryOptionType = (typeof Options)[keyof typeof Options];
31+
3032
export default {
3133
...Options,
32-
forEach: (passedFunction: (optionName: string) => void) => {
34+
forEach: (passedFunction: (optionName: UnaryOptionType) => void) => {
3335
if (!passedFunction) {
3436
throw new Error('A function has to be passed to loop over the unary options.');
3537
}
3638
optionNames.forEach(optionName => {
37-
passedFunction(optionName);
39+
passedFunction(optionName as UnaryOptionType);
3840
});
3941
},
4042
exists: (optionName: string) => (Object.values(Options) as string[]).includes(optionName),

lib/jdl/core/models/abstract-jdl-option.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,18 @@
1717
* limitations under the License.
1818
*/
1919

20+
import type { JDLOptionName } from '../built-in-options/binary-options.ts';
2021
import { merge } from '../utils/object-utils.ts';
2122
import { addAll } from '../utils/set-utils.ts';
2223

2324
export type JDLOptionParams = {
24-
name: string;
25+
name: JDLOptionName;
2526
entityNames?: Set<string> | string[];
2627
excludedNames?: Set<string> | string[];
2728
};
2829

2930
export default class AbstractJDLOption {
30-
name: string;
31+
name: JDLOptionName;
3132
entityNames: Set<string>;
3233
excludedNames: Set<string>;
3334

0 commit comments

Comments
 (0)