Skip to content

Commit

Permalink
Support comments for top-level variables
Browse files Browse the repository at this point in the history
  • Loading branch information
sgrishchenko committed Sep 15, 2024
1 parent 8b853bc commit 4df0325
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/converter/plugins/convertVariableDeclaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import {createSimplePlugin} from "../plugin.js";
import {CheckCoverageService, checkCoverageServiceKey} from "./CheckCoveragePlugin.js";
import {TypeScriptService, typeScriptServiceKey} from "./TypeScriptPlugin.js";
import {NamespaceInfoService, namespaceInfoServiceKey} from "./NamespaceInfoPlugin.js";
import {CommentService, commentServiceKey} from "./CommentPlugin.js";
import {ConfigurationService, configurationServiceKey} from "./ConfigurationPlugin.js";

export const convertVariableDeclaration = createSimplePlugin((node, context, render) => {
if (!ts.isVariableDeclaration(node)) return null
Expand All @@ -13,6 +15,8 @@ export const convertVariableDeclaration = createSimplePlugin((node, context, ren
// skip initializer
node.initializer && checkCoverageService?.cover(node.initializer)

const commentService = context.lookupService<CommentService>(commentServiceKey)
const configurationService = context.lookupService<ConfigurationService>(configurationServiceKey)
const typeScriptService = context.lookupService<TypeScriptService>(typeScriptServiceKey)
const namespaceInfoService = context.lookupService<NamespaceInfoService>(namespaceInfoServiceKey)

Expand All @@ -30,6 +34,18 @@ export const convertVariableDeclaration = createSimplePlugin((node, context, ren
externalModifier = ""
}

let leadingComment = ""

if (
configurationService?.configuration.granularity === "top-level"
&& (
namespace === undefined
|| namespaceInfoService?.resolveNamespaceStrategy(namespace) === "package"
)
) {
leadingComment = commentService?.renderLeadingComments(node.parent) ?? ""
}

let type: string

if (node.type) {
Expand All @@ -38,5 +54,5 @@ export const convertVariableDeclaration = createSimplePlugin((node, context, ren
type = "Any? /* should be inferred */" // TODO: infer types
}

return `${externalModifier}${modifier}${name}: ${type}`
return `${leadingComment}${externalModifier}${modifier}${name}: ${type}`
})
11 changes: 11 additions & 0 deletions test/functional/base/generated/variable/simple.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Generated by Karakum - do not modify it manually!

@file:JsModule("sandbox-base/variable/simple")
@file:Suppress(
"NON_EXTERNAL_DECLARATION_IN_INAPPROPRIATE_FILE",
)

package sandbox.base.variable

/** my favorite constant */
external val myConstWithComment: Double
2 changes: 2 additions & 0 deletions test/functional/base/lib/variable/simple.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/** my favorite constant */
const myConstWithComment: number
4 changes: 4 additions & 0 deletions test/functional/namespace/generated/index.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ package sandbox.namespace

external object ObjectNamespace {
val objectNamespaceValue: Double
/** my favorite constant */
val objectNamespaceValueWithComment: Double
sealed interface objectNamespaceUnion {
companion object {
@seskar.js.JsValue("0")
Expand All @@ -25,3 +27,5 @@ val innerObjectNamespaceValue: Double
}

external val topLevelValue: Double
/** my favorite constant */
external val topLevelValueWithComment: Double
6 changes: 6 additions & 0 deletions test/functional/namespace/lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ declare module "package-namespace" {
declare namespace ObjectNamespace {
const objectNamespaceValue: number

/** my favorite constant */
const objectNamespaceValueWithComment: number

type objectNamespaceUnion = "0" | "1";

interface ObjectNamespaceInterface {
Expand All @@ -33,6 +36,9 @@ declare namespace ObjectNamespace {

declare namespace IgnoreNamespace {
const topLevelValue: number

/** my favorite constant */
const topLevelValueWithComment: number
}

declare module "will-be-mapped" {
Expand Down
8 changes: 8 additions & 0 deletions test/functional/top-level/generated/myConstWithComment.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Generated by Karakum - do not modify it manually!

@file:JsModule("sandbox-top-level")

package sandbox.top.level

/** my favorite constant */
external val myConstWithComment: Double
3 changes: 3 additions & 0 deletions test/functional/top-level/lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ const myConst1: string

const myConst2: number

/** my favorite constant */
const myConstWithComment: number

function myFunction1(firstParam: string, secondParam: number): void

function myFunction1(firstParam: boolean, secondParam: number): void
Expand Down

0 comments on commit 4df0325

Please sign in to comment.