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

Add better class regex for javascript/javascriptreact/typescript/typescriptreact #109

Merged
merged 5 commits into from
Feb 22, 2021
Merged

Add better class regex for javascript/javascriptreact/typescript/typescriptreact #109

merged 5 commits into from
Feb 22, 2021

Conversation

petertriho
Copy link
Contributor

@petertriho petertriho commented Feb 19, 2021

Posting same regex I posted for coc-tailwincss iamcco/coc-tailwindcss#53
This should be a better regex that works with curly braces {} in react jsx

Now works with the following:
className="class1 class2"
className='class1 class2'
className={"class1 class2"}
className={'class1 class2'}
className={clsx("class1 class2")}
className={clsx(foo, bar, "class1 class2", bar)}
className={classname(foo, bar, "class1 class2", bar)}
className={anything(foo, bar, "class1 class2", bar)}
className={`flex-col flex ${className}`}

Issues:

  • Only sorts first string it encounters i.e.only sorts the first string ("class1 class2") in
    className={clsx("class1 class2", foo, bar, "class3 class4", bar)}

Fixes #58 #85 #93

@praveenperera
Copy link
Contributor

Thanks @petertriho this looks great. Could you add some test cases that cover the above examples?

@petertriho
Copy link
Contributor Author

@praveenperera I've added a few tests, had to refactor src/extension.ts > activate to test the regexes

@praveenperera
Copy link
Contributor

This looks great @petertriho thanks for the work on this. I can merge this now if you're ready.

@praveenperera praveenperera merged commit a579f51 into heybourn:master Feb 22, 2021
@petertriho petertriho deleted the better-headwind-classregex branch February 22, 2021 13:47
@michael-land
Copy link

is this [clsx, classnames] still work in latest release v1.7.0? or is it get reverted?

I can't get this format

image

@petertriho
Copy link
Contributor Author

petertriho commented Apr 22, 2021

@xiaoyu-tamu 1.7.0 does not include these changes but these changes can just be added by changing headwind.classRegex to match these regexes

@michael-land
Copy link

@petertriho Thanks for your quick reply. So i have to build latest by myself right?

@petertriho
Copy link
Contributor Author

petertriho commented Apr 22, 2021

These changes specifically can just be added to your settings.json in vscode

"headwind.classRegex": {
        "css": "\\B@apply\\s+([_a-zA-Z0-9\\s\\-\\:\\/]+);",
        "html": "\\bclass\\s*=\\s*[\\\"\\']([_a-zA-Z0-9\\s\\-\\:\\/]+)[\\\"\\']",
        "javascript": "(?:\\bclassName\\s*=\\s*[\\\"\\']([_a-zA-Z0-9\\s\\-\\:\\/]+)[\\\"\\'])|(?:\\btw[\\.\\sa-zA-Z]*`([_a-zA-Z0-9\\s\\-\\:\\/]*)`)",
        "javascriptreact": "(?:\\bclassName\\s*=\\s*[\\\"\\']([_a-zA-Z0-9\\s\\-\\:\\/]+)[\\\"\\'])|(?:\\btw[\\.\\sa-zA-Z]*`([_a-zA-Z0-9\\s\\-\\:\\/]*)`)",
        "typescript": "(?:\\bclassName\\s*=\\s*[\\\"\\']([_a-zA-Z0-9\\s\\-\\:\\/]+)[\\\"\\'])|(?:\\btw[\\.\\sa-zA-Z]*`([_a-zA-Z0-9\\s\\-\\:\\/]*)`)",
        "typescriptreact": "(?:\\bclassName\\s*=\\s*[\\\"\\']([_a-zA-Z0-9\\s\\-\\:\\/]+)[\\\"\\'])|(?:\\btw[\\.\\sa-zA-Z]*`([_a-zA-Z0-9\\s\\-\\:\\/]*)`)"
    },

@praveenperera
Copy link
Contributor

I've created a .vsix file you can manually install: #122

I will release Headwind 2.0.0 on May 1st

@msreekm
Copy link

msreekm commented May 9, 2021

doesnt seem to work for me ,I downloaded .vsix and tried

@bitabs
Copy link

bitabs commented Dec 21, 2021

These changes specifically can just be added to your settings.json in vscode

