Skip to content

Commit

Permalink
compiler,nodeJs: initial refactor for d.ts generation
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Giannini authored and Matthew Giannini committed Mar 28, 2024
1 parent ac735af commit cf2a877
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 25 deletions.
30 changes: 26 additions & 4 deletions src/compiler/fan/steps/CompileJs.fan
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@
class CompileJs : CompilerStep
{

new make(Compiler compiler) : super(compiler) {}
new make(Compiler compiler) : super(compiler)
{
this.hasJs = compiler.types.any { it.hasFacet("sys::Js") }
}

** Is any type annotated with @Js
private const Bool hasJs

override Void run()
{
Expand All @@ -29,8 +35,8 @@ class CompileJs : CompilerStep
compile("compilerEs::CompileEsPlugin")
}

// generate d.ts files when forcing js
if (compiler.input.forceJs || pod.name == "sys") compile("nodeJs::CompileTsPlugin")
// generate d.ts files
genTsDecl
}

private Void compile(Str qname)
Expand All @@ -47,6 +53,22 @@ class CompileJs : CompilerStep
t.make([compiler])->run
}

private Void genTsDecl()
{
// find the tool to generate d.ts
t := Type.find("nodeJs::GenTsDecl", false)
if (t == null)
{
log.info("WARN: GenTsDecl not available")
return
}

// run it
buf := Buf()
t.make([buf.out, pod, compiler.input.forceJs || compiler.isSys])->run
compiler.tsDecl = buf.seek(0).readAllStr
}

Bool needCompileJs()
{
// in JS mode we force JS compilation
Expand All @@ -62,7 +84,7 @@ class CompileJs : CompilerStep
if (compiler.jsPropsFiles != null && !compiler.jsPropsFiles.isEmpty) return true

// run JS compiler if any type has @Js facet
return compiler.types.any { it.hasFacet("sys::Js") }
return this.hasJs
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,29 @@ using fandoc
**
** Generate TypeScript declaration file for a pod
**
class CompileTsPlugin : CompilerStep
class GenTsDecl
{
new make(Compiler compiler) : super(compiler)
new make(OutStream out, CPod pod, Bool allTypes := false)
{
this.c = compiler
docParser = FandocParser()
this.out = out
this.pod = pod
this.allTypes = allTypes
this.docParser = FandocParser()
this.docWriter = TsDocWriter(out)
}

private Compiler c
private OutStream? out
private OutStream out
private CPod pod
private const Bool allTypes
private FandocParser docParser
private TsDocWriter? docWriter
private TsDocWriter docWriter

//////////////////////////////////////////////////////////////////////////
// Main writing method
//////////////////////////////////////////////////////////////////////////

override Void run()
Void run()
{
buf := Buf()
out = buf.out
docWriter = TsDocWriter(out)

// Write dependencies
deps := pod.depends.map |CDepend dep->Str| { dep.name }
deps.each |dep|
Expand All @@ -47,13 +47,16 @@ class CompileTsPlugin : CompilerStep
out.write('\n')

// Write declaration for each type
pod.typeDefs.findAll { !it.isSynthetic }.each |type|
pod.types.findAll { !it.isSynthetic }.each |type|
{
// TODO: for now generate declaration for all types regardless of whether
// they have the @Js facet or not
// if (!type.hasFacet(jsFacet)) return
// if we aren't generating all types, short-circuit if missing @Js facet
if (!allTypes && !type.hasFacet("sys::Js")) return

// skip internal types
if (type.isInternal) return

// TODO: skip @NoDoc???

isList := false
isMap := false

Expand Down Expand Up @@ -84,7 +87,7 @@ class CompileTsPlugin : CompilerStep
else if (isList) extends += "implements Iterable<V> "

// Write class documentation & header
printDoc(type.doc, 0)
// printDoc(type, 0)
out.print("export ${abstr}class $type.name$classParams $extends{\n")

hasItBlockCtor := type.ctors.any |CMethod m->Bool| {
Expand Down Expand Up @@ -169,9 +172,6 @@ class CompileTsPlugin : CompilerStep
out.print("}\n")
}
if (pod.name == "sys") printObjUtil

buf.seek(0)
c.tsDecl = buf.readAllStr
}

//////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -212,7 +212,7 @@ class CompileTsPlugin : CompilerStep
case "R": return "R"
default: return "unknown"
}

// List/map types
if (type.isList || type.isMap)
{
Expand Down

0 comments on commit cf2a877

Please sign in to comment.