Skip to content
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

Create styled-components to Tailwind converting flow #10

Merged
merged 8 commits into from
Jul 5, 2023

Conversation

fdukat
Copy link
Member

@fdukat fdukat commented Jul 1, 2023

No description provided.

let tailwindClasses = ""

cssRules.forEach((rule) => {
if (rule.includes("{")) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure what "{" means in that context, could we extract it to a well named const variable? CASED_LIKE_THIS

@@ -0,0 +1,100 @@
export function convertToTailwindCSS(cssCode: string): string {
const cssRules = cssCode.split("}").map((rule) => rule.trim())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as below remark ⬇️

const [selectors, declarations] = rule.split("{").map((part) => part.trim())
const selectorClasses = getSelectorClasses(selectors)
const declarationClasses = getDeclarationClasses(declarations)
if (selectorClasses !== "") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's avoid if {} else statments, exit the function early instead of using else

if (selectorClasses !== "") {
tailwindClasses += `${selectorClasses} { ${declarationClasses} }\n\n`
} else {
tailwindClasses += `${declarationClasses}\n\n`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the reasoning behind adding \n\n? could we document it here? If it is possible to document it using only code then great, if it's not then use comments


function getTailwindClass(property: string, value: string): string | null {
const propertyMappings: Record<string, Record<string, string>> = {
backgroundColor: {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks awesome, good job 🖖🏻

import { convertToTailwindCSS } from "../src/CSStoTailwind"
import { stripWhitespace } from "./test-utils"

describe("#convertToTailwind", () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's add add a a few more tests:

  • a one that converts invalid CSS property to tailwind
  • a one that converts prefixed CSS property to tailwind (e.g. --webkit)
  • a shorthand CSS property with spaces - tailwind doesn't support spaces in arbitrary values and you have to use _ instead AFAIK (e.g. padding: 1rem 3rem 3rem 2rem)
  • a CSS property with decimal

const input = `.mzoqw { background-color: white; color: red; font-size: 1rem; margin: 4rem; }`
const result = convertToTailwindCSS(input)

expect(stripWhitespace(result)).toEqual(expect.stringContaining("{bg-whitetext-red-500text-basem-16}"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like the lack of spaces in the output, can't we use stripWhitespaces on input instead? (everywhere)

import { convertToTailwindCSS } from "../src/CSStoTailwind"

describe("styled-components to TailwindCSS converter flow", () => {
it("Should correctly transform valid JS input including styled-components into TailwindCSS utility classes", () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

awesome, for the DEMO purposes you can add some timeouts in between each step

\`
`

expect(() => transformCodeToAST(input)).toThrowError(/^Input is not valid JavaScript code/)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's also add expect here that confirm it doesn't throw any other error
expect(() => transformCodeToAST(input)).not.toThrowError(/^gugugaga/)

},
"include": ["src/**/*"]
"include": ["src", "dist/exampleInput.jsx", "dist/index.js"],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we probably don't need these files

@bmstefanski
Copy link
Contributor

Awesome work. I left some remarks, mainly about tests and foundations (that we haven't set up before) of the project. I think it would be best if we created a copy of that branch (locally) for tomorrows DEMO and kept working on this very branch

@fdukat fdukat merged commit 85bf246 into master Jul 5, 2023
1 check passed
@fdukat fdukat deleted the styled-to-tailwind branch July 5, 2023 17:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants