Skip to content

Commit

Permalink
Handle variables as top-level declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
sgrishchenko committed Sep 8, 2024
1 parent eedeabe commit 849dd54
Show file tree
Hide file tree
Showing 22 changed files with 161 additions and 74 deletions.
2 changes: 1 addition & 1 deletion src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ export async function generate(partialConfiguration: PartialConfiguration) {

const targetFileName = path.resolve(output, outputFileName)

const body = item.statements
const body = item.nodes
.map(node => render(node))
.join("\n\n")

Expand Down
8 changes: 4 additions & 4 deletions src/structure/namespace/collectNamespaceInfo.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import ts, {SourceFile, Statement} from "typescript";
import ts, {SourceFile, Node} from "typescript";
import {Configuration} from "../../configuration/configuration.js";
import {traverse} from "../../utils/traverse.js";
import {createNamespaceInfoItem, NamespaceInfoItem} from "./createNamespaceInfoItem.js";
Expand Down Expand Up @@ -29,15 +29,15 @@ export function collectNamespaceInfo(
configuration,
)

let statements: Statement[] = []
let nodes: Node[] = []

if (node.body && ts.isModuleBlock(node.body)) {
statements = Array.from(node.body.statements)
nodes = Array.from(node.body.statements)
}

result.push({
...item,
statements,
nodes,
meta: {
type: "Namespace",
name: item.name
Expand Down
86 changes: 56 additions & 30 deletions src/structure/prepareStructure.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import ts, {Node} from "typescript";
import ts, {Node, Declaration} from "typescript";
import {Configuration} from "../configuration/configuration.js";
import {InputStructureItem} from "./structure.js";
import {applyPackageNameMapper} from "./package/applyPackageNameMapper.js";
Expand All @@ -7,20 +7,22 @@ import {createBundleInfoItem} from "./bundle/createBundleInfoItem.js";

interface TopLevelMatch {
name: string,
node: Declaration,
hasRuntime: boolean,
}

type TopLevelMatcher = (node: Node) => TopLevelMatch | null
type TopLevelMatcher = (node: Node) => TopLevelMatch[] | null

const classMatcher: TopLevelMatcher = node => {
if (ts.isClassDeclaration(node)) {
const name = node.name?.text
if (!name) return null

return {
return [{
name,
node,
hasRuntime: true,
}
}]
}

return null
Expand All @@ -30,10 +32,11 @@ const interfaceMatcher: TopLevelMatcher = node => {
if (ts.isInterfaceDeclaration(node)) {
const name = node.name.text;

return {
return [{
name,
node,
hasRuntime: false,
}
}]
}

return null
Expand All @@ -43,10 +46,11 @@ const typeAliasMatcher: TopLevelMatcher = node => {
if (ts.isTypeAliasDeclaration(node)) {
const name = node.name.text;

return {
return [{
name,
node,
hasRuntime: false,
}
}]
}

return null
Expand All @@ -56,10 +60,11 @@ const enumMatcher: TopLevelMatcher = node => {
if (ts.isEnumDeclaration(node)) {
const name = node.name.text;

return {
return [{
name,
node,
hasRuntime: true,
}
}]
}

return null
Expand All @@ -70,10 +75,30 @@ const functionMatcher: TopLevelMatcher = node => {
const name = node.name?.text
if (!name) return null

return {
return [{
name,
node,
hasRuntime: true,
}
}]
}

return null
}

const variableMatcher: TopLevelMatcher = node => {
if (ts.isVariableStatement(node)) {
return node.declarationList.declarations
.map(declaration => {
const nameNode = declaration.name
if (!ts.isIdentifier(nameNode)) return null

return {
name: nameNode.text,
node: declaration as Node,
hasRuntime: true,
}
})
.filter((match): match is TopLevelMatch => match != null)
}

return null
Expand All @@ -85,6 +110,7 @@ const topLevelMatchers: TopLevelMatcher[] = [
typeAliasMatcher,
enumMatcher,
functionMatcher,
variableMatcher,
]

const topLevelMatcher: TopLevelMatcher = node => {
Expand All @@ -111,7 +137,7 @@ function applyGranularity(

return [{
...bundleItem,
statements: items.flatMap(item => item.statements),
nodes: items.flatMap(item => item.nodes),
meta: {
type: "Bundle",
name: items
Expand All @@ -125,24 +151,24 @@ function applyGranularity(
return items.flatMap(item => {
const result: InputStructureItem[] = []

for (const statement of item.statements) {
let fileName = item.fileName
let hasRuntime = item.hasRuntime

const topLevelMatch = topLevelMatcher(statement)

if (topLevelMatch !== null) {
fileName = `${topLevelMatch.name}.kt`
hasRuntime = topLevelMatch.hasRuntime
for (const node of item.nodes) {
const topLevelMatches = topLevelMatcher(node)

if (topLevelMatches !== null) {
for (const topLevelMatch of topLevelMatches) {
result.push({
...item,
fileName: `${topLevelMatch.name}.kt`,
hasRuntime: topLevelMatch.hasRuntime,
nodes: [topLevelMatch.node],
})
}
} else {
result.push({
...item,
nodes: [node],
})
}

result.push({
...item,

fileName,
hasRuntime,
statements: [statement],
})
}

return result
Expand Down
4 changes: 2 additions & 2 deletions src/structure/sourceFile/collectSourceFileInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ export function collectSourceFileInfo(
configuration,
)

const statements = sourceFile.statements
const nodes = sourceFile.statements

return {
...item,
statements,
nodes,
meta: {
type: "Source File",
name: sourceFile.fileName
Expand Down
4 changes: 2 additions & 2 deletions src/structure/structure.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Statement} from "typescript";
import {Node} from "typescript";

export interface StructureItem {
fileName: string
Expand All @@ -10,7 +10,7 @@ export interface StructureItem {
}

export interface InputStructureItem extends StructureItem {
statements: ReadonlyArray<Statement>
nodes: ReadonlyArray<Node>
meta: {
type: string
name: string
Expand Down
10 changes: 10 additions & 0 deletions test/functional/namespace/generated/import/consumer/ignored.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Generated by Karakum - do not modify it manually!

@file:JsModule("import-consumer")

package import.consumer

// unhandled import: Ignored from "ignored-import"
// unhandled import: other as OtherIgnored from "ignored-import"

external val ignored: Ignored
11 changes: 11 additions & 0 deletions test/functional/namespace/generated/import/consumer/myX.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("import-consumer")

package import.consumer

import other.import.provider.X as MyX
// unhandled import: Ignored from "ignored-import"
// unhandled import: other as OtherIgnored from "ignored-import"

external val myX: MyX
10 changes: 10 additions & 0 deletions test/functional/namespace/generated/import/consumer/myXyz.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Generated by Karakum - do not modify it manually!

@file:JsModule("import-consumer")

package import.consumer

// unhandled import: Ignored from "ignored-import"
// unhandled import: other as OtherIgnored from "ignored-import"

external val myXyz: /* import("other-import-provider") */ Any
11 changes: 11 additions & 0 deletions test/functional/namespace/generated/import/consumer/myY.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("import-consumer")

package import.consumer

import other.import.provider.Y as MyY
// unhandled import: Ignored from "ignored-import"
// unhandled import: other as OtherIgnored from "ignored-import"

external val myY: MyY
30 changes: 1 addition & 29 deletions test/functional/namespace/generated/import/consumer/namespace.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,5 @@

package import.consumer

import import.provider.SomeValue
import other.import.provider.X
import other.import.provider.Y as MyY
import other.import.provider.X as MyX
// unhandled import: Ignored from "ignored-import"
// unhandled import: other as OtherIgnored from "ignored-import"











external val someValue: SomeValue

external val x: X

external val myX: MyX

external val myY: MyY

external val myXyz: /* import("other-import-provider") */ Any

external val ignored: Ignored

external val otherIgnored: OtherIgnored
// unhandled import: other as OtherIgnored from "ignored-import"
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Generated by Karakum - do not modify it manually!

@file:JsModule("import-consumer")

package import.consumer

// unhandled import: Ignored from "ignored-import"
// unhandled import: other as OtherIgnored from "ignored-import"

external val otherIgnored: OtherIgnored
11 changes: 11 additions & 0 deletions test/functional/namespace/generated/import/consumer/someValue.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("import-consumer")

package import.consumer

import import.provider.SomeValue
// unhandled import: Ignored from "ignored-import"
// unhandled import: other as OtherIgnored from "ignored-import"

external val someValue: SomeValue
11 changes: 11 additions & 0 deletions test/functional/namespace/generated/import/consumer/x.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("import-consumer")

package import.consumer

import other.import.provider.X
// unhandled import: Ignored from "ignored-import"
// unhandled import: other as OtherIgnored from "ignored-import"

external val x: X
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

package `package`.namespace

external val packageNamespaceValue: Double



external object ObjectNamespace {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Generated by Karakum - do not modify it manually!

@file:JsModule("package-namespace")

package `package`.namespace

external val packageNamespaceValue: Double
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Generated by Karakum - do not modify it manually!

@file:JsModule("will-be-mapped")

package was.mapped.main

external val mappedValue: Double
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,3 @@
@file:JsModule("will-be-mapped")

package was.mapped.main

external val mappedValue: Double
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,3 @@
package sandbox.top.level

external val myConst1: String

external val myConst2: Double
7 changes: 7 additions & 0 deletions test/functional/top-level/generated/myConst2.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Generated by Karakum - do not modify it manually!

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

package sandbox.top.level

external val myConst2: Double

0 comments on commit 849dd54

Please sign in to comment.