Skip to content

Commit

Permalink
feat: init basic typescript support
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Dec 9, 2023
1 parent 56cc56f commit 985293f
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 8 deletions.
2 changes: 1 addition & 1 deletion docs/adr/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
layout: default
title: ADR
nav_order: 3
nav_order: 98
has_children: true
permalink: /docs/adr
---
Expand Down
2 changes: 1 addition & 1 deletion docs/home.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ nav_order: 1
permalink: /
---

# UnitEval
<h1 align="center">Unit Eval</h1>

<p align="center">
<a href="https://github.com/unit-mesh/unit-eval/actions/workflows/build.yml">
Expand Down
2 changes: 1 addition & 1 deletion docs/instruction/instruction.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ permalink: /instruction

# InstructionType

InstructionType is a enum class that defines the type of instruction. It is used to determine which builder to use to
InstructionType is an enum class that defines the type of instruction. It is used to determine which builder to use to
build the instruction.

```kotlin
Expand Down
2 changes: 1 addition & 1 deletion docs/roadmap.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ permalink: /roadmap
- [ ] Language support by [Chapi](https://github.com/phodal/chapi)
- [x] Java
- [ ] Kotlin
- [ ] TypeScript/JavaScript
- [x] TypeScript/JavaScript
- [ ] Python
- [ ] Golang

Expand Down
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ test-assertj = { group = "org.assertj", name = "assertj-core", version.ref = "as
chapi-domain = { group = "com.phodal.chapi", name = "chapi-domain", version.ref = "chapi" }
chapi-java = { group = "com.phodal.chapi", name = "chapi-ast-java", version.ref = "chapi" }
chapi-kotlin = { group = "com.phodal.chapi", name = "chapi-ast-kotlin", version.ref = "chapi" }
chapi-typescript = { group = "com.phodal.chapi", name = "chapi-ast-typescript", version.ref = "chapi" }


# ArchGurad
Expand Down
1 change: 1 addition & 0 deletions unit-picker/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ dependencies {
implementation(libs.chapi.domain)
implementation(libs.chapi.java)
implementation(libs.chapi.kotlin)
implementation(libs.chapi.typescript)

implementation(libs.archguard.scanner.core)
implementation(libs.archguard.analyser.estimate)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package cc.unitmesh.pick.worker

import cc.unitmesh.pick.config.InstructionFileJob
import cc.unitmesh.pick.prompt.Instruction
import cc.unitmesh.pick.worker.worker.JavaLangWorker
import cc.unitmesh.pick.worker.worker.JavaWorker
import cc.unitmesh.pick.worker.worker.TypescriptWorker
import org.archguard.rule.common.Language

class WorkerManager(workerContext: WorkerContext) {
private val workers: Map<Language, LangWorker> = mapOf(
Language.JAVA to JavaLangWorker(workerContext),
Language.JAVA to JavaWorker(workerContext),
Language.TYPESCRIPT to TypescriptWorker(workerContext),
Language.JAVASCRIPT to TypescriptWorker(workerContext),
)

fun addJob(job: InstructionFileJob) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import java.io.File
* - by Horizontal (with Import File):
* - by Vertical (with History Change):
*/
class JavaLangWorker(private val context: WorkerContext) : LangWorker() {
class JavaWorker(private val context: WorkerContext) : LangWorker() {
private val jobs: MutableList<InstructionFileJob> = mutableListOf()
private val fileTree: HashMap<String, InstructionFileJob> = hashMapOf()

Expand Down Expand Up @@ -64,7 +64,7 @@ class JavaLangWorker(private val context: WorkerContext) : LangWorker() {
}

override suspend fun start(): Collection<Instruction> = coroutineScope {
val file: File = File(context.pureDataFileName)
val file = File(context.pureDataFileName)
if (!file.exists()) {
file.createNewFile()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package cc.unitmesh.pick.worker.worker

import cc.unitmesh.pick.config.InstructionFileJob
import cc.unitmesh.pick.ext.CodeDataStructUtil
import cc.unitmesh.pick.prompt.Instruction
import cc.unitmesh.pick.prompt.InstructionContext
import cc.unitmesh.pick.worker.LangWorker
import cc.unitmesh.pick.worker.WorkerContext
import chapi.ast.typescriptast.TypeScriptAnalyser
import kotlinx.coroutines.coroutineScope
import java.io.File

class TypescriptWorker(private val context: WorkerContext) : LangWorker() {
private val jobs: MutableList<InstructionFileJob> = mutableListOf()
private val fileTree: HashMap<String, InstructionFileJob> = hashMapOf()

override fun addJob(job: InstructionFileJob) {
this.jobs.add(job)

// since the Java Analyser imports will be in data structures
val container = TypeScriptAnalyser().analysis(job.code, job.fileSummary.location)
job.codeLines = job.code.lines()
container.DataStructures.map { ds ->
ds.Imports = container.Imports

ds.Content = CodeDataStructUtil.contentByPosition(job.codeLines, ds.Position)
ds.Functions.map {
it.apply {
it.Content = CodeDataStructUtil.contentByPosition(job.codeLines, it.Position)
}
}
}

job.container = container
}

override suspend fun start(): Collection<Instruction> = coroutineScope {
val file = File(context.pureDataFileName)
if (!file.exists()) {
file.createNewFile()
}

val lists = jobs.map { job ->
val instructionContext = InstructionContext(job, context.qualityTypes, fileTree, context.builderConfig)

context.instructionTypes.map { type ->
val instructionBuilder = type.builder(instructionContext)
val list = instructionBuilder.build()
list.map {
file.appendText(it.toString() + "\n")
}
instructionBuilder.unique(list as List<Nothing>)
}.flatten()
}.flatten()

return@coroutineScope lists
}
}

0 comments on commit 985293f

Please sign in to comment.