Skip to content

Commit

Permalink
style: simplify string formatting (#66772)
Browse files Browse the repository at this point in the history
Co-authored-by: Jiwon Choi <devjiwonchoi@gmail.com>
  • Loading branch information
hamirmahal and devjiwonchoi committed Jul 11, 2024
1 parent 0849391 commit 16cc5fa
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,7 @@ impl Fold for OptimizeBarrel {
span: DUMMY_SP,
src: Box::new(Str {
span: DUMMY_SP,
value: format!("__barrel_optimize__?names=__PLACEHOLDER__!=!{}", src)
.into(),
value: format!("__barrel_optimize__?names=__PLACEHOLDER__!=!{src}").into(),
raw: None,
}),
with: None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl Fold for PageConfig {
type_ann: None,
}),
init: Some(Box::new(Expr::Lit(Lit::Str(Str {
value: format!("{} {}", STRING_LITERAL_DROP_BUNDLE, timestamp).into(),
value: format!("{STRING_LITERAL_DROP_BUNDLE} {timestamp}").into(),
span: DUMMY_SP,
raw: None,
})))),
Expand Down Expand Up @@ -171,8 +171,8 @@ impl Fold for PageConfig {
impl PageConfig {
fn handle_error(&mut self, details: &str, span: Span) {
if self.is_page_file {
let message = format!("Invalid page config export found. {} \
See: https://nextjs.org/docs/messages/invalid-page-config", details);
let message = format!("Invalid page config export found. {details} \
See: https://nextjs.org/docs/messages/invalid-page-config");
HANDLER.with(|handler| handler.struct_span_err(span, &message).emit());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@ fn extract_value(ctx: &ExprCtx, init: &Expr, id: String) -> Option<Const> {
Some(elem) => {
if elem.spread.is_some() {
return Some(Const::Unsupported(format!(
"Unsupported spread operator in the Array Expression at \"{}\"",
id
"Unsupported spread operator in the Array Expression at \"{id}\""
)));
}

Expand Down Expand Up @@ -139,21 +138,19 @@ fn extract_value(ctx: &ExprCtx, init: &Expr, id: String) -> Option<Const> {
PropName::Str(s) => s.value.as_ref(),
_ => {
return Some(Const::Unsupported(format!(
"Unsupported key type in the Object Expression at \"{}\"",
id
"Unsupported key type in the Object Expression at \"{id}\""
)))
}
},
&kv.value,
),
_ => {
return Some(Const::Unsupported(format!(
"Unsupported spread operator in the Object Expression at \"{}\"",
id
"Unsupported spread operator in the Object Expression at \"{id}\""
)))
}
};
let new_value = extract_value(ctx, value, format!("{}.{}", id, key));
let new_value = extract_value(ctx, value, format!("{id}.{key}"));
if let Some(Const::Unsupported(msg)) = new_value {
return Some(Const::Unsupported(msg));
}
Expand All @@ -169,8 +166,7 @@ fn extract_value(ctx: &ExprCtx, init: &Expr, id: String) -> Option<Const> {
// [TODO] should we add support for `${'e'}d${'g'}'e'`?
if !tpl.exprs.is_empty() {
Some(Const::Unsupported(format!(
"Unsupported template literal with expressions at \"{}\".",
id
"Unsupported template literal with expressions at \"{id}\"."
)))
} else {
Some(
Expand All @@ -196,15 +192,13 @@ fn extract_value(ctx: &ExprCtx, init: &Expr, id: String) -> Option<Const> {
))
})
.unwrap_or(Const::Unsupported(format!(
"Unsupported node type at \"{}\"",
id
"Unsupported node type at \"{id}\""
))),
)
}
}
_ => Some(Const::Unsupported(format!(
"Unsupported node type at \"{}\"",
id
"Unsupported node type at \"{id}\""
))),
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ fn report_error(app_dir: &Option<PathBuf>, filepath: &str, error_kind: RSCErrorK
"react-dom/server" => "You're importing a component that imports react-dom/server. To fix it, render or return the content directly as a Server Component instead for perf and security.\nLearn more: https://nextjs.org/docs/getting-started/react-essentials".to_string(),
// If importing "next/router", we should tell them to use "next/navigation".
"next/router" => r#"You have a Server Component that imports next/router. Use next/navigation instead.\nLearn more: https://nextjs.org/docs/app/api-reference/functions/use-router"#.to_string(),
_ => format!(r#"You're importing a component that imports {}. It only works in a Client Component but none of its parents are marked with "use client", so they're Server Components by default.\nLearn more: https://nextjs.org/docs/getting-started/react-essentials\n\n"#, source)
_ => format!(r#"You're importing a component that imports {source}. It only works in a Client Component but none of its parents are marked with "use client", so they're Server Components by default.\nLearn more: https://nextjs.org/docs/getting-started/react-essentials\n\n"#)
};

(msg, span)
Expand All @@ -257,37 +257,37 @@ fn report_error(app_dir: &Option<PathBuf>, filepath: &str, error_kind: RSCErrorK
.unwrap_or_default();

let msg = if !is_app_dir {
format!("You're importing a component that needs \"{}\". That only works in a Server Component which is not supported in the pages/ directory. Read more: https://nextjs.org/docs/getting-started/react-essentials#server-components\n\n", source)
format!("You're importing a component that needs \"{source}\". That only works in a Server Component which is not supported in the pages/ directory. Read more: https://nextjs.org/docs/getting-started/react-essentials#server-components\n\n")
} else {
format!("You're importing a component that needs \"{}\". That only works in a Server Component but one of its parents is marked with \"use client\", so it's a Client Component.\nLearn more: https://nextjs.org/docs/getting-started/react-essentials\n\n", source)
format!("You're importing a component that needs \"{source}\". That only works in a Server Component but one of its parents is marked with \"use client\", so it's a Client Component.\nLearn more: https://nextjs.org/docs/getting-started/react-essentials\n\n")
};
(msg, span)
}
RSCErrorKind::NextRscErrReactApi((source, span)) => {
let msg = if source == "Component" {
"You’re importing a class component. It only works in a Client Component but none of its parents are marked with \"use client\", so they're Server Components by default.\nLearn more: https://nextjs.org/docs/getting-started/react-essentials#client-components\n\n".to_string()
} else {
format!("You're importing a component that needs `{}`. This React hook only works in a client component. To fix, mark the file (or its parent) with the `\"use client\"` directive.\n\n Learn more: https://nextjs.org/docs/app/building-your-application/rendering/client-components\n\n", source)
format!("You're importing a component that needs `{source}`. This React hook only works in a client component. To fix, mark the file (or its parent) with the `\"use client\"` directive.\n\n Learn more: https://nextjs.org/docs/app/building-your-application/rendering/client-components\n\n")
};

(msg,span)
},
RSCErrorKind::NextRscErrErrorFileServerComponent(span) => {
(
format!("{} must be a Client Component. Add the \"use client\" directive the top of the file to resolve this issue.\nLearn more: https://nextjs.org/docs/getting-started/react-essentials#client-components\n\n", filepath),
format!("{filepath} must be a Client Component. Add the \"use client\" directive the top of the file to resolve this issue.\nLearn more: https://nextjs.org/docs/getting-started/react-essentials#client-components\n\n"),
span
)
},
RSCErrorKind::NextRscErrClientMetadataExport((source, span)) => {
(format!("You are attempting to export \"{}\" from a component marked with \"use client\", which is disallowed. Either remove the export, or the \"use client\" directive. Read more: https://nextjs.org/docs/getting-started/react-essentials#the-use-client-directive\n\n", source), span)
(format!("You are attempting to export \"{source}\" from a component marked with \"use client\", which is disallowed. Either remove the export, or the \"use client\" directive. Read more: https://nextjs.org/docs/getting-started/react-essentials#the-use-client-directive\n\n"), span)
},
RSCErrorKind::NextRscErrConflictMetadataExport(span) => (
"\"metadata\" and \"generateMetadata\" cannot be exported at the same time, please keep one of them. Read more: https://nextjs.org/docs/app/api-reference/file-conventions/metadata\n\n".to_string(),
span
),
//NEXT_RSC_ERR_INVALID_API
RSCErrorKind::NextRscErrInvalidApi((source, span)) => (
format!("\"{}\" is not supported in app/. Read more: https://nextjs.org/docs/app/building-your-application/data-fetching\n\n", source), span
format!("\"{source}\" is not supported in app/. Read more: https://nextjs.org/docs/app/building-your-application/data-fetching\n\n"), span
),
};

Expand Down Expand Up @@ -833,7 +833,7 @@ pub fn server_components_assert(
};

let filename = match filename {
FileName::Custom(path) => format!("<{}>", path),
FileName::Custom(path) => format!("<{path}>"),
_ => filename.to_string(),
};
ReactServerComponentValidator::new(is_react_server_layer, filename, app_dir)
Expand All @@ -855,7 +855,7 @@ pub fn server_components<C: Comments>(
is_react_server_layer,
comments,
filepath: match filename {
FileName::Custom(path) => format!("<{}>", path),
FileName::Custom(path) => format!("<{path}>"),
_ => filename.to_string(),
},
app_dir,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ impl<C: Comments> ServerActions<C> {
let mut pats = vec![];
for i in 0..ids_from_closure.len() {
pats.push(Some(Pat::Ident(
Ident::new(format!("$$ACTION_ARG_{}", i).into(), DUMMY_SP).into(),
Ident::new(format!("$$ACTION_ARG_{i}").into(), DUMMY_SP).into(),
)));
}
let decryption_decl = VarDecl {
Expand Down Expand Up @@ -326,7 +326,7 @@ impl<C: Comments> ServerActions<C> {
let mut pats = vec![];
for i in 0..ids_from_closure.len() {
pats.push(Some(Pat::Ident(
Ident::new(format!("$$ACTION_ARG_{}", i).into(), DUMMY_SP).into(),
Ident::new(format!("$$ACTION_ARG_{i}").into(), DUMMY_SP).into(),
)));
}
let decryption_decl = VarDecl {
Expand Down Expand Up @@ -1228,7 +1228,7 @@ fn retain_names_from_declared_idents(child_names: &mut Vec<Name>, current_declar
}

fn gen_ident(cnt: &mut u32) -> JsWord {
let id: JsWord = format!("$$ACTION_{}", cnt).into();
let id: JsWord = format!("$$ACTION_{cnt}").into();
*cnt += 1;
id
}
Expand Down Expand Up @@ -1402,9 +1402,8 @@ fn remove_server_directive_index_in_module(
.struct_span_err(
*span,
format!(
"Did you mean \"use server\"? \"{}\" is not a supported \
directive name.",
value
"Did you mean \"use server\"? \"{value}\" is not a supported \
directive name."
)
.as_str(),
)
Expand Down Expand Up @@ -1507,9 +1506,8 @@ fn remove_server_directive_index_in_fn(
.struct_span_err(
*span,
format!(
"Did you mean \"use server\"? \"{}\" is not a supported \
directive name.",
value
"Did you mean \"use server\"? \"{value}\" is not a supported \
directive name."
)
.as_str(),
)
Expand Down Expand Up @@ -1635,7 +1633,7 @@ impl VisitMut for ClosureReplacer<'_> {
if let Some(index) = self.index(e) {
*e = Expr::Ident(Ident::new(
// $$ACTION_ARG_0
format!("$$ACTION_ARG_{}", index).into(),
format!("$$ACTION_ARG_{index}").into(),
DUMMY_SP,
));
}
Expand All @@ -1651,7 +1649,7 @@ impl VisitMut for ClosureReplacer<'_> {
key: PropName::Ident(i.clone()),
value: Box::new(Expr::Ident(Ident::new(
// $$ACTION_ARG_0
format!("$$ACTION_ARG_{}", index).into(),
format!("$$ACTION_ARG_{index}").into(),
DUMMY_SP,
))),
})));
Expand Down
15 changes: 6 additions & 9 deletions scripts/send-trace-to-jaeger/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ where

/// Log the url to view the trace in the browser.
fn log_web_url(jaeger_web_ui_url: &str, trace_id: &str) {
println!(
"Jaeger trace will be available on {}/trace/{}",
jaeger_web_ui_url, trace_id
)
println!("Jaeger trace will be available on {jaeger_web_ui_url}/trace/{trace_id}")
}

/// Send trace JSON to Jaeger using ZipKin API.
Expand All @@ -45,7 +42,7 @@ fn send_json_to_zipkin(zipkin_api: &str, value: String) {
fn pad_zeros(num: u64) -> String {
let mut num_str = num.to_string();
while num_str.len() < 16 {
num_str = format!("0{}", num_str);
num_str = format!("0{num_str}");
}
num_str
}
Expand All @@ -54,9 +51,9 @@ fn main() {
let service_name = "nextjs";
let ipv4 = "127.0.0.1";
let port = 9411;
let zipkin_url = format!("http://{}:{}", ipv4, port);
let jaeger_web_ui_url = format!("http://{}:16686", ipv4);
let zipkin_api = format!("{}/api/v2/spans", zipkin_url);
let zipkin_url = format!("http://{ipv4}:{port}");
let jaeger_web_ui_url = format!("http://{ipv4}:16686");
let zipkin_api = format!("{zipkin_url}/api/v2/spans");
let mut logged_url = false;

let mut local_endpoint = Map::new();
Expand Down Expand Up @@ -100,7 +97,7 @@ fn main() {
})
.collect::<Value>(),
Err(e) => {
println!("{}", e);
println!("{e}");
continue;
}
};
Expand Down

0 comments on commit 16cc5fa

Please sign in to comment.