Skip to content

Commit

Permalink
fix: fixed generator and parser to use some of the types directly (#104)
Browse files Browse the repository at this point in the history
* fix: fixed component parsing and generation

* fix: using only public methods

* fix: updated snapshots

* chore: updated snapshots
  • Loading branch information
g-cheishvili authored Dec 6, 2023
1 parent 4125275 commit 2cfdd57
Show file tree
Hide file tree
Showing 8 changed files with 679 additions and 359 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,6 @@ export function ComponentWrapperCreator(
return 'HTMLElement';
})();

const methodsTypeStr = componentFile.componentData.methods.map((method) => {
const parameters = method.parameters.map((parameter) => {
return `${parameter.name}: ${parameter.type}`;
}).join(', ');
return `
${method.name}(${parameters}): ${method.returnValue};
`;
}).join('\n');

const elementType = `
${outputs.length > 0 ? eventsMap : ''}
Expand All @@ -172,8 +163,6 @@ export function ComponentWrapperCreator(
${analyzedSlots.map(([name, type]) => `${name}: ${type};`).join('\n')}
${outputs.length > 0 ? outputsTypeStr : ''}
${methodsTypeStr}
}
`;
return `
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ function getComponentFile(componentData: ComponentData, options: AngularGenerato
} catch (e) {
componentFile = new ComponentFile(componentData, options, cache);
}
if (componentData.imports.length > 0) {
componentData.imports.forEach(i => {
componentFile.addImport(i);
});
}
cache.set(componentData, componentFile);
return componentFile;
}
Expand Down
3 changes: 2 additions & 1 deletion libs/api-json-parser/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ComponentData } from "@ui5/webcomponents-wrapper";
* Parses the api.json files and returns the list of the component data
* @param configuration
*/
export default async function (configuration: ParserConfiguration): Promise<ComponentData[]> {
async function parser(configuration: ParserConfiguration): Promise<ComponentData[]> {
const {implementers, symbols} = await getImplementersAndSymbols(configuration);
return getComponents({
implementers,
Expand All @@ -20,3 +20,4 @@ export default async function (configuration: ParserConfiguration): Promise<Comp
}, [])
});
}
export default parser;
63 changes: 37 additions & 26 deletions libs/api-json-parser/src/lib/get-components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,41 @@ export function getComponents({
}
const mappedType = typesMap[type.toLowerCase()];
if (!mappedType && symbols[type]) {
if (symbols[type].kind === 'class' || symbols[type].kind === 'enum') {
let types = symbols[type].properties.map(p => JSON.stringify(p.type));
types = types.length ? types : ['any'];
return types.join(' | ');
}
if (symbols[type].kind === 'interface') {
const interfaceImplementers = implementers[type].map(getComponentData).filter(Boolean) as ComponentData[];
if (interfaceImplementers.length > 0) {
component.dependencies.push(...interfaceImplementers);
return interfaceImplementers;
} else {
return 'any';
const symbol = symbols[type];

if (symbol.kind) {
if (symbol.kind === 'interface') {
const interfaceImplementers = implementers[type].map(getComponentData).filter(Boolean) as ComponentData[];
if (interfaceImplementers.length > 0) {
interfaceImplementers.forEach(impl => {
component.imports.push({
specifiers: [
{
local: impl.baseName,
imported: 'default'
}
],
path: impl.path,
})
})
return interfaceImplementers.map(({baseName}) => baseName).join(' | ');
} else {
return 'any';
}
}
if (symbol.kind === 'enum') {
return symbol.properties.map(p => `'${p.name}'`).join(' | ');
}
} else {
console.warn(`Could not map type "${type}" for ${tagname} ${identifier} in ${component.baseName} component`);
component.imports.push({
specifiers: [
{
local: symbol.basename,
imported: 'default'
}
],
path: symbol.resource.replace(/.js$/, ''),
});
return symbol.basename;
}
}
if (typeof mappedType === 'function') {
Expand Down Expand Up @@ -154,19 +174,9 @@ export function getComponents({
}

function getMethods(symbol: SymbolObject, componentData: ComponentData): ComponentData['methods'] {
return symbol.methods.map((method) => {
const parameters: ParameterType[] = (method.parameters || []).map(parameter => {
return {
name: parameter.name,
type: parameter.type.split('|').map(t => getPropertyType(t.trim(), symbol.tagname, method.name, componentData) as string).join(' | '),
}
});
const returnValue = method.returnValue ? getPropertyType(method.returnValue.type, symbol.tagname, method.name, componentData) as string : 'any';
return symbol.methods.filter(({visibility}) => visibility === 'public').map((method) => {
return {
description: method.description,
name: method.name,
parameters,
returnValue,
name: method.name
}
});
}
Expand All @@ -179,6 +189,7 @@ export function getComponents({
description: symbol.description,
baseName: symbol.basename,
dependencies,
imports: [],
implements: symbol.implements,
selector: symbol.tagname,
path: symbol.resource,
Expand Down
9 changes: 5 additions & 4 deletions libs/api-json-parser/src/lib/symbol-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,13 @@ export interface Event {
}

export interface Method {
name: string,
visibility: string;
name: string;
returnValue?: {
type: string
},
parameters: Parameter[],
description: string
};
parameters: Parameter[];
description: string;
}


Expand Down
Loading

0 comments on commit 2cfdd57

Please sign in to comment.