Skip to content

Commit 29c336e

Browse files
committed
feat(dts-generator): trust source data for event parameter optionality
Previously, all properties in generated $Xxx$YyyEventParameters interfaces were unconditionally forced to optional (?), regardless of their actual "optional" value in the api.json source data. Now the generator respects each parameter's optional field by default: - optional: false → required (no ?) - optional: true or undefined → optional (?) For events whose parameter optionality has not yet been verified in the OpenUI5/SAPUI5 sources, a new "eventsWithAllParamsOptional" directive (string[] in .dtsgenrc) preserves the legacy all-optional behavior. Format: "fully.qualified.ClassName:eventName" Changes: - generate-from-objects.ts: Add eventsWithAllParamsOptional to Directives. - generate.ts: Initialize the new directive field. - json-fixer.ts: Pass directives to addEventParameterInterfaces(). Add "events" to mapLikeProperties so .dtsgenrc overlays can target individual events by name. - json-event-parameter-interfaces.ts: Invert buildProperties() to respect source data by default; only force all-optional when the event is in the eventsWithAllParamsOptional deny-list. Log a warning when a parameter has no "optional" value (unexpected edge case). - Regenerated snapshot .d.ts files (parameters with optional: false now appear as required). Needed for #529
1 parent dc82050 commit 29c336e

13 files changed

Lines changed: 1850 additions & 936 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@ui5/dts-generator": minor
3+
---
4+
5+
Event parameter interfaces now reflect actual optionality from the UI5 source data. Previously all event parameters were unconditionally generated as optional (`?`); now parameters marked `optional: false` in the API are generated as required. A new `eventsWithAllParamsOptional` directive in `.dtsgenrc` lets you preserve the legacy all-optional behavior for specific events whose optionality hasn't been verified upstream yet.

