prettier-plugin-tailwind-styled-components
Version:
A Prettier plugin for sorting and formatting Tailwind CSS classes when using Tailwind Styled-Components.
10 lines (9 loc) • 4.39 kB
JavaScript
import {createRequire as __global__createRequire__} from 'module'
import {dirname as __global__dirname__} from 'path'
import {fileURLToPath} from 'url'
const require=__global__createRequire__(import.meta.url)
const __filename=fileURLToPath(import.meta.url)
const __dirname=__global__dirname__(__filename)
var w=(r=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(r,{get:(t,s)=>(typeof require<"u"?require:t)[s]}):r)(function(r){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+r+'" is not supported')});import*as O from"prettier/plugins/acorn";import*as f from"prettier/plugins/babel";import*as k from"prettier/plugins/flow";import*as S from"prettier/plugins/meriyah";import*as T from"prettier/plugins/typescript";var q=(r,t)=>{let s=(e,n,l,p,i={})=>{if(typeof t=="function"){if(t(e,n,l,p,i)===!1)return}else if(e.type in t&&t[e.type](e,n,l,p,i)===!1)return;let a=Object.keys(e);for(let c=0;c<a.length;c++){let o=e[a[c]];if(Array.isArray(o))for(let u=0;u<o.length;u++)o[u]!==null&&s(o[u],e,a[c],u,{...i});else typeof(o==null?void 0:o.type)=="string"&&s(o,e,a[c],c,{...i})}};s(r)},A=r=>(r==null?void 0:r.type)==="TemplateLiteral"||(r==null?void 0:r.type)==="Template"&&typeof r.value=="string",m=r=>r.replace(/\r\n\t/g," ").replace(/\s+/g," ").trim(),B=(r,t)=>{var s,e;return(r.tag.type==="MemberExpression"&&((s=r.tag.object)==null?void 0:s.type)==="Identifier"&&r.tag.object.name===t||r.tag.type==="CallExpression"&&((e=r.tag.callee)==null?void 0:e.type)==="Identifier"&&r.tag.callee.name===t)&&A(r.quasi)},g=r=>r.endOfLine==="lf"?`
`:r.endOfLine==="cr"?"\r":r.endOfLine==="crlf"?`\r
`:"",d=r=>r.useTabs?" ":" ".repeat(r.tabWidth),$=(r,t)=>{let s=g(t),e=d(t);return r.startsWith(s)?r:`${s}${e}${r}`},P=(r,t,s)=>{let e=g(t);if(!r.endsWith(e)){if(s){let n=d(t);return`${r}${e}${n}`}return`${r}${e}`}return r},h=(r,t,s)=>{let e=g(t),n=d(t),l=r.split(" ");return s.splice(0,l.length).join(`${e}${n}`)},j=r=>/[\S]/g.exec(r),C=(r,t,s,e)=>{if(j(r)){let n=m(r);return n=h(n,t,s),n=$(n,t),n=P(n,t,e),n}if(e){let n=m(r);return n=P(n,t,e),n}return r},x=(r,t,s)=>{if(Array.isArray(r.quasis))for(let e=0;e<r.quasis.length;e++){let n=r.quasis[e];n.value&&(n.value.raw=C(n.value.raw,t,s.rawClassList,!n.tail),n.value.cooked=C(n.value.cooked,t,s.cookedClassList,!n.tail))}},z=r=>{if(Array.isArray(r.quasis)){let t=r.quasis.map(i=>{var a;return((a=i.value)==null?void 0:a.raw)||""}).join(""),s=r.quasis.map(i=>{var a;return((a=i.value)==null?void 0:a.cooked)||""}).join(""),n=m(t).split(" "),p=m(s).split(" ");return{rawClassList:n,cookedClassList:p}}return{rawClassList:[],cookedClassList:[]}},_=(r,t)=>e=>{if(!(!e.quasi||!e.tag)&&B(e,t)){let n=z(e.quasi);x(e.quasi,r,n)}},N=(r,t)=>{var l,p;if(!Array.isArray(r.body))return;let s=t.tailwindStyledComponentsImport||"tailwind-styled-components",n=(((l=r.body.find(i=>{var a;return i.type==="ImportDeclaration"&&((a=i.source)==null?void 0:a.value)===s}))==null?void 0:l.specifiers)||[]).find(i=>{var a;return i.type==="ImportDefaultSpecifier"||i.type==="ImportSpecifier"&&((a=i.imported)==null?void 0:a.name)==="default"});return(p=n==null?void 0:n.local)==null?void 0:p.name},V=async(r,t)=>{let s=N(r,t);s&&q(r,_(t,s))},b=r=>r==null?void 0:r.toLocaleLowerCase().replaceAll("\\","/"),I=async(r,t)=>{if(!r.parsers)return;let s=t.parser,e=(typeof s=="string"?r.parsers[s]:void 0)??r.parsers.typescript;return typeof e=="function"?await e():e},L=async(r,t)=>{if(!t.plugins)return r;let s={...r},e="prettier-plugin-tailwindcss",n=null;try{n=w.resolve(e)}catch{return s}let l=b(n),p=`file:///${l}`,i=t.plugins.find(c=>{let o=b(c.name);return o===e||o===l||o===p});if(!i)return s;let a=await I(i,t);return Object.assign(s,a),s},y=async r=>({...r,async preprocess(t,s){let e=await L(r,s);return e.preprocess?e.preprocess(t,s):t},async parse(t,s){let n=await(await L(r,s)).parse(t,s);return await V(n,s),n}}),F={babel:y(f.parsers.babel),"babel-flow":y(f.parsers["babel-flow"]),flow:y(k.parsers.flow),typescript:y(T.parsers.typescript),"babel-ts":y(f.parsers["babel-ts"]),acorn:y(O.parsers.acorn),meriyah:y(S.parsers.meriyah),__js_expression:y(f.parsers.__js_expression)},R={},W={tailwindStyledComponentsImport:{type:"string",default:"tailwind-styled-components",category:"Tailwind Styled Components",description:"Tailwind styled components import"}};export{y as createParser,W as options,F as parsers,R as printers};