Skip to content

Commit 44a8302

Browse files
authored
refactor(BREAKING): external formatter accepts string instead of MediaType (#723)
1 parent 526fe02 commit 44a8302

File tree

4 files changed

+22
-29
lines changed

4 files changed

+22
-29
lines changed

src/format_text.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,8 @@ mod test {
159159
extension: None,
160160
text: "const content = html`<div>broken html</p>`".into(),
161161
config: &config,
162-
external_formatter: Some(&|media_type, _text, _config| {
163-
assert!(matches!(media_type, deno_ast::MediaType::Html));
162+
external_formatter: Some(&|lang, _text, _config| {
163+
assert!(matches!(lang, "html"));
164164
Err(anyhow::anyhow!("Syntax error from external formatter"))
165165
}),
166166
});

src/generation/context.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ use super::*;
1818
use crate::configuration::*;
1919
use crate::utils::Stack;
2020

21-
/// A callback that will be called when encountering certain tagged templates.
21+
/// A callback that will be called when encountering tagged templates.
2222
///
23-
/// Currently supports `css`, `html` and `sql` tagged templated.
23+
/// It is up to the caller to decide if a certain tagged template should be formatted
24+
/// by the external formatter.
2425
///
2526
/// Examples:
2627
/// ```ignore
@@ -41,11 +42,11 @@ use crate::utils::Stack;
4142
/// active IS TRUE;
4243
/// ```
4344
///
44-
/// External formatter should return `None` if it doesn't understand given `MediaType`, in such
45+
/// External formatter should return `None` if it doesn't understand given language, in such
4546
/// cases the templates will be left as they are.
4647
///
4748
/// Only templates with no interpolation are supported.
48-
pub type ExternalFormatter = dyn Fn(MediaType, String, &Configuration) -> anyhow::Result<Option<String>>;
49+
pub type ExternalFormatter = dyn Fn(&str, String, &Configuration) -> anyhow::Result<Option<String>>;
4950

5051
pub(crate) struct GenerateDiagnostic {
5152
pub message: String,

src/generation/generate.rs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3016,13 +3016,13 @@ fn gen_spread_element<'a>(node: &SpreadElement<'a>, context: &mut Context<'a>) -
30163016
/// Detects the type of embedded language automatically.
30173017
fn maybe_gen_tagged_tpl_with_external_formatter<'a>(node: &TaggedTpl<'a>, context: &mut Context<'a>) -> Option<PrintItems> {
30183018
let external_formatter = context.external_formatter.as_ref()?;
3019-
let media_type = detect_embedded_language_type(node)?;
3019+
let embedded_lang = normalize_embedded_language_type(node)?;
30203020

30213021
let placeholder_css = "@dpr1nt_";
30223022
let placeholder_other = "dpr1nt_";
30233023
// First creates text with placeholders for the expressions.
3024-
let placeholder_text = match media_type {
3025-
MediaType::Css => placeholder_css,
3024+
let placeholder_text = match embedded_lang {
3025+
"css" => placeholder_css,
30263026
_ => placeholder_other,
30273027
};
30283028
let text = capacity_builder::StringBuilder::<String>::build(|builder| {
@@ -3043,7 +3043,7 @@ fn maybe_gen_tagged_tpl_with_external_formatter<'a>(node: &TaggedTpl<'a>, contex
30433043
.unwrap();
30443044

30453045
// Then formats the text with the external formatter.
3046-
let formatted_tpl = match external_formatter(media_type, text.replace(r"\\", "\\"), context.config) {
3046+
let formatted_tpl = match external_formatter(embedded_lang, text.replace(r"\\", "\\"), context.config) {
30473047
Ok(formatted_tpl) => formatted_tpl?.replace("\\", r"\\"),
30483048
Err(err) => {
30493049
context.diagnostics.push(context::GenerateDiagnostic {
@@ -3102,29 +3102,22 @@ fn maybe_gen_tagged_tpl_with_external_formatter<'a>(node: &TaggedTpl<'a>, contex
31023102
Some(items)
31033103
}
31043104

3105-
/// Detects the type of embedded language in a tagged template literal.
3106-
fn detect_embedded_language_type<'a>(node: &TaggedTpl<'a>) -> Option<MediaType> {
3105+
/// Normalizes the type of embedded language in a tagged template literal.
3106+
fn normalize_embedded_language_type<'a>(node: &TaggedTpl<'a>) -> Option<&'a str> {
31073107
match node.tag {
3108-
Expr::Ident(ident) => {
3109-
match ident.sym().as_str() {
3110-
"css" => Some(MediaType::Css), // css`...`
3111-
"html" => Some(MediaType::Html), // html`...`
3112-
"sql" => Some(MediaType::Sql), // sql`...`
3113-
_ => None,
3114-
}
3115-
}
3108+
Expr::Ident(ident) => return Some(ident.sym().as_str()),
31163109
Expr::Member(member_expr) => {
31173110
if let Expr::Ident(ident) = member_expr.obj {
31183111
if ident.sym().as_str() == "styled" {
3119-
return Some(MediaType::Css); // styled.foo`...`
3112+
return Some("css"); // styled.foo`...`
31203113
}
31213114
}
31223115
None
31233116
}
31243117
Expr::Call(call_expr) => {
31253118
if let Callee::Expr(Expr::Ident(ident)) = call_expr.callee {
31263119
if ident.sym().as_str() == "styled" {
3127-
return Some(MediaType::Css); // styled(Button)`...`
3120+
return Some("css"); // styled(Button)`...`
31283121
}
31293122
}
31303123
None

tests/spec_test.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@ use std::path::{Path, PathBuf};
22
use std::sync::Arc;
33

44
use anyhow::Result;
5-
use deno_ast::MediaType;
65
use dprint_core::configuration::*;
76
use dprint_development::*;
87
use dprint_plugin_typescript::configuration::*;
98
use dprint_plugin_typescript::*;
109

11-
fn external_formatter(media_type: MediaType, text: String, config: &Configuration) -> Result<Option<String>> {
12-
match media_type {
13-
MediaType::Css => format_embedded_css(&text, config),
14-
MediaType::Html => format_html(&text, config),
15-
MediaType::Sql => format_sql(&text, config),
16-
_ => unreachable!(),
10+
fn external_formatter(lang: &str, text: String, config: &Configuration) -> Result<Option<String>> {
11+
match lang {
12+
"css" => format_embedded_css(&text, config),
13+
"html" => format_html(&text, config),
14+
"sql" => format_sql(&text, config),
15+
_ => Ok(None),
1716
}
1817
}
1918

0 commit comments

Comments
 (0)