Skip to content

Commit

Permalink
Fix analyzer
Browse files Browse the repository at this point in the history
  • Loading branch information
ilgonmic authored and woainikk committed Sep 11, 2023
1 parent af557ca commit 59a5dc9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import com.intellij.psi.PsiFile
import component.KotlinEnvironment
import model.Completion
import org.jetbrains.kotlin.analyzer.AnalysisResult
import org.jetbrains.kotlin.cli.common.messages.AnalyzerWithCompilerReport
import org.jetbrains.kotlin.cli.js.klib.TopDownAnalyzerFacadeForJSIR
import org.jetbrains.kotlin.cli.jvm.compiler.CliBindingTrace
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
import org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM
Expand All @@ -29,7 +31,9 @@ import org.jetbrains.kotlin.diagnostics.rendering.DefaultErrorMessages
import org.jetbrains.kotlin.frontend.di.configureModule
import org.jetbrains.kotlin.incremental.components.InlineConstTracker
import org.jetbrains.kotlin.incremental.components.LookupTracker
import org.jetbrains.kotlin.js.analyze.TopDownAnalyzerFacadeForJS
import org.jetbrains.kotlin.ir.backend.js.MainModule
import org.jetbrains.kotlin.ir.backend.js.ModulesStructure
import org.jetbrains.kotlin.ir.backend.js.prepareAnalyzedSourceModule
import org.jetbrains.kotlin.js.config.JsConfig
import org.jetbrains.kotlin.js.resolve.JsPlatformAnalyzerServices
import org.jetbrains.kotlin.name.Name
Expand Down Expand Up @@ -111,18 +115,41 @@ class ErrorAnalyzer(
kotlinEnvironment.JS_LIBRARIES.toSet()
)

val module = ContextForNewModule(
val mainModule = MainModule.SourceFiles(files)
val sourceModule = ModulesStructure(
project,
mainModule,
kotlinEnvironment.jsConfiguration,
kotlinEnvironment.JS_LIBRARIES,
emptyList()
)

val mds = sourceModule.allDependencies.map {
sourceModule.getModuleDescriptor(it) as ModuleDescriptorImpl
}

val analysisResult = TopDownAnalyzerFacadeForJSIR.analyzeFiles(
mainModule.files,
project,
kotlinEnvironment.jsConfiguration,
mds,
emptyList(),
AnalyzerWithCompilerReport(kotlinEnvironment.jsConfiguration).targetEnvironment
)

val context = ContextForNewModule(
projectContext = ProjectContext(project, "COMPILER-SERVER-JS"),
moduleName = Name.special("<" + configuration.moduleId + ">"),
builtIns = JsPlatformAnalyzerServices.builtIns, platform = null
)
module.setDependencies(computeDependencies(module.module, configuration))
val dependencies = mutableSetOf(context.module) + mds + JsPlatformAnalyzerServices.builtIns.builtInsModule
context.module.setDependencies(dependencies.toList())
val trace = CliBindingTrace()
val providerFactory = FileBasedDeclarationProviderFactory(module.storageManager, files)
val analyzerAndProvider = createContainerForTopDownAnalyzerForJs(module, trace, providerFactory)
val providerFactory = FileBasedDeclarationProviderFactory(context.storageManager, files)
val analyzerAndProvider = createContainerForTopDownAnalyzerForJs(context, trace, providerFactory)
return Analysis(
componentProvider = analyzerAndProvider.second,
analysisResult = TopDownAnalyzerFacadeForJS.analyzeFiles(files, configuration)
analysisResult = analysisResult
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class TestProjectRunner {
args: String = "",
convert: KotlinProjectExecutor.(Project) -> TranslationResultWithJsCode
) {
val project = generateSingleProject(text = code, args = args, projectType = ProjectType.JS)
val project = generateSingleProject(text = code, args = args, projectType = ProjectType.JS_IR)
convertAndTest(project, contains, convert)
}

Expand All @@ -43,7 +43,7 @@ class TestProjectRunner {
contains: String,
convert: KotlinProjectExecutor.(Project) -> TranslationResultWithJsCode
) {
val project = generateMultiProject(*code.toTypedArray(), projectType = ProjectType.JS)
val project = generateMultiProject(*code.toTypedArray(), projectType = ProjectType.JS_IR)
convertAndTest(project, contains, convert)
}

Expand All @@ -56,7 +56,7 @@ class TestProjectRunner {
}

fun translateToJs(code: String): TranslationResultWithJsCode {
val project = generateSingleProject(text = code, projectType = ProjectType.JS)
val project = generateSingleProject(text = code, projectType = ProjectType.JS_IR)
return kotlinProjectExecutor.convertToJs(project)
}

Expand Down Expand Up @@ -87,7 +87,7 @@ class TestProjectRunner {
completions: List<String>,
isJs: Boolean = false
) {
val type = if (isJs) ProjectType.JS else ProjectType.JAVA
val type = if (isJs) ProjectType.JS_IR else ProjectType.JAVA
val project = generateSingleProject(text = code, projectType = type)
val result = kotlinProjectExecutor.complete(project, line, character)
.map { it.displayText }
Expand All @@ -103,7 +103,7 @@ class TestProjectRunner {
character: Int,
isJs: Boolean = false
): List<Completion> {
val type = if (isJs) ProjectType.JS else ProjectType.JAVA
val type = if (isJs) ProjectType.JS_IR else ProjectType.JAVA
val project = generateSingleProject(text = code, projectType = type)
return kotlinProjectExecutor.complete(project, line, character)
}
Expand All @@ -114,7 +114,7 @@ class TestProjectRunner {
}

fun highlightJS(code: String): Map<String, List<ErrorDescriptor>> {
val project = generateSingleProject(text = code, projectType = ProjectType.JS)
val project = generateSingleProject(text = code, projectType = ProjectType.JS_IR)
return kotlinProjectExecutor.highlight(project)
}

Expand Down

0 comments on commit 59a5dc9

Please sign in to comment.