-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Fix false-positive migrations in addEventListener
and JavaScript variable names
#18718
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
Merged
+129
−56
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5d99490
refactor is-safe-migration test
RobinMalfait d839a5f
add failing tests
RobinMalfait dc19148
ignore migrations in the event name of `addEventListener`
RobinMalfait 67586f8
add "`" as valid quotes
RobinMalfait b9b0d28
check if we are in a string
RobinMalfait 9784f7b
update changelog
RobinMalfait File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 86 additions & 51 deletions
137
packages/@tailwindcss-upgrade/src/codemods/template/is-safe-migration.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,103 @@ | ||
import { __unstable__loadDesignSystem } from '@tailwindcss/node' | ||
import { expect, test, vi } from 'vitest' | ||
import { describe, expect, test, vi } from 'vitest' | ||
import * as versions from '../../utils/version' | ||
import { migrateCandidate } from './migrate' | ||
vi.spyOn(versions, 'isMajor').mockReturnValue(true) | ||
|
||
test('does not replace classes in invalid positions', async () => { | ||
describe('is-safe-migration', async () => { | ||
let designSystem = await __unstable__loadDesignSystem('@import "tailwindcss";', { | ||
base: __dirname, | ||
}) | ||
|
||
async function shouldNotReplace(example: string, candidate = '!border') { | ||
test.each([ | ||
[`let notBorder = !border \n`, '!border'], | ||
[`{ "foo": !border.something + ""}\n`, '!border'], | ||
[`<div v-if="something && !border"></div>\n`, '!border'], | ||
[`<div v-else-if="something && !border"></div>\n`, '!border'], | ||
[`<div v-show="something && !border"></div>\n`, '!border'], | ||
[`<div v-if="!border || !border"></div>\n`, '!border'], | ||
[`<div v-else-if="!border || !border"></div>\n`, '!border'], | ||
[`<div v-show="!border || !border"></div>\n`, '!border'], | ||
[`<div v-if="!border"></div>\n`, '!border'], | ||
[`<div v-else-if="!border"></div>\n`, '!border'], | ||
[`<div v-show="!border"></div>\n`, '!border'], | ||
[`<div x-if="!border"></div>\n`, '!border'], | ||
|
||
[`let notShadow = shadow \n`, 'shadow'], | ||
[`{ "foo": shadow.something + ""}\n`, 'shadow'], | ||
[`<div v-if="something && shadow"></div>\n`, 'shadow'], | ||
[`<div v-else-if="something && shadow"></div>\n`, 'shadow'], | ||
[`<div v-show="something && shadow"></div>\n`, 'shadow'], | ||
[`<div v-if="shadow || shadow"></div>\n`, 'shadow'], | ||
[`<div v-else-if="shadow || shadow"></div>\n`, 'shadow'], | ||
[`<div v-show="shadow || shadow"></div>\n`, 'shadow'], | ||
[`<div v-if="shadow"></div>\n`, 'shadow'], | ||
[`<div v-else-if="shadow"></div>\n`, 'shadow'], | ||
[`<div v-show="shadow"></div>\n`, 'shadow'], | ||
[`<div x-if="shadow"></div>\n`, 'shadow'], | ||
[`<div style={{filter: 'drop-shadow(30px 10px 4px #4444dd)'}}/>\n`, 'shadow'], | ||
|
||
// Next.js Image placeholder cases | ||
[`<Image placeholder="blur" src="/image.jpg" />`, 'blur'], | ||
[`<Image placeholder={'blur'} src="/image.jpg" />`, 'blur'], | ||
[`<Image placeholder={blur} src="/image.jpg" />`, 'blur'], | ||
|
||
// https://github.com/tailwindlabs/tailwindcss/issues/17974 | ||
['<div v-if="!duration">', '!duration'], | ||
['<div :active="!duration">', '!duration'], | ||
['<div :active="!visible">', '!visible'], | ||
|
||
// Alpine/Livewire wire:… | ||
['<x-input.number required="foo" wire:model.blur="coins" />', 'blur'], | ||
|
||
// Vue 3 events | ||
[`emit('blur', props.modelValue)\n`, 'blur'], | ||
[`$emit('blur', props.modelValue)\n`, 'blur'], | ||
|
||
// JavaScript / TypeScript | ||
[`document.addEventListener('blur',handleBlur)`, 'blur'], | ||
[`document.addEventListener('blur', handleBlur)`, 'blur'], | ||
|
||
[`function foo({ outline = true })`, 'outline'], | ||
[`function foo({ before = false, outline = true })`, 'outline'], | ||
[`function foo({before=false,outline=true })`, 'outline'], | ||
[`function foo({outline=true })`, 'outline'], | ||
// https://github.com/tailwindlabs/tailwindcss/issues/18675 | ||
[ | ||
// With default value | ||
`function foo({ size = "1.25rem", digit, outline = true, textClass = "", className = "" })`, | ||
'outline', | ||
], | ||
[ | ||
// Without default value | ||
`function foo({ size = "1.25rem", digit, outline, textClass = "", className = "" })`, | ||
'outline', | ||
], | ||
[ | ||
// As the last argument | ||
`function foo({ size = "1.25rem", digit, outline })`, | ||
'outline', | ||
], | ||
[ | ||
// As the last argument, but there is techinically another `"` on the same line | ||
`function foo({ size = "1.25rem", digit, outline }): { return "foo" }`, | ||
'outline', | ||
], | ||
[ | ||
// Tricky quote balancing | ||
`function foo({ before = "'", outline, after = "'" }): { return "foo" }`, | ||
'outline', | ||
], | ||
|
||
[`function foo(blur, foo)`, 'blur'], | ||
[`function foo(blur,foo)`, 'blur'], | ||
])('does not replace classes in invalid positions #%#', async (example, candidate) => { | ||
expect( | ||
await migrateCandidate(designSystem, {}, candidate, { | ||
contents: example, | ||
start: example.indexOf(candidate), | ||
end: example.indexOf(candidate) + candidate.length, | ||
}), | ||
).toEqual(candidate) | ||
} | ||
|
||
await shouldNotReplace(`let notBorder = !border \n`) | ||
await shouldNotReplace(`{ "foo": !border.something + ""}\n`) | ||
await shouldNotReplace(`<div v-if="something && !border"></div>\n`) | ||
await shouldNotReplace(`<div v-else-if="something && !border"></div>\n`) | ||
await shouldNotReplace(`<div v-show="something && !border"></div>\n`) | ||
await shouldNotReplace(`<div v-if="!border || !border"></div>\n`) | ||
await shouldNotReplace(`<div v-else-if="!border || !border"></div>\n`) | ||
await shouldNotReplace(`<div v-show="!border || !border"></div>\n`) | ||
await shouldNotReplace(`<div v-if="!border"></div>\n`) | ||
await shouldNotReplace(`<div v-else-if="!border"></div>\n`) | ||
await shouldNotReplace(`<div v-show="!border"></div>\n`) | ||
await shouldNotReplace(`<div x-if="!border"></div>\n`) | ||
|
||
await shouldNotReplace(`let notShadow = shadow \n`, 'shadow') | ||
await shouldNotReplace(`{ "foo": shadow.something + ""}\n`, 'shadow') | ||
await shouldNotReplace(`<div v-if="something && shadow"></div>\n`, 'shadow') | ||
await shouldNotReplace(`<div v-else-if="something && shadow"></div>\n`, 'shadow') | ||
await shouldNotReplace(`<div v-show="something && shadow"></div>\n`, 'shadow') | ||
await shouldNotReplace(`<div v-if="shadow || shadow"></div>\n`, 'shadow') | ||
await shouldNotReplace(`<div v-else-if="shadow || shadow"></div>\n`, 'shadow') | ||
await shouldNotReplace(`<div v-show="shadow || shadow"></div>\n`, 'shadow') | ||
await shouldNotReplace(`<div v-if="shadow"></div>\n`, 'shadow') | ||
await shouldNotReplace(`<div v-else-if="shadow"></div>\n`, 'shadow') | ||
await shouldNotReplace(`<div v-show="shadow"></div>\n`, 'shadow') | ||
await shouldNotReplace(`<div x-if="shadow"></div>\n`, 'shadow') | ||
await shouldNotReplace( | ||
`<div style={{filter: 'drop-shadow(30px 10px 4px #4444dd)'}}/>\n`, | ||
'shadow', | ||
) | ||
|
||
// Next.js Image placeholder cases | ||
await shouldNotReplace(`<Image placeholder="blur" src="/image.jpg" />`, 'blur') | ||
await shouldNotReplace(`<Image placeholder={'blur'} src="/image.jpg" />`, 'blur') | ||
await shouldNotReplace(`<Image placeholder={blur} src="/image.jpg" />`, 'blur') | ||
|
||
// https://github.com/tailwindlabs/tailwindcss/issues/17974 | ||
await shouldNotReplace('<div v-if="!duration">', '!duration') | ||
await shouldNotReplace('<div :active="!duration">', '!duration') | ||
await shouldNotReplace('<div :active="!visible">', '!visible') | ||
|
||
// Alpine/Livewire wire:… | ||
await shouldNotReplace('<x-input.number required="foo" wire:model.blur="coins" />', 'blur') | ||
|
||
// Vue 3 events | ||
await shouldNotReplace(`emit('blur', props.modelValue)\n`, 'blur') | ||
await shouldNotReplace(`$emit('blur', props.modelValue)\n`, 'blur') | ||
}) | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we consider that in JS backtick strings can span lines — or just not worry about that unless it comes up?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We haven't in the past and nobody had issues with this yet (afaik at least) so I don't think we need to add the additional complexity of this (yet) 🤔