Skip to content

Commit ba47a7c

Browse files
authored
Turbopack: Update jsonc-parser, dedupe with SWC (vercel#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`.
1 parent 1f8ea2a commit ba47a7c

File tree

5 files changed

+34
-43
lines changed

5 files changed

+34
-43
lines changed

Cargo.lock

Lines changed: 4 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

turbopack/crates/turbo-tasks-fs/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ dunce = { workspace = true }
3333
futures = { workspace = true }
3434
include_dir = { version = "0.7.2", features = ["nightly"] }
3535
indexmap = { workspace = true }
36-
jsonc-parser = { version = "0.21.0", features = ["serde"] }
36+
jsonc-parser = { version = "0.26.3", features = ["serde"] }
3737
mime = { workspace = true }
3838
notify = { workspace = true }
3939
parking_lot = { workspace = true }

turbopack/crates/turbo-tasks-fs/src/json.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
use std::{
2-
borrow::Cow,
3-
fmt::{Display, Formatter, Write},
4-
};
1+
use std::fmt::{Display, Formatter, Write};
52

63
use anyhow::Result;
74
use serde::{Deserialize, Serialize};
5+
use turbo_rcstr::RcStr;
86
use turbo_tasks::{NonLocalValue, trace::TraceRawVcs};
97

108
use crate::{rope::Rope, source_context::get_source_context};
119

1210
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, TraceRawVcs, NonLocalValue)]
1311
pub struct UnparsableJson {
14-
pub message: Cow<'static, str>,
15-
pub path: Option<String>,
12+
pub message: RcStr,
13+
pub path: Option<RcStr>,
1614
/// The start line and column of the error.
1715
/// Line and column is 0-based.
1816
pub start_location: Option<(u32, u32)>,
@@ -34,18 +32,18 @@ fn byte_to_location(pos: usize, text: &str) -> (u32, u32) {
3432
impl UnparsableJson {
3533
pub fn from_jsonc_error(e: jsonc_parser::errors::ParseError, text: &str) -> Self {
3634
Self {
37-
message: e.message.clone().into(),
35+
message: RcStr::from(e.kind().to_string()),
3836
path: None,
39-
start_location: Some(byte_to_location(e.range.start, text)),
40-
end_location: Some(byte_to_location(e.range.end, text)),
37+
start_location: Some(byte_to_location(e.range().start, text)),
38+
end_location: Some(byte_to_location(e.range().end, text)),
4139
}
4240
}
4341

4442
pub fn from_serde_path_to_error(e: serde_path_to_error::Error<serde_json::Error>) -> Self {
4543
let inner = e.inner();
4644
Self {
47-
message: inner.to_string().into(),
48-
path: Some(e.path().to_string()),
45+
message: RcStr::from(inner.to_string()),
46+
path: Some(RcStr::from(e.path().to_string())),
4947
start_location: Some((
5048
inner.line().saturating_sub(1) as u32,
5149
inner.column().saturating_sub(1) as u32,

turbopack/crates/turbo-tasks-fs/src/lib.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1942,15 +1942,15 @@ impl FileContent {
19421942
) {
19431943
Ok(data) => match data {
19441944
Some(value) => FileJsonContent::Content(value),
1945-
None => FileJsonContent::unparsable(
1946-
"text content doesn't contain any json data",
1947-
),
1945+
None => FileJsonContent::unparsable(rcstr!(
1946+
"text content doesn't contain any json data"
1947+
)),
19481948
},
19491949
Err(e) => FileJsonContent::Unparsable(Box::new(
19501950
UnparsableJson::from_jsonc_error(e, string.as_ref()),
19511951
)),
19521952
},
1953-
Err(_) => FileJsonContent::unparsable("binary is not valid utf-8 text"),
1953+
Err(_) => FileJsonContent::unparsable(rcstr!("binary is not valid utf-8 text")),
19541954
},
19551955
FileContent::NotFound => FileJsonContent::NotFound,
19561956
}
@@ -1969,15 +1969,15 @@ impl FileContent {
19691969
) {
19701970
Ok(data) => match data {
19711971
Some(value) => FileJsonContent::Content(value),
1972-
None => FileJsonContent::unparsable(
1973-
"text content doesn't contain any json data",
1974-
),
1972+
None => FileJsonContent::unparsable(rcstr!(
1973+
"text content doesn't contain any json data"
1974+
)),
19751975
},
19761976
Err(e) => FileJsonContent::Unparsable(Box::new(
19771977
UnparsableJson::from_jsonc_error(e, string.as_ref()),
19781978
)),
19791979
},
1980-
Err(_) => FileJsonContent::unparsable("binary is not valid utf-8 text"),
1980+
Err(_) => FileJsonContent::unparsable(rcstr!("binary is not valid utf-8 text")),
19811981
},
19821982
FileContent::NotFound => FileJsonContent::NotFound,
19831983
}
@@ -2084,16 +2084,16 @@ impl FileJsonContent {
20842084
}
20852085
}
20862086
impl FileJsonContent {
2087-
pub fn unparsable(message: &'static str) -> Self {
2087+
pub fn unparsable(message: RcStr) -> Self {
20882088
FileJsonContent::Unparsable(Box::new(UnparsableJson {
2089-
message: Cow::Borrowed(message),
2089+
message,
20902090
path: None,
20912091
start_location: None,
20922092
end_location: None,
20932093
}))
20942094
}
20952095

2096-
pub fn unparsable_with_message(message: Cow<'static, str>) -> Self {
2096+
pub fn unparsable_with_message(message: RcStr) -> Self {
20972097
FileJsonContent::Unparsable(Box::new(UnparsableJson {
20982098
message,
20992099
path: None,

turbopack/crates/turbopack-core/src/asset.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use anyhow::Result;
2-
use turbo_rcstr::RcStr;
2+
use turbo_rcstr::{RcStr, rcstr};
33
use turbo_tasks::{ResolvedVc, Vc};
44
use turbo_tasks_fs::{
55
FileContent, FileJsonContent, FileLinesContent, FileSystemPath, LinkContent, LinkType,
@@ -43,9 +43,10 @@ impl AssetContent {
4343
let this = self.await?;
4444
match &*this {
4545
AssetContent::File(content) => Ok(content.parse_json()),
46-
AssetContent::Redirect { .. } => {
47-
Ok(FileJsonContent::unparsable("a redirect can't be parsed as json").cell())
48-
}
46+
AssetContent::Redirect { .. } => Ok(FileJsonContent::unparsable(rcstr!(
47+
"a redirect can't be parsed as json"
48+
))
49+
.cell()),
4950
}
5051
}
5152

@@ -81,9 +82,10 @@ impl AssetContent {
8182
let this = self.await?;
8283
match &*this {
8384
AssetContent::File(content) => Ok(content.parse_json_with_comments()),
84-
AssetContent::Redirect { .. } => {
85-
Ok(FileJsonContent::unparsable("a redirect can't be parsed as json").cell())
86-
}
85+
AssetContent::Redirect { .. } => Ok(FileJsonContent::unparsable(rcstr!(
86+
"a redirect can't be parsed as json"
87+
))
88+
.cell()),
8789
}
8890
}
8991

0 commit comments

Comments
 (0)