Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions apps/oxfmt/test/tailwindcss/tailwindcss.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,7 @@ describe("Tailwind CSS Sorting works with other options", () => {
const input = `
<div className={clsx('text-md before:content-["hello"]')}>Hello</div>;
<div className={clsx("text-md before:content-['hello']")}>Hello</div>;
<div className={showLandingPage ? "container pb-6" : 'hidden'}>title</div>
`;

const result = await format("test.tsx", input, {
Expand All @@ -1067,6 +1068,7 @@ describe("Tailwind CSS Sorting works with other options", () => {
expect(result.code).toMatchInlineSnapshot(`
"<div className={clsx('text-md before:content-["hello"]')}>Hello</div>;
<div className={clsx("text-md before:content-['hello']")}>Hello</div>;
<div className={showLandingPage ? 'container pb-6' : 'hidden'}>title</div>;
"
`);
});
Expand All @@ -1075,6 +1077,7 @@ describe("Tailwind CSS Sorting works with other options", () => {
const input = `
<div className={clsx('text-md before:content-["hello"]')}>Hello</div>;
<div className={clsx("text-md before:content-['hello']")}>Hello</div>;
<div className={showLandingPage ? "container pb-6" : 'hidden'}>title</div>
`;

const result = await format("test.tsx", input, {
Expand All @@ -1086,6 +1089,7 @@ describe("Tailwind CSS Sorting works with other options", () => {
expect(result.code).toMatchInlineSnapshot(`
"<div className={clsx('text-md before:content-["hello"]')}>Hello</div>;
<div className={clsx("text-md before:content-['hello']")}>Hello</div>;
<div className={showLandingPage ? "container pb-6" : "hidden"}>title</div>;
"
`);
});
Expand Down
6 changes: 1 addition & 5 deletions crates/oxc_formatter/src/formatter/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ use super::{Comments, SourceText};
/// Entry in the Tailwind context stack, tracking whether we're inside a Tailwind class context.
#[derive(Clone, Copy, Debug)]
pub struct TailwindContextEntry {
/// Whether the context is inside a JSX attribute (affects quote style).
pub is_jsx: bool,
/// Whether to preserve whitespace (newlines) in template literals.
pub preserve_whitespace: bool,
/// Whether we're inside a template literal expression (between `${` and `}`).
Expand All @@ -42,9 +40,8 @@ pub struct TailwindContextEntry {

impl TailwindContextEntry {
/// Create a new context entry for JSX attributes or function calls.
pub fn new(is_jsx: bool, preserve_whitespace: bool) -> Self {
pub fn new(preserve_whitespace: bool) -> Self {
Self {
is_jsx,
preserve_whitespace,
in_template_expression: false,
quasi_before_has_trailing_ws: true, // Default: can collapse
Expand All @@ -63,7 +60,6 @@ impl TailwindContextEntry {
quasi_after_has_leading_ws: bool,
) -> Self {
Self {
is_jsx: parent.is_jsx,
preserve_whitespace: parent.preserve_whitespace,
in_template_expression: true,
quasi_before_has_trailing_ws,
Expand Down
4 changes: 3 additions & 1 deletion crates/oxc_formatter/src/utils/tailwindcss.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,9 @@ pub fn write_tailwind_string_literal<'a>(

let normalized_string = FormatLiteralStringToken::new(
f.source_text().text_for(&string_literal),
ctx.is_jsx,
// `className="string"`
// ^^^^^^^^
matches!(string_literal.parent, AstNodes::JSXAttribute(_)),
StringLiteralParentKind::Expression,
)
.clean_text(f);
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_formatter/src/write/jsx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ impl<'a> FormatWrite<'a> for AstNode<'a, JSXAttribute<'a>> {
.experimental_tailwindcss
.as_ref()
.filter(|opts| is_tailwind_jsx_attribute(&self.name, opts))
.map(|opts| TailwindContextEntry::new(true, opts.preserve_whitespace));
.map(|opts| TailwindContextEntry::new(opts.preserve_whitespace));

if let Some(ctx) = tailwind_ctx_to_push {
f.context_mut().push_tailwind_context(ctx);
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_formatter/src/write/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ impl<'a> FormatWrite<'a> for AstNode<'a, CallExpression<'a>> {
f.options()
.experimental_tailwindcss
.as_ref()
.map(|opts| TailwindContextEntry::new(false, opts.preserve_whitespace))
.map(|opts| TailwindContextEntry::new(opts.preserve_whitespace))
} else {
None
};
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_formatter/src/write/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl<'a> FormatWrite<'a> for AstNode<'a, TaggedTemplateExpression<'a>> {
.experimental_tailwindcss
.as_ref()
.filter(|opts| is_tailwind_function_call(&self.tag, opts))
.map(|opts| TailwindContextEntry::new(false, opts.preserve_whitespace));
.map(|opts| TailwindContextEntry::new(opts.preserve_whitespace));

if let Some(ctx) = tailwind_ctx_to_push {
f.context_mut().push_tailwind_context(ctx);
Expand Down
Loading