Skip to content

Don't repeatedly add backslashes to escape sequences when formatting #381

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Add support for OXC + Hermes Prettier plugins ([#376](https://github.com/tailwindlabs/prettier-plugin-tailwindcss/pull/376), [#380](https://github.com/tailwindlabs/prettier-plugin-tailwindcss/pull/380))
- Sort template literals in Angular expressions ([#377](https://github.com/tailwindlabs/prettier-plugin-tailwindcss/pull/377))
- Don't repeatedly add backslashes to escape sequences when formatting ([#381](https://github.com/tailwindlabs/prettier-plugin-tailwindcss/pull/381))

## [0.6.13] - 2025-06-19

Expand Down
44 changes: 23 additions & 21 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import { spliceChangesIntoString, visit } from './utils.js'

let base = await loadPlugins()

const ESCAPE_SEQUENCE_PATTERN = /\\(['"\\nrtbfv0-7xuU])/g

function createParser(
parserFormat: string,
transform: (ast: any, context: TransformerContext) => void,
Expand Down Expand Up @@ -505,41 +507,41 @@ function sortStringLiteral(
removeDuplicates,
collapseWhitespace,
})

let didChange = result !== node.value
node.value = result

// A string literal was escaped if:
// - There are backslashes in the raw value; AND
// - The raw value is not the same as the value (excluding the surrounding quotes)
let wasEscaped = false
if (!didChange) return false

if (node.extra) {
// JavaScript (StringLiteral)
wasEscaped =
node.extra?.rawValue.includes('\\') &&
node.extra?.raw.slice(1, -1) !== node.value
} else {
// TypeScript (Literal)
wasEscaped =
node.value.includes('\\') && node.raw.slice(1, -1) !== node.value
}
node.value = result

let escaped = wasEscaped ? result.replace(/\\/g, '\\\\') : result
// Preserve the original escaping level for the new content
let raw = node.extra?.raw ?? node.raw
let quote = raw[0]
let originalRawContent = raw.slice(1, -1)
let originalValue = node.extra?.rawValue ?? node.value

if (node.extra) {
// The original list has ecapes so we ensure that the sorted list also
// maintains those by replacing backslashes from escape sequences.
//
// It seems that TypeScript-based ASTs don't need this special handling
// which is why this is guarded inside the `node.extra` check
if (originalRawContent !== originalValue && originalValue.includes('\\')) {
result = result.replace(ESCAPE_SEQUENCE_PATTERN, '\\\\$1')
}

// JavaScript (StringLiteral)
let raw = node.extra.raw
node.extra = {
...node.extra,
rawValue: result,
raw: raw[0] + escaped + raw.slice(-1),
raw: quote + result + quote,
}
} else {
// TypeScript (Literal)
let raw = node.raw
node.raw = raw[0] + escaped + raw.slice(-1)
node.raw = quote + result + quote
}
return didChange

return true
}

function isStringLiteral(node: any) {
Expand Down
4 changes: 4 additions & 0 deletions tests/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ export let javascript: TestEntry[] = [
`;<div class={'object-cover' + (standalone ? ' aspect-square w-full' : ' min-h-0 grow basis-0')}></div>`,
`;<div class={'object-cover' + (standalone ? ' aspect-square w-full' : ' min-h-0 grow basis-0')}></div>`,
],
[
`;<div class="[&>.a\\_p]:after:content-['\\2'] [&>.a\\_p]:z-0"></div>`,
`;<div class="[&>.a\\_p]:z-0 [&>.a\\_p]:after:content-['\\2']"></div>`,
],
]
javascript = javascript.concat(
javascript.map((test) => [
Expand Down