‎packages/dts-generator/api-report/dts-generator.api.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export interface Directives {
4242
deprecatedEnumAliases: {
4343
[fqn: string]: string;
4444
};
45+
eventsWithAllParamsOptional?: string[];
4546
forwardDeclarations: {
4647
[libraryName: string]: ConcreteSymbol[];
4748
};

‎packages/dts-generator/src/generate-from-objects.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,13 @@ export interface Directives {
103103
* to be listed.
104104
*/
105105
modulesWithNamedExports: string[];
106+
107+
/**
108+
* Events listed here retain the legacy "all parameters optional" behavior.
109+
* Format: "fully.qualified.ClassName:eventName"
110+
* Used for events whose parameter optionality has not yet been verified in the source.
111+
*/
112+
eventsWithAllParamsOptional?: string[];
106113
}
107114

108115
/**
@@ -145,6 +152,7 @@ const defaultOptions: GenerateFromObjectsConfig = {
145152
overlays: {},
146153
deprecatedEnumAliases: {},
147154
modulesWithNamedExports: [],
155+
eventsWithAllParamsOptional: [],
148156
},
149157
generateGlobals: false,
150158
};

‎packages/dts-generator/src/generate.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ async function loadDirectives(directivesPaths: string[]) {
3535
overlays: {},
3636
deprecatedEnumAliases: {},
3737
modulesWithNamedExports: [],
38+
eventsWithAllParamsOptional: [],
3839
};
3940

4041
function mergeDirectives(loadedDirectives: Directives) {

‎packages/dts-generator/src/phases/json-fixer.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ function mergeOverlays(
4949
);
5050
}
5151

52-
const mapLikeProperties = new Set(["methods", "properties"]);
52+
const mapLikeProperties = new Set(["methods", "properties", "events"]);
5353

5454
function mergeProp(
5555
obj: { [key: string]: unknown },
@@ -984,7 +984,7 @@ export function fixApiJsons(
984984

985985
// Part 2: add interfaces for settings objects and event parameter objects
986986
addConstructorSettingsInterfaces(targetLibFixedJson, depsFixedJsons);
987-
addEventParameterInterfaces(targetLibFixedJson, depsFixedJsons);
987+
addEventParameterInterfaces(targetLibFixedJson, depsFixedJsons, directives);
988988

989989
// Part 3: "fix JSON"
990990
targetLibFixedJson = _fixApiJson(targetLibFixedJson, directives.badSymbols);

‎packages/dts-generator/src/utils/json-event-parameter-interfaces.ts‎

Lines changed: 53 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,20 @@ import {
44
ClassSymbol,
55
ConcreteSymbol,
66
InterfaceSymbol,
7+
NestedProperties,
78
ObjCallableParameter,
89
TypedefSymbol,
910
Ui5Event,
1011
} from "../types/api-json.js";
11-
const log = getLogger("@ui5/dts-generator/constructor-settings-interfaces");
12+
const log = getLogger("@ui5/dts-generator/event-parameter-interfaces");
1213
import { calculateDerivedNames } from "./base-utils.js";
1314
import {
1415
addJsDocProps,
1516
isA,
1617
makeSettingsNames,
1718
} from "./json-constructor-settings-interfaces.js";
1819
import { FunctionType, TypeReference } from "../types/ast.js";
20+
import { Directives } from "../generate-from-objects.js";
1921

2022
/**
2123
* Creates for each event in an `EventProvider` subclass an interface that describes the
@@ -40,6 +42,7 @@ function createEventParameterInterfaces(
4042
symbols: ConcreteSymbol[],
4143
dependencies: ApiJSON[],
4244
addDetails: boolean,
45+
directives?: Directives,
4346
) {
4447
const typeUniverse = new Map();
4548

@@ -71,20 +74,32 @@ function createEventParameterInterfaces(
7174
};
7275
};
7376

74-
function buildProperties(srcProperties) {
77+
function buildProperties(
78+
srcProperties: NestedProperties,
79+
allOptional: boolean,
80+
context: string,
81+
) {
7582
const transformedProperties = [];
7683
for (let propertyName in srcProperties) {
7784
const prop = srcProperties[propertyName];
78-
transformedProperties.push(
79-
addJsDocProps(
80-
{
81-
name: prop.name,
82-
type: prop.type,
83-
visibility: "public", // prop.visibility,
84-
},
85-
prop,
86-
),
85+
const transformed = addJsDocProps(
86+
{
87+
name: prop.name,
88+
type: prop.type,
89+
visibility: "public", // prop.visibility,
90+
},
91+
prop,
8792
);
93+
if (!allOptional) {
94+
if (prop.optional === false) {
95+
transformed.optional = false;
96+
} else if (prop.optional !== true) {
97+
log.warn(
98+
`Event parameter "${prop.name}" in ${context} has no "optional" value; treating as optional.`,
99+
);
100+
}
101+
}
102+
transformedProperties.push(transformed);
88103
}
89104
return transformedProperties;
90105
}
@@ -118,7 +133,9 @@ function createEventParameterInterfaces(
118133
superEvent: superEvents[0],
119134
};
120135
} else if (superEvents && superEvents.length > 1) {
121-
debugger; // should never happen
136+
log.warn(
137+
`Multiple events named "${eventName}" found on superclass ${superClassSymbol.name}`,
138+
);
122139
} else {
123140
// superclass does not have the event -> go up the class hierarchy
124141
superClassSymbol = nextEventProviderSuperClass(superClassSymbol);
@@ -270,12 +287,12 @@ function createEventParameterInterfaces(
270287
};
271288

272289
// collect parameters
273-
const allParameters =
290+
const allParameters: NestedProperties =
274291
(addDetails &&
275292
event.parameters &&
276293
(event.parameters[0] as ObjCallableParameter).parameterProperties
277294
?.getParameters?.parameterProperties) ||
278-
[];
295+
{};
279296
const hasParameters = Object.keys(allParameters).length > 0; // remember because elements from allParameters are removed below; if true, then at least somesuperclass has parameters
280297

281298
// search superclasses for same event
@@ -320,7 +337,15 @@ function createEventParameterInterfaces(
320337
}
321338

322339
// now add the parameters to the interface
323-
const parameters = buildProperties(allParameters);
340+
const allOptional =
341+
directives?.eventsWithAllParamsOptional?.includes(
342+
`${symbol.name}:${event.name}`,
343+
) ?? false;
344+
const parameters = buildProperties(
345+
allParameters,
346+
allOptional,
347+
`${symbol.name}#${event.name}`,
348+
);
324349
eventParametersInterface.properties = parameters;
325350
eventParametersInterface.description = `Parameters of the ${symbol.basename}#${event.name} event.`;
326351
if (event.deprecated) {
@@ -418,9 +443,20 @@ function createEventParameterInterfaces(
418443
export function addEventParameterInterfaces(
419444
apijson: ApiJSON,
420445
dependencies: ApiJSON[],
446+
directives?: Directives,
421447
) {
422448
dependencies.forEach((dep) => {
423-
createEventParameterInterfaces(dep.symbols, dependencies, false);
449+
createEventParameterInterfaces(
450+
dep.symbols,
451+
dependencies,
452+
false,
453+
directives,
454+
);
424455
});
425-
createEventParameterInterfaces(apijson.symbols, dependencies, true);
456+
createEventParameterInterfaces(
457+
apijson.symbols,
458+
dependencies,
459+
true,
460+
directives,
461+
);
426462
}

0 commit comments

Comments
 (0)