Skip to content

Commit

Permalink
Send structured diagnostic along with errors to ELP
Browse files Browse the repository at this point in the history
Summary:
This adds a `diagnostic` field to the `Error` struct sent to ELP, containing the structured diagnostic.

For now, the field is ignored on ELP's end.

Reviewed By: michalmuskala

Differential Revision: D54413114

fbshipit-source-id: db2ca3b56d0dabc7dcb769178657ddce01029133
  • Loading branch information
VLanvin authored and facebook-github-bot committed Mar 1, 2024
1 parent 5899e99 commit ef8dbf3
Show file tree
Hide file tree
Showing 5 changed files with 6,975 additions and 154 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ package com.whatsapp.eqwalizer.ast

import com.whatsapp.eqwalizer.ast.Types.Type
import com.whatsapp.eqwalizer.util.Diagnostic.Diagnostic
import com.github.plokhotnyuk.jsoniter_scala.macros._
import com.github.plokhotnyuk.jsoniter_scala.core._

object InvalidDiagnostics {
sealed trait Invalid extends Diagnostic {
Expand Down Expand Up @@ -77,4 +79,13 @@ object InvalidDiagnostics {
val msg = "Bad map key"
def errorName = "bad_map_key"
}

implicit val codec: JsonValueCodec[Invalid] = JsonCodecMaker.make(
CodecMakerConfig.withAllowRecursiveTypes(true).withDiscriminatorFieldName(None).withFieldNameMapper {
case "pos" => "location"
case "mod" => "module"
case s if !s.charAt(0).isUpper => JsonCodecMaker.enforce_snake_case(s)
case s => s
}
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
package com.whatsapp.eqwalizer.util

import scala.collection.mutable

import com.whatsapp.eqwalizer.{Pipeline, ast}
import com.whatsapp.eqwalizer.ast.Forms.{ElpMetadata, FuncDecl, InternalForm, InvalidForm, MisBehaviour}
import com.whatsapp.eqwalizer.ast.InvalidDiagnostics.Invalid
import com.whatsapp.eqwalizer.ast.{Pos, Show, TextRange}
import com.whatsapp.eqwalizer.ast.stub.DbApi
import com.whatsapp.eqwalizer.io.Ipc
import com.whatsapp.eqwalizer.tc.TcDiagnostics.TypeError
import com.whatsapp.eqwalizer.tc.{Options, noOptions}
import com.github.plokhotnyuk.jsoniter_scala.core._

object ELPDiagnostics {
case class Error(
Expand All @@ -23,6 +25,7 @@ object ELPDiagnostics {
errorName: String,
explanation: Option[String],
shownExpression: Option[String],
diagnostic: Diagnostic.Diagnostic,
)

def getDiagnosticsString(module: String, astStorage: DbApi.AstStorage, options: Options = noOptions): String =
Expand Down Expand Up @@ -86,6 +89,7 @@ object ELPDiagnostics {
te.errorName,
explanation = te.explanation,
shownExpression = te.erroneousExpr.map(Show.show),
diagnostic = te,
)
}
}
Expand All @@ -111,6 +115,10 @@ object ELPDiagnostics {
case Some(s) => ujson.Str(s)
case None => ujson.Null
}
val diagnosticJson = e.diagnostic match {
case te: TypeError => ujson.Obj("TypeError" -> ujson.read(writeToString(te)))
case inv: Invalid => ujson.Obj("InvalidForm" -> ujson.read(writeToString(inv)))
}
ujson.Obj(
"range" -> range,
"lineAndCol" -> lineAndCol,
Expand All @@ -119,6 +127,7 @@ object ELPDiagnostics {
"code" -> ujson.Str(e.errorName),
"expressionOrNull" -> expressionOrNull,
"explanationOrNull" -> explanationOrNull,
"diagnostic" -> diagnosticJson,
)
})
})
Expand Down
Loading

0 comments on commit ef8dbf3

Please sign in to comment.