Add diagnostic code to the ScriptDiagnostic
to allow checking for specific errors, e.g. incomplete statements in the REPL
This commit is contained in:
@@ -14,12 +14,14 @@ import java.io.Serializable
|
||||
|
||||
/**
|
||||
* The single script diagnostic report
|
||||
* @param code diagnostic identifier
|
||||
* @param message diagnostic message
|
||||
* @param severity diagnostic severity ({@link ScriptDiagnostic#Severity})
|
||||
* @param location optional source location for the diagnostic
|
||||
* @param exception optional exception caused the diagnostic
|
||||
*/
|
||||
data class ScriptDiagnostic(
|
||||
val code: Int,
|
||||
val message: String,
|
||||
val severity: Severity = Severity.ERROR,
|
||||
val sourcePath: String? = null,
|
||||
@@ -77,6 +79,14 @@ data class ScriptDiagnostic(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val serialVersionUID: Long = 0L
|
||||
|
||||
const val unspecifiedInfo = 0
|
||||
const val unspecifiedError = -1
|
||||
const val unspecifiedException = -2
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -198,24 +208,29 @@ fun makeFailureResult(vararg reports: ScriptDiagnostic): ResultWithDiagnostics.F
|
||||
* Makes Failure result with diagnostic [message] with optional [path] and [location]
|
||||
*/
|
||||
fun makeFailureResult(message: String, path: String? = null, location: SourceCode.Location? = null): ResultWithDiagnostics.Failure =
|
||||
ResultWithDiagnostics.Failure(message.asErrorDiagnostics(path, location))
|
||||
ResultWithDiagnostics.Failure(message.asErrorDiagnostics(ScriptDiagnostic.unspecifiedError, path, location))
|
||||
|
||||
/**
|
||||
* Converts the receiver Throwable to the Failure results wrapper with optional [customMessage], [path] and [location]
|
||||
*/
|
||||
fun Throwable.asDiagnostics(
|
||||
code: Int = ScriptDiagnostic.unspecifiedException,
|
||||
customMessage: String? = null,
|
||||
path: String? = null,
|
||||
location: SourceCode.Location? = null,
|
||||
severity: ScriptDiagnostic.Severity = ScriptDiagnostic.Severity.ERROR
|
||||
): ScriptDiagnostic =
|
||||
ScriptDiagnostic(customMessage ?: message ?: "$this", severity, path, location, this)
|
||||
ScriptDiagnostic(code, customMessage ?: message ?: "$this", severity, path, location, this)
|
||||
|
||||
/**
|
||||
* Converts the receiver String to error diagnostic report with optional [path] and [location]
|
||||
*/
|
||||
fun String.asErrorDiagnostics(path: String? = null, location: SourceCode.Location? = null): ScriptDiagnostic =
|
||||
ScriptDiagnostic(this, ScriptDiagnostic.Severity.ERROR, path, location)
|
||||
fun String.asErrorDiagnostics(
|
||||
code: Int = ScriptDiagnostic.unspecifiedError,
|
||||
path: String? = null,
|
||||
location: SourceCode.Location? = null
|
||||
): ScriptDiagnostic =
|
||||
ScriptDiagnostic(code, this, ScriptDiagnostic.Severity.ERROR, path, location)
|
||||
|
||||
/**
|
||||
* Extracts the result value from the receiver wrapper or null if receiver represents a Failure
|
||||
|
||||
+1
-1
@@ -14,7 +14,7 @@ import kotlin.script.experimental.api.ScriptDiagnostic
|
||||
fun makeResolveFailureResult(message: String) = makeResolveFailureResult(listOf(message))
|
||||
|
||||
fun makeResolveFailureResult(messages: Iterable<String>) =
|
||||
ResultWithDiagnostics.Failure(messages.map { ScriptDiagnostic(it, ScriptDiagnostic.Severity.WARNING) })
|
||||
ResultWithDiagnostics.Failure(messages.map { ScriptDiagnostic(ScriptDiagnostic.unspecifiedError, it, ScriptDiagnostic.Severity.WARNING) })
|
||||
|
||||
fun RepositoryCoordinates.toRepositoryUrlOrNull(): URL? =
|
||||
try {
|
||||
|
||||
@@ -33,6 +33,7 @@ class JsScriptEvaluator : ScriptEvaluator {
|
||||
} catch (e: Exception) {
|
||||
ResultWithDiagnostics.Failure(
|
||||
ScriptDiagnostic(
|
||||
ScriptDiagnostic.unspecifiedError,
|
||||
message = e.localizedMessage,
|
||||
severity = ScriptDiagnostic.Severity.ERROR,
|
||||
exception = e
|
||||
|
||||
+2
-2
@@ -44,7 +44,7 @@ open class BasicJvmScriptClassFilesGenerator(val outputDir: File) : ScriptEvalua
|
||||
return ResultWithDiagnostics.Success(EvaluationResult(ResultValue.NotEvaluated, scriptEvaluationConfiguration))
|
||||
} catch (e: Throwable) {
|
||||
return ResultWithDiagnostics.Failure(
|
||||
e.asDiagnostics("Cannot generate script classes: ${e.message}", path = compiledScript.sourceLocationId)
|
||||
e.asDiagnostics(customMessage = "Cannot generate script classes: ${e.message}", path = compiledScript.sourceLocationId)
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -103,7 +103,7 @@ open class BasicJvmScriptJarGenerator(val outputJar: File) : ScriptEvaluator {
|
||||
return ResultWithDiagnostics.Success(EvaluationResult(ResultValue.NotEvaluated, scriptEvaluationConfiguration))
|
||||
} catch (e: Throwable) {
|
||||
return ResultWithDiagnostics.Failure(
|
||||
e.asDiagnostics("Cannot generate script jar: ${e.message}", path = compiledScript.sourceLocationId)
|
||||
e.asDiagnostics(customMessage = "Cannot generate script jar: ${e.message}", path = compiledScript.sourceLocationId)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
+4
-2
@@ -51,10 +51,12 @@ fun mapToLegacyScriptReportPosition(pos: SourceCode.Location?): ScriptReport.Pos
|
||||
pos?.let { ScriptReport.Position(pos.start.line, pos.start.col, pos.end?.line, pos.end?.col) }
|
||||
|
||||
fun Iterable<ScriptReport>.mapToDiagnostics(): List<ScriptDiagnostic> = map { (message, severity, position) ->
|
||||
ScriptDiagnostic(message, mapLegacyDiagnosticSeverity(severity), null, mapLegacyScriptPosition(position))
|
||||
ScriptDiagnostic(
|
||||
ScriptDiagnostic.unspecifiedError, message, mapLegacyDiagnosticSeverity(severity), null, mapLegacyScriptPosition(position)
|
||||
)
|
||||
}
|
||||
|
||||
fun Iterable<ScriptDiagnostic>.mapToLegacyReports(): List<ScriptReport> = map { (message, severity, _, location, exception) ->
|
||||
fun Iterable<ScriptDiagnostic>.mapToLegacyReports(): List<ScriptReport> = map { (_, message, severity, _, location, exception) ->
|
||||
val reportMessage = if (exception == null) message else "$message ($exception)"
|
||||
ScriptReport(reportMessage, mapToLegacyScriptReportSeverity(severity), mapToLegacyScriptReportPosition(location))
|
||||
}
|
||||
|
||||
@@ -86,6 +86,7 @@ class KJvmCompiledScript<out ScriptBase : Any> internal constructor(
|
||||
} catch (e: Throwable) {
|
||||
ResultWithDiagnostics.Failure(
|
||||
ScriptDiagnostic(
|
||||
ScriptDiagnostic.unspecifiedError,
|
||||
"Unable to instantiate class ${data.scriptClassFQName}",
|
||||
sourcePath = sourceLocationId,
|
||||
exception = e
|
||||
|
||||
Reference in New Issue
Block a user