Skip to content

Commit

Permalink
use different quote style in Vue directive (fix #18)
Browse files Browse the repository at this point in the history
  • Loading branch information
g-plane committed Apr 27, 2024
1 parent c0192cb commit 918d704
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 31 deletions.
23 changes: 19 additions & 4 deletions dprint_plugin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ use dprint_core::{
configuration::{ConfigKeyMap, GlobalConfiguration, ResolveConfigurationResult},
plugins::{FileMatchingInfo, PluginInfo, SyncPluginHandler, SyncPluginInfo},
};
use markup_fmt::{config::FormatOptions, detect_language, format_text, FormatError};
use markup_fmt::{
config::{FormatOptions, Quotes},
detect_language, format_text, FormatError,
};
use std::path::Path;

mod config;
Expand Down Expand Up @@ -75,14 +78,26 @@ impl SyncPluginHandler<FormatOptions> for MarkupFmtPluginHandler {
let mut additional_config = ConfigKeyMap::new();
additional_config.insert("lineWidth".into(), (print_width as i32).into());
additional_config.insert("printWidth".into(), (print_width as i32).into());
if let Some("expr.ts" | "binding.ts" | "type_params.ts") =
path.file_name().and_then(|s| s.to_str())
{

let file_name = path.file_name().and_then(|s| s.to_str());
if let Some("expr.ts" | "binding.ts" | "type_params.ts") = &file_name {
// dprint-plugin-typescript
additional_config.insert("semiColons".into(), "asi".into());
// Biome
additional_config.insert("semicolons".into(), "asNeeded".into());
}
if let Some("attr_expr.tsx") = &file_name {
// Only for dprint-plugin-typescript currently,
// because it conflicts with the `quoteStyle` option in Biome.
match config.language.quotes {
Quotes::Double => {
additional_config.insert("quoteStyle".into(), "alwaysSingle".into());
}
Quotes::Single => {
additional_config.insert("quoteStyle".into(), "alwaysDouble".into());
}
}
}

format_with_host(path, code.into(), &additional_config).and_then(|result| {
match result {
Expand Down
7 changes: 7 additions & 0 deletions dprint_plugin/tests/integration/biome/quotes.vue.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
source: dprint_plugin/tests/integration.rs
---
<template>
<div v-if='(label = "a")'></div>
<div v-if='(label = "a")'></div>
</template>
2 changes: 1 addition & 1 deletion dprint_plugin/tests/integration/dprint_ts/basic.vue.snap
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function greet(msg: string) {
</script>

<template>
<span v-if='name != ""'>{{ ":" + name }}</span>
<span v-if="name != ''">{{ ":" + name }}</span>
<button @click="greet(name)">Click</button>

<ul>
Expand Down
7 changes: 7 additions & 0 deletions dprint_plugin/tests/integration/dprint_ts/quotes.vue.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
source: dprint_plugin/tests/integration.rs
---
<template>
<div v-if="label = 'a'"></div>
<div v-if="label = 'a'"></div>
</template>
4 changes: 4 additions & 0 deletions dprint_plugin/tests/integration/quotes.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<template>
<div v-if="label = 'a'"></div>
<div v-if='label = "a"'></div>
</template>
12 changes: 10 additions & 2 deletions markup_fmt/src/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,15 @@ where
}
}

pub(crate) fn format_expr(&mut self, code: &str) -> String {
pub(crate) fn format_general_expr(&mut self, code: &str) -> String {
self.format_expr(code, Path::new("expr.tsx"))
}

pub(crate) fn format_attr_expr(&mut self, code: &str) -> String {
self.format_expr(code, Path::new("attr_expr.tsx"))
}

fn format_expr(&mut self, code: &str, path: &Path) -> String {
if code.trim().is_empty() {
String::new()
} else {
Expand All @@ -98,7 +106,7 @@ where
// though external formatter isn't available.
let wrapped = format!("<>{{{}}}</>", code.trim());
let formatted = self.format_with_external_formatter(
Path::new("expr.tsx"),
&path,
&wrapped,
self.print_width
.saturating_sub(self.indent_level)
Expand Down
48 changes: 24 additions & 24 deletions markup_fmt/src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl<'s> DocGen<'s> for AstroAttribute<'s> {
where
F: for<'a> FnMut(&Path, &'a str, usize) -> Result<Cow<'a, str>, E>,
{
let expr_code = ctx.format_expr(self.expr);
let expr_code = ctx.format_general_expr(self.expr);
let expr = Doc::text("{")
.concat(reflow_with_indent(&expr_code))
.append(Doc::text("}"));
Expand Down Expand Up @@ -58,7 +58,7 @@ impl<'s> DocGen<'s> for AstroExpr<'s> {
}
})
.join(PLACEHOLDER);
let formatted_script = ctx.format_expr(&script);
let formatted_script = ctx.format_general_expr(&script);

let templates = self.children.iter().filter_map(|child| {
if let AstroExprChild::Template(nodes) = child {
Expand Down Expand Up @@ -633,7 +633,7 @@ impl<'s> DocGen<'s> for NativeAttribute<'s> {
}
Language::Svelte if !ctx.options.strict_svelte_attr => {
if let Some(expr) = value.strip_prefix('{').and_then(|s| s.strip_suffix('}')) {
let formatted_expr = ctx.format_expr(expr);
let formatted_expr = ctx.format_general_expr(expr);
return match self.name.split_once(':') {
Some((_, name))
if matches!(ctx.options.svelte_directive_shorthand, Some(true))
Expand Down Expand Up @@ -762,7 +762,7 @@ impl<'s> DocGen<'s> for SvelteAtTag<'s> {
Doc::text("{@")
.append(Doc::text(self.name))
.append(Doc::space())
.append(Doc::text(ctx.format_expr(self.expr)))
.append(Doc::text(ctx.format_general_expr(self.expr)))
.append(Doc::text("}"))
}
}
Expand All @@ -772,7 +772,7 @@ impl<'s> DocGen<'s> for SvelteAttribute<'s> {
where
F: for<'a> FnMut(&Path, &'a str, usize) -> Result<Cow<'a, str>, E>,
{
let expr_code = ctx.format_expr(self.expr);
let expr_code = ctx.format_general_expr(self.expr);
let expr = Doc::text("{")
.concat(reflow_with_indent(&expr_code))
.append(Doc::text("}"));
Expand Down Expand Up @@ -828,7 +828,7 @@ impl<'s> DocGen<'s> for SvelteAwaitBlock<'s> {
{
let mut head = Vec::with_capacity(5);
head.push(Doc::text("{#await "));
head.push(Doc::text(ctx.format_expr(self.expr)));
head.push(Doc::text(ctx.format_general_expr(self.expr)));

if let Some(then_binding) = self.then_binding {
head.push(Doc::line_or_space());
Expand Down Expand Up @@ -893,7 +893,7 @@ impl<'s> DocGen<'s> for SvelteEachBlock<'s> {
{
let mut head = Vec::with_capacity(5);
head.push(Doc::text("{#each "));
head.push(Doc::text(ctx.format_expr(self.expr)));
head.push(Doc::text(ctx.format_general_expr(self.expr)));
head.push(Doc::text(" as"));
head.push(Doc::line_or_space());
head.push(Doc::text(ctx.format_binding(self.binding)));
Expand All @@ -907,7 +907,7 @@ impl<'s> DocGen<'s> for SvelteEachBlock<'s> {
if let Some(key) = self.key {
head.push(Doc::line_or_space());
head.push(Doc::text("("));
head.push(Doc::text(ctx.format_expr(key)));
head.push(Doc::text(ctx.format_general_expr(key)));
head.push(Doc::text(")"));
}

Expand Down Expand Up @@ -943,7 +943,7 @@ impl<'s> DocGen<'s> for SvelteElseIfBlock<'s> {
F: for<'a> FnMut(&Path, &'a str, usize) -> Result<Cow<'a, str>, E>,
{
Doc::text("{:else if ")
.append(Doc::text(ctx.format_expr(self.expr)))
.append(Doc::text(ctx.format_general_expr(self.expr)))
.append(Doc::text("}"))
.append(format_control_structure_block_children(
&self.children,
Expand All @@ -960,7 +960,7 @@ impl<'s> DocGen<'s> for SvelteIfBlock<'s> {
{
let mut docs = Vec::with_capacity(5);
docs.push(Doc::text("{#if "));
docs.push(Doc::text(ctx.format_expr(self.expr)));
docs.push(Doc::text(ctx.format_general_expr(self.expr)));
docs.push(Doc::text("}"));
docs.push(format_control_structure_block_children(
&self.children,
Expand Down Expand Up @@ -993,7 +993,7 @@ impl<'s> DocGen<'s> for SvelteInterpolation<'s> {
{
Doc::text("{")
.append(Doc::line_or_nil())
.concat(reflow_with_indent(&ctx.format_expr(self.expr)))
.concat(reflow_with_indent(&ctx.format_general_expr(self.expr)))
.nest_with_ctx(ctx)
.append(Doc::line_or_nil())
.append(Doc::text("}"))
Expand All @@ -1007,7 +1007,7 @@ impl<'s> DocGen<'s> for SvelteKeyBlock<'s> {
F: for<'a> FnMut(&Path, &'a str, usize) -> Result<Cow<'a, str>, E>,
{
Doc::text("{#key ")
.append(Doc::text(ctx.format_expr(self.expr)))
.append(Doc::text(ctx.format_general_expr(self.expr)))
.append(Doc::text("}"))
.append(format_control_structure_block_children(
&self.children,
Expand Down Expand Up @@ -1117,9 +1117,9 @@ impl<'s> DocGen<'s> for VentoInterpolation<'s> {
Doc::text("{{")
.append(Doc::line_or_space())
.concat(itertools::intersperse(
self.expr
.split("|>")
.map(|expr| Doc::list(reflow_with_indent(&ctx.format_expr(expr)).collect())),
self.expr.split("|>").map(|expr| {
Doc::list(reflow_with_indent(&ctx.format_general_expr(expr)).collect())
}),
Doc::line_or_space()
.append(Doc::text("|>"))
.append(Doc::space()),
Expand Down Expand Up @@ -1182,13 +1182,13 @@ impl<'s> DocGen<'s> for VentoTag<'s> {
let (template, data) = rest.split_at(index);
Doc::text(tag_name.to_string())
.append(Doc::space())
.concat(reflow_with_indent(&ctx.format_expr(template)))
.concat(reflow_with_indent(&ctx.format_general_expr(template)))
.append(Doc::text(" "))
.concat(reflow_with_indent(&ctx.format_expr(data)))
.concat(reflow_with_indent(&ctx.format_general_expr(data)))
} else {
Doc::text(tag_name.to_string())
.append(Doc::space())
.concat(reflow_with_indent(&ctx.format_expr(parsed_tag.1)))
.concat(reflow_with_indent(&ctx.format_general_expr(parsed_tag.1)))
}
} else if parsed_tag.0 == "function" || parsed_tag.1.starts_with("function") {
// unsupported at present
Expand All @@ -1199,7 +1199,7 @@ impl<'s> DocGen<'s> for VentoTag<'s> {
.append(Doc::space())
.concat(reflow_with_indent(&ctx.format_binding(binding)))
.append(Doc::text(" = "))
.concat(reflow_with_indent(&ctx.format_expr(expr)))
.concat(reflow_with_indent(&ctx.format_general_expr(expr)))
} else {
Doc::text(tag_name.to_string())
.append(Doc::space())
Expand Down Expand Up @@ -1349,11 +1349,11 @@ impl<'s> DocGen<'s> for VueDirective<'s> {
};
format_v_for(left, delimiter, right, ctx)
} else {
ctx.format_expr(value)
ctx.format_attr_expr(value)
}
}
"#" | "slot" => ctx.format_binding(value),
_ => ctx.format_expr(value),
_ => ctx.format_attr_expr(value),
};
if !(matches!(ctx.options.v_bind_same_name_short_hand, Some(true))
&& is_v_bind
Expand Down Expand Up @@ -1385,7 +1385,7 @@ impl<'s> DocGen<'s> for VueInterpolation<'s> {
{
Doc::text("{{")
.append(Doc::line_or_space())
.concat(reflow_with_indent(&ctx.format_expr(self.expr)))
.concat(reflow_with_indent(&ctx.format_general_expr(self.expr)))
.nest_with_ctx(ctx)
.append(Doc::line_or_space())
.append(Doc::text("}}"))
Expand Down Expand Up @@ -1736,8 +1736,8 @@ fn format_v_for<'s, E, F>(
where
F: for<'a> FnMut(&Path, &'a str, usize) -> Result<Cow<'a, str>, E>,
{
let left = ctx.format_expr(left);
let right = ctx.format_expr(right);
let left = ctx.format_general_expr(left);
let right = ctx.format_general_expr(right);
if left.contains(',') && !left.contains('(') {
format!("({left}) {delimiter} {right}")
} else {
Expand Down

0 comments on commit 918d704

Please sign in to comment.