Unverified Commit ba47a7c3 authored by Benjamin Woodruff's avatar Benjamin Woodruff Committed by GitHub

Turbopack: Update `jsonc-parser`, dedupe with SWC (#82343)

I noticed we had two copies of this library.

There are a few minor API changes (some things that were properties are
now methods, "message" is now "kind"), and I switched our error type
over to use `RcStr` since it implements `Clone`.
parent 1f8ea2a1
...@@ -3475,18 +3475,9 @@ dependencies = [ ...@@ -3475,18 +3475,9 @@ dependencies = [
[[package]] [[package]]
name = "jsonc-parser" name = "jsonc-parser"
version = "0.21.0" version = "0.26.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3a1853e40333206f9a685358046d13ab200169e3ee573019bddf0ede0dc29307"
dependencies = [
"serde_json",
]
[[package]]
name = "jsonc-parser"
version = "0.26.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b558af6b49fd918e970471374e7a798b2c9bbcda624a210ffa3901ee5614bc8e" checksum = "1d6d80e6d70e7911a29f3cf3f44f452df85d06f73572b494ca99a2cad3fcf8f4"
dependencies = [ dependencies = [
"serde_json", "serde_json",
] ]
...@@ -6978,7 +6969,7 @@ dependencies = [ ...@@ -6978,7 +6969,7 @@ dependencies = [
"dashmap 5.5.3", "dashmap 5.5.3",
"either", "either",
"indexmap 2.9.0", "indexmap 2.9.0",
"jsonc-parser 0.26.2", "jsonc-parser",
"napi", "napi",
"napi-derive", "napi-derive",
"once_cell", "once_cell",
...@@ -9347,7 +9338,7 @@ dependencies = [ ...@@ -9347,7 +9338,7 @@ dependencies = [
"futures", "futures",
"include_dir", "include_dir",
"indexmap 2.9.0", "indexmap 2.9.0",
"jsonc-parser 0.21.0", "jsonc-parser",
"mime", "mime",
"notify", "notify",
"parking_lot", "parking_lot",
......
...@@ -33,7 +33,7 @@ dunce = { workspace = true } ...@@ -33,7 +33,7 @@ dunce = { workspace = true }
futures = { workspace = true } futures = { workspace = true }
include_dir = { version = "0.7.2", features = ["nightly"] } include_dir = { version = "0.7.2", features = ["nightly"] }
indexmap = { workspace = true } indexmap = { workspace = true }
jsonc-parser = { version = "0.21.0", features = ["serde"] } jsonc-parser = { version = "0.26.3", features = ["serde"] }
mime = { workspace = true } mime = { workspace = true }
notify = { workspace = true } notify = { workspace = true }
parking_lot = { workspace = true } parking_lot = { workspace = true }
......
use std::{ use std::fmt::{Display, Formatter, Write};
borrow::Cow,
fmt::{Display, Formatter, Write},
};
use anyhow::Result; use anyhow::Result;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use turbo_rcstr::RcStr;
use turbo_tasks::{NonLocalValue, trace::TraceRawVcs}; use turbo_tasks::{NonLocalValue, trace::TraceRawVcs};
use crate::{rope::Rope, source_context::get_source_context}; use crate::{rope::Rope, source_context::get_source_context};
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, TraceRawVcs, NonLocalValue)] #[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, TraceRawVcs, NonLocalValue)]
pub struct UnparsableJson { pub struct UnparsableJson {
pub message: Cow<'static, str>, pub message: RcStr,
pub path: Option<String>, pub path: Option<RcStr>,
/// The start line and column of the error. /// The start line and column of the error.
/// Line and column is 0-based. /// Line and column is 0-based.
pub start_location: Option<(u32, u32)>, pub start_location: Option<(u32, u32)>,
...@@ -34,18 +32,18 @@ fn byte_to_location(pos: usize, text: &str) -> (u32, u32) { ...@@ -34,18 +32,18 @@ fn byte_to_location(pos: usize, text: &str) -> (u32, u32) {
impl UnparsableJson { impl UnparsableJson {
pub fn from_jsonc_error(e: jsonc_parser::errors::ParseError, text: &str) -> Self { pub fn from_jsonc_error(e: jsonc_parser::errors::ParseError, text: &str) -> Self {
Self { Self {
message: e.message.clone().into(), message: RcStr::from(e.kind().to_string()),
path: None, path: None,
start_location: Some(byte_to_location(e.range.start, text)), start_location: Some(byte_to_location(e.range().start, text)),
end_location: Some(byte_to_location(e.range.end, text)), end_location: Some(byte_to_location(e.range().end, text)),
} }
} }
pub fn from_serde_path_to_error(e: serde_path_to_error::Error<serde_json::Error>) -> Self { pub fn from_serde_path_to_error(e: serde_path_to_error::Error<serde_json::Error>) -> Self {
let inner = e.inner(); let inner = e.inner();
Self { Self {
message: inner.to_string().into(), message: RcStr::from(inner.to_string()),
path: Some(e.path().to_string()), path: Some(RcStr::from(e.path().to_string())),
start_location: Some(( start_location: Some((
inner.line().saturating_sub(1) as u32, inner.line().saturating_sub(1) as u32,
inner.column().saturating_sub(1) as u32, inner.column().saturating_sub(1) as u32,
......
...@@ -1942,15 +1942,15 @@ impl FileContent { ...@@ -1942,15 +1942,15 @@ impl FileContent {
) { ) {
Ok(data) => match data { Ok(data) => match data {
Some(value) => FileJsonContent::Content(value), Some(value) => FileJsonContent::Content(value),
None => FileJsonContent::unparsable( None => FileJsonContent::unparsable(rcstr!(
"text content doesn't contain any json data", "text content doesn't contain any json data"
), )),
}, },
Err(e) => FileJsonContent::Unparsable(Box::new( Err(e) => FileJsonContent::Unparsable(Box::new(
UnparsableJson::from_jsonc_error(e, string.as_ref()), UnparsableJson::from_jsonc_error(e, string.as_ref()),
)), )),
}, },
Err(_) => FileJsonContent::unparsable("binary is not valid utf-8 text"), Err(_) => FileJsonContent::unparsable(rcstr!("binary is not valid utf-8 text")),
}, },
FileContent::NotFound => FileJsonContent::NotFound, FileContent::NotFound => FileJsonContent::NotFound,
} }
...@@ -1969,15 +1969,15 @@ impl FileContent { ...@@ -1969,15 +1969,15 @@ impl FileContent {
) { ) {
Ok(data) => match data { Ok(data) => match data {
Some(value) => FileJsonContent::Content(value), Some(value) => FileJsonContent::Content(value),
None => FileJsonContent::unparsable( None => FileJsonContent::unparsable(rcstr!(
"text content doesn't contain any json data", "text content doesn't contain any json data"
), )),
}, },
Err(e) => FileJsonContent::Unparsable(Box::new( Err(e) => FileJsonContent::Unparsable(Box::new(
UnparsableJson::from_jsonc_error(e, string.as_ref()), UnparsableJson::from_jsonc_error(e, string.as_ref()),
)), )),
}, },
Err(_) => FileJsonContent::unparsable("binary is not valid utf-8 text"), Err(_) => FileJsonContent::unparsable(rcstr!("binary is not valid utf-8 text")),
}, },
FileContent::NotFound => FileJsonContent::NotFound, FileContent::NotFound => FileJsonContent::NotFound,
} }
...@@ -2084,16 +2084,16 @@ impl FileJsonContent { ...@@ -2084,16 +2084,16 @@ impl FileJsonContent {
} }
} }
impl FileJsonContent { impl FileJsonContent {
pub fn unparsable(message: &'static str) -> Self { pub fn unparsable(message: RcStr) -> Self {
FileJsonContent::Unparsable(Box::new(UnparsableJson { FileJsonContent::Unparsable(Box::new(UnparsableJson {
message: Cow::Borrowed(message), message,
path: None, path: None,
start_location: None, start_location: None,
end_location: None, end_location: None,
})) }))
} }
pub fn unparsable_with_message(message: Cow<'static, str>) -> Self { pub fn unparsable_with_message(message: RcStr) -> Self {
FileJsonContent::Unparsable(Box::new(UnparsableJson { FileJsonContent::Unparsable(Box::new(UnparsableJson {
message, message,
path: None, path: None,
......
use anyhow::Result; use anyhow::Result;
use turbo_rcstr::RcStr; use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{ResolvedVc, Vc}; use turbo_tasks::{ResolvedVc, Vc};
use turbo_tasks_fs::{ use turbo_tasks_fs::{
FileContent, FileJsonContent, FileLinesContent, FileSystemPath, LinkContent, LinkType, FileContent, FileJsonContent, FileLinesContent, FileSystemPath, LinkContent, LinkType,
...@@ -43,9 +43,10 @@ impl AssetContent { ...@@ -43,9 +43,10 @@ impl AssetContent {
let this = self.await?; let this = self.await?;
match &*this { match &*this {
AssetContent::File(content) => Ok(content.parse_json()), AssetContent::File(content) => Ok(content.parse_json()),
AssetContent::Redirect { .. } => { AssetContent::Redirect { .. } => Ok(FileJsonContent::unparsable(rcstr!(
Ok(FileJsonContent::unparsable("a redirect can't be parsed as json").cell()) "a redirect can't be parsed as json"
} ))
.cell()),
} }
} }
...@@ -81,9 +82,10 @@ impl AssetContent { ...@@ -81,9 +82,10 @@ impl AssetContent {
let this = self.await?; let this = self.await?;
match &*this { match &*this {
AssetContent::File(content) => Ok(content.parse_json_with_comments()), AssetContent::File(content) => Ok(content.parse_json_with_comments()),
AssetContent::Redirect { .. } => { AssetContent::Redirect { .. } => Ok(FileJsonContent::unparsable(rcstr!(
Ok(FileJsonContent::unparsable("a redirect can't be parsed as json").cell()) "a redirect can't be parsed as json"
} ))
.cell()),
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment