Skip to content

Commit

Permalink
Support annotating of anonymous declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
sgrishchenko committed Jun 28, 2024
1 parent 5695845 commit ebc80a2
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
6 changes: 5 additions & 1 deletion src/converter/annotation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import {ConverterContext} from "./context.js";
import {Node} from "typescript"

export interface AnnotationContext extends ConverterContext {
isAnonymousDeclaration: boolean
}

export type Annotation<TNode extends Node = Node> =
(node: TNode, context: ConverterContext) => string | null
(node: TNode, context: AnnotationContext) => string | null
17 changes: 15 additions & 2 deletions src/converter/plugins/AnnotationPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {ConverterPlugin} from "../plugin.js";
import ts, {Node} from "typescript";
import {ConverterContext} from "../context.js";
import {Render} from "../render.js";
import {Annotation} from "../annotation.js";
import {Annotation, AnnotationContext} from "../annotation.js";
import {GeneratedFile} from "../generated.js";

export const annotationServiceKey = Symbol()
Expand All @@ -11,11 +11,24 @@ export class AnnotationService {
constructor(readonly annotations: Annotation[]) {
}

resolveAnonymousAnnotations(node: Node, context: ConverterContext): string[] {
return this.internalResolveAnnotations(node, true, context)
}

resolveAnnotations(node: Node, context: ConverterContext): string[] {
return this.internalResolveAnnotations(node, false, context)
}

private internalResolveAnnotations(node: Node, isAnonymousDeclaration: boolean, context: ConverterContext) {
const annotationContext: AnnotationContext = {
...context,
isAnonymousDeclaration,
}

const annotations: string[] = []

for (const annotation of this.annotations) {
const result = annotation(node, context)
const result = annotation(node, annotationContext)

if (result !== null) annotations.push(result)
}
Expand Down
7 changes: 6 additions & 1 deletion src/converter/plugins/AnonymousDeclarationPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {generateDerivedDeclarations} from "../../structure/derived/generateDeriv
import {DerivedDeclaration} from "../../structure/derived/derivedDeclaration.js";
import {TypeScriptService, typeScriptServiceKey} from "./TypeScriptPlugin.js";
import {DerivedFile} from "../generated.js";
import {AnnotationService, annotationServiceKey} from "./AnnotationPlugin.js";

export interface AnonymousDeclarationContext extends ConverterContext {
resolveName(node: Node): string
Expand Down Expand Up @@ -45,7 +46,11 @@ class AnonymousDeclarationPlugin<TNode extends Node = Node> implements Converter
const typeScriptService = context.lookupService<TypeScriptService>(typeScriptServiceKey)
if (typeScriptService === undefined) throw new Error("AnonymousDeclarationPlugin can't work without TypeScriptService")

const annotationService = context.lookupService<AnnotationService>(annotationServiceKey)
if (annotationService === undefined) throw new Error("AnonymousDeclarationPlugin can't work without AnnotationService")

const resolveName = (node: TNode) => nameResolverService.resolveName(node, context)
const annotations = annotationService.resolveAnonymousAnnotations(node, context)

const anonymousDeclarationContext = {
...context,
Expand All @@ -69,7 +74,7 @@ class AnonymousDeclarationPlugin<TNode extends Node = Node> implements Converter
sourceFileName,
namespace,
fileName: `${name}.kt`,
body: declaration,
body: [...annotations, declaration].join("\n"),
})

return reference;
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export {InjectionType} from "./converter/injection.js"
export {createSimpleInjection} from "./converter/injection.js"

// annotation
export type {Annotation} from "./converter/annotation.js"
export type {Annotation, AnnotationContext} from "./converter/annotation.js"

// name resolver
export type {NameResolver} from "./converter/nameResolver.js"
Expand Down

0 comments on commit ebc80a2

Please sign in to comment.