Skip to content

Commit

Permalink
Return structured data from extractTypeParameters to allow some pos…
Browse files Browse the repository at this point in the history
…t-processing
  • Loading branch information
sgrishchenko committed Mar 8, 2024
1 parent 7f20f68 commit 52edc2c
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 26 deletions.
25 changes: 11 additions & 14 deletions src/converter/extractTypeParameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,15 @@ import {TypeScriptService, typeScriptServiceKey} from "./plugins/TypeScriptPlugi
import {ConverterContext} from "./context.js";
import {Render} from "./render.js";

export interface TypeParameterExtractionResult {
declaration: string
reference: string
}
export type TypeParameterExtractionResult = [ts.Node, Declaration][]

export function extractTypeParameters(
node: ts.Node,
context: ConverterContext,
render: Render
context: ConverterContext
): TypeParameterExtractionResult {
const typeScriptService = context.lookupService<TypeScriptService>(typeScriptServiceKey)

const result: [ts.Node, Declaration][] = []
const result: TypeParameterExtractionResult = []

const typeChecker = typeScriptService?.program.getTypeChecker()

Expand Down Expand Up @@ -51,18 +47,19 @@ export function extractTypeParameters(
}
})

const declaration = result
return result
}

export function renderDeclaration(result: TypeParameterExtractionResult, render: Render): string {
return result
.map(([, declaration]) => render(declaration))
.filter(Boolean)
.join(", ")
}

const reference = result
export function renderReference(result: TypeParameterExtractionResult, render: Render): string {
return result
.map(([node]) => render(node))
.filter(Boolean)
.join(", ")

return {
declaration,
reference,
}
}
8 changes: 4 additions & 4 deletions src/converter/plugins/InheritedTypeLiteralPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {ifPresent, Render} from "../render.js";
import {CheckCoverageService, checkCoverageServiceKey} from "./CheckCoveragePlugin.js";
import {InheritanceModifierService, inheritanceModifierServiceKey} from "./InheritanceModifierPlugin.js";
import {createAnonymousDeclarationPlugin} from "./AnonymousDeclarationPlugin.js";
import {extractTypeParameters} from "../extractTypeParameters.js";
import {extractTypeParameters, renderDeclaration, renderReference} from "../extractTypeParameters.js";
import {ConverterContext} from "../context.js";
import {InjectionService, injectionServiceKey} from "./InjectionPlugin.js";
import {convertMappedTypeBody} from "./MappedTypePlugin.js";
Expand Down Expand Up @@ -73,11 +73,11 @@ export const inheritedTypeLiteralPlugin = createAnonymousDeclarationPlugin(

const name = context.resolveName(node)

const typeParameters = extractTypeParameters(node, context, render)
const typeParameters = extractTypeParameters(node, context)

const declaration = convertInheritedTypeLiteral(node, name, typeParameters.declaration, context, render)
const declaration = convertInheritedTypeLiteral(node, name, renderDeclaration(typeParameters, render), context, render)

const reference = `${name}${ifPresent(typeParameters.reference, it => `<${it}>`)}`
const reference = `${name}${ifPresent(renderReference(typeParameters, render), it => `<${it}>`)}`

return {name, declaration, reference};
}
Expand Down
8 changes: 4 additions & 4 deletions src/converter/plugins/MappedTypePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {CheckCoverageService, checkCoverageServiceKey} from "./CheckCoveragePlug
import {ifPresent, Render, renderNullable} from "../render.js";
import {InheritanceModifierService, inheritanceModifierServiceKey} from "./InheritanceModifierPlugin.js";
import {createAnonymousDeclarationPlugin} from "./AnonymousDeclarationPlugin.js";
import {extractTypeParameters} from "../extractTypeParameters.js";
import {extractTypeParameters, renderDeclaration, renderReference} from "../extractTypeParameters.js";
import {ConverterContext} from "../context.js";
import {InjectionService, injectionServiceKey} from "./InjectionPlugin.js";

Expand Down Expand Up @@ -75,11 +75,11 @@ export const mappedTypePlugin = createAnonymousDeclarationPlugin(

const name = context.resolveName(node)

const typeParameters = extractTypeParameters(node, context, render)
const typeParameters = extractTypeParameters(node, context)

const declaration = convertMappedType(node, name, typeParameters.declaration, context, render)
const declaration = convertMappedType(node, name, renderDeclaration(typeParameters, render), context, render)

const reference = `${name}${ifPresent(typeParameters.reference, it => `<${it}>`)}`
const reference = `${name}${ifPresent(renderReference(typeParameters, render), it => `<${it}>`)}`

return {name, declaration, reference};
}
Expand Down
8 changes: 4 additions & 4 deletions src/converter/plugins/TypeLiteralPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {ifPresent, Render} from "../render.js";
import {CheckCoverageService, checkCoverageServiceKey} from "./CheckCoveragePlugin.js";
import {InheritanceModifierService, inheritanceModifierServiceKey} from "./InheritanceModifierPlugin.js";
import {createAnonymousDeclarationPlugin} from "./AnonymousDeclarationPlugin.js";
import {extractTypeParameters} from "../extractTypeParameters.js";
import {extractTypeParameters, renderDeclaration, renderReference} from "../extractTypeParameters.js";
import {ConverterContext} from "../context.js";
import {InjectionService, injectionServiceKey} from "./InjectionPlugin.js";

Expand Down Expand Up @@ -50,11 +50,11 @@ export const typeLiteralPlugin = createAnonymousDeclarationPlugin(

const name = context.resolveName(node)

const typeParameters = extractTypeParameters(node, context, render)
const typeParameters = extractTypeParameters(node, context)

const declaration = convertTypeLiteral(node, name, typeParameters.declaration, context, render)
const declaration = convertTypeLiteral(node, name, renderDeclaration(typeParameters, render), context, render)

const reference = `${name}${ifPresent(typeParameters.reference, it => `<${it}>`)}`
const reference = `${name}${ifPresent(renderReference(typeParameters, render), it => `<${it}>`)}`

return {name, declaration, reference};
}
Expand Down
7 changes: 7 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ export {
convertParameterDeclarations
} from "./converter/plugins/convertParameterDeclaration.js"

export {
type TypeParameterExtractionResult,
extractTypeParameters,
renderDeclaration,
renderReference,
} from "./converter/extractTypeParameters.js"

// render
export type {Render} from "./converter/render.js"
export {ifPresent, renderNullable} from "./converter/render.js"
Expand Down

0 comments on commit 52edc2c

Please sign in to comment.