eslint-plugin-perfectionist
Advanced tools
Comparing version 1.1.2 to 1.2.0
@@ -1,3 +0,3 @@ | ||
"use strict";var ee=Object.defineProperty;var te=(a,o,s)=>o in a?ee(a,o,{enumerable:!0,configurable:!0,writable:!0,value:s}):a[o]=s;var V=(a,o,s)=>(te(a,typeof o!="symbol"?o+"":o,s),s);const u=require("@typescript-eslint/types"),v=require("@typescript-eslint/utils"),ae=require("natural-compare-lite"),C=require("minimatch"),H=require("is-core-module"),ne=require("json5"),se=require("path"),re=require("fs");let N=v.ESLintUtils.RuleCreator(a=>`https://eslint-plugin-perfectionist.azat.io/rules/${a}`),O=a=>a.replaceAll(/\s\s+/g," ").trim(),P=a=>{let[o,s]=a;return s-o};var l=(a=>(a.alphabetical="alphabetical",a["line-length"]="line-length",a.natural="natural",a))(l||{}),f=(a=>(a.desc="desc",a.asc="asc",a))(f||{});let A=(a,o,s)=>{var e,i;if((e=o.dependencies)!=null&&e.includes(a.name))return!1;if((i=a.dependencies)!=null&&i.includes(o.name))return!0;let r=s.order==="asc"?1:-1,d,n=t=>s["ignore-case"]?t.toLowerCase():t;return s.type===l.alphabetical?d=(t,p)=>n(t.name).localeCompare(n(p.name)):s.type===l.natural?d=(t,p)=>ae(n(t.name),n(p.name)):d=(t,p)=>t.size-p.size,r*d(a,o)>0},b=(a,o)=>[...a].sort((s,r)=>Number(A(s,r,o))||-1),ie=(a,o)=>{let s=o.getTokenAfter(a,{filter:({type:r,value:d})=>!(r===u.AST_TOKEN_TYPES.Punctuator&&[",",";"].includes(d)),includeComments:!0});return((s==null?void 0:s.type)===u.AST_TOKEN_TYPES.Block||(s==null?void 0:s.type)===u.AST_TOKEN_TYPES.Line)&&a.loc.end.line===s.loc.end.line?s:null},Q=(a,o)=>{let[s,r]=o.getTokensBefore(a,{filter:({type:d,value:n})=>!(d===u.AST_TOKEN_TYPES.Punctuator&&[",",";"].includes(n)),includeComments:!0,count:2});return((r==null?void 0:r.type)===u.AST_TOKEN_TYPES.Block||(r==null?void 0:r.type)===u.AST_TOKEN_TYPES.Line)&&a.loc.start.line-r.loc.end.line<=1&&(s==null?void 0:s.loc.end.line)!==r.loc.start.line?r:null},D=(a,o)=>{var e;let s=a.range.at(0),r=a.range.at(1),d=o.text.slice(s,r);if(v.ASTUtils.isParenthesized(a,o)){let i=o.getTokenBefore(a,v.ASTUtils.isOpeningParenToken),t=o.getTokenAfter(a,v.ASTUtils.isClosingParenToken);s=i.range.at(0),r=t.range.at(1)}let n=Q(a,o);if(d.endsWith(";")||d.endsWith(",")){let i=o.getTokensAfter(a,{includeComments:!0,count:2});a.loc.start.line===((e=i.at(1))==null?void 0:e.loc.start.line)&&(r-=1)}return n&&(s=n.range.at(0)),[s,r]},Y=(a,o,s,r)=>{let d=[];return o.forEach(({node:n},e)=>{d.push(a.replaceTextRange(D(n,r),r.text.slice(...D(s[e].node,r))));let i=ie(s[e].node,r);if(i){let p=[r.getTokenBefore(i).range.at(1),i.range.at(1)];d.push(a.replaceTextRange(p,""));let g=r.getTokenAfter(n);d.push(a.insertTextAfter((g==null?void 0:g.loc.end.line)===n.loc.end.line?g:n,r.text.slice(...p)))}}),d},w=(a={},o)=>Object.assign(o,a),I=(a,o)=>{if(a.length>1)for(let s=1;s<a.length;s++){let r=a.at(s-1),d=a.at(s);r&&d&&o(r,d,s-1)}};const L="sort-array-includes",oe=N({name:L,meta:{type:"suggestion",docs:{description:"enforce sorted arrays before include method",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[l.alphabetical,l.natural,l["line-length"]],default:l.natural},order:{enum:[f.asc,f.desc],default:f.asc},"ignore-case":{type:"boolean",default:!1},"spread-last":{type:"boolean",default:!1}},additionalProperties:!1}],messages:{unexpectedArrayIncludesOrder:'Expected "{{right}}" to come before "{{left}}"'}},defaultOptions:[{type:l.alphabetical,order:f.asc}],create:a=>({MemberExpression:o=>{if((o.object.type===u.AST_NODE_TYPES.ArrayExpression||o.object.type===u.AST_NODE_TYPES.NewExpression)&&o.property.type===u.AST_NODE_TYPES.Identifier&&o.property.name==="includes"){let s=o.object.type===u.AST_NODE_TYPES.ArrayExpression?o.object.elements:o.object.arguments;if(s.length>1){let r=w(a.options.at(0),{type:l.alphabetical,order:f.asc,"ignore-case":!1,"spread-last":!1}),d=a.getSourceCode(),n=s.reduce((e,i)=>(i!==null&&e.at(0).push({name:i.type===u.AST_NODE_TYPES.Literal?i.raw:d.text.slice(...i.range),size:P(i.range),type:i.type,node:i}),e),[[],[]]).flat();I(n,(e,i)=>{let t;r["spread-last"]&&e.node.type===u.AST_NODE_TYPES.Literal&&i.node.type===u.AST_NODE_TYPES.SpreadElement?t=!1:r["spread-last"]&&e.node.type===u.AST_NODE_TYPES.SpreadElement&&i.node.type===u.AST_NODE_TYPES.Literal?t=!0:t=A(e,i,r),t&&a.report({messageId:"unexpectedArrayIncludesOrder",data:{left:O(e.name),right:O(i.name)},node:i.node,fix:p=>{let g=b(n,r);return r["spread-last"]&&g.forEach((S,T)=>{S.node.type===u.AST_NODE_TYPES.SpreadElement&&g.push(g.splice(T,1).at(0))}),Y(p,n,g,d)}})})}}}})}),$="sort-named-exports",le=N({name:$,meta:{type:"suggestion",docs:{description:"enforce sorted named exports",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[l.alphabetical,l.natural,l["line-length"]],default:l.natural},order:{enum:[f.asc,f.desc],default:f.asc},"ignore-case":{type:"boolean",default:!1}},additionalProperties:!1}],messages:{unexpectedNamedExportsOrder:'Expected "{{right}}" to come before "{{left}}"'}},defaultOptions:[{type:l.alphabetical,order:f.asc}],create:a=>({ExportNamedDeclaration:o=>{if(o.specifiers.length>1){let s=w(a.options.at(0),{type:l.alphabetical,"ignore-case":!1,order:f.asc}),r=a.getSourceCode(),d=o.specifiers.map(n=>({size:P(n.range),name:n.local.name,node:n}));I(d,(n,e)=>{A(n,e,s)&&a.report({messageId:"unexpectedNamedExportsOrder",data:{left:n.name,right:e.name},node:e.node,fix:i=>Y(i,d,b(d,s),r)})})}}})}),B="sort-named-imports",pe=N({name:B,meta:{type:"suggestion",docs:{description:"enforce sorted named imports",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[l.alphabetical,l.natural,l["line-length"]],default:l.natural},order:{enum:[f.asc,f.desc],default:f.asc},"ignore-case":{type:"boolean",default:!1}},additionalProperties:!1}],messages:{unexpectedNamedImportsOrder:'Expected "{{right}}" to come before "{{left}}"'}},defaultOptions:[{type:l.alphabetical,order:f.asc}],create:a=>({ImportDeclaration:o=>{let s=o.specifiers.filter(({type:r})=>r===u.AST_NODE_TYPES.ImportSpecifier);if(s.length>1){let r=w(a.options.at(0),{type:l.alphabetical,"ignore-case":!1,order:f.asc}),d=a.getSourceCode(),n=s.map(e=>({size:P(e.range),name:e.local.name,node:e}));I(n,(e,i)=>{A(e,i,r)&&a.report({messageId:"unexpectedNamedImportsOrder",data:{left:e.name,right:i.name},node:i.node,fix:t=>Y(t,n,b(n,r),d)})})}}})}),M="sort-object-types",ce=N({name:M,meta:{type:"suggestion",docs:{description:"enforce sorted object types",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[l.alphabetical,l.natural,l["line-length"]],default:l.natural},order:{enum:[f.asc,f.desc],default:f.asc},"ignore-case":{type:"boolean",default:!1}},additionalProperties:!1}],messages:{unexpectedObjectTypesOrder:'Expected "{{right}}" to come before "{{left}}"'}},defaultOptions:[{type:l.alphabetical,order:f.asc}],create:a=>({TSTypeLiteral:o=>{if(o.members.length>1){let s=w(a.options.at(0),{type:l.alphabetical,"ignore-case":!1,order:f.asc}),r=a.getSourceCode(),d=o.members.map(n=>{var S,T;let e,i=r.text.slice(n.range.at(0),n.range.at(1)),t=y=>y.replace(/(,|;)$/,"");if(n.type===u.AST_NODE_TYPES.TSPropertySignature)n.key.type===u.AST_NODE_TYPES.Identifier?{name:e}=n.key:n.key.type===u.AST_NODE_TYPES.Literal?e=`${n.key.value}`:e=r.text.slice(n.range.at(0),(S=n.typeAnnotation)==null?void 0:S.range.at(0));else if(n.type===u.AST_NODE_TYPES.TSIndexSignature){let y=((T=n.typeAnnotation)==null?void 0:T.range.at(0))??n.range.at(1);e=t(r.text.slice(n.range.at(0),y))}else e=t(r.text.slice(n.range.at(0),n.range.at(1)));let g=i.endsWith(";")||i.endsWith(",")?1:0;return{size:P(n.range)-g,node:n,name:e}});I(d,(n,e)=>{A(n,e,s)&&a.report({messageId:"unexpectedObjectTypesOrder",data:{left:O(n.name),right:O(e.name)},node:e.node,fix:i=>Y(i,d,b(d,s),r)})})}}})}),U="sort-map-elements",de=N({name:U,meta:{type:"suggestion",docs:{description:"enforce sorted Map elements",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[l.alphabetical,l.natural,l["line-length"]],default:l.natural},order:{enum:[f.asc,f.desc],default:f.asc},"ignore-case":{type:"boolean",default:!1}},additionalProperties:!1}],messages:{unexpectedMapElementsOrder:'Expected "{{right}}" to come before "{{left}}"'}},defaultOptions:[{type:l.alphabetical,order:f.asc}],create:a=>({NewExpression:o=>{if(o.callee.type===u.AST_NODE_TYPES.Identifier&&o.callee.name==="Map"&&o.arguments.length&&o.arguments[0].type===u.AST_NODE_TYPES.ArrayExpression){let[{elements:s}]=o.arguments;if(s.length>1){let r=w(a.options.at(0),{type:l.alphabetical,"ignore-case":!1,order:f.asc}),d=a.getSourceCode();s.reduce((e,i)=>(i===null||i.type===u.AST_NODE_TYPES.SpreadElement?e.push([]):e.at(-1).push(i),e),[[]]).forEach(e=>{let i=e.map(t=>{let p;if(t.type===u.AST_NODE_TYPES.ArrayExpression){let[g]=t.elements;g?g.type===u.AST_NODE_TYPES.Literal?p=g.raw:p=d.text.slice(...g.range):p=`${g}`}else p=d.text.slice(...t.range);return{size:P(t.range),node:t,name:p}});I(i,(t,p)=>{A(t,p,r)&&a.report({messageId:"unexpectedMapElementsOrder",data:{left:O(t.name),right:O(p.name)},node:p.node,fix:g=>Y(g,i,b(i,r),d)})})})}}}})}),z="sort-union-types",fe=N({name:z,meta:{type:"suggestion",docs:{description:"enforce sorted union types",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[l.alphabetical,l.natural,l["line-length"]],default:l.natural},order:{enum:[f.asc,f.desc],default:f.asc},"ignore-case":{type:"boolean",default:!1}},additionalProperties:!1}],messages:{unexpectedUnionTypesOrder:'Expected "{{right}}" to come before "{{left}}"'}},defaultOptions:[{type:l.alphabetical,order:f.asc}],create:a=>({TSUnionType:o=>{let s=a.getSourceCode(),r=w(a.options.at(0),{type:l.alphabetical,"ignore-case":!1,order:f.asc}),d=o.types.map(n=>({name:s.text.slice(...n.range),size:P(n.range),node:n}));I(d,(n,e)=>{A(n,e,r)&&a.report({messageId:"unexpectedUnionTypesOrder",data:{left:O(n.name),right:O(e.name)},node:e.node,fix:i=>Y(i,d,b(d,r),s)})})}})}),q="sort-interfaces",ue=N({name:q,meta:{type:"suggestion",docs:{description:"enforce sorted interface properties",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[l.alphabetical,l.natural,l["line-length"]],default:l.natural},order:{enum:[f.asc,f.desc],default:f.asc},"ignore-case":{type:"boolean",default:!1},"ignore-pattern":{type:"array",default:[]}},additionalProperties:!1}],messages:{unexpectedInterfacePropertiesOrder:'Expected "{{right}}" to come before "{{left}}"'}},defaultOptions:[{type:l.alphabetical,order:f.asc}],create:a=>({TSInterfaceDeclaration:o=>{if(o.body.body.length>1){let s=w(a.options.at(0),{type:l.alphabetical,"ignore-case":!1,order:f.asc,"ignore-pattern":[]});if(!s["ignore-pattern"].some(r=>C.minimatch(o.id.name,r))){let r=a.getSourceCode(),d=o.body.body.map(n=>{var i,t,p;let e;if(n.type===u.AST_NODE_TYPES.TSPropertySignature)if(n.key.type===u.AST_NODE_TYPES.Identifier)({name:e}=n.key);else if(n.key.type===u.AST_NODE_TYPES.Literal)e=`${n.key.value}`;else{let g=((i=n.typeAnnotation)==null?void 0:i.range.at(0))??n.range.at(1)-(n.optional?1:0);e=r.text.slice(n.range.at(0),g)}else if(n.type===u.AST_NODE_TYPES.TSIndexSignature){let g=((t=n.typeAnnotation)==null?void 0:t.range.at(0))??n.range.at(1);e=r.text.slice(n.range.at(0),g)}else{let g=((p=n.returnType)==null?void 0:p.range.at(0))??n.range.at(1);e=r.text.slice(n.range.at(0),g)}return{size:P(n.range),node:n,name:e}});I(d,(n,e)=>{A(n,e,s)&&a.report({messageId:"unexpectedInterfacePropertiesOrder",data:{left:O(n.name),right:O(e.name)},node:e.node,fix:i=>Y(i,d,b(d,s),r)})})}}}})});let Z=(a,o)=>a.reduce((s,r)=>{let d=o(r);return d in s||(s[d]=[]),s[d].push(r),s},{});const J="sort-jsx-props",ge=N({name:J,meta:{type:"suggestion",docs:{description:"enforce sorted JSX props",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[l.alphabetical,l.natural,l["line-length"]],default:l.natural},order:{enum:[f.asc,f.desc],default:f.asc},"always-on-top":{type:"array",default:[]},"ignore-case":{type:"boolean",default:!1},shorthand:{enum:["first","last","ignore"]},callback:{enum:["first","last","ignore"]},multiline:{enum:["first","last","ignore"]}},additionalProperties:!1}],messages:{unexpectedJSXPropsOrder:'Expected "{{right}}" to come before "{{left}}"'}},defaultOptions:[{type:l.alphabetical,order:f.asc}],create:a=>({JSXElement:o=>{if(o.openingElement.attributes.length>1){let s=w(a.options.at(0),{type:l.alphabetical,shorthand:"ignore",multiline:"ignore",callback:"ignore","always-on-top":[],"ignore-case":!1,order:f.asc}),r=a.getSourceCode();o.openingElement.attributes.reduce((n,e)=>{if(e.type===u.AST_NODE_TYPES.JSXSpreadAttribute)return n.push([]),n;let i="ignore";e.name.type===u.AST_NODE_TYPES.JSXIdentifier&&s["always-on-top"].includes(e.name.name)?i="exception":(s.shorthand!=="ignore"&&e.value===null&&(i=s.shorthand),s.callback!=="ignore"&&e.name.type===u.AST_NODE_TYPES.JSXIdentifier&&e.name.name.indexOf("on")===0&&e.value!==null?i=s.callback:s.multiline!=="ignore"&&e.loc.start.line!==e.loc.end.line&&(i=s.multiline));let t={name:e.name.type===u.AST_NODE_TYPES.JSXNamespacedName?`${e.name.namespace.name}:${e.name.name.name}`:e.name.name,size:P(e.range),node:e,position:i};return n.at(-1).push(t),n},[[]]).forEach(n=>{I(n,(e,i)=>{let t;if(e.position==="exception"&&i.position==="exception")t=s["always-on-top"].indexOf(e.name)>s["always-on-top"].indexOf(i.name);else if(e.position===i.position)t=A(e,i,s);else{let p={exception:2,first:1,ignore:0,last:-1};t=p[e.position]<p[i.position]}t&&a.report({messageId:"unexpectedJSXPropsOrder",data:{left:e.name,right:i.name},node:i.node,fix:p=>{let g=Z(n,({position:y})=>y),S=y=>y in g?g[y]:[],T=[S("exception").sort((y,c)=>s["always-on-top"].indexOf(y.name)-s["always-on-top"].indexOf(c.name)),b(S("first"),s),b(S("ignore"),s),b(S("last"),s)].flat();return Y(p,n,T,r)}})})})}}})});class F{static get(){return this._instance?this._instance:(this._instance=ne.parse(re.readFileSync(se.resolve(process.cwd(),"tsconfig.json"),"utf8")),this._instance)}}V(F,"_instance");const W="sort-imports",me=N({name:W,meta:{type:"suggestion",docs:{description:"enforce sorted imports",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[l.alphabetical,l.natural,l["line-length"]],default:l.natural},order:{enum:[f.asc,f.desc],default:f.asc},"ignore-case":{type:"boolean",default:!1},groups:{type:"array",default:[]},"internal-pattern":{type:"array",default:["~/**"]},"newlines-between":{enum:["ignore","always","never"],default:"always"},"read-tsconfig":{type:"boolean",default:!1}},additionalProperties:!1}],messages:{unexpectedImportsOrder:'Expected "{{right}}" to come before "{{left}}"',missedSpacingBetweenImports:'Missed spacing between "{{left}}" and "{{right}}" imports',extraSpacingBetweenImports:'Extra spacing between "{{left}}" and "{{right}}" imports'}},defaultOptions:[{type:l.alphabetical,order:f.asc}],create:a=>{var i;let o=w(a.options.at(0),{"newlines-between":"always","internal-pattern":["~/**"],type:l.alphabetical,"read-tsconfig":!1,order:f.asc,"ignore-case":!1,groups:[]}),s=[];if(o["read-tsconfig"]){let t=F.get();(i=t.compilerOptions)!=null&&i.paths&&Object.keys(t.compilerOptions.paths).forEach(p=>{s.push(p)})}let r=a.getSourceCode(),d=[],n=t=>{let p,g=E=>[".less",".scss",".sass",".pcss",".css",".sss"].some(_=>E.endsWith(_)),S=E=>["./index.d.js","./index.d.ts","./index.js","./index.ts","./index","./","."].includes(E),T=E=>E.indexOf("..")===0,y=E=>E.indexOf("./")===0,c=E=>{!p&&o.groups.flat().includes(E)&&(p=E)},m=E=>o["internal-pattern"].length&&o["internal-pattern"].some(_=>C.minimatch(E.source.value,_))||s.some(_=>C.minimatch(E.source.value,_));return t.importKind==="type"&&(t.type===u.AST_NODE_TYPES.ImportDeclaration&&(H(t.source.value)&&c("builtin-type"),m(t)&&c("internal-type"),S(t.source.value)&&c("index-type"),T(t.source.value)&&c("parent-type"),y(t.source.value)&&c("sibling-type")),c("type")),!p&&t.type===u.AST_NODE_TYPES.ImportDeclaration&&(H(t.source.value)&&c("builtin"),m(t)&&c("internal"),g(t.source.value)&&c("style"),t.specifiers.length===0&&c("side-effect"),S(t.source.value)&&c("index"),T(t.source.value)&&c("parent"),y(t.source.value)&&c("sibling"),c("external")),p??"unknown"},e=t=>{let p;t.type===u.AST_NODE_TYPES.ImportDeclaration?p=t.source.value:t.moduleReference.type===u.AST_NODE_TYPES.TSExternalModuleReference&&t.moduleReference.expression.type===u.AST_NODE_TYPES.Literal?p=`${t.moduleReference.expression.value}`:p=r.text.slice(...t.moduleReference.range),d.push({size:P(t.range),group:n(t),name:p,node:t})};return{TSImportEqualsDeclaration:e,ImportDeclaration:e,"Program:exit":()=>{let t=y=>{for(let c=0,m=o.groups.length;c<m;c++){let E=o.groups[c];if(y.group===E||Array.isArray(E)&&E.includes(y.group))return c}return o.groups.length},p=(y,c)=>!!r.getTokensBetween(y.node,Q(c.node,r)||c.node,{includeComments:!0}).length,g=(y,c)=>r.lines.slice(y.node.loc.end.line,c.node.loc.start.line-1).filter(E=>!E.trim().length).length,S=(y,c)=>{let m=[],E=c.reduce((x,h)=>{let j=t(h);return j in x?x[j]=b([...x[j],h],o):x[j]=[h],x},{}),_=Object.keys(E).sort().reduce((x,h)=>[...x,...E[h]],[]);return _.forEach((x,h)=>{if(m.push(y.replaceTextRange(D(c.at(h).node,r),r.text.slice(...D(x.node,r)))),o["newlines-between"]!=="ignore"){let j=_.at(h+1);if(j){let k=g(c.at(h),c.at(h+1));(o["newlines-between"]==="always"&&t(x)===t(j)&&k!==0||o["newlines-between"]==="never"&&k>0)&&m.push(y.removeRange([D(c.at(h).node,r).at(1),D(c.at(h+1).node,r).at(0)-1])),o["newlines-between"]==="always"&&k>1&&m.push(y.replaceTextRange([D(c.at(h).node,r).at(1),D(c.at(h+1).node,r).at(0)-1],` | ||
`)),o["newlines-between"]==="always"&&t(x)!==t(j)&&k===0&&m.push(y.insertTextAfterRange(D(c.at(h).node,r),` | ||
`))}}}),m};d.reduce((y,c)=>{var E;let m=(E=y.at(-1))==null?void 0:E.at(-1);return m&&p(m,c)?y.push([c]):y.at(-1).push(c),y},[[]]).forEach(y=>{I(y,(c,m)=>{let E=t(c),_=t(m),x=g(c,m);!p(c,m)&&(E>_||E===_&&A(c,m,o))&&a.report({messageId:"unexpectedImportsOrder",data:{left:c.name,right:m.name},node:m.node,fix:h=>S(h,y)}),o["newlines-between"]==="never"&&x>0&&a.report({messageId:"extraSpacingBetweenImports",data:{left:c.name,right:m.name},node:m.node,fix:h=>S(h,y)}),o["newlines-between"]==="always"&&(E<_&&x===0?a.report({messageId:"missedSpacingBetweenImports",data:{left:c.name,right:m.name},node:m.node,fix:h=>S(h,y)}):(x>1||E===_&&x>0)&&a.report({messageId:"extraSpacingBetweenImports",data:{left:c.name,right:m.name},node:m.node,fix:h=>S(h,y)}))})})}}}}),X="sort-classes",ye=N({name:X,meta:{type:"suggestion",docs:{description:"enforce sorted classes",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[l.alphabetical,l.natural,l["line-length"]],default:l.natural},"ignore-case":{type:"boolean",default:!1},order:{enum:[f.asc,f.desc],default:f.asc},groups:{type:"array",default:["property","constructor","method","unknown"]}},additionalProperties:!1}],messages:{unexpectedClassesOrder:'Expected "{{right}}" to come before "{{left}}"'}},defaultOptions:[{type:l.alphabetical,order:f.asc}],create:a=>({ClassBody:o=>{if(o.body.length>1){let s=w(a.options.at(0),{type:l.alphabetical,order:f.asc,"ignore-case":!1,groups:["property","constructor","method","unknown"]}),r=a.getSourceCode(),d=o.body.map(e=>{var g;let i,t;e.type===u.AST_NODE_TYPES.StaticBlock?t="static":e.type===u.AST_NODE_TYPES.TSIndexSignature?t=r.text.slice(e.range.at(0),((g=e.typeAnnotation)==null?void 0:g.range.at(0))??e.range.at(1)):e.key.type===u.AST_NODE_TYPES.Identifier?{name:t}=e.key:t=r.text.slice(...e.key.range);let p=S=>{!i&&s.groups.flat().includes(S)&&(i=S)};return e.type===u.AST_NODE_TYPES.MethodDefinition?(e.kind==="constructor"&&p("constructor"),e.static&&p("static-method"),e.accessibility==="private"&&p("private-method"),p("method")):e.type===u.AST_NODE_TYPES.PropertyDefinition&&(e.static&&p("static-property"),e.accessibility==="private"&&p("private-property"),p("property")),{size:P(e.range),group:i??"unknown",node:e,name:t}}),n=e=>{for(let i=0,t=s.groups.length;i<t;i++){let p=s.groups[i];if(e.group===p||Array.isArray(p)&&p.includes(e.group))return i}return s.groups.length};I(d,(e,i)=>{let t=n(e),p=n(i);(t>p||t===p&&A(e,i,s))&&a.report({messageId:"unexpectedClassesOrder",data:{left:O(e.name),right:O(i.name)},node:i.node,fix:g=>{let S=[],T=d.reduce((c,m)=>{let E=n(m);return E in c?c[E]=b([...c[E],m],s):c[E]=[m],c},{});return Object.keys(T).sort().reduce((c,m)=>[...c,...T[m]],[]).forEach((c,m)=>{S.push(g.replaceTextRange(D(d.at(m).node,r),r.text.slice(...D(c.node,r))))}),S}})})}}})}),G="sort-objects",Ee=N({name:G,meta:{type:"suggestion",docs:{description:"enforce sorted objects",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[l.alphabetical,l.natural,l["line-length"]],default:l.natural},order:{enum:[f.asc,f.desc],default:f.asc},"ignore-case":{type:"boolean",default:!1},"always-on-top":{type:"array",default:[]}},additionalProperties:!1}],messages:{unexpectedObjectsOrder:'Expected "{{right}}" to come before "{{left}}"'}},defaultOptions:[{type:l.alphabetical,order:f.asc}],create:a=>{let o=s=>{if(s.properties.length>1){let r=w(a.options.at(0),{type:l.alphabetical,"ignore-case":!1,order:f.asc,"always-on-top":[]}),d=a.getSourceCode();(e=>e.reduce((i,t)=>{if(t.type===u.AST_NODE_TYPES.SpreadElement||t.type===u.AST_NODE_TYPES.RestElement)return i.push([]),i;let p,g="ignore",S=[];if(t.key.type===u.AST_NODE_TYPES.Identifier?{name:p}=t.key:t.key.type===u.AST_NODE_TYPES.Literal?p=`${t.key.value}`:p=d.text.slice(...t.key.range),t.key.type===u.AST_NODE_TYPES.Identifier&&r["always-on-top"].includes(t.key.name)&&(g="exception"),t.value.type===u.AST_NODE_TYPES.AssignmentPattern){let y=(c,m)=>{c.right.type===u.AST_NODE_TYPES.Identifier&&S.push(c.right.name),!m&&c.left.type===u.AST_NODE_TYPES.Identifier&&S.push(c.left.name);let E=_=>{_.right.type===u.AST_NODE_TYPES.Identifier&&S.push(_.right.name),_.left.type===u.AST_NODE_TYPES.BinaryExpression&&y(_.left,!1)};c.right.type===u.AST_NODE_TYPES.BinaryExpression&&E(c.right)};y(t.value,!0)}let T={size:P(t.range),dependencies:S,node:t,position:g,name:p};return i.at(-1).push(T),i},[[]]))(s.properties).forEach(e=>{I(e,(i,t)=>{let p;if(i.position==="exception"&&t.position==="exception")p=r["always-on-top"].indexOf(i.name)>r["always-on-top"].indexOf(t.name);else if(i.position===t.position)p=A(i,t,r);else{let g={exception:1,ignore:0};p=g[i.position]<g[t.position]}if(p){let g=S=>{let T=Z(e,({position:m})=>m),y=m=>m in T?T[m]:[],c=[y("exception").sort((m,E)=>r["always-on-top"].indexOf(m.name)-r["always-on-top"].indexOf(E.name)),b(y("ignore"),r)].flat();return Y(S,e,c,d)};a.report({messageId:"unexpectedObjectsOrder",data:{left:O(i.name),right:O(t.name)},node:t.node,fix:g})}})})}};return{ObjectExpression:o,ObjectPattern:o}}}),K="sort-enums",Se=N({name:K,meta:{type:"suggestion",docs:{description:"enforce sorted TypeScript enums",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[l.alphabetical,l.natural,l["line-length"]],default:l.natural},"ignore-case":{type:"boolean",default:!1},order:{enum:[f.asc,f.desc],default:f.asc}},additionalProperties:!1}],messages:{unexpectedEnumsOrder:'Expected "{{right}}" to come before "{{left}}"'}},defaultOptions:[{type:l.alphabetical,order:f.asc}],create:a=>({TSEnumDeclaration:o=>{if(o.members.length>1&&o.members.some(({initializer:s})=>s)){let s=w(a.options.at(0),{type:l.alphabetical,order:f.asc,"ignore-case":!1}),r=a.getSourceCode(),d=o.members.map(n=>({name:n.id.type===u.AST_NODE_TYPES.Literal?`${n.id.value}`:`${r.text.slice(...n.id.range)}`,size:P(n.range),node:n}));I(d,(n,e)=>{A(n,e,s)&&a.report({messageId:"unexpectedEnumsOrder",data:{left:O(n.name),right:O(e.name)},node:e.node,fix:i=>Y(i,d,b(d,s),r)})})}}})}),he="eslint-plugin-perfectionist";let R=a=>{let o={[W]:["error",{groups:["type",["builtin","external"],"internal-type","internal",["parent-type","sibling-type","index-type"],["parent","sibling","index"],"object","unknown"],"newlines-between":"always","internal-pattern":["~/**"],"read-tsconfig":!1}],[X]:["error",{groups:["static-property","private-property","property","constructor","static-method","private-method","method","unknown"]}],[J]:["error",{"always-on-top":[],shorthand:"ignore",multiline:"first",callback:"ignore"}],[L]:["error",{"spread-last":!0}],[G]:["error",{"always-on-top":[]}],[B]:["error"],[$]:["error"],[M]:["error"],[U]:["error"],[z]:["error"],[q]:["error"],[K]:["error"]};return{rules:Object.fromEntries(Object.entries(o).map(([s,[r,d={}]])=>[`perfectionist/${s}`,[r,Object.assign(d,a)]])),plugins:["perfectionist"]}};const Te={rules:{[L]:oe,[X]:ye,[K]:Se,[W]:me,[q]:ue,[J]:ge,[U]:de,[$]:le,[B]:pe,[M]:ce,[G]:Ee,[z]:fe},configs:{"recommended-alphabetical":R({type:l.alphabetical,order:f.asc,"ignore-case":!1}),"recommended-natural":R({type:l.natural,order:f.asc,"ignore-case":!1}),"recommended-line-length":R({type:l["line-length"],order:f.desc})},name:he};module.exports=Te; | ||
"use strict";var te=Object.defineProperty;var ae=(t,l,s)=>l in t?te(t,l,{enumerable:!0,configurable:!0,writable:!0,value:s}):t[l]=s;var H=(t,l,s)=>(ae(t,typeof l!="symbol"?l+"":l,s),s);const u=require("@typescript-eslint/types"),v=require("@typescript-eslint/utils"),re=require("natural-compare-lite"),C=require("minimatch"),Q=require("is-core-module"),se=require("json5"),ne=require("path"),oe=require("fs");let N=v.ESLintUtils.RuleCreator(t=>`https://eslint-plugin-perfectionist.azat.io/rules/${t}`),O=t=>t.replaceAll(/\s\s+/g," ").trim(),P=t=>{let[l,s]=t;return s-l};var i=(t=>(t.alphabetical="alphabetical",t["line-length"]="line-length",t.natural="natural",t))(i||{}),d=(t=>(t.desc="desc",t.asc="asc",t))(d||{});let A=(t,l,s)=>{var e,o;if((e=l.dependencies)!=null&&e.includes(t.name))return!1;if((o=t.dependencies)!=null&&o.includes(l.name))return!0;let n=s.order==="asc"?1:-1,p,r=a=>s["ignore-case"]?a.toLowerCase():a;return s.type===i.alphabetical?p=(a,c)=>r(a.name).localeCompare(r(c.name)):s.type===i.natural?p=(a,c)=>re(r(a.name),r(c.name)):p=(a,c)=>a.size-c.size,n*p(t,l)>0},b=(t,l)=>[...t].sort((s,n)=>Number(A(s,n,l))||-1),le=(t,l)=>{let s=l.getTokenAfter(t,{filter:({type:n,value:p})=>!(n===u.AST_TOKEN_TYPES.Punctuator&&[",",";"].includes(p)),includeComments:!0});return((s==null?void 0:s.type)===u.AST_TOKEN_TYPES.Block||(s==null?void 0:s.type)===u.AST_TOKEN_TYPES.Line)&&t.loc.end.line===s.loc.end.line?s:null},Z=(t,l)=>{let[s,n]=l.getTokensBefore(t,{filter:({type:p,value:r})=>!(p===u.AST_TOKEN_TYPES.Punctuator&&[",",";"].includes(r)),includeComments:!0,count:2});return((n==null?void 0:n.type)===u.AST_TOKEN_TYPES.Block||(n==null?void 0:n.type)===u.AST_TOKEN_TYPES.Line)&&t.loc.start.line-n.loc.end.line<=1&&(s==null?void 0:s.loc.end.line)!==n.loc.start.line?n:null},I=(t,l)=>{var e;let s=t.range.at(0),n=t.range.at(1),p=l.text.slice(s,n);if(v.ASTUtils.isParenthesized(t,l)){let o=l.getTokenBefore(t,v.ASTUtils.isOpeningParenToken),a=l.getTokenAfter(t,v.ASTUtils.isClosingParenToken);s=o.range.at(0),n=a.range.at(1)}let r=Z(t,l);if(p.endsWith(";")||p.endsWith(",")){let o=l.getTokensAfter(t,{includeComments:!0,count:2});t.loc.start.line===((e=o.at(1))==null?void 0:e.loc.start.line)&&(n-=1)}return r&&(s=r.range.at(0)),[s,n]},Y=(t,l,s,n)=>{let p=[];return l.forEach(({node:r},e)=>{p.push(t.replaceTextRange(I(r,n),n.text.slice(...I(s[e].node,n))));let o=le(s[e].node,n);if(o){let c=[n.getTokenBefore(o).range.at(1),o.range.at(1)];p.push(t.replaceTextRange(c,""));let g=n.getTokenAfter(r);p.push(t.insertTextAfter((g==null?void 0:g.loc.end.line)===r.loc.end.line?g:r,n.text.slice(...c)))}}),p},w=(t={},l)=>Object.assign(l,t),D=(t,l)=>{if(t.length>1)for(let s=1;s<t.length;s++){let n=t.at(s-1),p=t.at(s);n&&p&&l(n,p,s-1)}};const L="sort-array-includes",ie=N({name:L,meta:{type:"suggestion",docs:{description:"enforce sorted arrays before include method",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[i.alphabetical,i.natural,i["line-length"]],default:i.natural},order:{enum:[d.asc,d.desc],default:d.asc},"ignore-case":{type:"boolean",default:!1},"spread-last":{type:"boolean",default:!1}},additionalProperties:!1}],messages:{unexpectedArrayIncludesOrder:'Expected "{{right}}" to come before "{{left}}"'}},defaultOptions:[{type:i.alphabetical,order:d.asc}],create:t=>({MemberExpression:l=>{if((l.object.type===u.AST_NODE_TYPES.ArrayExpression||l.object.type===u.AST_NODE_TYPES.NewExpression)&&l.property.type===u.AST_NODE_TYPES.Identifier&&l.property.name==="includes"){let s=l.object.type===u.AST_NODE_TYPES.ArrayExpression?l.object.elements:l.object.arguments;if(s.length>1){let n=w(t.options.at(0),{type:i.alphabetical,order:d.asc,"ignore-case":!1,"spread-last":!1}),p=t.getSourceCode(),r=s.reduce((e,o)=>(o!==null&&e.at(0).push({name:o.type===u.AST_NODE_TYPES.Literal?o.raw:p.text.slice(...o.range),size:P(o.range),type:o.type,node:o}),e),[[],[]]).flat();D(r,(e,o)=>{let a;n["spread-last"]&&e.node.type===u.AST_NODE_TYPES.Literal&&o.node.type===u.AST_NODE_TYPES.SpreadElement?a=!1:n["spread-last"]&&e.node.type===u.AST_NODE_TYPES.SpreadElement&&o.node.type===u.AST_NODE_TYPES.Literal?a=!0:a=A(e,o,n),a&&t.report({messageId:"unexpectedArrayIncludesOrder",data:{left:O(e.name),right:O(o.name)},node:o.node,fix:c=>{let g=b(r,n);return n["spread-last"]&&g.forEach((S,T)=>{S.node.type===u.AST_NODE_TYPES.SpreadElement&&g.push(g.splice(T,1).at(0))}),Y(c,r,g,p)}})})}}}})}),$="sort-named-exports",pe=N({name:$,meta:{type:"suggestion",docs:{description:"enforce sorted named exports",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[i.alphabetical,i.natural,i["line-length"]],default:i.natural},order:{enum:[d.asc,d.desc],default:d.asc},"ignore-case":{type:"boolean",default:!1}},additionalProperties:!1}],messages:{unexpectedNamedExportsOrder:'Expected "{{right}}" to come before "{{left}}"'}},defaultOptions:[{type:i.alphabetical,order:d.asc}],create:t=>({ExportNamedDeclaration:l=>{if(l.specifiers.length>1){let s=w(t.options.at(0),{type:i.alphabetical,"ignore-case":!1,order:d.asc}),n=t.getSourceCode(),p=l.specifiers.map(r=>({size:P(r.range),name:r.local.name,node:r}));D(p,(r,e)=>{A(r,e,s)&&t.report({messageId:"unexpectedNamedExportsOrder",data:{left:r.name,right:e.name},node:e.node,fix:o=>Y(o,p,b(p,s),n)})})}}})}),M="sort-named-imports",ce=N({name:M,meta:{type:"suggestion",docs:{description:"enforce sorted named imports",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[i.alphabetical,i.natural,i["line-length"]],default:i.natural},order:{enum:[d.asc,d.desc],default:d.asc},"ignore-case":{type:"boolean",default:!1}},additionalProperties:!1}],messages:{unexpectedNamedImportsOrder:'Expected "{{right}}" to come before "{{left}}"'}},defaultOptions:[{type:i.alphabetical,order:d.asc}],create:t=>({ImportDeclaration:l=>{let s=l.specifiers.filter(({type:n})=>n===u.AST_NODE_TYPES.ImportSpecifier);if(s.length>1){let n=w(t.options.at(0),{type:i.alphabetical,"ignore-case":!1,order:d.asc}),p=t.getSourceCode(),r=s.map(e=>({size:P(e.range),name:e.local.name,node:e}));D(r,(e,o)=>{A(e,o,n)&&t.report({messageId:"unexpectedNamedImportsOrder",data:{left:e.name,right:o.name},node:o.node,fix:a=>Y(a,r,b(r,n),p)})})}}})}),B="sort-object-types",de=N({name:B,meta:{type:"suggestion",docs:{description:"enforce sorted object types",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[i.alphabetical,i.natural,i["line-length"]],default:i.natural},order:{enum:[d.asc,d.desc],default:d.asc},"ignore-case":{type:"boolean",default:!1}},additionalProperties:!1}],messages:{unexpectedObjectTypesOrder:'Expected "{{right}}" to come before "{{left}}"'}},defaultOptions:[{type:i.alphabetical,order:d.asc}],create:t=>({TSTypeLiteral:l=>{if(l.members.length>1){let s=w(t.options.at(0),{type:i.alphabetical,"ignore-case":!1,order:d.asc}),n=t.getSourceCode(),p=l.members.map(r=>{var S,T;let e,o=n.text.slice(r.range.at(0),r.range.at(1)),a=y=>y.replace(/(,|;)$/,"");if(r.type===u.AST_NODE_TYPES.TSPropertySignature)r.key.type===u.AST_NODE_TYPES.Identifier?{name:e}=r.key:r.key.type===u.AST_NODE_TYPES.Literal?e=`${r.key.value}`:e=n.text.slice(r.range.at(0),(S=r.typeAnnotation)==null?void 0:S.range.at(0));else if(r.type===u.AST_NODE_TYPES.TSIndexSignature){let y=((T=r.typeAnnotation)==null?void 0:T.range.at(0))??r.range.at(1);e=a(n.text.slice(r.range.at(0),y))}else e=a(n.text.slice(r.range.at(0),r.range.at(1)));let g=o.endsWith(";")||o.endsWith(",")?1:0;return{size:P(r.range)-g,node:r,name:e}});D(p,(r,e)=>{A(r,e,s)&&t.report({messageId:"unexpectedObjectTypesOrder",data:{left:O(r.name),right:O(e.name)},node:e.node,fix:o=>Y(o,p,b(p,s),n)})})}}})}),U="sort-map-elements",fe=N({name:U,meta:{type:"suggestion",docs:{description:"enforce sorted Map elements",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[i.alphabetical,i.natural,i["line-length"]],default:i.natural},order:{enum:[d.asc,d.desc],default:d.asc},"ignore-case":{type:"boolean",default:!1}},additionalProperties:!1}],messages:{unexpectedMapElementsOrder:'Expected "{{right}}" to come before "{{left}}"'}},defaultOptions:[{type:i.alphabetical,order:d.asc}],create:t=>({NewExpression:l=>{if(l.callee.type===u.AST_NODE_TYPES.Identifier&&l.callee.name==="Map"&&l.arguments.length&&l.arguments[0].type===u.AST_NODE_TYPES.ArrayExpression){let[{elements:s}]=l.arguments;if(s.length>1){let n=w(t.options.at(0),{type:i.alphabetical,"ignore-case":!1,order:d.asc}),p=t.getSourceCode();s.reduce((e,o)=>(o===null||o.type===u.AST_NODE_TYPES.SpreadElement?e.push([]):e.at(-1).push(o),e),[[]]).forEach(e=>{let o=e.map(a=>{let c;if(a.type===u.AST_NODE_TYPES.ArrayExpression){let[g]=a.elements;g?g.type===u.AST_NODE_TYPES.Literal?c=g.raw:c=p.text.slice(...g.range):c=`${g}`}else c=p.text.slice(...a.range);return{size:P(a.range),node:a,name:c}});D(o,(a,c)=>{A(a,c,n)&&t.report({messageId:"unexpectedMapElementsOrder",data:{left:O(a.name),right:O(c.name)},node:c.node,fix:g=>Y(g,o,b(o,n),p)})})})}}}})}),z="sort-union-types",ue=N({name:z,meta:{type:"suggestion",docs:{description:"enforce sorted union types",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[i.alphabetical,i.natural,i["line-length"]],default:i.natural},order:{enum:[d.asc,d.desc],default:d.asc},"ignore-case":{type:"boolean",default:!1}},additionalProperties:!1}],messages:{unexpectedUnionTypesOrder:'Expected "{{right}}" to come before "{{left}}"'}},defaultOptions:[{type:i.alphabetical,order:d.asc}],create:t=>({TSUnionType:l=>{let s=t.getSourceCode(),n=w(t.options.at(0),{type:i.alphabetical,"ignore-case":!1,order:d.asc}),p=l.types.map(r=>({name:s.text.slice(...r.range),size:P(r.range),node:r}));D(p,(r,e)=>{A(r,e,n)&&t.report({messageId:"unexpectedUnionTypesOrder",data:{left:O(r.name),right:O(e.name)},node:e.node,fix:o=>Y(o,p,b(p,n),s)})})}})}),q="sort-interfaces",ge=N({name:q,meta:{type:"suggestion",docs:{description:"enforce sorted interface properties",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[i.alphabetical,i.natural,i["line-length"]],default:i.natural},order:{enum:[d.asc,d.desc],default:d.asc},"ignore-case":{type:"boolean",default:!1},"ignore-pattern":{type:"array",default:[]}},additionalProperties:!1}],messages:{unexpectedInterfacePropertiesOrder:'Expected "{{right}}" to come before "{{left}}"'}},defaultOptions:[{type:i.alphabetical,order:d.asc}],create:t=>({TSInterfaceDeclaration:l=>{if(l.body.body.length>1){let s=w(t.options.at(0),{type:i.alphabetical,"ignore-case":!1,order:d.asc,"ignore-pattern":[]});if(!s["ignore-pattern"].some(n=>C.minimatch(l.id.name,n))){let n=t.getSourceCode(),p=l.body.body.map(r=>{var o,a,c;let e;if(r.type===u.AST_NODE_TYPES.TSPropertySignature)if(r.key.type===u.AST_NODE_TYPES.Identifier)({name:e}=r.key);else if(r.key.type===u.AST_NODE_TYPES.Literal)e=`${r.key.value}`;else{let g=((o=r.typeAnnotation)==null?void 0:o.range.at(0))??r.range.at(1)-(r.optional?1:0);e=n.text.slice(r.range.at(0),g)}else if(r.type===u.AST_NODE_TYPES.TSIndexSignature){let g=((a=r.typeAnnotation)==null?void 0:a.range.at(0))??r.range.at(1);e=n.text.slice(r.range.at(0),g)}else{let g=((c=r.returnType)==null?void 0:c.range.at(0))??r.range.at(1);e=n.text.slice(r.range.at(0),g)}return{size:P(r.range),node:r,name:e}});D(p,(r,e)=>{A(r,e,s)&&t.report({messageId:"unexpectedInterfacePropertiesOrder",data:{left:O(r.name),right:O(e.name)},node:e.node,fix:o=>Y(o,p,b(p,s),n)})})}}}})});let F=(t,l)=>t.reduce((s,n)=>{let p=l(n);return p in s||(s[p]=[]),s[p].push(n),s},{});const J="sort-jsx-props",me=N({name:J,meta:{type:"suggestion",docs:{description:"enforce sorted JSX props",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[i.alphabetical,i.natural,i["line-length"]],default:i.natural},order:{enum:[d.asc,d.desc],default:d.asc},"always-on-top":{type:"array",default:[]},"ignore-case":{type:"boolean",default:!1},shorthand:{enum:["first","last","ignore"]},callback:{enum:["first","last","ignore"]},multiline:{enum:["first","last","ignore"]}},additionalProperties:!1}],messages:{unexpectedJSXPropsOrder:'Expected "{{right}}" to come before "{{left}}"'}},defaultOptions:[{type:i.alphabetical,order:d.asc}],create:t=>({JSXElement:l=>{if(l.openingElement.attributes.length>1){let s=w(t.options.at(0),{type:i.alphabetical,shorthand:"ignore",multiline:"ignore",callback:"ignore","always-on-top":[],"ignore-case":!1,order:d.asc}),n=t.getSourceCode();l.openingElement.attributes.reduce((r,e)=>{if(e.type===u.AST_NODE_TYPES.JSXSpreadAttribute)return r.push([]),r;let o="ignore";e.name.type===u.AST_NODE_TYPES.JSXIdentifier&&s["always-on-top"].includes(e.name.name)?o="exception":(s.shorthand!=="ignore"&&e.value===null&&(o=s.shorthand),s.callback!=="ignore"&&e.name.type===u.AST_NODE_TYPES.JSXIdentifier&&e.name.name.indexOf("on")===0&&e.value!==null?o=s.callback:s.multiline!=="ignore"&&e.loc.start.line!==e.loc.end.line&&(o=s.multiline));let a={name:e.name.type===u.AST_NODE_TYPES.JSXNamespacedName?`${e.name.namespace.name}:${e.name.name.name}`:e.name.name,size:P(e.range),node:e,position:o};return r.at(-1).push(a),r},[[]]).forEach(r=>{D(r,(e,o)=>{let a;if(e.position==="exception"&&o.position==="exception")a=s["always-on-top"].indexOf(e.name)>s["always-on-top"].indexOf(o.name);else if(e.position===o.position)a=A(e,o,s);else{let c={exception:2,first:1,ignore:0,last:-1};a=c[e.position]<c[o.position]}a&&t.report({messageId:"unexpectedJSXPropsOrder",data:{left:e.name,right:o.name},node:o.node,fix:c=>{let g=F(r,({position:y})=>y),S=y=>y in g?g[y]:[],T=[S("exception").sort((y,f)=>s["always-on-top"].indexOf(y.name)-s["always-on-top"].indexOf(f.name)),b(S("first"),s),b(S("ignore"),s),b(S("last"),s)].flat();return Y(c,r,T,n)}})})})}}})});class ee{static get(){return this._instance?this._instance:(this._instance=se.parse(oe.readFileSync(ne.resolve(process.cwd(),"tsconfig.json"),"utf8")),this._instance)}}H(ee,"_instance");const W="sort-imports",ye=N({name:W,meta:{type:"suggestion",docs:{description:"enforce sorted imports",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[i.alphabetical,i.natural,i["line-length"]],default:i.natural},order:{enum:[d.asc,d.desc],default:d.asc},"ignore-case":{type:"boolean",default:!1},groups:{type:"array",default:[]},"internal-pattern":{type:"array",default:["~/**"]},"newlines-between":{enum:["ignore","always","never"],default:"always"},"read-tsconfig":{type:"boolean",default:!1}},additionalProperties:!1}],messages:{unexpectedImportsOrder:'Expected "{{right}}" to come before "{{left}}"',missedSpacingBetweenImports:'Missed spacing between "{{left}}" and "{{right}}" imports',extraSpacingBetweenImports:'Extra spacing between "{{left}}" and "{{right}}" imports'}},defaultOptions:[{type:i.alphabetical,order:d.asc}],create:t=>{var o;let l=w(t.options.at(0),{"newlines-between":"always","internal-pattern":["~/**"],type:i.alphabetical,"read-tsconfig":!1,order:d.asc,"ignore-case":!1,groups:[]}),s=[];if(l["read-tsconfig"]){let a=ee.get();(o=a.compilerOptions)!=null&&o.paths&&Object.keys(a.compilerOptions.paths).forEach(c=>{s.push(c)})}let n=t.getSourceCode(),p=[],r=a=>{let c,g=E=>[".less",".scss",".sass",".styl",".pcss",".css",".sss"].some(x=>E.endsWith(x)),S=E=>["./index.d.js","./index.d.ts","./index.js","./index.ts","./index","./","."].includes(E),T=E=>E.indexOf("..")===0,y=E=>E.indexOf("./")===0,f=E=>{!c&&l.groups.flat().includes(E)&&(c=E)},m=E=>l["internal-pattern"].length&&l["internal-pattern"].some(x=>C.minimatch(E.source.value,x))||s.some(x=>C.minimatch(E.source.value,x));return a.importKind==="type"&&(a.type===u.AST_NODE_TYPES.ImportDeclaration&&(Q(a.source.value)&&f("builtin-type"),m(a)&&f("internal-type"),S(a.source.value)&&f("index-type"),T(a.source.value)&&f("parent-type"),y(a.source.value)&&f("sibling-type")),f("type")),!c&&a.type===u.AST_NODE_TYPES.ImportDeclaration&&(Q(a.source.value)&&f("builtin"),m(a)&&f("internal"),g(a.source.value)&&f("style"),a.specifiers.length===0&&f("side-effect"),S(a.source.value)&&f("index"),T(a.source.value)&&f("parent"),y(a.source.value)&&f("sibling"),f("external")),c??"unknown"},e=a=>{let c;a.type===u.AST_NODE_TYPES.ImportDeclaration?c=a.source.value:a.moduleReference.type===u.AST_NODE_TYPES.TSExternalModuleReference&&a.moduleReference.expression.type===u.AST_NODE_TYPES.Literal?c=`${a.moduleReference.expression.value}`:c=n.text.slice(...a.moduleReference.range),p.push({size:P(a.range),group:r(a),name:c,node:a})};return{TSImportEqualsDeclaration:e,ImportDeclaration:e,"Program:exit":()=>{let a=y=>{for(let f=0,m=l.groups.length;f<m;f++){let E=l.groups[f];if(y.group===E||Array.isArray(E)&&E.includes(y.group))return f}return l.groups.length},c=(y,f)=>!!n.getTokensBetween(y.node,Z(f.node,n)||f.node,{includeComments:!0}).length,g=(y,f)=>n.lines.slice(y.node.loc.end.line,f.node.loc.start.line-1).filter(E=>!E.trim().length).length,S=(y,f)=>{let m=[],E=f.reduce((_,h)=>{let j=a(h);return j in _?_[j]=b([..._[j],h],l):_[j]=[h],_},{}),x=Object.keys(E).sort().reduce((_,h)=>[..._,...E[h]],[]);return x.forEach((_,h)=>{if(m.push(y.replaceTextRange(I(f.at(h).node,n),n.text.slice(...I(_.node,n)))),l["newlines-between"]!=="ignore"){let j=x.at(h+1);if(j){let k=g(f.at(h),f.at(h+1));(l["newlines-between"]==="always"&&a(_)===a(j)&&k!==0||l["newlines-between"]==="never"&&k>0)&&m.push(y.removeRange([I(f.at(h).node,n).at(1),I(f.at(h+1).node,n).at(0)-1])),l["newlines-between"]==="always"&&k>1&&m.push(y.replaceTextRange([I(f.at(h).node,n).at(1),I(f.at(h+1).node,n).at(0)-1],` | ||
`)),l["newlines-between"]==="always"&&a(_)!==a(j)&&k===0&&m.push(y.insertTextAfterRange(I(f.at(h).node,n),` | ||
`))}}}),m};p.reduce((y,f)=>{var E;let m=(E=y.at(-1))==null?void 0:E.at(-1);return m&&c(m,f)?y.push([f]):y.at(-1).push(f),y},[[]]).forEach(y=>{D(y,(f,m)=>{let E=a(f),x=a(m),_=g(f,m);!c(f,m)&&(E>x||E===x&&A(f,m,l))&&t.report({messageId:"unexpectedImportsOrder",data:{left:f.name,right:m.name},node:m.node,fix:h=>S(h,y)}),l["newlines-between"]==="never"&&_>0&&t.report({messageId:"extraSpacingBetweenImports",data:{left:f.name,right:m.name},node:m.node,fix:h=>S(h,y)}),l["newlines-between"]==="always"&&(E<x&&_===0?t.report({messageId:"missedSpacingBetweenImports",data:{left:f.name,right:m.name},node:m.node,fix:h=>S(h,y)}):(_>1||E===x&&_>0)&&t.report({messageId:"extraSpacingBetweenImports",data:{left:f.name,right:m.name},node:m.node,fix:h=>S(h,y)}))})})}}}}),X="sort-classes",Ee=N({name:X,meta:{type:"suggestion",docs:{description:"enforce sorted classes",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[i.alphabetical,i.natural,i["line-length"]],default:i.natural},"ignore-case":{type:"boolean",default:!1},order:{enum:[d.asc,d.desc],default:d.asc},groups:{type:"array",default:["property","constructor","method","unknown"]}},additionalProperties:!1}],messages:{unexpectedClassesOrder:'Expected "{{right}}" to come before "{{left}}"'}},defaultOptions:[{type:i.alphabetical,order:d.asc}],create:t=>({ClassBody:l=>{if(l.body.length>1){let s=w(t.options.at(0),{type:i.alphabetical,order:d.asc,"ignore-case":!1,groups:["property","constructor","method","unknown"]}),n=t.getSourceCode(),p=l.body.map(e=>{var g;let o,a;e.type===u.AST_NODE_TYPES.StaticBlock?a="static":e.type===u.AST_NODE_TYPES.TSIndexSignature?a=n.text.slice(e.range.at(0),((g=e.typeAnnotation)==null?void 0:g.range.at(0))??e.range.at(1)):e.key.type===u.AST_NODE_TYPES.Identifier?{name:a}=e.key:a=n.text.slice(...e.key.range);let c=S=>{!o&&s.groups.flat().includes(S)&&(o=S)};return e.type===u.AST_NODE_TYPES.MethodDefinition?(e.kind==="constructor"&&c("constructor"),e.static&&c("static-method"),e.accessibility==="private"&&c("private-method"),c("method")):e.type===u.AST_NODE_TYPES.PropertyDefinition&&(e.static&&c("static-property"),e.accessibility==="private"&&c("private-property"),c("property")),{size:P(e.range),group:o??"unknown",node:e,name:a}}),r=e=>{for(let o=0,a=s.groups.length;o<a;o++){let c=s.groups[o];if(e.group===c||Array.isArray(c)&&c.includes(e.group))return o}return s.groups.length};D(p,(e,o)=>{let a=r(e),c=r(o);(a>c||a===c&&A(e,o,s))&&t.report({messageId:"unexpectedClassesOrder",data:{left:O(e.name),right:O(o.name)},node:o.node,fix:g=>{let S=[],T=p.reduce((f,m)=>{let E=r(m);return E in f?f[E]=b([...f[E],m],s):f[E]=[m],f},{});return Object.keys(T).sort().reduce((f,m)=>[...f,...T[m]],[]).forEach((f,m)=>{S.push(g.replaceTextRange(I(p.at(m).node,n),n.text.slice(...I(f.node,n))))}),S}})})}}})}),G="sort-objects",Se=N({name:G,meta:{type:"suggestion",docs:{description:"enforce sorted objects",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[i.alphabetical,i.natural,i["line-length"]],default:i.natural},order:{enum:[d.asc,d.desc],default:d.asc},"ignore-case":{type:"boolean",default:!1},"always-on-top":{type:"array",default:[]}},additionalProperties:!1}],messages:{unexpectedObjectsOrder:'Expected "{{right}}" to come before "{{left}}"'}},defaultOptions:[{type:i.alphabetical,order:d.asc}],create:t=>{let l=s=>{if(s.properties.length>1){let n=w(t.options.at(0),{type:i.alphabetical,"ignore-case":!1,order:d.asc,"always-on-top":[]}),p=t.getSourceCode();(e=>e.reduce((o,a)=>{if(a.type===u.AST_NODE_TYPES.SpreadElement||a.type===u.AST_NODE_TYPES.RestElement)return o.push([]),o;let c,g="ignore",S=[];if(a.key.type===u.AST_NODE_TYPES.Identifier?{name:c}=a.key:a.key.type===u.AST_NODE_TYPES.Literal?c=`${a.key.value}`:c=p.text.slice(...a.key.range),a.key.type===u.AST_NODE_TYPES.Identifier&&n["always-on-top"].includes(a.key.name)&&(g="exception"),a.value.type===u.AST_NODE_TYPES.AssignmentPattern){let y=(f,m)=>{f.right.type===u.AST_NODE_TYPES.Identifier&&S.push(f.right.name),!m&&f.left.type===u.AST_NODE_TYPES.Identifier&&S.push(f.left.name);let E=x=>{x.right.type===u.AST_NODE_TYPES.Identifier&&S.push(x.right.name),x.left.type===u.AST_NODE_TYPES.BinaryExpression&&y(x.left,!1)};f.right.type===u.AST_NODE_TYPES.BinaryExpression&&E(f.right)};y(a.value,!0)}let T={size:P(a.range),dependencies:S,node:a,position:g,name:c};return o.at(-1).push(T),o},[[]]))(s.properties).forEach(e=>{D(e,(o,a)=>{let c;if(o.position==="exception"&&a.position==="exception")c=n["always-on-top"].indexOf(o.name)>n["always-on-top"].indexOf(a.name);else if(o.position===a.position)c=A(o,a,n);else{let g={exception:1,ignore:0};c=g[o.position]<g[a.position]}if(c){let g=S=>{let T=F(e,({position:m})=>m),y=m=>m in T?T[m]:[],f=[y("exception").sort((m,E)=>n["always-on-top"].indexOf(m.name)-n["always-on-top"].indexOf(E.name)),b(y("ignore"),n)].flat();return Y(S,e,f,p)};t.report({messageId:"unexpectedObjectsOrder",data:{left:O(o.name),right:O(a.name)},node:a.node,fix:g})}})})}};return{ObjectExpression:l,ObjectPattern:l}}}),K="sort-exports",he=N({name:K,meta:{type:"suggestion",docs:{description:"enforce sorted exports",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[i.alphabetical,i.natural,i["line-length"]],default:i.natural},order:{enum:[d.asc,d.desc],default:d.asc},"ignore-case":{type:"boolean",default:!1}},additionalProperties:!1}],messages:{unexpectedExportsOrder:'Expected "{{right}}" to come before "{{left}}"'}},defaultOptions:[{type:i.alphabetical,order:d.asc}],create:t=>{let l=w(t.options.at(0),{type:i.alphabetical,order:d.asc,"ignore-case":!1}),s=[[]],n=p=>{p.type===u.AST_NODE_TYPES.ExportAllDeclaration&&p.exported===null?s.push([]):s.at(-1).push({size:P(p.range),name:p.source.value,node:p})};return{ExportAllDeclaration:n,ExportNamedDeclaration:p=>{p.source!==null&&n(p)},"Program:exit":()=>{let p=t.getSourceCode();s.forEach(r=>{D(r,(e,o)=>{A(e,o,l)&&t.report({messageId:"unexpectedExportsOrder",data:{left:e.name,right:o.name},node:o.node,fix:a=>Y(a,r,b(r,l),p)})})})}}}}),V="sort-enums",Te=N({name:V,meta:{type:"suggestion",docs:{description:"enforce sorted TypeScript enums",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[i.alphabetical,i.natural,i["line-length"]],default:i.natural},"ignore-case":{type:"boolean",default:!1},order:{enum:[d.asc,d.desc],default:d.asc}},additionalProperties:!1}],messages:{unexpectedEnumsOrder:'Expected "{{right}}" to come before "{{left}}"'}},defaultOptions:[{type:i.alphabetical,order:d.asc}],create:t=>({TSEnumDeclaration:l=>{if(l.members.length>1&&l.members.some(({initializer:s})=>s)){let s=w(t.options.at(0),{type:i.alphabetical,order:d.asc,"ignore-case":!1}),n=t.getSourceCode(),p=l.members.map(r=>({name:r.id.type===u.AST_NODE_TYPES.Literal?`${r.id.value}`:`${n.text.slice(...r.id.range)}`,size:P(r.range),node:r}));D(p,(r,e)=>{A(r,e,s)&&t.report({messageId:"unexpectedEnumsOrder",data:{left:O(r.name),right:O(e.name)},node:e.node,fix:o=>Y(o,p,b(p,s),n)})})}}})}),xe="eslint-plugin-perfectionist";let R=t=>{let l={[W]:["error",{groups:["type",["builtin","external"],"internal-type","internal",["parent-type","sibling-type","index-type"],["parent","sibling","index"],"object","unknown"],"newlines-between":"always","internal-pattern":["~/**"],"read-tsconfig":!1}],[X]:["error",{groups:["static-property","private-property","property","constructor","static-method","private-method","method","unknown"]}],[J]:["error",{"always-on-top":[],shorthand:"ignore",multiline:"first",callback:"ignore"}],[L]:["error",{"spread-last":!0}],[G]:["error",{"always-on-top":[]}],[M]:["error"],[$]:["error"],[B]:["error"],[U]:["error"],[z]:["error"],[q]:["error"],[K]:["error"],[V]:["error"]};return{rules:Object.fromEntries(Object.entries(l).map(([s,[n,p={}]])=>[`perfectionist/${s}`,[n,Object.assign(p,t)]])),plugins:["perfectionist"]}};const _e={rules:{[L]:ie,[X]:Ee,[V]:Te,[K]:he,[W]:ye,[q]:ge,[J]:me,[U]:fe,[$]:pe,[M]:ce,[B]:de,[G]:Se,[z]:ue},configs:{"recommended-alphabetical":R({type:i.alphabetical,order:d.asc,"ignore-case":!1}),"recommended-natural":R({type:i.natural,order:d.asc,"ignore-case":!1}),"recommended-line-length":R({type:i["line-length"],order:d.desc})},name:xe};module.exports=_e; |
{ | ||
"name": "eslint-plugin-perfectionist", | ||
"description": "ESLint plugin for sorting various data such as objects, imports, types, enums, JSX props, etc.", | ||
"version": "1.1.2", | ||
"version": "1.2.0", | ||
"homepage": "https://eslint-plugin-perfectionist.azat.io", | ||
@@ -6,0 +6,0 @@ "repository": "https://github.com/azat-io/eslint-plugin-perfectionist", |
@@ -70,4 +70,4 @@ # ESLint Plugin Perfectionist | ||
{ | ||
"type": "line-length", | ||
"order": "desc" | ||
"type": "natural", | ||
"order": "asc" | ||
} | ||
@@ -93,4 +93,4 @@ ] | ||
{ | ||
type: 'line-length', | ||
order: 'desc', | ||
type: 'natural', | ||
order: 'asc', | ||
}, | ||
@@ -113,3 +113,3 @@ ], | ||
"extends": [ | ||
"plugin:perfectionist/recommended-line-length" | ||
"plugin:perfectionist/recommended-natural" | ||
] | ||
@@ -123,6 +123,6 @@ } | ||
```js | ||
import perfectionistLineLength from 'eslint-plugin-perfectionist/configs/recommended-line-length' | ||
import perfectionistNatural from 'eslint-plugin-perfectionist/configs/recommended-natural' | ||
export default [ | ||
perfectionistLineLength, | ||
perfectionistNatural, | ||
] | ||
@@ -150,2 +150,3 @@ ``` | ||
| [sort-enums](https://eslint-plugin-perfectionist.azat.io/rules/sort-enums) | enforce sorted TypeScript enums | 🔧 | | ||
| [sort-exports](https://eslint-plugin-perfectionist.azat.io/rules/sort-exports) | enforce sorted exports | 🔧 | | ||
| [sort-imports](https://eslint-plugin-perfectionist.azat.io/rules/sort-imports) | enforce sorted imports | 🔧 | | ||
@@ -152,0 +153,0 @@ | [sort-interfaces](https://eslint-plugin-perfectionist.azat.io/rules/sort-interfaces) | enforce sorted interface properties | 🔧 | |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
83075
1734
256