Skip to content

Commit

Permalink
Modify generate-type-predicates to only include interfaces that have …
Browse files Browse the repository at this point in the history
…the correct type
  • Loading branch information
Roaders committed Nov 21, 2024
1 parent 2514768 commit 339477c
Show file tree
Hide file tree
Showing 2 changed files with 1,044 additions and 9 deletions.
75 changes: 66 additions & 9 deletions packages/fdc3-schema/code-generation/generate-type-predicates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,47 @@ matchedInterfaces.forEach(matched => {
writeTypeConstant(matched.matchingInterface)
});

writeUnionType("RequestMessage", interfaces, "Request");
writeUnionType("ResponseMessage", interfaces, "Response");
writeUnionType("EventMessage", interfaces, "Event");
const typeAliases = sourceFile.getChildrenOfKind(SyntaxKind.TypeAliasDeclaration);

/**
* Looks for a string union type in the form:
* export type NAME = "stringOne" | "stringTwo" | "stringThree";
* and returns the string values
* if the union type is not found returns undefined
* @param name
* @returns
*/
function findUnionType(name: string): string[] | undefined {
const typeAlias = typeAliases.find(alias => {
const identifiers = alias.getChildrenOfKind(SyntaxKind.Identifier);

return identifiers[0].getText() === name;

});

return typeAlias?.getChildrenOfKind(SyntaxKind.UnionType)?.[0]
.getDescendantsOfKind(SyntaxKind.StringLiteral)
.map(literal => literal.getLiteralText());
}

// get the types listed in the types union type
// i.e. look for: export type RequestMessageType = "addContextListenerRequest" | "whatever"
const requestMessageUnion = findUnionType("RequestMessageType");
if(requestMessageUnion != null){
// Write a union type of all interfaces that have a type that extends RequestMessageType
writeUnionType("RequestMessage", interfaces, requestMessageUnion);
}

const responseMessageUnion = findUnionType("ResponseMessageType");
if(responseMessageUnion != null){
writeUnionType("ResponseMessage", interfaces, responseMessageUnion);
}

const eventMessageUnion = findUnionType("EventMessageType");
if(eventMessageUnion != null){
writeUnionType("EventMessage", interfaces, eventMessageUnion);
}


function writePredicate(matchingInterface: InterfaceDeclaration, func: MethodDeclaration): void {
const predicateName = `is${matchingInterface.getName()}`;
Expand All @@ -59,14 +97,33 @@ function writeTypeConstant(matchingInterface: InterfaceDeclaration): void {

}


function writeUnionType(unionName: string, interfaces: InterfaceDeclaration[], nameEndsWith: string): void {
const matchingInterfaces = interfaces
.map(currentInterface => currentInterface.getName())
.filter(interfaceName => interfaceName.length > nameEndsWith.length && interfaceName.indexOf(nameEndsWith) === interfaceName.length - nameEndsWith.length);
/**
* Writes a union type of all the interfaces that have a type property that extends the type values passed in.
* For example:
* export type RequestMessage = AddContextListenerRequest | AddEventListenerRequest ...
* @param unionName
* @param interfaces
* @param typeValues
*/
function writeUnionType(unionName: string, interfaces: InterfaceDeclaration[], typeValues: string[]): void {
// look for interfaces that have a type property that extends one of the values in typeValues
const matchingInterfaces = interfaces.filter(currentInterface => {
const typeProperty = currentInterface.getChildrenOfKind(SyntaxKind.PropertySignature).filter(propertySignature => {
return propertySignature.getChildrenOfKind(SyntaxKind.Identifier).find(identifier => identifier.getText() === "type") != null;
})[0];

if(typeProperty == null){
return false;
}

const stringLiterals = typeProperty.getDescendantsOfKind(SyntaxKind.StringLiteral)
.map(literal => literal.getLiteralText());

return stringLiterals.some(literal => typeValues.some(typeValue => typeValue === literal));
})

sourceFile.addStatements(`
export type ${unionName} = ${matchingInterfaces.join(" | ")}; `);
export type ${unionName} = ${matchingInterfaces.map(match => match.getName()).join(" | ")}; `);
}

sourceFile.formatText();
Expand Down
Loading

0 comments on commit 339477c

Please sign in to comment.