Skip to content

Commit 164ee10

Browse files
committed
feat(dts-generator): introduce new directive for modules with named exports
The new .dtsgenrc option "modulesWithNamedExports" can be an array of module names for which named exports are generated. For library modules, this happens automatically, the y don't have to be listed. The new option is useful for the webcomponent package files created by ui5-tooling-modules. They should behave similar to library modules.
1 parent ee2e491 commit 164ee10

File tree

7 files changed

+38
-3
lines changed

7 files changed

+38
-3
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export interface Directives {
4848
fqnToIgnore: {
4949
[fqn: string]: string;
5050
};
51+
modulesWithNamedExports: string[];
5152
namespacesToInterfaces: {
5253
[orgNamespace: string]: true | [true] | [true, "keep_original_ns"];
5354
};

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,15 @@ export interface Directives {
9494
deprecatedEnumAliases: {
9595
[fqn: string]: string;
9696
};
97+
98+
/**
99+
* A list of modules (their names w/o the *.js extension) for which named exports
100+
* will be generated.
101+
*
102+
* All `** /library.js` modules automatically use named exports, they don't have
103+
* to be listed.
104+
*/
105+
modulesWithNamedExports: string[];
97106
}
98107

99108
/**
@@ -135,6 +144,7 @@ const defaultOptions: GenerateFromObjectsConfig = {
135144
fqnToIgnore: {},
136145
overlays: {},
137146
deprecatedEnumAliases: {},
147+
modulesWithNamedExports: [],
138148
},
139149
generateGlobals: false,
140150
};

packages/dts-generator/src/generate.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ async function loadDirectives(directivesPaths: string[]) {
3333
fqnToIgnore: {},
3434
overlays: {},
3535
deprecatedEnumAliases: {},
36+
modulesWithNamedExports: [],
3637
};
3738

3839
function mergeDirectives(loadedDirectives: Directives) {

packages/dts-generator/src/js-utils/ui5-metadata.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,8 @@ export async function loadDirectives(directivesFiles) {
243243
forwardDeclarations: {},
244244
fqnToIgnore: {},
245245
overlays: {},
246+
deprecatedEnumAliases: {},
247+
modulesWithNamedExports: [],
246248
};
247249
function mergeDirectives(loadedDirectives) {
248250
Object.keys(loadedDirectives).forEach((key) => {

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,23 @@ function markDeprecatedAliasesForEnums(
899899
});
900900
}
901901

902+
function markEnumsAsUsingNamedExports(
903+
symbols: ConcreteSymbol[],
904+
directives: Directives,
905+
) {
906+
const modulesWithNamedExports = new Set(directives.modulesWithNamedExports);
907+
symbols.forEach((symbol) => {
908+
if (
909+
symbol.kind === "enum" &&
910+
symbol.module &&
911+
symbol.export &&
912+
modulesWithNamedExports.has(symbol.module)
913+
) {
914+
symbol.useNamedExport = true;
915+
}
916+
});
917+
}
918+
902919
function _prepareApiJson(
903920
json: ApiJSON,
904921
directives: Directives,
@@ -915,6 +932,7 @@ function _prepareApiJson(
915932
determineMissingExportsForTypes(json.symbols);
916933
parseTypeExpressions(json.symbols);
917934
markDeprecatedAliasesForEnums(json.symbols, directives);
935+
markEnumsAsUsingNamedExports(json.symbols, directives);
918936
if (options.mainLibrary) {
919937
addForwardDeclarations(json, directives);
920938
addInterfaceWithModuleNames(json.symbols);

packages/dts-generator/src/phases/json-to-ast.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,8 @@ class ModuleBuilder extends ScopeBuilder {
700700
if (
701701
!moduleImport.module.endsWith("/library") &&
702702
exportName != "" &&
703-
symbol.kind === "enum"
703+
symbol.kind === "enum" &&
704+
!symbol.useNamedExport
704705
) {
705706
const defaultFQN = fqn.substring(0, fqn.lastIndexOf("."));
706707
const defaultImportName =
@@ -1656,7 +1657,7 @@ function buildInterfaceFromObject(ui5Object): Interface {
16561657
*/
16571658
function buildEnum(ui5Enum: EnumSymbol) {
16581659
assertKnownProps(
1659-
["name", "basename", "properties", "deprecatedAliasFor"],
1660+
["name", "basename", "properties", "deprecatedAliasFor", "useNamedExport"],
16601661
ui5Enum,
16611662
);
16621663

@@ -1668,7 +1669,8 @@ function buildEnum(ui5Enum: EnumSymbol) {
16681669
kind: "Enum",
16691670
name: ui5Enum.basename,
16701671
withValues: true,
1671-
isLibraryEnum: ui5Enum.module.endsWith("/library"),
1672+
isLibraryEnum:
1673+
ui5Enum.useNamedExport || ui5Enum.module.endsWith("/library"),
16721674
deprecatedAliasFor: ui5Enum.deprecatedAliasFor,
16731675
values: _.map(ui5Enum.properties, (prop) =>
16741676
buildVariableWithValue(prop, isStandardEnum),

packages/dts-generator/src/types/api-json.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ export type EnumSymbol = SymbolBase & {
158158
stereotype?: "enum";
159159
};
160160
deprecatedAliasFor?: string;
161+
useNamedExport?: boolean;
161162
[k: string]: any;
162163
};
163164
/**

0 commit comments

Comments
 (0)