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
3 changes: 3 additions & 0 deletions .changelog/upgrade-string-literal-names.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Fixed

- Upgrade: Do not migrate a string that names a union member or sits in a sentence, like `{ kind: 'ring' }`, `case 'ring':`, `type Shape = 'ring' | 'circle'` or `title="Add a shadow to the card."`
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,25 @@ describe('is-safe-migration', async () => {
[`<?php $variant = $active ? 'outline' : 'ghost'; ?>`, 'outline'],
[`@php($variant = $active ? 'outline' : 'ghost')`, 'outline'],
[`$variant = $attributes->get('variant') ?? 'outline';`, 'outline'],

// TypeScript discriminated unions and other string literal names
[` | { kind: 'ring'; x: number; y: number }`, 'ring'],
[`effects.push({ kind: 'ring', x, y })`, 'ring'],
[`if (effect.kind === 'ring') {}`, 'ring'],
[`if (effect.type !== "shadow") {}`, 'shadow'],
[`const chart = { axisPointer: { type: 'shadow' } }`, 'shadow'],
[` case 'ring':`, 'ring'],
[`switch (effect.kind) { case "ring": break }`, 'ring'],
[`type Shape = 'ring' | 'circle'`, 'ring'],
[`type Shape = 'circle' | 'ring'`, 'ring'],

// A word in a sentence
[
'const help = `A badge with only an outline: you have enough ki, but not the range.`',
'outline',
],
[`<p title="Add a shadow to the card.">`, 'shadow'],
[`alert('No ring! Try again')`, 'ring'],
])('does not replace classes in invalid positions #%#', async (example, candidate) => {
expect(
await migrateCandidate(designSystem, {}, candidate, {
Expand Down Expand Up @@ -221,6 +240,16 @@ describe('is-safe-migration', async () => {
'shadow',
'shadow-sm',
],

// Only the value of `kind` or `type` itself is a name
[`Button({ type: "submit", className: "shadow" })`, 'shadow', 'shadow-sm'],
[`cva("", { variants: { kind: { primary: "shadow" } } })`, 'shadow', 'shadow-sm'],
[`const kinds = cn("shadow")`, 'shadow', 'shadow-sm'],
[`cn(isOpen || "shadow")`, 'shadow', 'shadow-sm'],

// Punctuation outside of the class string does not make it a sentence
[`<div class="flex shadow">Hello. World!</div>`, 'shadow', 'shadow-sm'],
[`<div :class="{ shadow: isOpen, ring: isFocused }"></div>`, 'shadow', 'shadow-sm'],
])('replaces classes in valid positions #%#', async (example, candidate, expected) => {
expect(
await migrateCandidate(designSystem, {}, candidate, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ const CONDITIONAL_TEMPLATE_SYNTAX = [
// JavaScript / TypeScript
/addEventListener\(['"`]$/,

// A `kind` or `type` value names a member of a union, e.g. `{ kind: 'ring' }`
// or `effect.kind === 'ring'`
/\b(?:kind|type)\s*(?::|[!=]==?)\s*['"`]$/,

// A `switch` case, e.g. `case 'ring':`
/\bcase\s+['"`]$/,

// Alpine
/wire:[^\s]*?$/,

Expand All @@ -22,6 +29,9 @@ const CONDITIONAL_TEMPLATE_SYNTAX = [
]
const NEXT_PLACEHOLDER_PROP = /placeholder=\{?['"`]$/
const VUE_3_EMIT = /\b\$?emit\(['"`]$/
const STRING_UNION_BEFORE = /(?<!\|)\|\s*['"`]$/
const STRING_UNION_AFTER = /^['"`]\s*\|(?![|>])/
const SENTENCE_END = /[.!](?:\s|$)/

export function isSafeMigration(
rawCandidate: string,
Expand Down Expand Up @@ -172,6 +182,26 @@ export function isSafeMigration(
return false
}

// Heuristic: Disallow a member of a string literal union type
//
// E.g.: `type Shape = 'ring' | 'circle'`
if (
STRING_UNION_BEFORE.test(currentLineBeforeCandidate) ||
STRING_UNION_AFTER.test(currentLineAfterCandidate)
) {
return false
}

// Heuristic: Disallow a word in a sentence. A list of classes never contains
// a `.` or `!` followed by a space or at its end.
//
// E.g.: `title="Add a shadow to the card."`
if (
SENTENCE_END.test(stringAroundCandidate(currentLineBeforeCandidate, currentLineAfterCandidate))
) {
return false
}

// Heuristic: Disallow logical operators preceding or following the candidate
for (let operator of LOGICAL_OPERATORS) {
if (
Expand Down Expand Up @@ -335,7 +365,14 @@ const EQUALS = 0x3d
const GREATER_THAN = 0x3e

function isMiddleOfString(line: string): boolean {
return openQuoteIndex(line) !== -1
}

// The index of the quote that opens the string the end of the line is in, or
// -1 when the line ends outside of a string
function openQuoteIndex(line: string): number {
let currentQuote: number | null = null
let index = -1

for (let i = 0; i < line.length; i++) {
let char = line.charCodeAt(i)
Expand All @@ -351,17 +388,29 @@ function isMiddleOfString(line: string): boolean {
// Found matching quote, we are outside of a string
if (currentQuote === char) {
currentQuote = null
index = -1
}

// Found a quote, we are inside a string
else if (currentQuote === null) {
currentQuote = char
index = i
}
break
}
}

return currentQuote !== null
return index
}

// The contents of the string around the candidate, without the candidate
//
// shortcut: an escaped quote after the candidate ends the string early, which
// only makes the sentence heuristic miss a sentence.
function stringAroundCandidate(before: string, after: string): string {
let start = openQuoteIndex(before)
let end = after.indexOf(before[start])
return before.slice(start + 1) + (end === -1 ? after : after.slice(0, end))
}

const inlineStyleAttributeValueRanges = new DefaultMap((source: string) => {
Expand Down