From 2debf2aee1a52c63d28c7bbec9de0d045a496da0 Mon Sep 17 00:00:00 2001 From: Edwin Smith Date: Tue, 13 Dec 2022 07:07:27 -0800 Subject: [PATCH] --json Summary: Add a new `--json` flag that behaves like `hh --json`; in this mode, stdout will be a json blob with errors: { errors: ... passed: false } this is a subset of `hh` output, which has other fields like `version` and `last_recheck` with many hh-specific fields. Reviewed By: bobrenjc93 Differential Revision: D41917506 fbshipit-source-id: 2c3b1b8a0f6d96d513a13e24a8568ac35246447a --- hphp/hack/src/errors/cargo/errors/Cargo.toml | 15 ++++++++++++ hphp/hack/src/errors/errors.rs | 8 +++++++ hphp/hack/src/errors/user_error.rs | 36 ++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+) create mode 100644 hphp/hack/src/errors/cargo/errors/Cargo.toml create mode 100644 hphp/hack/src/errors/errors.rs create mode 100644 hphp/hack/src/errors/user_error.rs diff --git a/hphp/hack/src/errors/cargo/errors/Cargo.toml b/hphp/hack/src/errors/cargo/errors/Cargo.toml new file mode 100644 index 00000000000..a76638d27f0 --- /dev/null +++ b/hphp/hack/src/errors/cargo/errors/Cargo.toml @@ -0,0 +1,15 @@ +# @generated by autocargo from //hphp/hack/src/errors:errors_rs +[package] +name = "errors_rs" +version = "0.0.0" +edition = "2021" + +[lib] +path = "../../errors.rs" +crate-type = ["lib", "staticlib"] + +[dependencies] +oxidized = { path = "../../../oxidized" } +rc_pos = { path = "../../../utils/rust/pos" } +relative_path = { path = "../../../utils/rust/relative_path" } +serde_json = { version = "1.0.79", features = ["float_roundtrip", "unbounded_depth"] } diff --git a/hphp/hack/src/errors/errors.rs b/hphp/hack/src/errors/errors.rs new file mode 100644 index 00000000000..6d896a45f78 --- /dev/null +++ b/hphp/hack/src/errors/errors.rs @@ -0,0 +1,8 @@ +// Copyright (c) Meta Platforms, Inc. and its affiliates. +// +// This source code is licensed under the MIT license found in the +// LICENSE file in the "hack" directory of this source tree. +mod user_error; + +pub use oxidized::errors::*; +pub use user_error::*; diff --git a/hphp/hack/src/errors/user_error.rs b/hphp/hack/src/errors/user_error.rs new file mode 100644 index 00000000000..d20477a2ed3 --- /dev/null +++ b/hphp/hack/src/errors/user_error.rs @@ -0,0 +1,36 @@ +// Copyright (c) Meta Platforms, Inc. and its affiliates. +// +// This source code is licensed under the MIT license found in the +// LICENSE file in the "hack" directory of this source tree. +use oxidized::message::Message; +use oxidized::user_error::UserError; +use rc_pos::Pos; +use relative_path::RelativePathCtx; +use serde_json::json; + +use crate::ErrorCode; + +pub fn to_json(e: &UserError, ctx: &RelativePathCtx) -> serde_json::Value { + let mut messages = Vec::new(); + messages.push(msg_json(&e.claim, e.code, ctx)); + messages.extend(e.reasons.iter().map(|m| msg_json(m, e.code, ctx))); + json!({ + "message": messages, + }) +} + +fn msg_json( + Message(pos, descr): &Message, + code: ErrorCode, + ctx: &RelativePathCtx, +) -> serde_json::Value { + let (line, scol, ecol) = pos.info_pos(); + json!({ + "descr": descr.to_string(), + "path": pos.filename().to_absolute(ctx), + "line": line, + "start": scol, + "end": ecol, + "code": code, + }) +} -- 2.11.4.GIT