diff --git a/apps/oxfmt/src-js/libs/prettier.ts b/apps/oxfmt/src-js/libs/prettier.ts index dce02e193af77..b90686f77ceaf 100644 --- a/apps/oxfmt/src-js/libs/prettier.ts +++ b/apps/oxfmt/src-js/libs/prettier.ts @@ -72,10 +72,12 @@ export async function formatEmbeddedCode({ // SAFETY: `options` is created in Rust side, so it's safe to mutate here options.parser = parserName; - return prettier - .format(code, options) - .then((formatted) => formatted.trimEnd()) - .catch(() => code); + + // NOTE: This will throw if: + // - Specified parser is not available + // - Or, code has syntax errors + // In such cases, Rust side will fallback to original code + return prettier.format(code, options); } // --- diff --git a/apps/oxfmt/src/core/external_formatter.rs b/apps/oxfmt/src/core/external_formatter.rs index c2991e8a69ecf..872ccbfe567af 100644 --- a/apps/oxfmt/src/core/external_formatter.rs +++ b/apps/oxfmt/src/core/external_formatter.rs @@ -230,7 +230,12 @@ fn wrap_format_embedded(cb: JsFormatEmbeddedCb) -> FormatEmbeddedWithConfigCallb .await; match status { Ok(promise) => match promise.await { - Ok(formatted_code) => Ok(formatted_code), + Ok(mut formatted_code) => { + // Trim trailing newline added by Prettier without allocation + let trimmed_len = formatted_code.trim_end().len(); + formatted_code.truncate(trimmed_len); + Ok(formatted_code) + } Err(err) => { Err(format!("JS formatter promise rejected for tag '{tag_name}': {err}")) } diff --git a/apps/oxfmt/test/cli/embedded_languages/__snapshots__/embedded_languages.test.ts.snap b/apps/oxfmt/test/cli/embedded_languages/__snapshots__/embedded_languages.test.ts.snap index 1f7408178f9ac..69f5530c5f7e6 100644 --- a/apps/oxfmt/test/cli/embedded_languages/__snapshots__/embedded_languages.test.ts.snap +++ b/apps/oxfmt/test/cli/embedded_languages/__snapshots__/embedded_languages.test.ts.snap @@ -86,7 +86,7 @@ const ignoredGql = gql\`query GetUser($id:ID!){user(id:$id){name email}}\`; const normalGql = gql\`query GetPosts{posts{title author}}\`; // ============================================================================ -// Unsupported Tags - Tags not recognized by the formatter +// Unsupported Tags - Tags not recognized by the formatter, to be left unformatted // ============================================================================ const unknown = customTag\` @@ -118,6 +118,22 @@ const sql = sql\` SELECT * FROM users WHERE id = 1 \`; +// ============================================================================ +// Supported Tags contains invalid syntax - To be left unformatted too +// ============================================================================ + +// Value only, key missing +const invalidCss = css\` + repeating-linear-gradient( + 0deg, +var(--schemas-lines-color), +var(--schemas-lines-color) 3px, + transparent 3px, + transparent 5px, + var(--schemas-lines-color) 5px + ); +\`; + --- AFTER ---------- // ============================================================================ // CSS - Tagged template literals with css and styled tags @@ -269,7 +285,7 @@ const normalGql = gql\` \`; // ============================================================================ -// Unsupported Tags - Tags not recognized by the formatter +// Unsupported Tags - Tags not recognized by the formatter, to be left unformatted // ============================================================================ const unknown = customTag\` @@ -301,6 +317,22 @@ const sql = sql\` SELECT * FROM users WHERE id = 1 \`; +// ============================================================================ +// Supported Tags contains invalid syntax - To be left unformatted too +// ============================================================================ + +// Value only, key missing +const invalidCss = css\` + repeating-linear-gradient( + 0deg, +var(--schemas-lines-color), +var(--schemas-lines-color) 3px, + transparent 3px, + transparent 5px, + var(--schemas-lines-color) 5px + ); +\`; + --------------------" `; @@ -390,7 +422,7 @@ const ignoredGql = gql\`query GetUser($id:ID!){user(id:$id){name email}}\`; const normalGql = gql\`query GetPosts{posts{title author}}\`; // ============================================================================ -// Unsupported Tags - Tags not recognized by the formatter +// Unsupported Tags - Tags not recognized by the formatter, to be left unformatted // ============================================================================ const unknown = customTag\` @@ -422,6 +454,22 @@ const sql = sql\` SELECT * FROM users WHERE id = 1 \`; +// ============================================================================ +// Supported Tags contains invalid syntax - To be left unformatted too +// ============================================================================ + +// Value only, key missing +const invalidCss = css\` + repeating-linear-gradient( + 0deg, +var(--schemas-lines-color), +var(--schemas-lines-color) 3px, + transparent 3px, + transparent 5px, + var(--schemas-lines-color) 5px + ); +\`; + --- AFTER ---------- // ============================================================================ // CSS - Tagged template literals with css and styled tags @@ -573,7 +621,7 @@ const normalGql = gql\` \`; // ============================================================================ -// Unsupported Tags - Tags not recognized by the formatter +// Unsupported Tags - Tags not recognized by the formatter, to be left unformatted // ============================================================================ const unknown = customTag\` @@ -605,6 +653,22 @@ const sql = sql\` SELECT * FROM users WHERE id = 1 \`; +// ============================================================================ +// Supported Tags contains invalid syntax - To be left unformatted too +// ============================================================================ + +// Value only, key missing +const invalidCss = css\` + repeating-linear-gradient( + 0deg, +var(--schemas-lines-color), +var(--schemas-lines-color) 3px, + transparent 3px, + transparent 5px, + var(--schemas-lines-color) 5px + ); +\`; + --------------------" `; @@ -694,7 +758,7 @@ const ignoredGql = gql\`query GetUser($id:ID!){user(id:$id){name email}}\`; const normalGql = gql\`query GetPosts{posts{title author}}\`; // ============================================================================ -// Unsupported Tags - Tags not recognized by the formatter +// Unsupported Tags - Tags not recognized by the formatter, to be left unformatted // ============================================================================ const unknown = customTag\` @@ -726,6 +790,22 @@ const sql = sql\` SELECT * FROM users WHERE id = 1 \`; +// ============================================================================ +// Supported Tags contains invalid syntax - To be left unformatted too +// ============================================================================ + +// Value only, key missing +const invalidCss = css\` + repeating-linear-gradient( + 0deg, +var(--schemas-lines-color), +var(--schemas-lines-color) 3px, + transparent 3px, + transparent 5px, + var(--schemas-lines-color) 5px + ); +\`; + --- AFTER ---------- // ============================================================================ // CSS - Tagged template literals with css and styled tags @@ -809,7 +889,7 @@ const ignoredGql = gql\`query GetUser($id:ID!){user(id:$id){name email}}\`; const normalGql = gql\`query GetPosts{posts{title author}}\`; // ============================================================================ -// Unsupported Tags - Tags not recognized by the formatter +// Unsupported Tags - Tags not recognized by the formatter, to be left unformatted // ============================================================================ const unknown = customTag\` @@ -841,5 +921,21 @@ const sql = sql\` SELECT * FROM users WHERE id = 1 \`; +// ============================================================================ +// Supported Tags contains invalid syntax - To be left unformatted too +// ============================================================================ + +// Value only, key missing +const invalidCss = css\` + repeating-linear-gradient( + 0deg, +var(--schemas-lines-color), +var(--schemas-lines-color) 3px, + transparent 3px, + transparent 5px, + var(--schemas-lines-color) 5px + ); +\`; + --------------------" `; diff --git a/apps/oxfmt/test/cli/embedded_languages/fixtures/embedded_languages.js b/apps/oxfmt/test/cli/embedded_languages/fixtures/embedded_languages.js index bf39d19ab1435..1719c57f9ee06 100644 --- a/apps/oxfmt/test/cli/embedded_languages/fixtures/embedded_languages.js +++ b/apps/oxfmt/test/cli/embedded_languages/fixtures/embedded_languages.js @@ -80,7 +80,7 @@ const ignoredGql = gql`query GetUser($id:ID!){user(id:$id){name email}}`; const normalGql = gql`query GetPosts{posts{title author}}`; // ============================================================================ -// Unsupported Tags - Tags not recognized by the formatter +// Unsupported Tags - Tags not recognized by the formatter, to be left unformatted // ============================================================================ const unknown = customTag` @@ -111,3 +111,19 @@ const sql = sql` SELECT * FROM users WHERE id = 1 `; + +// ============================================================================ +// Supported Tags contains invalid syntax - To be left unformatted too +// ============================================================================ + +// Value only, key missing +const invalidCss = css` + repeating-linear-gradient( + 0deg, +var(--schemas-lines-color), +var(--schemas-lines-color) 3px, + transparent 3px, + transparent 5px, + var(--schemas-lines-color) 5px + ); +`;