Skip to content

Commit 1d07bda

Browse files
committed
move migrate-deprecated-utilities to core
1 parent 954b6a3 commit 1d07bda

File tree

4 files changed

+82
-54
lines changed

4 files changed

+82
-54
lines changed

packages/@tailwindcss-upgrade/src/codemods/template/migrate-deprecated-utilities.ts

Lines changed: 0 additions & 52 deletions
This file was deleted.

packages/@tailwindcss-upgrade/src/codemods/template/migrate.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import { migrateArbitraryVariants } from './migrate-arbitrary-variants'
1212
import { migrateAutomaticVarInjection } from './migrate-automatic-var-injection'
1313
import { migrateCamelcaseInNamedValue } from './migrate-camelcase-in-named-value'
1414
import { migrateCanonicalizeCandidate } from './migrate-canonicalize-candidate'
15-
import { migrateDeprecatedUtilities } from './migrate-deprecated-utilities'
1615
import { migrateDropUnnecessaryDataTypes } from './migrate-drop-unnecessary-data-types'
1716
import { migrateEmptyArbitraryValues } from './migrate-handle-empty-arbitrary-values'
1817
import { migrateLegacyArbitraryValues } from './migrate-legacy-arbitrary-values'
@@ -41,7 +40,6 @@ export const DEFAULT_MIGRATIONS: Migration[] = [
4140
migrateVariantOrder, // sync, v3 → v4, Has to happen before migrations that modify variants
4241
migrateAutomaticVarInjection, // sync, v3 → v4
4342
migrateLegacyArbitraryValues, // sync, v3 → v4 (could also consider it a v4 optimization)
44-
migrateDeprecatedUtilities, // sync, v4 (deprecation map, order-none → order-0)
4543
migrateModernizeArbitraryValues, // sync, v3 and v4 optimizations, split up?
4644
migrateArbitraryVariants, // sync, v4
4745
migrateDropUnnecessaryDataTypes, // sync, v4 (I think this can be dropped?)

packages/tailwindcss/src/canonicalize-candidates.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,34 @@ describe.each([['default'], ['with-variant'], ['important'], ['prefix']])('%s',
494494
await expectCanonicalization(input, candidate, expected)
495495
})
496496
})
497+
498+
describe('deprecated utilities', () => {
499+
test('`order-none` → `order-0`', async () => {
500+
let candidate = 'order-none'
501+
let expected = 'order-0'
502+
503+
let input = css`
504+
@import 'tailwindcss';
505+
`
506+
507+
await expectCanonicalization(input, candidate, expected)
508+
})
509+
510+
test('`order-none` → `order-none` with custom implementation', async () => {
511+
let candidate = 'order-none'
512+
let expected = 'order-none'
513+
514+
let input = css`
515+
@import 'tailwindcss';
516+
517+
@utility order-none {
518+
order: none; /* imagine this exists */
519+
}
520+
`
521+
522+
await expectCanonicalization(input, candidate, expected)
523+
})
524+
})
497525
})
498526

499527
describe('theme to var', () => {

packages/tailwindcss/src/canonicalize-candidates.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ const CANONICALIZATIONS = [
3636
themeToVar,
3737
arbitraryUtilities,
3838
bareValueUtilities,
39+
deprecatedUtilities,
3940
print,
4041
]
4142

@@ -428,6 +429,14 @@ function parseCandidate(designSystem: DesignSystem, input: string) {
428429
)
429430
}
430431

432+
function printUnprefixedCandidate(designSystem: DesignSystem, candidate: Candidate) {
433+
let candidateString = designSystem.printCandidate(candidate)
434+
435+
return designSystem.theme.prefix && candidateString.startsWith(`${designSystem.theme.prefix}:`)
436+
? candidateString.slice(designSystem.theme.prefix.length + 1)
437+
: candidateString
438+
}
439+
431440
// ----
432441

433442
const baseReplacementsCache = new DefaultMap<DesignSystem, Map<string, Candidate>>(
@@ -811,3 +820,48 @@ function bareValueUtilities(designSystem: DesignSystem, rawCandidate: string): s
811820
}
812821
}
813822
}
823+
824+
// ----
825+
826+
const DEPRECATION_MAP = new Map([['order-none', 'order-0']])
827+
828+
function deprecatedUtilities(designSystem: DesignSystem, rawCandidate: string): string {
829+
let signatures = computeUtilitySignature.get(designSystem)
830+
831+
for (let readonlyCandidate of designSystem.parseCandidate(rawCandidate)) {
832+
// The below logic makes use of mutation. Since candidates in the
833+
// DesignSystem are cached, we can't mutate them directly.
834+
let candidate = structuredClone(readonlyCandidate) as Writable<typeof readonlyCandidate>
835+
836+
// Create a basic stripped candidate without variants or important flag. We
837+
// will re-add those later but they are irrelevant for what we are trying to
838+
// do here (and will increase cache hits because we only have to deal with
839+
// the base utility, nothing more).
840+
let targetCandidate = baseCandidate(candidate)
841+
let targetCandidateString = printUnprefixedCandidate(designSystem, targetCandidate)
842+
843+
let replacementString = DEPRECATION_MAP.get(targetCandidateString) ?? null
844+
if (replacementString === null) return rawCandidate
845+
846+
let legacySignature = signatures.get(targetCandidateString)
847+
if (typeof legacySignature !== 'string') return rawCandidate
848+
849+
let replacementSignature = signatures.get(replacementString)
850+
if (typeof replacementSignature !== 'string') return rawCandidate
851+
852+
// Not the same signature, not safe to migrate
853+
if (legacySignature !== replacementSignature) return rawCandidate
854+
855+
let [replacement] = parseCandidate(designSystem, replacementString)
856+
857+
// Re-add the variants and important flag from the original candidate
858+
return designSystem.printCandidate(
859+
Object.assign(structuredClone(replacement), {
860+
variants: candidate.variants,
861+
important: candidate.important,
862+
}),
863+
)
864+
}
865+
866+
return rawCandidate
867+
}

0 commit comments

Comments
 (0)