-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
115 additions
and
39 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { parse } from "@babel/parser"; | ||
import traverse from "@babel/traverse"; | ||
import { NodePath } from "@babel/core"; | ||
import getCssName from "../lib/getCssName.cjs"; | ||
import type { TaggedTemplateExpression } from "@babel/types"; | ||
import { describe, it, expect } from "vitest"; | ||
|
||
function extractConditionsWithBabel(code: string) { | ||
let result: string = ""; | ||
const ast = parse(code); | ||
traverse(ast, { | ||
TaggedTemplateExpression(path: NodePath<TaggedTemplateExpression>) { | ||
if (path.node.tag.type === "Identifier" && path.node.tag.name === "css") { | ||
result = getCssName(path); | ||
} | ||
}, | ||
}); | ||
return result; | ||
} | ||
|
||
describe("getCssName", () => { | ||
it("should guess the css name from the condition of a logical expression", () => { | ||
const code = `({$active}) => $active && css\`\``; | ||
const cssName = extractConditionsWithBabel(code); | ||
expect(cssName).toBe("is_active"); | ||
}); | ||
|
||
it("should guess the css name from the condition of a ternary expression", () => { | ||
const code = `({$active}) => $active ? css\`\` : null`; | ||
const cssName = extractConditionsWithBabel(code); | ||
expect(cssName).toBe("is_active"); | ||
}); | ||
|
||
it("should guess the css name from the condition of a logical expression with negation", () => { | ||
const code = `({$active}) => !$active && css\`\``; | ||
const cssName = extractConditionsWithBabel(code); | ||
expect(cssName).toBe("is_not_active"); | ||
}); | ||
|
||
it("should guess the css name from the condition of a ternary expression for the else case", () => { | ||
const code = `({$active}) => $active ? null : css\`\``; | ||
const cssName = extractConditionsWithBabel(code); | ||
expect(cssName).toBe("is_not_active"); | ||
}); | ||
|
||
it("should guess the css name from the condition of a logical expression with multiple conditions", () => { | ||
const code = `({$active, $visible}) => $active && $visible && css\`\``; | ||
const cssName = extractConditionsWithBabel(code); | ||
expect(cssName).toBe("is_active_and_visible"); | ||
}); | ||
|
||
it("should guess the css name from the condition of a ternary expression with multiple conditions", () => { | ||
const code = `({$active, $visible}) => $active ? ($visible ? css\`\` : null) : null`; | ||
const cssName = extractConditionsWithBabel(code); | ||
expect(cssName).toBe("is_active_and_visible"); | ||
}); | ||
|
||
it("should guess the css name from the condition of a logical expression with negation and multiple conditions", () => { | ||
const code = `({$active, $visible}) => !$active && $visible && css\`\``; | ||
const cssName = extractConditionsWithBabel(code); | ||
expect(cssName).toBe("is_not_active_and_visible"); | ||
}); | ||
|
||
it("should guess the css name from the condition of a ternary expression with negation and multiple conditions", () => { | ||
const code = `({$active, $visible}) => !$active ? ($visible ? css\`\`: null) : null`; | ||
const cssName = extractConditionsWithBabel(code); | ||
expect(cssName).toBe("is_not_active_and_visible"); | ||
}); | ||
|
||
it("should guess the css name from the condition of a logical expression with negation and multiple conditions for the else case", () => { | ||
const code = `({$active, $visible}) => $active && !$visible && css\`\``; | ||
const cssName = extractConditionsWithBabel(code); | ||
expect(cssName).toBe("is_active_and_not_visible"); | ||
}); | ||
|
||
it("should guess the css name from the condition of a ternary expression with negation and multiple conditions for the else case", () => { | ||
const code = `({$active, $visible}) => $active ? ($visible ? null : css\`\`) : null`; | ||
const cssName = extractConditionsWithBabel(code); | ||
expect(cssName).toBe("is_active_and_not_visible"); | ||
}); | ||
}); |
This file contains 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
This file contains 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
This file contains 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