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
10 changes: 6 additions & 4 deletions apps/oxfmt/src-js/libs/prettier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

// ---
Expand Down
7 changes: 6 additions & 1 deletion apps/oxfmt/src/core/external_formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}"))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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\`
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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\`
Expand Down Expand Up @@ -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
);
\`;

--------------------"
`;

Expand Down Expand Up @@ -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\`
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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\`
Expand Down Expand Up @@ -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
);
\`;

--------------------"
`;

Expand Down Expand Up @@ -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\`
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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\`
Expand Down Expand Up @@ -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
);
\`;

--------------------"
`;
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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
);
`;
Loading