Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(update-sapui5-types script): Adjust metadata generation of UI5 types #452

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions scripts/metadataProvider/MetadataProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@ import {
import {
BaseUI5Node,
UI5Class,
UI5Namespace,
UI5Typedef,
UI5Interface,
UI5SemanticModel,
} from "@ui5-language-assistant/semantic-model-types";

type SymbolOption = "aggregation" | "association" | "defaultAggregation" | "event" | "property";
type SymbolOptionValues = Record<string, SymbolOption>;

export default class MetadataProvider {
#model: UI5SemanticModel | null = null;
async init(apiJsonsRoot: string, sapui5Version: string) {
Expand Down Expand Up @@ -45,4 +51,68 @@ export default class MetadataProvider {
// console.timeEnd("getDefaultAggregationForSymbol");
return classMetadata.defaultAggregation?.name;
}

/**
* Collects all option values (aggregation, association, defaultAggregation, event, method, property)
* for a given symbol (incl. borrowed ones).
* @param {BaseUI5Node} symbol
* @returns {SymbolOptionValues}
*/
collectOptionValuesForSymbol(symbol: BaseUI5Node): SymbolOptionValues | undefined {
const symbolOptionValues: SymbolOptionValues = {};
let symbolMetadata;

switch (symbol.kind) {
case "UI5Class":
symbolMetadata = symbol as UI5Class;
// Collect own and borrowed option values:
while (symbolMetadata.extends) {
symbolMetadata.aggregations.forEach((aggregation) => {
symbolOptionValues[aggregation.name] = "aggregation";
});
symbolMetadata.associations.forEach((association) => {
symbolOptionValues[association.name] = "association";
});
if (symbolMetadata.defaultAggregation) {
symbolOptionValues[symbolMetadata.defaultAggregation.name] = "defaultAggregation";
}
symbolMetadata.events.forEach((event) => {
symbolOptionValues[event.name] = "event";
});
symbolMetadata.properties.forEach((property) => {
symbolOptionValues[property.name] = "property";
});
symbolMetadata = symbolMetadata.extends;
}
break;
/* case "UI5Enum": // Disabled because UI5Enum does not contain any option values
symbolMetadata = symbol as UI5Enum;
break;
case "UI5Function": // Disabled because UI5Function does not contain any option values
symbolMetadata = symbol as UI5Function;
break; */
case "UI5Namespace":
symbolMetadata = symbol as UI5Namespace;
symbolMetadata.events.forEach((event) => {
symbolOptionValues[event.name] = "event";
});
break;
case "UI5Typedef":
symbolMetadata = symbol as UI5Typedef;
// NPM package "@ui5-language-assistant/semantic-model-types" was not updated:
// symbolMetadata.properties.forEach((property) => {
// symbolOptionValues[property.name] = "property";
// });
break;
case "UI5Interface":
symbolMetadata = symbol as UI5Interface;
symbolMetadata.events.forEach((event) => {
symbolOptionValues[event.name] = "event";
});
break;
default:
return undefined;
}
return symbolOptionValues;
}
}
17 changes: 12 additions & 5 deletions scripts/metadataProvider/createMetadataInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default async function createMetadataInfo(apiJsonsRoot: string, sapui5Ver
name: "SAPUI5",
version: sapui5Version,
},
defaultAggregations: {},
metadata: {},
deprecations: {
UI5Class: {},
UI5Enum: {},
Expand All @@ -54,19 +54,26 @@ export default async function createMetadataInfo(apiJsonsRoot: string, sapui5Ver
};

forEachSymbol(semanticModel, (symbol, symbolName) => {
const defaultAggregation = metadataProvider.getDefaultAggregationForSymbol(symbol);
if (defaultAggregation) {
apiExtract.defaultAggregations[symbolName] = defaultAggregation;
apiExtract.metadata[symbolName] = {};

// Generate metadata:
if (isAllowedSymbolKind(symbol.kind)) {
const symbolOptionValues = metadataProvider.collectOptionValuesForSymbol(symbol);
if (symbolOptionValues) {
Object.entries(symbolOptionValues).forEach(([optionName, optionValue]) => {
apiExtract.metadata[symbolName][optionName] = optionValue;
});
}
}

// deprecations:
if (symbol.deprecatedInfo?.isDeprecated) {
const deprecationText = getDeprecationText(symbol.deprecatedInfo) ?? "deprecated";
if (isAllowedSymbolKind(symbol.kind)) {
apiExtract.deprecations[symbol.kind] = apiExtract.deprecations[symbol.kind] ?? {};
apiExtract.deprecations[symbol.kind][symbolName] = deprecationText;
}
}

if (hasFieldsProperty(symbol)) {
symbol.fields?.forEach((field) => {
if (field?.deprecatedInfo?.isDeprecated) {
Expand Down
2 changes: 1 addition & 1 deletion scripts/metadataProvider/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export async function createSemanticModel(apiJsonsRoot: string, sapui5Version: s
/* eslint-disable no-console */
let originalConsoleError;
if (!isLogLevelEnabled("verbose")) {
// Overwrite console.error with a noop since #generate produces a lot of messages error messages
// Overwrite console.error with a noop since #generate produces a lot of error messages
// that we don't want to deal with right now
originalConsoleError = console.error;
// eslint-disable-next-line @typescript-eslint/no-empty-function
Expand Down
7 changes: 5 additions & 2 deletions src/utils/ApiExtract.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import {readFile} from "node:fs/promises";

export type AllowedSymbolKind = "UI5Class" | "UI5Enum" | "UI5Interface" | "UI5Namespace" | "UI5Typedef" | "UI5Function";
export type AllowedSymbolOption = "aggregation" | "association" | "defaultAggregation" | "event" | "property";

export interface ApiExtractJson {
framework: {
name: string;
version: string;
};
defaultAggregations: Record<string, string>;
metadata: Record<string, Record<string, AllowedSymbolOption>>;
deprecations: Record<AllowedSymbolKind, Record<string, string>>;
}

Expand All @@ -28,8 +29,10 @@
this.data = data;
}

getDefaultAggregation(className: string): string {

Check failure on line 32 in src/utils/ApiExtract.ts

View workflow job for this annotation

GitHub Actions / General checks, tests and coverage reporting

'className' is defined but never used. Allowed unused args must match /^_/u
return this.data.defaultAggregations[className];
// return this.data.defaultAggregations[className];
// TODO: Implement this method
return "";
}

getDeprecationInfo(symbolName: string): DeprecationInfo | undefined {
Expand Down
Loading