From d8562b79a4a6cddf628b5d1ebde0426565f8bf2a Mon Sep 17 00:00:00 2001 From: commandblock2 Date: Wed, 22 Jan 2025 23:15:28 +0800 Subject: [PATCH 1/6] fix: stackoverflow caused by iterable types returns itself as element --- .../ntrrgc/tsGenerator/TypeScriptGenerator.kt | 47 +++++++++++-------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/src/main/kotlin/me/ntrrgc/tsGenerator/TypeScriptGenerator.kt b/src/main/kotlin/me/ntrrgc/tsGenerator/TypeScriptGenerator.kt index f1cd491..0ba9a56 100644 --- a/src/main/kotlin/me/ntrrgc/tsGenerator/TypeScriptGenerator.kt +++ b/src/main/kotlin/me/ntrrgc/tsGenerator/TypeScriptGenerator.kt @@ -140,11 +140,7 @@ class TypeScriptGenerator( } private fun generateDefinition(): String { - return if (klass.java.isEnum) { - generateEnum(klass) - } else { - generateInterface(klass) - } + return generateInterface(klass) } @@ -169,7 +165,11 @@ class TypeScriptGenerator( ) arrayFromKType(kType) else if (classifier.isSubclassOf(Map::class)) - mapFromKType(kType) + try { + mapFromKType(kType) + } catch (_: Exception) { + nonPrimitiveFromKType(kType) + } else nonPrimitiveFromKType(kType) ) @@ -244,28 +244,37 @@ class TypeScriptGenerator( getIterableElementType(kType) ?: kType.arguments.singleOrNull()?.type ?: KotlinAnyOrNull } } - return "${formatKType(itemType).formatWithParenthesis()}[]" + // a Path is iterable and it returns Path s for subdirectories + return if (kType == itemType) + "${nonPrimitiveFromKType(kType)}[]" // can it be others like maps? + else + "${formatKType(itemType).formatWithParenthesis()}[]" } + // https://github.com/ntrrgc/ts-generator/pull/39/files#diff-15868d315697c109f701fa6b29d6b1beaabb6c461122d4cbca76194bba08da6eR194 + // GPLv3 does not apply for this function private fun mapFromKType(kType: KType): String { - // Use native JS associative object + val rawKeyType = kType.arguments[0].type ?: KotlinAnyOrNull val keyType = formatKType(rawKeyType) val valueType = formatKType(kType.arguments[1].type ?: KotlinAnyOrNull) - return if ((rawKeyType.classifier as? KClass<*>)?.java?.isEnum == true) - "{ [key in ${keyType.formatWithoutParenthesis()}]: ${valueType.formatWithoutParenthesis()} }" - else - "{ [key: ${keyType.formatWithoutParenthesis()}]: ${valueType.formatWithoutParenthesis()} }" - } - private fun generateEnum(klass: KClass<*>): String { - return "type ${klass.simpleName} = ${ - klass.java.enumConstants.joinToString(" | ") { constant: Any -> - constant.toString().toJSString() - } - };" + val isKeyEnum = (rawKeyType.classifier as? KClass<*>)?.java?.isEnum == true + + return when { + isKeyEnum -> + "{ [key in ${keyType.formatWithoutParenthesis()}]: ${valueType.formatWithoutParenthesis()} }" + + keyType.formatWithoutParenthesis() == "string" || keyType.formatWithoutParenthesis() == "number" -> + "{ [key: ${keyType.formatWithoutParenthesis()}]: ${valueType.formatWithoutParenthesis()} }" + + else -> + "Map<${keyType.formatWithoutParenthesis()}, ${valueType.formatWithoutParenthesis()}>" + } + } + private fun generateInterface(klass: KClass<*>): String { val supertypes = klass.supertypes .filterNot { it.classifier in ignoredSuperclasses } From 58fcaee22f76de891f63f4d8a8d54d7b8913ac3c Mon Sep 17 00:00:00 2001 From: commandblock2 Date: Wed, 22 Jan 2025 23:48:14 +0800 Subject: [PATCH 2/6] chore: print exception and re-throw --- .../tsGenerator/tests/generatorTests.kt | 40 ++++++++++++------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/src/test/kotlin/me/ntrrgc/tsGenerator/tests/generatorTests.kt b/src/test/kotlin/me/ntrrgc/tsGenerator/tests/generatorTests.kt index c08f0f7..8a9272c 100644 --- a/src/test/kotlin/me/ntrrgc/tsGenerator/tests/generatorTests.kt +++ b/src/test/kotlin/me/ntrrgc/tsGenerator/tests/generatorTests.kt @@ -77,21 +77,27 @@ fun runModuleGenerationWithoutVerification( ignoreSuperclasses: Set> = setOf(), voidType: VoidType = VoidType.NULL ) { - val generator = TypeScriptGenerator( - listOf(klass), mappings, classTransformers, - ignoreSuperclasses, intTypeName = "number", voidType = voidType - ) + try { + val generator = TypeScriptGenerator( + listOf(klass), mappings, classTransformers, + ignoreSuperclasses, intTypeName = "number", voidType = voidType + ) + + val modules = generator.definitionsAsModules - val modules = generator.definitionsAsModules + for (module in modules) { + println("file: ${module.key}") + println() + println("content: ${module.value}") + println() + } - for (module in modules) { - println("file: ${module.key}") - println() - println("content: ${module.value}") - println() + true shouldBe true + } catch (exception: Exception) { + exception.printStackTrace() + throw exception } - true shouldBe true } @Suppress("unused") @@ -967,8 +973,14 @@ class ModuleOutput : StringSpec({ class Tests : StringSpec({ "generates NPM package without spitting error" { - TypeScriptGenerator(listOf(ClassWithMethodsThatReturnsOrTakesFunctionalType::class)) - .generateNPMPackage("test-generated-package-types") - .writePackageTo(Path("./runs")) + try { + TypeScriptGenerator(listOf(ClassWithMethodsThatReturnsOrTakesFunctionalType::class)) + .generateNPMPackage("test-generated-package-types") + .writePackageTo(Path("./runs")) + } catch (exception: Exception) { + exception.printStackTrace() + throw exception + } + } }) \ No newline at end of file From d8b73aa76228d8c1c2bb2cb0f0b413f81ee3ec99 Mon Sep 17 00:00:00 2001 From: commandblock2 Date: Wed, 22 Jan 2025 23:53:36 +0800 Subject: [PATCH 3/6] chore: updated actions --- .github/workflows/gradle.yml | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml index d87646d..619de0a 100644 --- a/.github/workflows/gradle.yml +++ b/.github/workflows/gradle.yml @@ -15,13 +15,22 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 - - name: Set up JDK 21 - uses: actions/setup-java@v4.5.0 - with: - java-version: '21' - distribution: 'adopt' - - name: Grant execute permission for gradlew - run: chmod +x gradlew - - name: Build with Gradle - run: ./gradlew build + - uses: actions/checkout@v2 + - name: Set up JDK 21 + uses: actions/setup-java@v4.5.0 + with: + java-version: '21' + distribution: 'adopt' + - name: Grant execute permission for gradlew + run: chmod +x gradlew + - name: Build with Gradle + run: ./gradlew build + + # upload file:///home/runner/work/ts-generator/ts-generator/build/reports/tests/test/index.html + - name: Upload test report to GitHub Actions + uses: actions/upload-artifact@v4 + with: + name: 'test-report' + path: 'ts-generator/build/reports/tests/test/' + + From 4ce5a06e9000ac35203ea3374dd7fe4350f34e68 Mon Sep 17 00:00:00 2001 From: commandblock2 Date: Wed, 22 Jan 2025 23:56:14 +0800 Subject: [PATCH 4/6] chore: always run the action --- .github/workflows/gradle.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml index 619de0a..1eacbb9 100644 --- a/.github/workflows/gradle.yml +++ b/.github/workflows/gradle.yml @@ -28,6 +28,7 @@ jobs: # upload file:///home/runner/work/ts-generator/ts-generator/build/reports/tests/test/index.html - name: Upload test report to GitHub Actions + if: always() uses: actions/upload-artifact@v4 with: name: 'test-report' From 74b2a22c023c70359a53db69226cf1f9a871876b Mon Sep 17 00:00:00 2001 From: commandblock2 Date: Wed, 22 Jan 2025 23:59:25 +0800 Subject: [PATCH 5/6] chore: changed path to be uploaded --- .github/workflows/gradle.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml index 1eacbb9..921f0e4 100644 --- a/.github/workflows/gradle.yml +++ b/.github/workflows/gradle.yml @@ -27,11 +27,12 @@ jobs: run: ./gradlew build # upload file:///home/runner/work/ts-generator/ts-generator/build/reports/tests/test/index.html + # Warning: No files were found with the provided path: ts-generator/build/reports/tests/test/. No artifacts will be uploaded. - name: Upload test report to GitHub Actions if: always() uses: actions/upload-artifact@v4 with: name: 'test-report' - path: 'ts-generator/build/reports/tests/test/' + path: 'build/reports/tests/test/' From 9c6e079b7c9fd171757146bb9f94c3391f597fdb Mon Sep 17 00:00:00 2001 From: commandblock2 Date: Thu, 23 Jan 2025 00:13:09 +0800 Subject: [PATCH 6/6] chore: also catch IllegalArgumentException, idk this doesn't really seem to make sense --- src/main/kotlin/me/ntrrgc/tsGenerator/TypeScriptGenerator.kt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/kotlin/me/ntrrgc/tsGenerator/TypeScriptGenerator.kt b/src/main/kotlin/me/ntrrgc/tsGenerator/TypeScriptGenerator.kt index 0ba9a56..09d9e87 100644 --- a/src/main/kotlin/me/ntrrgc/tsGenerator/TypeScriptGenerator.kt +++ b/src/main/kotlin/me/ntrrgc/tsGenerator/TypeScriptGenerator.kt @@ -334,6 +334,9 @@ class TypeScriptGenerator( } catch (exception: kotlin.reflect.jvm.internal.KotlinReflectionInternalError) { print(exception.toString()) "" + } catch (exception: java.lang.IllegalArgumentException) { + print(exception.toString()) + "" } private fun functionsOf(klass: KClass<*>): String = try {