import { parseCandidate } from '../../../../tailwindcss/src/candidate'
import type { DesignSystem } from '../../../../tailwindcss/src/design-system'
import { DefaultMap } from '../../../../tailwindcss/src/utils/default-map'
import * as version from '../../utils/version'
const QUOTES = ['"', "'", '`']
const LOGICAL_OPERATORS = ['&&', '||', '?', '===', '==', '!=', '!==', '>', '>=', '<', '<=']
const CONDITIONAL_TEMPLATE_SYNTAX = [
/v-else-if=['"]$/,
/v-if=['"]$/,
/v-show=['"]$/,
/(?<!:?class)=['"]$/,
/x-if=['"]$/,
/x-show=['"]$/,
/wire:[^\s]*?$/,
]
const NEXT_PLACEHOLDER_PROP = /placeholder=\{?['"]$/
const VUE_3_EMIT = /\b\$?emit\(['"]$/
export function isSafeMigration(
rawCandidate: string,
location: { contents: string; start: number; end: number },
designSystem: DesignSystem,
): boolean {
if (
location.contents[location.start - 1]?.match(/\s/) &&
location.contents.slice(location.end, location.end + 2)?.match(/^:\s/)
) {
let ranges = styleBlockRanges.get(location.contents)
for (let i = 0; i < ranges.length; i += 2) {
let start = ranges[i]
let end = ranges[i + 1]
if (location.start >= start && location.end <= end) {
return false
}
}
}
let [candidate] = parseCandidate(rawCandidate, designSystem)
if (!candidate && version.isGreaterThan(3)) {
return false
}
else if (candidate) {
if (candidate.variants.length > 0) {
return true
}
if (candidate.kind === 'arbitrary') {
return true
}
if (candidate.kind === 'static' && candidate.root.includes('-')) {
return true
}
if (
(candidate.kind === 'functional' && candidate.value !== null) ||
(candidate.kind === 'functional' && candidate.root.includes('-'))
) {
return true
}
if (candidate.kind === 'functional' && candidate.modifier) {
return true
}
}
let currentLineBeforeCandidate = ''
for (let i = location.start - 1; i >= 0; i--) {
let char = location.contents.at(i)!
if (char === '\n') {
break
}
currentLineBeforeCandidate = char + currentLineBeforeCandidate
}
let currentLineAfterCandidate = ''
for (let i = location.end; i < location.contents.length; i++) {
let char = location.contents.at(i)!
if (char === '\n') {
break
}
currentLineAfterCandidate += char
}
let isQuoteBeforeCandidate = QUOTES.some((quote) => currentLineBeforeCandidate.includes(quote))
let isQuoteAfterCandidate = QUOTES.some((quote) => currentLineAfterCandidate.includes(quote))
if (!isQuoteBeforeCandidate || !isQuoteAfterCandidate) {
return false
}
if (currentLineAfterCandidate[0] === '.') {
return false
}
if (currentLineAfterCandidate.trim().startsWith('(')) {
return false
}
for (let operator of LOGICAL_OPERATORS) {
if (
currentLineAfterCandidate.trim().startsWith(operator) ||
currentLineBeforeCandidate.trim().endsWith(operator)
) {
return false
}
}
for (let rule of CONDITIONAL_TEMPLATE_SYNTAX) {
if (rule.test(currentLineBeforeCandidate)) {
return false
}
}
if (NEXT_PLACEHOLDER_PROP.test(currentLineBeforeCandidate)) {
return false
}
if (VUE_3_EMIT.test(currentLineBeforeCandidate)) {
return false
}
return true
}
const styleBlockRanges = new DefaultMap((source: string) => {
let ranges: number[] = []
let offset = 0
while (true) {
let startTag = source.indexOf('<style', offset)
if (startTag === -1) return ranges
offset = startTag + 1
if (!source[startTag + 6].match(/[>\s]/)) continue
let endTag = source.indexOf('</style>', offset)
if (endTag === -1) return ranges
offset = endTag + 1
ranges.push(startTag, endTag)
}
})