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
36 changes: 12 additions & 24 deletions apps/oxfmt/test/api/xxx-in-js-conformance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,21 @@ describe("Prettier conformance for graphql-in-js", () => {
"format.test.js",
"comment-tag.js", // /* GraphQL */
]);
// TODO: Fix in next PR
// graphqlFixtures.push(
// {
// name: "inline/gql-trailing-space.js",
// content: `export const schema = gql\`
// type Mutation {
// create__TYPE_NAME__(input: Create__TYPE_NAME__Input!): __TYPE_NAME__!
// @skipAuth
// update__TYPE_NAME__(
// id: Int!
// input: Update__TYPE_NAME__Input!
// ): __TYPE_NAME__! @skipAuth
// delete__TYPE_NAME__(id: Int!): __TYPE_NAME__! @skipAuth
// }
// \`
// `,
// },
// );

graphqlFixtures.push({
name: "inline/gql-dummy.js",
name: "inline/gql-trailing-space.js",
content: `export const schema = gql\`
type Relation {
id: Int!
name: String!
type Mutation {
create__TYPE_NAME__(input: Create__TYPE_NAME__Input!): __TYPE_NAME__!
@skipAuth
update__TYPE_NAME__(
id: Int!
input: Update__TYPE_NAME__Input!
): __TYPE_NAME__! @skipAuth
delete__TYPE_NAME__(id: Int!): __TYPE_NAME__! @skipAuth
}
\`
`,
\`
`,
});

describe.concurrent.each(graphqlFixtures)("$name", ({ name, content }) => {
Expand Down
11 changes: 11 additions & 0 deletions crates/oxc_data_structures/src/code_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,17 @@ impl CodeBuffer {
unsafe { self.buf.set_len(len + bytes) };
}

/// Remove trailing whitespace (spaces and tabs) from the buffer.
///
/// This trims trailing whitespace before line breaks, matching Prettier's `trimEnd(out)` behavior.
/// <https://github.com/prettier/prettier/blob/90983f40dce5e20beea4e5618b5e0426a6a7f4f0/src/document/printer/printer.js#L535>
#[inline]
pub fn trim_trailing_ascii_whitespace(&mut self) {
while self.buf.last().is_some_and(|&b| b == b' ' || b == b'\t') {
self.buf.pop();
}
}

/// Get contents of buffer as a byte slice.
///
/// # Example
Expand Down
1 change: 1 addition & 0 deletions crates/oxc_formatter/src/formatter/printer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ impl<'a> Printer<'a> {

// Only print a newline if the current line isn't already empty
if self.state.line_width > 0 {
self.state.buffer.trim_trailing_ascii_whitespace();
self.print_char('\n');
self.state.has_empty_line = false;
}
Expand Down
Loading