Skip to content

Commit

Permalink
Fix raw JS support, better syntax
Browse files Browse the repository at this point in the history
I know understand andOr
  • Loading branch information
WarningImHack3r committed May 11, 2024
1 parent 614082e commit 283acfd
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ abstract class ClassReplacementVisitor(
private val jsCallExpressionPattern: PsiElementPattern.Capture<PsiElement> =
PlatformPatterns.psiElement(JSStubElementTypes.CALL_EXPRESSION)
private val jsOrTsVariablePattern: PsiElementPattern.Capture<PsiElement> =
PlatformPatterns.psiElement(TypeScriptStubElementTypes.TYPESCRIPT_VARIABLE)
PlatformPatterns.psiElement().andOr(
PlatformPatterns.psiElement(TypeScriptStubElementTypes.TYPESCRIPT_VARIABLE),
PlatformPatterns.psiElement(JSStubElementTypes.VARIABLE)
)

abstract fun classAttributeNameFromElement(element: PsiElement): String?

Expand Down Expand Up @@ -61,10 +64,12 @@ abstract class ClassReplacementVisitor(
}

jsCallExpressionPattern
.withChild(PlatformPatterns.psiElement().withText("tv"))
.withParent(jsOrTsVariablePattern)
.accepts(element) || jsCallExpressionPattern // there is probably a way to combine these two patterns
.withChild(PlatformPatterns.psiElement().withText("cva"))
.withChild(
PlatformPatterns.psiElement().andOr(
PlatformPatterns.psiElement().withText("tv"),
PlatformPatterns.psiElement().withText("cva")
)
)
.withParent(jsOrTsVariablePattern)
.accepts(element) -> {
// tailwind-variants & class-variance-authority
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ class ClassReplacementTests : BasePlatformTestCase() {
assertEquals(expected, actual)
}

fun testFullSvelteVueJSClassReplacement() {
val (expected, actual) = beforeAndAfterContents("fullSvelteVue.js")
assertEquals(expected, actual)
}

fun testFullSimpleVueComponentClassReplacement() {
val (expected, actual) = beforeAndAfterContents("fullSimple.vue")
assertEquals(expected, actual)
Expand Down
34 changes: 34 additions & 0 deletions src/test/testData/classReplacement/fullSvelteVue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {tv} from "tailwind-variants";
import Root from "./button.svelte";

const buttonVariants = tv({
base: "inline-flex items-center justify-center whitespace-nowrap rounded-md text-sm font-medium ring-offset-background transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50",
variants: {
variant: {
default: "bg-primary text-primary-foreground hover:bg-primary/90",
destructive: "bg-destructive text-destructive-foreground hover:bg-destructive/90",
outline:
"border border-input bg-background hover:bg-accent hover:text-accent-foreground",
secondary: "bg-secondary text-secondary-foreground hover:bg-secondary/80",
ghost: "hover:bg-accent hover:text-accent-foreground",
link: "text-primary underline-offset-4 hover:underline",
},
size: {
default: "h-10 px-4 py-2",
sm: "h-9 rounded-md px-3",
lg: "h-11 rounded-md px-8",
icon: "h-10 w-10",
},
},
defaultVariants: {
variant: "default",
size: "default",
},
});

export {
Root,
//
Root as Button,
buttonVariants,
};
34 changes: 34 additions & 0 deletions src/test/testData/classReplacement/fullSvelteVue_after.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {tv} from "tailwind-variants";
import Root from "./button.svelte";

const buttonVariants = tv({
base: "a-inline-flex a-items-center a-justify-center a-whitespace-nowrap a-rounded-md a-text-sm a-font-medium a-ring-offset-background a-transition-colors a-focus-visible:outline-none a-focus-visible:ring-2 a-focus-visible:ring-ring a-focus-visible:ring-offset-2 a-disabled:pointer-events-none a-disabled:opacity-50",
variants: {
variant: {
default: "a-bg-primary a-text-primary-foreground a-hover:bg-primary/90",
destructive: "a-bg-destructive a-text-destructive-foreground a-hover:bg-destructive/90",
outline:
"a-border a-border-input a-bg-background a-hover:bg-accent a-hover:text-accent-foreground",
secondary: "a-bg-secondary a-text-secondary-foreground a-hover:bg-secondary/80",
ghost: "a-hover:bg-accent a-hover:text-accent-foreground",
link: "a-text-primary a-underline-offset-4 a-hover:underline",
},
size: {
default: "a-h-10 a-px-4 a-py-2",
sm: "a-h-9 a-rounded-md a-px-3",
lg: "a-h-11 a-rounded-md a-px-8",
icon: "a-h-10 a-w-10",
},
},
defaultVariants: {
variant: "default",
size: "default",
},
});

export {
Root,
//
Root as Button,
buttonVariants,
};

0 comments on commit 283acfd

Please sign in to comment.