-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Ensure it's safe to perform suffix-less migrations #14979
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
Changes from 5 commits
bbfac78
136200b
f333d2b
5ae1ee2
f005932
aa24f31
25d61a6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,19 +2,7 @@ import type { Config } from 'tailwindcss' | |
| import { parseCandidate } from '../../../../tailwindcss/src/candidate' | ||
| import type { DesignSystem } from '../../../../tailwindcss/src/design-system' | ||
| import { printCandidate } from '../candidates' | ||
|
|
||
| const QUOTES = ['"', "'", '`'] | ||
| const LOGICAL_OPERATORS = ['&&', '||', '===', '==', '!=', '!==', '>', '>=', '<', '<='] | ||
| const CONDITIONAL_TEMPLATE_SYNTAX = [ | ||
| // Vue | ||
| /v-else-if=['"]$/, | ||
| /v-if=['"]$/, | ||
| /v-show=['"]$/, | ||
|
|
||
| // Alpine | ||
| /x-if=['"]$/, | ||
| /x-show=['"]$/, | ||
| ] | ||
| import { isSafeMigration } from '../is-safe-migration' | ||
|
|
||
| // In v3 the important modifier `!` sits in front of the utility itself, not | ||
| // before any of the variants. In v4, we want it to be at the end of the utility | ||
|
|
@@ -46,63 +34,15 @@ export function important( | |
| // with v3 in that it can read `!` in the front of the utility too, we err | ||
| // on the side of caution and only migrate candidates that we are certain | ||
| // are inside of a string. | ||
| if (location) { | ||
| 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 | ||
| } | ||
|
|
||
| // Heuristic 1: Require the candidate to be inside quotes | ||
| let isQuoteBeforeCandidate = QUOTES.some((quote) => | ||
| currentLineBeforeCandidate.includes(quote), | ||
| ) | ||
| let isQuoteAfterCandidate = QUOTES.some((quote) => | ||
| currentLineAfterCandidate.includes(quote), | ||
| ) | ||
| if (!isQuoteBeforeCandidate || !isQuoteAfterCandidate) { | ||
| continue nextCandidate | ||
| } | ||
|
|
||
| // Heuristic 2: Disallow object access immediately following the candidate | ||
| if (currentLineAfterCandidate[0] === '.') { | ||
| continue nextCandidate | ||
| } | ||
|
|
||
| // Heuristic 3: Disallow logical operators preceding or following the candidate | ||
| for (let operator of LOGICAL_OPERATORS) { | ||
| if ( | ||
| currentLineAfterCandidate.trim().startsWith(operator) || | ||
| currentLineBeforeCandidate.trim().endsWith(operator) | ||
| ) { | ||
| continue nextCandidate | ||
| } | ||
| } | ||
|
|
||
| // Heuristic 4: Disallow conditional template syntax | ||
| for (let rule of CONDITIONAL_TEMPLATE_SYNTAX) { | ||
| if (rule.test(currentLineBeforeCandidate)) { | ||
| continue nextCandidate | ||
| } | ||
| } | ||
| if (location && !isSafeMigration(location)) { | ||
| continue nextCandidate | ||
| } | ||
|
|
||
| // The printCandidate function will already put the exclamation mark in | ||
| // the right place, so we just need to mark this candidate as requiring a | ||
| // migration. | ||
| return printCandidate(designSystem, candidate) | ||
| } | ||
|
|
||
| // The printCandidate function will already put the exclamation mark in the | ||
| // right place, so we just need to mark this candidate as requiring a | ||
| // migration. | ||
| return printCandidate(designSystem, candidate) | ||
|
||
| } | ||
|
|
||
| return rawCandidate | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| const QUOTES = ['"', "'", '`'] | ||
| const LOGICAL_OPERATORS = ['&&', '||', '?', '===', '==', '!=', '!==', '>', '>=', '<', '<='] | ||
| const CONDITIONAL_TEMPLATE_SYNTAX = [ | ||
| // Vue | ||
| /v-else-if=['"]$/, | ||
| /v-if=['"]$/, | ||
| /v-show=['"]$/, | ||
|
|
||
| // Alpine | ||
| /x-if=['"]$/, | ||
| /x-show=['"]$/, | ||
| ] | ||
|
|
||
| export function isSafeMigration(location: { contents: string; start: number; end: number }) { | ||
| 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 | ||
| } | ||
|
|
||
| // Heuristic 1: Require the candidate to be inside quotes | ||
| let isQuoteBeforeCandidate = QUOTES.some((quote) => currentLineBeforeCandidate.includes(quote)) | ||
| let isQuoteAfterCandidate = QUOTES.some((quote) => currentLineAfterCandidate.includes(quote)) | ||
| if (!isQuoteBeforeCandidate || !isQuoteAfterCandidate) { | ||
| return false | ||
| } | ||
|
|
||
| // Heuristic 2: Disallow object access immediately following the candidate | ||
| if (currentLineAfterCandidate[0] === '.') { | ||
| return false | ||
| } | ||
|
|
||
| // Heuristic 3: Disallow logical operators preceding or following the candidate | ||
| for (let operator of LOGICAL_OPERATORS) { | ||
| if ( | ||
| currentLineAfterCandidate.trim().startsWith(operator) || | ||
| currentLineBeforeCandidate.trim().endsWith(operator) | ||
| ) { | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| // Heuristic 4: Disallow conditional template syntax | ||
| for (let rule of CONDITIONAL_TEMPLATE_SYNTAX) { | ||
| if (rule.test(currentLineBeforeCandidate)) { | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| return true | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.