"headwind.classRegex": {
        "css": "\\B@apply\\s+([_a-zA-Z0-9\\s\\-\\:\\/]+);",
        "html": "\\bclass\\s*=\\s*[\\\"\\']([_a-zA-Z0-9\\s\\-\\:\\/]+)[\\\"\\']",
        "javascript": "(?:\\bclassName\\s*=\\s*[\\\"\\']([_a-zA-Z0-9\\s\\-\\:\\/]+)[\\\"\\'])|(?:\\btw[\\.\\sa-zA-Z]*`([_a-zA-Z0-9\\s\\-\\:\\/]*)`)",
        "javascriptreact": "(?:\\bclassName\\s*=\\s*[\\\"\\']([_a-zA-Z0-9\\s\\-\\:\\/]+)[\\\"\\'])|(?:\\btw[\\.\\sa-zA-Z]*`([_a-zA-Z0-9\\s\\-\\:\\/]*)`)",
        "typescript": "(?:\\bclassName\\s*=\\s*[\\\"\\']([_a-zA-Z0-9\\s\\-\\:\\/]+)[\\\"\\'])|(?:\\btw[\\.\\sa-zA-Z]*`([_a-zA-Z0-9\\s\\-\\:\\/]*)`)",
        "typescriptreact": "(?:\\bclassName\\s*=\\s*[\\\"\\']([_a-zA-Z0-9\\s\\-\\:\\/]+)[\\\"\\'])|(?:\\btw[\\.\\sa-zA-Z]*`([_a-zA-Z0-9\\s\\-\\:\\/]*)`)"
    },

Unfortunately, this regex doesn't work for me. I've also added them in my settings.json 😢

Example test:

// hello.tsx

<div className={c("w-5 flex")} /> // before
<div className={c("w-5 flex")} /> // current
<div className={c("flex w-5 ")} /> // expected

@japboy
Copy link

japboy commented Dec 24, 2021

this doesnt work for me 😢 #109 (comment)

Example:

import classnames from "classnames";
import React, { FunctionComponent, ReactNode } from "react";

export interface Props {
  className: string;
  children: ReactNode;
}

const Balloon: FunctionComponent<Props> = ({ className = "", children }) => {
  return (
    <div className={classnames('bg-gray-800 dark:bg-white p-4 relative rounded text-gray-100 dark:text-gray-800 after:absolute after:border-b-[12px] after:border-b-transparent after:border-t-[12px] after:border-t-gray-800 dark:after:border-t-white after:border-x-[12px] after:border-x-transparent after:content-[""] after:left-[50%] after:-ml-[6px] after:top-full', className)}>
      {children}
    </div>
  );
};

export default Balloon;

@ihatem
Copy link

ihatem commented Mar 20, 2022

These settings from this file (from the commit of this PR) worked for me:

"headwind.classRegex": {
  "html": "\\bclass\\s*=\\s*[\\\"\\']([_a-zA-Z0-9\\s\\-\\:\\/]+)[\\\"\\']",
  "css": "\\B@apply\\s+([_a-zA-Z0-9\\s\\-\\:\\/]+);",
  "javascript": "(?:\\bclass(?:Name)?\\s*=[\\w\\d\\s_,{}\\(\\)\\[\\]]*[\"'`]([\\w\\d\\s_\\-\\:\\/${}]+)[\"'`][\\w\\d\\s_,{}\\(\\)\\[\\]]*)|(?:\\btw\\s*`([\\w\\d\\s_\\-\\:\\/]*)`)",
  "javascriptreact": "(?:\\bclass(?:Name)?\\s*=[\\w\\d\\s_,{}\\(\\)\\[\\]]*[\"'`]([\\w\\d\\s_\\-\\:\\/${}]+)[\"'`][\\w\\d\\s_,{}\\(\\)\\[\\]]*)|(?:\\btw\\s*`([\\w\\d\\s_\\-\\:\\/]*)`)",
  "typescript": "(?:\\bclass(?:Name)?\\s*=[\\w\\d\\s_,{}\\(\\)\\[\\]]*[\"'`]([\\w\\d\\s_\\-\\:\\/${}]+)[\"'`][\\w\\d\\s_,{}\\(\\)\\[\\]]*)|(?:\\btw\\s*`([\\w\\d\\s_\\-\\:\\/]*)`)",
  "typescriptreact": "(?:\\bclass(?:Name)?\\s*=[\\w\\d\\s_,{}\\(\\)\\[\\]]*[\"'`]([\\w\\d\\s_\\-\\:\\/${}]+)[\"'`][\\w\\d\\s_,{}\\(\\)\\[\\]]*)|(?:\\btw\\s*`([\\w\\d\\s_\\-\\:\\/]*)`)"
},

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.

Support classnames package
7 participants