Skip to content
Open
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
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Fixed

- Restore class sorting in Astro templates when using `prettier-plugin-astro` v1 ([#473](https://github.com/tailwindlabs/prettier-plugin-tailwindcss/pull/473))
- Avoid changing class-object keys when normalization can overwrite independent conditions.

## [0.8.1] - 2026-07-15

Expand Down
5 changes: 1 addition & 4 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ import { cacheForDirs } from './utils.js'

let prettierConfigCache = expiringMap<string, string | null>(10_000)

async function resolvePrettierConfigDir(
filePath: string,
inputDir: string,
): Promise<string> {
async function resolvePrettierConfigDir(filePath: string, inputDir: string): Promise<string> {
// Check cache for this directory
let cached = prettierConfigCache.get(inputDir)
if (cached !== undefined) {
Expand Down
183 changes: 170 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Node as JavaScriptNode } from '@babel/types'
// @ts-ignore
import type * as Liquid from '@shopify/prettier-plugin-liquid/dist/types.js'
// @ts-ignore
Expand Down Expand Up @@ -34,10 +35,13 @@ function transformDynamicAngularAttribute(attr: any, env: TransformerEnv) {
}

let changes: StringChange[] = []
let preserveObjectKey = createObjectKeyGuard((key, path) =>
sortClasses(key.value, { env, collapseWhitespace: canCollapseWhitespaceIn(path, env) }),
)

visit(directiveAst, {
StringLiteral(node, path) {
if (!node.value) return
if (!node.value || preserveObjectKey(path)) return

let collapseWhitespace = canCollapseWhitespaceIn(path, env)

Expand All @@ -53,7 +57,7 @@ function transformDynamicAngularAttribute(attr: any, env: TransformerEnv) {
},

TemplateLiteral(node, path) {
if (!node.quasis.length) return
if (!node.quasis.length || preserveObjectKey(path)) return

let collapseWhitespace = canCollapseWhitespaceIn(path, env)

Expand Down Expand Up @@ -105,6 +109,17 @@ function transformDynamicJsAttribute(attr: any, env: TransformerEnv) {
)
}

let preserveObjectKey = createObjectKeyGuard((key, path) => {
let concat = findConcatEntry(path)
return normalizedObjectKey(key, {
env,
collapseWhitespace: {
start: concat?.key !== 'right',
end: concat?.key !== 'left',
},
})
})

function addChange(
start: number | null | undefined,
end: number | null | undefined,
Expand All @@ -128,6 +143,7 @@ function transformDynamicJsAttribute(attr: any, env: TransformerEnv) {

visit(ast, {
StringLiteral(node, path) {
if (preserveObjectKey(path)) return
let concat = findConcatEntry(path)
let sorted = sortStringLiteral(node, {
env,
Expand All @@ -147,7 +163,7 @@ function transformDynamicJsAttribute(attr: any, env: TransformerEnv) {
},

Literal(node: any, path) {
if (!isStringLiteral(node)) return
if (!isStringLiteral(node) || preserveObjectKey(path)) return

let concat = findConcatEntry(path)
let sorted = sortStringLiteral(node, {
Expand All @@ -168,6 +184,7 @@ function transformDynamicJsAttribute(attr: any, env: TransformerEnv) {
},

TemplateLiteral(node, path) {
if (preserveObjectKey(path)) return
let concat = findConcatEntry(path)
let originalQuasis = node.quasis.map((quasi) => quasi.value.raw)
let sorted = sortTemplateLiteral(node, {
Expand All @@ -189,6 +206,7 @@ function transformDynamicJsAttribute(attr: any, env: TransformerEnv) {
},

TaggedTemplateExpression(node, path) {
if (preserveObjectKey(path)) return
if (!isSortableTemplateExpression(node, matcher)) {
return
}
Expand Down Expand Up @@ -449,9 +467,11 @@ function sortStringLiteral(
return true
}

function isStringLiteral(node: any) {
function isStringLiteral<T extends { type: string; value?: unknown }>(
node: T,
): node is T & { value: string } {
return (
node.type === 'StringLiteral' || (node.type === 'Literal' && typeof node.value === 'string')
(node.type === 'StringLiteral' || node.type === 'Literal') && typeof node.value === 'string'
)
}

Expand Down Expand Up @@ -548,6 +568,128 @@ function isSortableExpression(
return false
}

interface ClassObjectKey {
type: string
name?: unknown
value?: unknown
raw?: string
extra?: { raw?: string; rawValue?: unknown }
properties?: ClassObjectProperty[]
}

interface ClassObjectProperty {
type: string
computed?: boolean
key?: ClassObjectKey
}

function normalizedObjectKey(
key: ClassObjectKey & { value: string },
options: Parameters<typeof sortStringLiteral>[1],
) {
let candidate = { ...key }
if (!sortStringLiteral(candidate, options)) return key.value

let raw = candidate.extra?.raw ?? candidate.raw
if (typeof raw !== 'string') throw new TypeError('Expected raw source for a class-object key')
if (raw.slice(1, -1) === candidate.value) return candidate.value

// The printer emits raw source; independently sorted cooked metadata can
// describe a different key when an escape represents class whitespace.
let parsed = prettierParserBabel.parsers.babel.parse(`(${raw})`, options.env.options)
return parsed.program.body[0].expression.value
}

function createObjectKeyGuard(
normalize: (
key: ClassObjectKey & { value: string },
path: Path<JavaScriptNode, unknown>,
) => string,
) {
let plans = new WeakMap<object, Set<ClassObjectKey>>()

function plan(properties: ClassObjectProperty[], path: Path<JavaScriptNode, unknown>) {
let entries: { key: ClassObjectKey; before: string; after: string }[] = []
for (let property of properties) {
let key = property.key
if (
!['ObjectProperty', 'ObjectMethod', 'Property'].includes(property.type) ||
property.computed ||
!key ||
(key.type === 'Identifier' && typeof key.name !== 'string') ||
(key.type !== 'Identifier' &&
!isStringLiteral(key) &&
typeof key.value !== 'number' &&
typeof key.value !== 'bigint' &&
key.type !== 'BigIntLiteral')
) {
// Treat spread/computed forms conservatively without evaluating expressions.
return new Set(properties.flatMap(({ key }) => (key ? [key] : [])))
}

let before =
typeof key.name === 'string' && key.type === 'Identifier' ? key.name : String(key.value)
entries.push({ key, before, after: isStringLiteral(key) ? normalize(key, path) : before })
}

// Existing duplicates may move together, but merging or splitting identities
// would change which property's condition wins at runtime.
let beforeToAfter = new Map<string, Set<string>>()
let afterToBefore = new Map<string, Set<string>>()
for (let { before, after } of entries) {
let afters = beforeToAfter.get(before) ?? new Set<string>()
afters.add(after)
beforeToAfter.set(before, afters)
let befores = afterToBefore.get(after) ?? new Set<string>()
befores.add(before)
afterToBefore.set(after, befores)
}

let preserved = new Set<ClassObjectKey>()
for (let { key, before, after } of entries) {
if (
beforeToAfter.get(before)!.size > 1 ||
afterToBefore.get(after)!.size > 1 ||
(after !== before && beforeToAfter.has(after)) ||
(after === '__proto__' && after !== before)
) {
preserved.add(key)
}
}
return preserved
}

return (path: Path<JavaScriptNode, unknown>) => {
for (let i = 0; i < path.length; i++) {
let entry = path[i]
if (entry.key !== 'key') continue
let object = path[i + 1]?.parent
if (object?.type !== 'ObjectExpression' || !object.properties) continue

// Keys supplied through an object spread can collide with its outer
// siblings even when the nested object has no local collision.
for (let j = i + 1; j < path.length; j++) {
if (
path[j].parent?.type === 'SpreadElement' &&
path[j].key === 'argument' &&
path[j + 1]?.parent?.type === 'ObjectExpression'
) {
return true
}
}

let preserved = plans.get(object)
if (!preserved) {
// Plan all siblings before the first mutation; later visits only read it.
preserved = plan(object.properties, path)
plans.set(object, preserved)
}
if (preserved.has(entry.node)) return true
}
return false
}
}

function canCollapseWhitespaceIn(
path: Path<import('@babel/types').Node, any>,
env: TransformerEnv,
Expand Down Expand Up @@ -604,10 +746,17 @@ function canCollapseWhitespaceIn(
// good enough. The actual AST we should be using is probably estree + ts.
function transformJavaScript(ast: import('@babel/types').Node, env: TransformerEnv) {
let { matcher } = env
let preserveObjectKey = createObjectKeyGuard((key, path) =>
normalizedObjectKey(key, { env, collapseWhitespace: canCollapseWhitespaceIn(path, env) }),
)

function sortInside(ast: import('@babel/types').Node) {
function sortInside(ast: JavaScriptNode, outerPath: Path<JavaScriptNode, unknown>) {
visit(ast, (node, path) => {
let collapseWhitespace = canCollapseWhitespaceIn(path, env)
let fullPath = [...path, ...outerPath]
if (preserveObjectKey(fullPath)) return false
// Keys need stable context across reentries without changing other strings' trim policy.
let isObjectKey = path[0].key === 'key' && path[1]?.parent?.type === 'ObjectExpression'
let collapseWhitespace = canCollapseWhitespaceIn(isObjectKey ? fullPath : path, env)

if (isStringLiteral(node)) {
sortStringLiteral(node, { env, collapseWhitespace })
Expand All @@ -622,7 +771,8 @@ function transformJavaScript(ast: import('@babel/types').Node, env: TransformerE
}

visit(ast, {
JSXAttribute(node) {
JSXAttribute(node, path) {
if (preserveObjectKey(path)) return false
node = node as import('@babel/types').JSXAttribute

if (!node.value) {
Expand All @@ -643,22 +793,24 @@ function transformJavaScript(ast: import('@babel/types').Node, env: TransformerE
} else if (node.value.type === 'JSXExpressionContainer') {
// Dynamic attrs (e.g. Astro's `class:list`) only carry expressions, never plain strings
if (matcher.hasStaticAttr(name) || matcher.hasDynamicAttr(name)) {
sortInside(node.value)
sortInside(node.value, path)
}
}
},

CallExpression(node) {
CallExpression(node, path) {
if (preserveObjectKey(path)) return false
node = node as import('@babel/types').CallExpression

if (!isSortableCallExpression(node, matcher)) {
return
}

node.arguments.forEach((arg) => sortInside(arg))
node.arguments.forEach((arg) => sortInside(arg, path))
},

TaggedTemplateExpression(node, path) {
if (preserveObjectKey(path)) return false
node = node as import('@babel/types').TaggedTemplateExpression

if (!isSortableTemplateExpression(node, matcher)) {
Expand Down Expand Up @@ -941,6 +1093,9 @@ function transformPug(ast: any, env: TransformerEnv) {

function transformSvelte(ast: any, env: TransformerEnv) {
let { matcher, changes } = env
let preserveObjectKey = createObjectKeyGuard((key) =>
normalizedObjectKey(key, { env, removeDuplicates: false, collapseWhitespace: false }),
)

for (let attr of ast.attributes ?? []) {
if (!matcher.hasStaticAttr(attr.name) || attr.type !== 'Attribute') {
Expand Down Expand Up @@ -971,7 +1126,8 @@ function transformSvelte(ast: any, env: TransformerEnv) {
})
} else if (value.type === 'MustacheTag' || value.type === 'ExpressionTag') {
visit(value.expression, {
Literal(node) {
Literal(node, path) {
if (preserveObjectKey(path)) return
if (isStringLiteral(node)) {
let before = node.raw
let sorted = sortStringLiteral(node, {
Expand All @@ -990,7 +1146,8 @@ function transformSvelte(ast: any, env: TransformerEnv) {
}
}
},
TemplateLiteral(node) {
TemplateLiteral(node, path) {
if (preserveObjectKey(path)) return
let before = node.quasis.map((quasi: any) => quasi.value.raw)
let sorted = sortTemplateLiteral(node, {
env,
Expand Down
Loading