eslint-plugin-perfectionist 0.11.1 → 0.11.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,3 +1,3 @@
1
- "use strict";var V=Object.defineProperty;var W=(r,n,a)=>n in r?V(r,n,{enumerable:!0,configurable:!0,writable:!0,value:a}):r[n]=a;var G=(r,n,a)=>(W(r,typeof n!="symbol"?n+"":n,a),a);const f=require("@typescript-eslint/types"),Y=require("@typescript-eslint/utils"),H=require("natural-compare-lite"),Q=require("is-core-module"),k=require("minimatch"),Z=require("json5"),ee=require("path"),te=require("fs");let O=Y.ESLintUtils.RuleCreator(r=>`https://eslint-plugin-perfectionist.azat.io/rules/${r}`),x=r=>r.replaceAll(/\s\s+/g," ").trim(),_=r=>{let[n,a]=r;return a-n};var i=(r=>(r.alphabetical="alphabetical",r.natural="natural",r["line-length"]="line-length",r))(i||{}),c=(r=>(r.asc="asc",r.desc="desc",r))(c||{});let b=(r,n,a)=>{let l=a.order==="asc"?1:-1,p,t=e=>a["ignore-case"]?e.toLowerCase():e;return a.type===i.alphabetical?p=(e,s)=>t(e.name).localeCompare(t(s.name)):a.type===i.natural?p=(e,s)=>H(t(e.name),t(s.name)):p=(e,s)=>e.size-s.size,l*p(r,n)>0},T=(r,n)=>[...r].sort((a,l)=>Number(b(a,l,n))||-1),ae=(r,n)=>{let a=n.getTokenBefore(r,{includeComments:!0});return(a==null?void 0:a.type)===f.AST_TOKEN_TYPES.Block||(a==null?void 0:a.type)===f.AST_TOKEN_TYPES.Line?a:null},P=(r,n)=>{let a=r.range.at(0),l=r.range.at(1);if(Y.ASTUtils.isParenthesized(r,n)){let t=n.getTokenBefore(r,Y.ASTUtils.isOpeningParenToken),e=n.getTokenAfter(r,Y.ASTUtils.isClosingParenToken);a=t.range.at(0),l=e.range.at(1)}let p=ae(r,n);return p&&(a=p.range.at(0)),[a,l]},w=(r,n,a,l)=>n.map(({node:p},t)=>r.replaceTextRange(P(p,l),l.text.slice(...P(a[t].node,l)))),N=(r={},n)=>Object.assign(n,r),A=(r,n)=>{if(r.length>1)for(let a=1;a<r.length;a++){let l=r.at(a-1),p=r.at(a);l&&p&&n(l,p,a-1)}};const L="sort-array-includes",re=O({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:[c.asc,c.desc],default:c.asc},"ignore-case":{type:"boolean",default:!1},"spread-last":{type:"boolean",default:!1}},additionalProperties:!1}],messages:{unexpectedArrayIncludesOrder:'Expected "{{second}}" to come before "{{first}}"'}},defaultOptions:[{type:i.alphabetical,order:c.asc}],create:r=>({MemberExpression:n=>{if((n.object.type===f.AST_NODE_TYPES.ArrayExpression||n.object.type===f.AST_NODE_TYPES.NewExpression)&&n.property.type===f.AST_NODE_TYPES.Identifier&&n.property.name==="includes"){let a=N(r.options.at(0),{type:i.alphabetical,order:c.asc,"ignore-case":!1,"spread-last":!1}),l=n.object.type===f.AST_NODE_TYPES.ArrayExpression?n.object.elements:n.object.arguments;if(l.length>1){let p=r.getSourceCode(),t=l.reduce((e,s)=>(s!==null&&e.at(0).push({name:s.type===f.AST_NODE_TYPES.Literal?s.raw:p.text.slice(...s.range),size:_(s.range),type:s.type,node:s}),e),[[],[]]).flat();A(t,(e,s)=>{let o;a["spread-last"]&&e.node.type===f.AST_NODE_TYPES.Literal&&s.node.type===f.AST_NODE_TYPES.SpreadElement?o=!1:a["spread-last"]&&e.node.type===f.AST_NODE_TYPES.SpreadElement&&s.node.type===f.AST_NODE_TYPES.Literal?o=!0:o=b(e,s,a),o&&r.report({messageId:"unexpectedArrayIncludesOrder",data:{first:x(e.name),second:x(s.name)},node:s.node,fix:d=>{let u=T(t,a);return a["spread-last"]&&u.forEach((m,y)=>{m.node.type===f.AST_NODE_TYPES.SpreadElement&&u.push(u.splice(y,1).at(0))}),w(d,t,u,p)}})})}}}})}),v="sort-classes",se=O({name:v,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:[c.asc,c.desc],default:c.asc},groups:{type:"array",default:["property","constructor","method","unknown"]}},additionalProperties:!1}],messages:{unexpectedClassesOrder:'Expected "{{second}}" to come before "{{first}}"'}},defaultOptions:[{type:i.alphabetical,order:c.asc}],create:r=>({ClassBody:n=>{if(n.body.length>1){let a=N(r.options.at(0),{type:i.alphabetical,order:c.asc,"ignore-case":!1,groups:["property","constructor","method","unknown"]}),l=r.getSourceCode(),p=n.body.map(e=>{var u;let s,o;e.type===f.AST_NODE_TYPES.StaticBlock?o="static":e.type===f.AST_NODE_TYPES.TSIndexSignature?o=l.text.slice(e.range.at(0),((u=e.typeAnnotation)==null?void 0:u.range.at(0))??e.range.at(1)):e.key.type===f.AST_NODE_TYPES.Identifier?{name:o}=e.key:o=l.text.slice(...e.key.range);let d=m=>{!s&&a.groups.flat().includes(m)&&(s=m)};return e.type===f.AST_NODE_TYPES.MethodDefinition?(e.kind==="constructor"&&d("constructor"),e.static&&d("static-method"),e.accessibility==="private"&&d("private-method"),d("method")):e.type===f.AST_NODE_TYPES.PropertyDefinition&&(e.static&&d("static-property"),e.accessibility==="private"&&d("private-property"),d("property")),{size:_(e.range),group:s??"unknown",node:e,name:o}}),t=e=>{for(let s=0,o=a.groups.length;s<o;s++){let d=a.groups[s];if(e.group===d||Array.isArray(d)&&d.includes(e.group))return s}return a.groups.length};A(p,(e,s)=>{let o=t(e),d=t(s);(o>d||o===d&&b(e,s,a))&&r.report({messageId:"unexpectedClassesOrder",data:{first:x(e.name),second:x(s.name)},node:s.node,fix:u=>{let m=[],y=p.reduce((h,E)=>{let S=t(E);return S in h?h[S]=T([...h[S],E],a):h[S]=[E],h},{});return Object.keys(y).sort().reduce((h,E)=>[...h,...y[E]],[]).forEach((h,E)=>{m.push(u.replaceTextRange(P(p.at(E).node,l),l.text.slice(...P(h.node,l))))}),m}})})}}})}),R="sort-enums",ne=O({name:R,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:[c.asc,c.desc],default:c.asc}},additionalProperties:!1}],messages:{unexpectedEnumsOrder:'Expected "{{second}}" to come before "{{first}}"'}},defaultOptions:[{type:i.alphabetical,order:c.asc}],create:r=>({TSEnumDeclaration:n=>{let a=N(r.options.at(0),{type:i.alphabetical,order:c.asc,"ignore-case":!1});if(n.members.length>1){let l=r.getSourceCode(),p=n.members.map(t=>({name:t.id.type===f.AST_NODE_TYPES.Literal?`${t.id.value}`:`${l.text.slice(...t.id.range)}`,size:_(t.range),node:t}));A(p,(t,e)=>{b(t,e,a)&&r.report({messageId:"unexpectedEnumsOrder",data:{first:x(t.name),second:x(e.name)},node:e.node,fix:s=>w(s,p,T(p,a),l)})})}}})});class F{static get(){return this._instance?this._instance:(this._instance=Z.parse(te.readFileSync(ee.resolve(process.cwd(),"tsconfig.json"),"utf8")),this._instance)}}G(F,"_instance");const C="sort-imports",oe=O({name:C,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:[c.asc,c.desc],default:c.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 "{{second}}" to come before "{{first}}"',missedSpacingBetweenImports:'Missed spacing between "{{first}}" and "{{second}}" imports',extraSpacingBetweenImports:'Extra spacing between "{{first}}" and "{{second}}" imports'}},defaultOptions:[{type:i.alphabetical,order:c.asc}],create:r=>{var s;let n=N(r.options.at(0),{"newlines-between":"always","internal-pattern":["~/**"],type:i.alphabetical,"read-tsconfig":!1,order:c.asc,"ignore-case":!1,groups:[]}),a=[];if(n["read-tsconfig"]){let o=F.get();(s=o.compilerOptions)!=null&&s.paths&&Object.keys(o.compilerOptions.paths).forEach(d=>{a.push(d)})}let l=r.getSourceCode(),p=[],t=o=>{let d,u=E=>["./index.d.js","./index.d.ts","./index.js","./index.ts","./index","./","."].includes(E),m=E=>E.indexOf("..")===0,y=E=>E.indexOf("./")===0,g=E=>{!d&&n.groups.flat().includes(E)&&(d=E)},h=E=>n["internal-pattern"].length&&n["internal-pattern"].some(S=>k.minimatch(E.source.value,S))||a.some(S=>k.minimatch(E.source.value,S));return o.importKind==="type"&&(o.type===f.AST_NODE_TYPES.ImportDeclaration&&(h(o)?g("internal-type"):u(o.source.value)?g("index-type"):m(o.source.value)?g("parent-type"):y(o.source.value)&&g("sibling-type")),g("type")),!d&&o.type===f.AST_NODE_TYPES.ImportDeclaration&&(Q(o.source.value)?g("builtin"):h(o)?g("internal"):u(o.source.value)?g("index"):m(o.source.value)?g("parent"):y(o.source.value)?g("sibling"):g("external")),d??"unknown"},e=o=>{let d;o.type===f.AST_NODE_TYPES.ImportDeclaration?d=o.source.value:o.moduleReference.type===f.AST_NODE_TYPES.TSExternalModuleReference&&o.moduleReference.expression.type===f.AST_NODE_TYPES.Literal?d=`${o.moduleReference.expression.value}`:d=l.text.slice(...o.moduleReference.range),p.push({size:_(o.range),group:t(o),name:d,node:o})};return{TSImportEqualsDeclaration:e,ImportDeclaration:e,"Program:exit":()=>{let o=m=>{for(let y=0,g=n.groups.length;y<g;y++){let h=n.groups[y];if(m.group===h||Array.isArray(h)&&h.includes(m.group))return y}return n.groups.length},d=(m,y)=>l.lines.slice(m.node.loc.end.line,y.node.loc.start.line-1).filter(h=>!h.trim().length).length,u=m=>{let y=[],g=p.reduce((E,S)=>{let I=o(S);return I in E?E[I]=T([...E[I],S],n):E[I]=[S],E},{}),h=Object.keys(g).sort().reduce((E,S)=>[...E,...g[S]],[]);return h.forEach((E,S)=>{if(y.push(m.replaceTextRange(P(p.at(S).node,l),l.text.slice(...P(E.node,l)))),n["newlines-between"]!=="ignore"){let I=h.at(S+1);if(I){let D=d(p.at(S),p.at(S+1));(n["newlines-between"]==="always"&&o(E)===o(I)&&D!==0||n["newlines-between"]==="never"&&D>0)&&y.push(m.removeRange([P(p.at(S).node,l).at(1),P(p.at(S+1).node,l).at(0)-1])),n["newlines-between"]==="always"&&D>1&&y.push(m.replaceTextRange([P(p.at(S).node,l).at(1),P(p.at(S+1).node,l).at(0)-1],`
2
- `)),n["newlines-between"]==="always"&&o(E)!==o(I)&&D===0&&y.push(m.insertTextAfterRange(P(p.at(S).node,l),`
3
- `))}}}),y};A(p,(m,y)=>{let g=o(m),h=o(y),E=d(m,y);(g>h||g===h&&b(m,y,n))&&r.report({messageId:"unexpectedImportsOrder",data:{first:m.name,second:y.name},node:y.node,fix:u}),n["newlines-between"]==="never"&&E>0&&r.report({messageId:"extraSpacingBetweenImports",data:{first:m.name,second:y.name},node:y.node,fix:u}),n["newlines-between"]==="always"&&(g<h&&E===0?r.report({messageId:"missedSpacingBetweenImports",data:{first:m.name,second:y.name},node:y.node,fix:u}):(E>1||g===h&&E>0)&&r.report({messageId:"extraSpacingBetweenImports",data:{first:m.name,second:y.name},node:y.node,fix:u}))})}}}}),M="sort-interfaces",ie=O({name:M,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:[c.asc,c.desc],default:c.asc},"ignore-case":{type:"boolean",default:!1},"ignore-pattern":{type:"array",default:[]}},additionalProperties:!1}],messages:{unexpectedInterfacePropertiesOrder:'Expected "{{second}}" to come before "{{first}}"'}},defaultOptions:[{type:i.alphabetical,order:c.asc}],create:r=>({TSInterfaceDeclaration:n=>{let a=N(r.options.at(0),{type:i.alphabetical,"ignore-case":!1,order:c.asc,"ignore-pattern":[]});if(!a["ignore-pattern"].some(l=>k.minimatch(n.id.name,l))&&n.body.body.length>1){let l=r.getSourceCode(),p=n.body.body.map(t=>{var s,o,d;let e;if(t.type===f.AST_NODE_TYPES.TSPropertySignature)if(t.key.type===f.AST_NODE_TYPES.Identifier)({name:e}=t.key);else if(t.key.type===f.AST_NODE_TYPES.Literal)e=`${t.key.value}`;else{let u=((s=t.typeAnnotation)==null?void 0:s.range.at(0))??t.range.at(1)-(t.optional?1:0);e=l.text.slice(t.range.at(0),u)}else if(t.type===f.AST_NODE_TYPES.TSIndexSignature){let u=((o=t.typeAnnotation)==null?void 0:o.range.at(0))??t.range.at(1);e=l.text.slice(t.range.at(0),u)}else{let u=((d=t.returnType)==null?void 0:d.range.at(0))??t.range.at(1);e=l.text.slice(t.range.at(0),u)}return{size:_(t.range),node:t,name:e}});A(p,(t,e)=>{b(t,e,a)&&r.report({messageId:"unexpectedInterfacePropertiesOrder",data:{first:x(t.name),second:x(e.name)},node:e.node,fix:s=>w(s,p,T(p,a),l)})})}}})});let K=(r,n)=>r.reduce((a,l)=>{let p=n(l);return p in a||(a[p]=[]),a[p].push(l),a},{});const $="sort-jsx-props",le=O({name:$,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:[c.asc,c.desc],default:c.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 "{{second}}" to come before "{{first}}"'}},defaultOptions:[{type:i.alphabetical,order:c.asc}],create:r=>({JSXElement:n=>{let a=N(r.options.at(0),{type:i.alphabetical,shorthand:"ignore",multiline:"ignore",callback:"ignore","always-on-top":[],"ignore-case":!1,order:c.asc}),l=r.getSourceCode();n.openingElement.attributes.reduce((t,e)=>{if(e.type===f.AST_NODE_TYPES.JSXSpreadAttribute)return t.push([]),t;let s="ignore";e.name.type===f.AST_NODE_TYPES.JSXIdentifier&&a["always-on-top"].includes(e.name.name)?s="exception":(a.shorthand!=="ignore"&&e.value===null&&(s=a.shorthand),a.callback!=="ignore"&&e.name.type===f.AST_NODE_TYPES.JSXIdentifier&&e.name.name.indexOf("on")===0&&e.value!==null?s=a.callback:a.multiline!=="ignore"&&e.loc.start.line!==e.loc.end.line&&(s=a.multiline));let o={name:e.name.type===f.AST_NODE_TYPES.JSXNamespacedName?`${e.name.namespace.name}:${e.name.name.name}`:e.name.name,size:_(e.range),node:e,position:s};return t.at(-1).push(o),t},[[]]).forEach(t=>{A(t,(e,s)=>{let o;if(e.position==="exception"&&s.position==="exception")o=a["always-on-top"].indexOf(e.name)>a["always-on-top"].indexOf(s.name);else if(e.position===s.position)o=b(e,s,a);else{let d={exception:2,first:1,ignore:0,last:-1};o=d[e.position]<d[s.position]}o&&r.report({messageId:"unexpectedJSXPropsOrder",data:{first:e.name,second:s.name},node:s.node,fix:d=>{let u=K(t,({position:g})=>g),m=g=>g in u?u[g]:[],y=[m("exception").sort((g,h)=>a["always-on-top"].indexOf(g.name)-a["always-on-top"].indexOf(h.name)),T(m("first"),a),T(m("ignore"),a),T(m("last"),a)].flat();return w(d,t,y,l)}})})})}})}),U="sort-map-elements",pe=O({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:[c.asc,c.desc],default:c.asc},"ignore-case":{type:"boolean",default:!1}},additionalProperties:!1}],messages:{unexpectedMapElementsOrder:'Expected "{{second}}" to come before "{{first}}"'}},defaultOptions:[{type:i.alphabetical,order:c.asc}],create:r=>({NewExpression:n=>{if(n.callee.type===f.AST_NODE_TYPES.Identifier&&n.callee.name==="Map"&&n.arguments[0].type===f.AST_NODE_TYPES.ArrayExpression){let a=N(r.options.at(0),{type:i.alphabetical,"ignore-case":!1,order:c.asc}),[{elements:l}]=n.arguments,p=r.getSourceCode();l.length>1&&l.reduce((e,s)=>(s===null||s.type===f.AST_NODE_TYPES.SpreadElement?e.push([]):e.at(-1).push(s),e),[[]]).forEach(e=>{let s=e.map(o=>{let d;if(o.type===f.AST_NODE_TYPES.ArrayExpression){let[u]=o.elements;u?u.type===f.AST_NODE_TYPES.Literal?d=u.raw:d=p.text.slice(...u.range):d=`${u}`}else d=p.text.slice(...o.range);return{size:_(o.range),node:o,name:d}});A(s,(o,d)=>{b(o,d,a)&&r.report({messageId:"unexpectedMapElementsOrder",data:{first:x(o.name),second:x(d.name)},node:d.node,fix:u=>w(u,s,T(s,a),p)})})})}}})}),z="sort-named-exports",ce=O({name:z,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:[c.asc,c.desc],default:c.asc},"ignore-case":{type:"boolean",default:!1}},additionalProperties:!1}],messages:{unexpectedNamedExportsOrder:'Expected "{{second}}" to come before "{{first}}"'}},defaultOptions:[{type:i.alphabetical,order:c.asc}],create:r=>({ExportNamedDeclaration:n=>{let a=N(r.options.at(0),{type:i.alphabetical,"ignore-case":!1,order:c.asc});if(n.specifiers.length>1){let l=r.getSourceCode(),p=n.specifiers.map(t=>({size:_(t.range),name:t.local.name,node:t}));A(p,(t,e)=>{b(t,e,a)&&r.report({messageId:"unexpectedNamedExportsOrder",data:{first:t.name,second:e.name},node:e.node,fix:s=>w(s,p,T(p,a),l)})})}}})}),B="sort-named-imports",de=O({name:B,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:[c.asc,c.desc],default:c.asc},"ignore-case":{type:"boolean",default:!1}},additionalProperties:!1}],messages:{unexpectedNamedImportsOrder:'Expected "{{second}}" to come before "{{first}}"'}},defaultOptions:[{type:i.alphabetical,order:c.asc}],create:r=>({ImportDeclaration:n=>{let a=N(r.options.at(0),{type:i.alphabetical,"ignore-case":!1,order:c.asc});if(n.specifiers.length>1){let l=r.getSourceCode(),p=n.specifiers.map(t=>({size:_(t.range),name:t.local.name,node:t}));A(p,(t,e)=>{b(t,e,a)&&r.report({messageId:"unexpectedNamedImportsOrder",data:{first:t.name,second:e.name},node:e.node,fix:s=>w(s,p,T(p,a),l)})})}}})}),q="sort-object-types",fe=O({name:q,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:[c.asc,c.desc],default:c.asc},"ignore-case":{type:"boolean",default:!1}},additionalProperties:!1}],messages:{unexpectedObjectTypesOrder:'Expected "{{second}}" to come before "{{first}}"'}},defaultOptions:[{type:i.alphabetical,order:c.asc}],create:r=>({TSTypeLiteral:n=>{if(n.members.length>1){let a=N(r.options.at(0),{type:i.alphabetical,"ignore-case":!1,order:c.asc}),l=r.getSourceCode(),p=n.members.map(t=>{var s,o;let e;if(t.type===f.AST_NODE_TYPES.TSPropertySignature)t.key.type===f.AST_NODE_TYPES.Identifier?{name:e}=t.key:t.key.type===f.AST_NODE_TYPES.Literal?e=`${t.key.value}`:e=l.text.slice(t.range.at(0),(s=t.typeAnnotation)==null?void 0:s.range.at(0));else if(t.type===f.AST_NODE_TYPES.TSIndexSignature){let d=((o=t.typeAnnotation)==null?void 0:o.range.at(0))??t.range.at(1);e=l.text.slice(t.range.at(0),d)}else e=l.text.slice(t.range.at(0),t.range.at(1));return{size:_(t.range),node:t,name:e}});A(p,(t,e)=>{b(t,e,a)&&r.report({messageId:"unexpectedObjectTypesOrder",data:{first:x(t.name),second:x(e.name)},node:e.node,fix:s=>w(s,p,T(p,a),l)})})}}})}),J="sort-objects",ue=O({name:J,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:[c.asc,c.desc],default:c.asc},"ignore-case":{type:"boolean",default:!1},"always-on-top":{type:"array",default:[]}},additionalProperties:!1}],messages:{unexpectedObjectsOrder:'Expected "{{second}}" to come before "{{first}}"'}},defaultOptions:[{type:i.alphabetical,order:c.asc}],create:r=>({ObjectExpression:n=>{if(n.properties.length>1){let a=N(r.options.at(0),{type:i.alphabetical,"ignore-case":!1,order:c.asc,"always-on-top":[]}),l=r.getSourceCode();(t=>t.reduce((e,s)=>{if(s.type===f.AST_NODE_TYPES.SpreadElement)return e.push([]),e;let o,d="ignore";s.key.type===f.AST_NODE_TYPES.Identifier?{name:o}=s.key:s.key.type===f.AST_NODE_TYPES.Literal?o=`${s.key.value}`:o=l.text.slice(...s.key.range),s.key.type===f.AST_NODE_TYPES.Identifier&&a["always-on-top"].includes(s.key.name)&&(d="exception");let u={size:_(s.range),node:s,position:d,name:o};return e.at(-1).push(u),e},[[]]))(n.properties).forEach(t=>{A(t,(e,s)=>{let o;if(e.position==="exception"&&s.position==="exception")o=a["always-on-top"].indexOf(e.name)>a["always-on-top"].indexOf(s.name);else if(e.position===s.position)o=b(e,s,a);else{let d={exception:1,ignore:0};o=d[e.position]<d[s.position]}o&&r.report({messageId:"unexpectedObjectsOrder",data:{first:x(e.name),second:x(s.name)},node:s.node,fix:d=>{let u=K(t,({position:g})=>g),m=g=>g in u?u[g]:[],y=[m("exception").sort((g,h)=>a["always-on-top"].indexOf(g.name)-a["always-on-top"].indexOf(h.name)),T(m("ignore"),a)].flat();return w(d,t,y,l)}})})})}}})}),X="sort-union-types",ge=O({name:X,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:[c.asc,c.desc],default:c.asc},"ignore-case":{type:"boolean",default:!1}},additionalProperties:!1}],messages:{unexpectedUnionTypesOrder:'Expected "{{second}}" to come before "{{first}}"'}},defaultOptions:[{type:i.alphabetical,order:c.asc}],create:r=>({TSUnionType:n=>{let a=r.getSourceCode(),l=N(r.options.at(0),{type:i.alphabetical,"ignore-case":!1,order:c.asc}),p=n.types.map(t=>({name:a.text.slice(...t.range),size:_(t.range),node:t}));A(p,(t,e)=>{b(t,e,l)&&r.report({messageId:"unexpectedUnionTypesOrder",data:{first:x(t.name),second:x(e.name)},node:e.node,fix:s=>w(s,p,T(p,l),a)})})}})}),me="eslint-plugin-perfectionist";let j=r=>{let n={[L]:["error",{"spread-last":!0}],[v]:["error",{groups:["static-property","private-property","property","constructor","static-method","private-method","method","unknown"]}],[R]:["error"],[C]:["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}],[M]:["error"],[$]:["error",{"always-on-top":[],shorthand:"ignore",multiline:"first",callback:"ignore"}],[U]:["error"],[z]:["error"],[B]:["error"],[q]:["error"],[J]:["error",{"always-on-top":[]}],[X]:["error"]};return{plugins:["perfectionist"],rules:Object.fromEntries(Object.entries(n).map(([a,[l,p={}]])=>[`perfectionist/${a}`,[l,Object.assign(p,r)]]))}};const ye={name:me,rules:{[L]:re,[v]:se,[R]:ne,[C]:oe,[M]:ie,[$]:le,[U]:pe,[z]:ce,[B]:de,[q]:fe,[J]:ue,[X]:ge},configs:{"recommended-alphabetical":j({type:i.alphabetical,order:c.asc,"ignore-case":!1}),"recommended-natural":j({type:i.natural,order:c.asc,"ignore-case":!1}),"recommended-line-length":j({type:i["line-length"],order:c.desc})}};module.exports=ye;
1
+ "use strict";var K=Object.defineProperty;var V=(r,n,a)=>n in r?K(r,n,{enumerable:!0,configurable:!0,writable:!0,value:a}):r[n]=a;var G=(r,n,a)=>(V(r,typeof n!="symbol"?n+"":n,a),a);const f=require("@typescript-eslint/types"),Y=require("@typescript-eslint/utils"),H=require("natural-compare-lite"),Q=require("is-core-module"),k=require("minimatch"),Z=require("json5"),ee=require("path"),te=require("fs");let O=Y.ESLintUtils.RuleCreator(r=>`https://eslint-plugin-perfectionist.azat.io/rules/${r}`),x=r=>r.replaceAll(/\s\s+/g," ").trim(),_=r=>{let[n,a]=r;return a-n};var i=(r=>(r.alphabetical="alphabetical",r.natural="natural",r["line-length"]="line-length",r))(i||{}),c=(r=>(r.asc="asc",r.desc="desc",r))(c||{});let b=(r,n,a)=>{let l=a.order==="asc"?1:-1,p,t=e=>a["ignore-case"]?e.toLowerCase():e;return a.type===i.alphabetical?p=(e,s)=>t(e.name).localeCompare(t(s.name)):a.type===i.natural?p=(e,s)=>H(t(e.name),t(s.name)):p=(e,s)=>e.size-s.size,l*p(r,n)>0},T=(r,n)=>[...r].sort((a,l)=>Number(b(a,l,n))||-1),ae=(r,n)=>{let a=n.getTokenBefore(r,{includeComments:!0});return(a==null?void 0:a.type)===f.AST_TOKEN_TYPES.Block||(a==null?void 0:a.type)===f.AST_TOKEN_TYPES.Line?a:null},P=(r,n)=>{let a=r.range.at(0),l=r.range.at(1),p=n.text.slice(a,l);if(Y.ASTUtils.isParenthesized(r,n)){let e=n.getTokenBefore(r,Y.ASTUtils.isOpeningParenToken),s=n.getTokenAfter(r,Y.ASTUtils.isClosingParenToken);a=e.range.at(0),l=s.range.at(1)}let t=ae(r,n);return(p.endsWith(";")||p.endsWith(","))&&(l-=1),t&&(a=t.range.at(0)),[a,l]},w=(r,n,a,l)=>n.map(({node:p},t)=>r.replaceTextRange(P(p,l),l.text.slice(...P(a[t].node,l)))),N=(r={},n)=>Object.assign(n,r),A=(r,n)=>{if(r.length>1)for(let a=1;a<r.length;a++){let l=r.at(a-1),p=r.at(a);l&&p&&n(l,p,a-1)}};const L="sort-array-includes",re=O({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:[c.asc,c.desc],default:c.asc},"ignore-case":{type:"boolean",default:!1},"spread-last":{type:"boolean",default:!1}},additionalProperties:!1}],messages:{unexpectedArrayIncludesOrder:'Expected "{{second}}" to come before "{{first}}"'}},defaultOptions:[{type:i.alphabetical,order:c.asc}],create:r=>({MemberExpression:n=>{if((n.object.type===f.AST_NODE_TYPES.ArrayExpression||n.object.type===f.AST_NODE_TYPES.NewExpression)&&n.property.type===f.AST_NODE_TYPES.Identifier&&n.property.name==="includes"){let a=n.object.type===f.AST_NODE_TYPES.ArrayExpression?n.object.elements:n.object.arguments;if(a.length>1){let l=N(r.options.at(0),{type:i.alphabetical,order:c.asc,"ignore-case":!1,"spread-last":!1}),p=r.getSourceCode(),t=a.reduce((e,s)=>(s!==null&&e.at(0).push({name:s.type===f.AST_NODE_TYPES.Literal?s.raw:p.text.slice(...s.range),size:_(s.range),type:s.type,node:s}),e),[[],[]]).flat();A(t,(e,s)=>{let o;l["spread-last"]&&e.node.type===f.AST_NODE_TYPES.Literal&&s.node.type===f.AST_NODE_TYPES.SpreadElement?o=!1:l["spread-last"]&&e.node.type===f.AST_NODE_TYPES.SpreadElement&&s.node.type===f.AST_NODE_TYPES.Literal?o=!0:o=b(e,s,l),o&&r.report({messageId:"unexpectedArrayIncludesOrder",data:{first:x(e.name),second:x(s.name)},node:s.node,fix:d=>{let u=T(t,l);return l["spread-last"]&&u.forEach((g,m)=>{g.node.type===f.AST_NODE_TYPES.SpreadElement&&u.push(u.splice(m,1).at(0))}),w(d,t,u,p)}})})}}}})}),v="sort-classes",se=O({name:v,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:[c.asc,c.desc],default:c.asc},groups:{type:"array",default:["property","constructor","method","unknown"]}},additionalProperties:!1}],messages:{unexpectedClassesOrder:'Expected "{{second}}" to come before "{{first}}"'}},defaultOptions:[{type:i.alphabetical,order:c.asc}],create:r=>({ClassBody:n=>{if(n.body.length>1){let a=N(r.options.at(0),{type:i.alphabetical,order:c.asc,"ignore-case":!1,groups:["property","constructor","method","unknown"]}),l=r.getSourceCode(),p=n.body.map(e=>{var u;let s,o;e.type===f.AST_NODE_TYPES.StaticBlock?o="static":e.type===f.AST_NODE_TYPES.TSIndexSignature?o=l.text.slice(e.range.at(0),((u=e.typeAnnotation)==null?void 0:u.range.at(0))??e.range.at(1)):e.key.type===f.AST_NODE_TYPES.Identifier?{name:o}=e.key:o=l.text.slice(...e.key.range);let d=g=>{!s&&a.groups.flat().includes(g)&&(s=g)};return e.type===f.AST_NODE_TYPES.MethodDefinition?(e.kind==="constructor"&&d("constructor"),e.static&&d("static-method"),e.accessibility==="private"&&d("private-method"),d("method")):e.type===f.AST_NODE_TYPES.PropertyDefinition&&(e.static&&d("static-property"),e.accessibility==="private"&&d("private-property"),d("property")),{size:_(e.range),group:s??"unknown",node:e,name:o}}),t=e=>{for(let s=0,o=a.groups.length;s<o;s++){let d=a.groups[s];if(e.group===d||Array.isArray(d)&&d.includes(e.group))return s}return a.groups.length};A(p,(e,s)=>{let o=t(e),d=t(s);(o>d||o===d&&b(e,s,a))&&r.report({messageId:"unexpectedClassesOrder",data:{first:x(e.name),second:x(s.name)},node:s.node,fix:u=>{let g=[],m=p.reduce((h,E)=>{let S=t(E);return S in h?h[S]=T([...h[S],E],a):h[S]=[E],h},{});return Object.keys(m).sort().reduce((h,E)=>[...h,...m[E]],[]).forEach((h,E)=>{g.push(u.replaceTextRange(P(p.at(E).node,l),l.text.slice(...P(h.node,l))))}),g}})})}}})}),R="sort-enums",ne=O({name:R,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:[c.asc,c.desc],default:c.asc}},additionalProperties:!1}],messages:{unexpectedEnumsOrder:'Expected "{{second}}" to come before "{{first}}"'}},defaultOptions:[{type:i.alphabetical,order:c.asc}],create:r=>({TSEnumDeclaration:n=>{if(n.members.length>1){let a=N(r.options.at(0),{type:i.alphabetical,order:c.asc,"ignore-case":!1}),l=r.getSourceCode(),p=n.members.map(t=>({name:t.id.type===f.AST_NODE_TYPES.Literal?`${t.id.value}`:`${l.text.slice(...t.id.range)}`,size:_(t.range),node:t}));A(p,(t,e)=>{b(t,e,a)&&r.report({messageId:"unexpectedEnumsOrder",data:{first:x(t.name),second:x(e.name)},node:e.node,fix:s=>w(s,p,T(p,a),l)})})}}})});class W{static get(){return this._instance?this._instance:(this._instance=Z.parse(te.readFileSync(ee.resolve(process.cwd(),"tsconfig.json"),"utf8")),this._instance)}}G(W,"_instance");const C="sort-imports",oe=O({name:C,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:[c.asc,c.desc],default:c.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 "{{second}}" to come before "{{first}}"',missedSpacingBetweenImports:'Missed spacing between "{{first}}" and "{{second}}" imports',extraSpacingBetweenImports:'Extra spacing between "{{first}}" and "{{second}}" imports'}},defaultOptions:[{type:i.alphabetical,order:c.asc}],create:r=>{var s;let n=N(r.options.at(0),{"newlines-between":"always","internal-pattern":["~/**"],type:i.alphabetical,"read-tsconfig":!1,order:c.asc,"ignore-case":!1,groups:[]}),a=[];if(n["read-tsconfig"]){let o=W.get();(s=o.compilerOptions)!=null&&s.paths&&Object.keys(o.compilerOptions.paths).forEach(d=>{a.push(d)})}let l=r.getSourceCode(),p=[],t=o=>{let d,u=E=>["./index.d.js","./index.d.ts","./index.js","./index.ts","./index","./","."].includes(E),g=E=>E.indexOf("..")===0,m=E=>E.indexOf("./")===0,y=E=>{!d&&n.groups.flat().includes(E)&&(d=E)},h=E=>n["internal-pattern"].length&&n["internal-pattern"].some(S=>k.minimatch(E.source.value,S))||a.some(S=>k.minimatch(E.source.value,S));return o.importKind==="type"&&(o.type===f.AST_NODE_TYPES.ImportDeclaration&&(h(o)?y("internal-type"):u(o.source.value)?y("index-type"):g(o.source.value)?y("parent-type"):m(o.source.value)&&y("sibling-type")),y("type")),!d&&o.type===f.AST_NODE_TYPES.ImportDeclaration&&(Q(o.source.value)?y("builtin"):h(o)?y("internal"):u(o.source.value)?y("index"):g(o.source.value)?y("parent"):m(o.source.value)?y("sibling"):y("external")),d??"unknown"},e=o=>{let d;o.type===f.AST_NODE_TYPES.ImportDeclaration?d=o.source.value:o.moduleReference.type===f.AST_NODE_TYPES.TSExternalModuleReference&&o.moduleReference.expression.type===f.AST_NODE_TYPES.Literal?d=`${o.moduleReference.expression.value}`:d=l.text.slice(...o.moduleReference.range),p.push({size:_(o.range),group:t(o),name:d,node:o})};return{TSImportEqualsDeclaration:e,ImportDeclaration:e,"Program:exit":()=>{let o=g=>{for(let m=0,y=n.groups.length;m<y;m++){let h=n.groups[m];if(g.group===h||Array.isArray(h)&&h.includes(g.group))return m}return n.groups.length},d=(g,m)=>l.lines.slice(g.node.loc.end.line,m.node.loc.start.line-1).filter(h=>!h.trim().length).length,u=g=>{let m=[],y=p.reduce((E,S)=>{let I=o(S);return I in E?E[I]=T([...E[I],S],n):E[I]=[S],E},{}),h=Object.keys(y).sort().reduce((E,S)=>[...E,...y[S]],[]);return h.forEach((E,S)=>{if(m.push(g.replaceTextRange(P(p.at(S).node,l),l.text.slice(...P(E.node,l)))),n["newlines-between"]!=="ignore"){let I=h.at(S+1);if(I){let D=d(p.at(S),p.at(S+1));(n["newlines-between"]==="always"&&o(E)===o(I)&&D!==0||n["newlines-between"]==="never"&&D>0)&&m.push(g.removeRange([P(p.at(S).node,l).at(1),P(p.at(S+1).node,l).at(0)-1])),n["newlines-between"]==="always"&&D>1&&m.push(g.replaceTextRange([P(p.at(S).node,l).at(1),P(p.at(S+1).node,l).at(0)-1],`
2
+ `)),n["newlines-between"]==="always"&&o(E)!==o(I)&&D===0&&m.push(g.insertTextAfterRange(P(p.at(S).node,l),`
3
+ `))}}}),m};A(p,(g,m)=>{let y=o(g),h=o(m),E=d(g,m);(y>h||y===h&&b(g,m,n))&&r.report({messageId:"unexpectedImportsOrder",data:{first:g.name,second:m.name},node:m.node,fix:u}),n["newlines-between"]==="never"&&E>0&&r.report({messageId:"extraSpacingBetweenImports",data:{first:g.name,second:m.name},node:m.node,fix:u}),n["newlines-between"]==="always"&&(y<h&&E===0?r.report({messageId:"missedSpacingBetweenImports",data:{first:g.name,second:m.name},node:m.node,fix:u}):(E>1||y===h&&E>0)&&r.report({messageId:"extraSpacingBetweenImports",data:{first:g.name,second:m.name},node:m.node,fix:u}))})}}}}),M="sort-interfaces",ie=O({name:M,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:[c.asc,c.desc],default:c.asc},"ignore-case":{type:"boolean",default:!1},"ignore-pattern":{type:"array",default:[]}},additionalProperties:!1}],messages:{unexpectedInterfacePropertiesOrder:'Expected "{{second}}" to come before "{{first}}"'}},defaultOptions:[{type:i.alphabetical,order:c.asc}],create:r=>({TSInterfaceDeclaration:n=>{if(n.body.body.length>1){let a=N(r.options.at(0),{type:i.alphabetical,"ignore-case":!1,order:c.asc,"ignore-pattern":[]});if(!a["ignore-pattern"].some(l=>k.minimatch(n.id.name,l))){let l=r.getSourceCode(),p=n.body.body.map(t=>{var s,o,d;let e;if(t.type===f.AST_NODE_TYPES.TSPropertySignature)if(t.key.type===f.AST_NODE_TYPES.Identifier)({name:e}=t.key);else if(t.key.type===f.AST_NODE_TYPES.Literal)e=`${t.key.value}`;else{let u=((s=t.typeAnnotation)==null?void 0:s.range.at(0))??t.range.at(1)-(t.optional?1:0);e=l.text.slice(t.range.at(0),u)}else if(t.type===f.AST_NODE_TYPES.TSIndexSignature){let u=((o=t.typeAnnotation)==null?void 0:o.range.at(0))??t.range.at(1);e=l.text.slice(t.range.at(0),u)}else{let u=((d=t.returnType)==null?void 0:d.range.at(0))??t.range.at(1);e=l.text.slice(t.range.at(0),u)}return{size:_(t.range),node:t,name:e}});A(p,(t,e)=>{b(t,e,a)&&r.report({messageId:"unexpectedInterfacePropertiesOrder",data:{first:x(t.name),second:x(e.name)},node:e.node,fix:s=>w(s,p,T(p,a),l)})})}}}})});let F=(r,n)=>r.reduce((a,l)=>{let p=n(l);return p in a||(a[p]=[]),a[p].push(l),a},{});const $="sort-jsx-props",le=O({name:$,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:[c.asc,c.desc],default:c.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 "{{second}}" to come before "{{first}}"'}},defaultOptions:[{type:i.alphabetical,order:c.asc}],create:r=>({JSXElement:n=>{if(n.openingElement.attributes.length>1){let a=N(r.options.at(0),{type:i.alphabetical,shorthand:"ignore",multiline:"ignore",callback:"ignore","always-on-top":[],"ignore-case":!1,order:c.asc}),l=r.getSourceCode();n.openingElement.attributes.reduce((t,e)=>{if(e.type===f.AST_NODE_TYPES.JSXSpreadAttribute)return t.push([]),t;let s="ignore";e.name.type===f.AST_NODE_TYPES.JSXIdentifier&&a["always-on-top"].includes(e.name.name)?s="exception":(a.shorthand!=="ignore"&&e.value===null&&(s=a.shorthand),a.callback!=="ignore"&&e.name.type===f.AST_NODE_TYPES.JSXIdentifier&&e.name.name.indexOf("on")===0&&e.value!==null?s=a.callback:a.multiline!=="ignore"&&e.loc.start.line!==e.loc.end.line&&(s=a.multiline));let o={name:e.name.type===f.AST_NODE_TYPES.JSXNamespacedName?`${e.name.namespace.name}:${e.name.name.name}`:e.name.name,size:_(e.range),node:e,position:s};return t.at(-1).push(o),t},[[]]).forEach(t=>{A(t,(e,s)=>{let o;if(e.position==="exception"&&s.position==="exception")o=a["always-on-top"].indexOf(e.name)>a["always-on-top"].indexOf(s.name);else if(e.position===s.position)o=b(e,s,a);else{let d={exception:2,first:1,ignore:0,last:-1};o=d[e.position]<d[s.position]}o&&r.report({messageId:"unexpectedJSXPropsOrder",data:{first:e.name,second:s.name},node:s.node,fix:d=>{let u=F(t,({position:y})=>y),g=y=>y in u?u[y]:[],m=[g("exception").sort((y,h)=>a["always-on-top"].indexOf(y.name)-a["always-on-top"].indexOf(h.name)),T(g("first"),a),T(g("ignore"),a),T(g("last"),a)].flat();return w(d,t,m,l)}})})})}}})}),U="sort-map-elements",pe=O({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:[c.asc,c.desc],default:c.asc},"ignore-case":{type:"boolean",default:!1}},additionalProperties:!1}],messages:{unexpectedMapElementsOrder:'Expected "{{second}}" to come before "{{first}}"'}},defaultOptions:[{type:i.alphabetical,order:c.asc}],create:r=>({NewExpression:n=>{if(n.callee.type===f.AST_NODE_TYPES.Identifier&&n.callee.name==="Map"&&n.arguments[0].type===f.AST_NODE_TYPES.ArrayExpression){let[{elements:a}]=n.arguments;if(a.length>1){let l=N(r.options.at(0),{type:i.alphabetical,"ignore-case":!1,order:c.asc}),p=r.getSourceCode();a.reduce((e,s)=>(s===null||s.type===f.AST_NODE_TYPES.SpreadElement?e.push([]):e.at(-1).push(s),e),[[]]).forEach(e=>{let s=e.map(o=>{let d;if(o.type===f.AST_NODE_TYPES.ArrayExpression){let[u]=o.elements;u?u.type===f.AST_NODE_TYPES.Literal?d=u.raw:d=p.text.slice(...u.range):d=`${u}`}else d=p.text.slice(...o.range);return{size:_(o.range),node:o,name:d}});A(s,(o,d)=>{b(o,d,l)&&r.report({messageId:"unexpectedMapElementsOrder",data:{first:x(o.name),second:x(d.name)},node:d.node,fix:u=>w(u,s,T(s,l),p)})})})}}}})}),z="sort-named-exports",ce=O({name:z,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:[c.asc,c.desc],default:c.asc},"ignore-case":{type:"boolean",default:!1}},additionalProperties:!1}],messages:{unexpectedNamedExportsOrder:'Expected "{{second}}" to come before "{{first}}"'}},defaultOptions:[{type:i.alphabetical,order:c.asc}],create:r=>({ExportNamedDeclaration:n=>{if(n.specifiers.length>1){let a=N(r.options.at(0),{type:i.alphabetical,"ignore-case":!1,order:c.asc}),l=r.getSourceCode(),p=n.specifiers.map(t=>({size:_(t.range),name:t.local.name,node:t}));A(p,(t,e)=>{b(t,e,a)&&r.report({messageId:"unexpectedNamedExportsOrder",data:{first:t.name,second:e.name},node:e.node,fix:s=>w(s,p,T(p,a),l)})})}}})}),B="sort-named-imports",de=O({name:B,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:[c.asc,c.desc],default:c.asc},"ignore-case":{type:"boolean",default:!1}},additionalProperties:!1}],messages:{unexpectedNamedImportsOrder:'Expected "{{second}}" to come before "{{first}}"'}},defaultOptions:[{type:i.alphabetical,order:c.asc}],create:r=>({ImportDeclaration:n=>{if(n.specifiers.length>1){let a=N(r.options.at(0),{type:i.alphabetical,"ignore-case":!1,order:c.asc}),l=r.getSourceCode(),p=n.specifiers.map(t=>({size:_(t.range),name:t.local.name,node:t}));A(p,(t,e)=>{b(t,e,a)&&r.report({messageId:"unexpectedNamedImportsOrder",data:{first:t.name,second:e.name},node:e.node,fix:s=>w(s,p,T(p,a),l)})})}}})}),q="sort-object-types",fe=O({name:q,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:[c.asc,c.desc],default:c.asc},"ignore-case":{type:"boolean",default:!1}},additionalProperties:!1}],messages:{unexpectedObjectTypesOrder:'Expected "{{second}}" to come before "{{first}}"'}},defaultOptions:[{type:i.alphabetical,order:c.asc}],create:r=>({TSTypeLiteral:n=>{if(n.members.length>1){let a=N(r.options.at(0),{type:i.alphabetical,"ignore-case":!1,order:c.asc}),l=r.getSourceCode(),p=n.members.map(t=>{var u,g;let e,s=l.text.slice(t.range.at(0),t.range.at(1));if(t.type===f.AST_NODE_TYPES.TSPropertySignature)t.key.type===f.AST_NODE_TYPES.Identifier?{name:e}=t.key:t.key.type===f.AST_NODE_TYPES.Literal?e=`${t.key.value}`:e=l.text.slice(t.range.at(0),(u=t.typeAnnotation)==null?void 0:u.range.at(0));else if(t.type===f.AST_NODE_TYPES.TSIndexSignature){let m=((g=t.typeAnnotation)==null?void 0:g.range.at(0))??t.range.at(1);e=l.text.slice(t.range.at(0),m)}else e=l.text.slice(t.range.at(0),t.range.at(1));let d=s.endsWith(";")||s.endsWith(",")?1:0;return{size:_(t.range)-d,node:t,name:e}});A(p,(t,e)=>{b(t,e,a)&&r.report({messageId:"unexpectedObjectTypesOrder",data:{first:x(t.name),second:x(e.name)},node:e.node,fix:s=>w(s,p,T(p,a),l)})})}}})}),J="sort-objects",ue=O({name:J,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:[c.asc,c.desc],default:c.asc},"ignore-case":{type:"boolean",default:!1},"always-on-top":{type:"array",default:[]}},additionalProperties:!1}],messages:{unexpectedObjectsOrder:'Expected "{{second}}" to come before "{{first}}"'}},defaultOptions:[{type:i.alphabetical,order:c.asc}],create:r=>({ObjectExpression:n=>{if(n.properties.length>1){let a=N(r.options.at(0),{type:i.alphabetical,"ignore-case":!1,order:c.asc,"always-on-top":[]}),l=r.getSourceCode();(t=>t.reduce((e,s)=>{if(s.type===f.AST_NODE_TYPES.SpreadElement)return e.push([]),e;let o,d="ignore";s.key.type===f.AST_NODE_TYPES.Identifier?{name:o}=s.key:s.key.type===f.AST_NODE_TYPES.Literal?o=`${s.key.value}`:o=l.text.slice(...s.key.range),s.key.type===f.AST_NODE_TYPES.Identifier&&a["always-on-top"].includes(s.key.name)&&(d="exception");let u={size:_(s.range),node:s,position:d,name:o};return e.at(-1).push(u),e},[[]]))(n.properties).forEach(t=>{A(t,(e,s)=>{let o;if(e.position==="exception"&&s.position==="exception")o=a["always-on-top"].indexOf(e.name)>a["always-on-top"].indexOf(s.name);else if(e.position===s.position)o=b(e,s,a);else{let d={exception:1,ignore:0};o=d[e.position]<d[s.position]}o&&r.report({messageId:"unexpectedObjectsOrder",data:{first:x(e.name),second:x(s.name)},node:s.node,fix:d=>{let u=F(t,({position:y})=>y),g=y=>y in u?u[y]:[],m=[g("exception").sort((y,h)=>a["always-on-top"].indexOf(y.name)-a["always-on-top"].indexOf(h.name)),T(g("ignore"),a)].flat();return w(d,t,m,l)}})})})}}})}),X="sort-union-types",ge=O({name:X,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:[c.asc,c.desc],default:c.asc},"ignore-case":{type:"boolean",default:!1}},additionalProperties:!1}],messages:{unexpectedUnionTypesOrder:'Expected "{{second}}" to come before "{{first}}"'}},defaultOptions:[{type:i.alphabetical,order:c.asc}],create:r=>({TSUnionType:n=>{let a=r.getSourceCode(),l=N(r.options.at(0),{type:i.alphabetical,"ignore-case":!1,order:c.asc}),p=n.types.map(t=>({name:a.text.slice(...t.range),size:_(t.range),node:t}));A(p,(t,e)=>{b(t,e,l)&&r.report({messageId:"unexpectedUnionTypesOrder",data:{first:x(t.name),second:x(e.name)},node:e.node,fix:s=>w(s,p,T(p,l),a)})})}})}),me="eslint-plugin-perfectionist";let j=r=>{let n={[L]:["error",{"spread-last":!0}],[v]:["error",{groups:["static-property","private-property","property","constructor","static-method","private-method","method","unknown"]}],[R]:["error"],[C]:["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}],[M]:["error"],[$]:["error",{"always-on-top":[],shorthand:"ignore",multiline:"first",callback:"ignore"}],[U]:["error"],[z]:["error"],[B]:["error"],[q]:["error"],[J]:["error",{"always-on-top":[]}],[X]:["error"]};return{plugins:["perfectionist"],rules:Object.fromEntries(Object.entries(n).map(([a,[l,p={}]])=>[`perfectionist/${a}`,[l,Object.assign(p,r)]]))}};const ye={name:me,rules:{[L]:re,[v]:se,[R]:ne,[C]:oe,[M]:ie,[$]:le,[U]:pe,[z]:ce,[B]:de,[q]:fe,[J]:ue,[X]:ge},configs:{"recommended-alphabetical":j({type:i.alphabetical,order:c.asc,"ignore-case":!1}),"recommended-natural":j({type:i.natural,order:c.asc,"ignore-case":!1}),"recommended-line-length":j({type:i["line-length"],order:c.desc})}};module.exports=ye;
package/dist/index.mjs CHANGED
@@ -1,7 +1,7 @@
1
- var W = Object.defineProperty;
2
- var H = (r, n, a) => n in r ? W(r, n, { enumerable: !0, configurable: !0, writable: !0, value: a }) : r[n] = a;
3
- var K = (r, n, a) => (H(r, typeof n != "symbol" ? n + "" : n, a), a);
4
- import { AST_TOKEN_TYPES as V, AST_NODE_TYPES as f } from "@typescript-eslint/types";
1
+ var q = Object.defineProperty;
2
+ var H = (r, n, a) => n in r ? q(r, n, { enumerable: !0, configurable: !0, writable: !0, value: a }) : r[n] = a;
3
+ var F = (r, n, a) => (H(r, typeof n != "symbol" ? n + "" : n, a), a);
4
+ import { AST_TOKEN_TYPES as K, AST_NODE_TYPES as u } from "@typescript-eslint/types";
5
5
  import { ESLintUtils as Q, ASTUtils as L } from "@typescript-eslint/utils";
6
6
  import Z from "natural-compare-lite";
7
7
  import ee from "is-core-module";
@@ -23,21 +23,21 @@ let O = (r, n, a) => {
23
23
  let a = n.getTokenBefore(r, {
24
24
  includeComments: !0
25
25
  });
26
- return (a == null ? void 0 : a.type) === V.Block || (a == null ? void 0 : a.type) === V.Line ? a : null;
26
+ return (a == null ? void 0 : a.type) === K.Block || (a == null ? void 0 : a.type) === K.Line ? a : null;
27
27
  }, k = (r, n) => {
28
- let a = r.range.at(0), l = r.range.at(1);
28
+ let a = r.range.at(0), l = r.range.at(1), p = n.text.slice(a, l);
29
29
  if (L.isParenthesized(r, n)) {
30
- let t = n.getTokenBefore(
30
+ let e = n.getTokenBefore(
31
31
  r,
32
32
  L.isOpeningParenToken
33
- ), e = n.getTokenAfter(
33
+ ), s = n.getTokenAfter(
34
34
  r,
35
35
  L.isClosingParenToken
36
36
  );
37
- a = t.range.at(0), l = e.range.at(1);
37
+ a = e.range.at(0), l = s.range.at(1);
38
38
  }
39
- let p = se(r, n);
40
- return p && (a = p.range.at(0)), [a, l];
39
+ let t = se(r, n);
40
+ return (p.endsWith(";") || p.endsWith(",")) && (l -= 1), t && (a = t.range.at(0)), [a, l];
41
41
  }, A = (r, n, a, l) => n.map(
42
42
  ({ node: p }, t) => r.replaceTextRange(
43
43
  k(p, l),
@@ -99,17 +99,17 @@ const C = "sort-array-includes", ne = I({
99
99
  ],
100
100
  create: (r) => ({
101
101
  MemberExpression: (n) => {
102
- if ((n.object.type === f.ArrayExpression || n.object.type === f.NewExpression) && n.property.type === f.Identifier && n.property.name === "includes") {
103
- let a = j(r.options.at(0), {
104
- type: i.alphabetical,
105
- order: d.asc,
106
- "ignore-case": !1,
107
- "spread-last": !1
108
- }), l = n.object.type === f.ArrayExpression ? n.object.elements : n.object.arguments;
109
- if (l.length > 1) {
110
- let p = r.getSourceCode(), t = l.reduce(
102
+ if ((n.object.type === u.ArrayExpression || n.object.type === u.NewExpression) && n.property.type === u.Identifier && n.property.name === "includes") {
103
+ let a = n.object.type === u.ArrayExpression ? n.object.elements : n.object.arguments;
104
+ if (a.length > 1) {
105
+ let l = j(r.options.at(0), {
106
+ type: i.alphabetical,
107
+ order: d.asc,
108
+ "ignore-case": !1,
109
+ "spread-last": !1
110
+ }), p = r.getSourceCode(), t = a.reduce(
111
111
  (e, s) => (s !== null && e.at(0).push({
112
- name: s.type === f.Literal ? s.raw : p.text.slice(...s.range),
112
+ name: s.type === u.Literal ? s.raw : p.text.slice(...s.range),
113
113
  size: S(s.range),
114
114
  type: s.type,
115
115
  node: s
@@ -118,7 +118,7 @@ const C = "sort-array-includes", ne = I({
118
118
  ).flat();
119
119
  N(t, (e, s) => {
120
120
  let o;
121
- a["spread-last"] && e.node.type === f.Literal && s.node.type === f.SpreadElement ? o = !1 : a["spread-last"] && e.node.type === f.SpreadElement && s.node.type === f.Literal ? o = !0 : o = O(e, s, a), o && r.report({
121
+ l["spread-last"] && e.node.type === u.Literal && s.node.type === u.SpreadElement ? o = !1 : l["spread-last"] && e.node.type === u.SpreadElement && s.node.type === u.Literal ? o = !0 : o = O(e, s, l), o && r.report({
122
122
  messageId: "unexpectedArrayIncludesOrder",
123
123
  data: {
124
124
  first: E(e.name),
@@ -126,10 +126,10 @@ const C = "sort-array-includes", ne = I({
126
126
  },
127
127
  node: s.node,
128
128
  fix: (c) => {
129
- let u = w(t, a);
130
- return a["spread-last"] && u.forEach((m, y) => {
131
- m.node.type === f.SpreadElement && u.push(u.splice(y, 1).at(0));
132
- }), A(c, t, u, p);
129
+ let f = w(t, l);
130
+ return l["spread-last"] && f.forEach((g, m) => {
131
+ g.node.type === u.SpreadElement && f.push(f.splice(m, 1).at(0));
132
+ }), A(c, t, f, p);
133
133
  }
134
134
  });
135
135
  });
@@ -193,16 +193,16 @@ const C = "sort-array-includes", ne = I({
193
193
  "ignore-case": !1,
194
194
  groups: ["property", "constructor", "method", "unknown"]
195
195
  }), l = r.getSourceCode(), p = n.body.map((e) => {
196
- var u;
196
+ var f;
197
197
  let s, o;
198
- e.type === f.StaticBlock ? o = "static" : e.type === f.TSIndexSignature ? o = l.text.slice(
198
+ e.type === u.StaticBlock ? o = "static" : e.type === u.TSIndexSignature ? o = l.text.slice(
199
199
  e.range.at(0),
200
- ((u = e.typeAnnotation) == null ? void 0 : u.range.at(0)) ?? e.range.at(1)
201
- ) : e.key.type === f.Identifier ? { name: o } = e.key : o = l.text.slice(...e.key.range);
202
- let c = (m) => {
203
- !s && a.groups.flat().includes(m) && (s = m);
200
+ ((f = e.typeAnnotation) == null ? void 0 : f.range.at(0)) ?? e.range.at(1)
201
+ ) : e.key.type === u.Identifier ? { name: o } = e.key : o = l.text.slice(...e.key.range);
202
+ let c = (g) => {
203
+ !s && a.groups.flat().includes(g) && (s = g);
204
204
  };
205
- return e.type === f.MethodDefinition ? (e.kind === "constructor" && c("constructor"), e.static && c("static-method"), e.accessibility === "private" && c("private-method"), c("method")) : e.type === f.PropertyDefinition && (e.static && c("static-property"), e.accessibility === "private" && c("private-property"), c("property")), {
205
+ return e.type === u.MethodDefinition ? (e.kind === "constructor" && c("constructor"), e.static && c("static-method"), e.accessibility === "private" && c("private-method"), c("method")) : e.type === u.PropertyDefinition && (e.static && c("static-property"), e.accessibility === "private" && c("private-property"), c("property")), {
206
206
  size: S(e.range),
207
207
  group: s ?? "unknown",
208
208
  node: e,
@@ -225,8 +225,8 @@ const C = "sort-array-includes", ne = I({
225
225
  second: E(s.name)
226
226
  },
227
227
  node: s.node,
228
- fix: (u) => {
229
- let m = [], y = p.reduce(
228
+ fix: (f) => {
229
+ let g = [], m = p.reduce(
230
230
  (x, h) => {
231
231
  let b = t(h);
232
232
  return b in x ? x[b] = w(
@@ -236,22 +236,22 @@ const C = "sort-array-includes", ne = I({
236
236
  },
237
237
  {}
238
238
  );
239
- return Object.keys(y).sort().reduce(
239
+ return Object.keys(m).sort().reduce(
240
240
  (x, h) => [
241
241
  ...x,
242
- ...y[h]
242
+ ...m[h]
243
243
  ],
244
244
  []
245
245
  ).forEach((x, h) => {
246
- m.push(
247
- u.replaceTextRange(
246
+ g.push(
247
+ f.replaceTextRange(
248
248
  k(p.at(h).node, l),
249
249
  l.text.slice(
250
250
  ...k(x.node, l)
251
251
  )
252
252
  )
253
253
  );
254
- }), m;
254
+ }), g;
255
255
  }
256
256
  });
257
257
  });
@@ -303,14 +303,13 @@ const C = "sort-array-includes", ne = I({
303
303
  ],
304
304
  create: (r) => ({
305
305
  TSEnumDeclaration: (n) => {
306
- let a = j(r.options.at(0), {
307
- type: i.alphabetical,
308
- order: d.asc,
309
- "ignore-case": !1
310
- });
311
306
  if (n.members.length > 1) {
312
- let l = r.getSourceCode(), p = n.members.map((t) => ({
313
- name: t.id.type === f.Literal ? `${t.id.value}` : `${l.text.slice(...t.id.range)}`,
307
+ let a = j(r.options.at(0), {
308
+ type: i.alphabetical,
309
+ order: d.asc,
310
+ "ignore-case": !1
311
+ }), l = r.getSourceCode(), p = n.members.map((t) => ({
312
+ name: t.id.type === u.Literal ? `${t.id.value}` : `${l.text.slice(...t.id.range)}`,
314
313
  size: S(t.range),
315
314
  node: t
316
315
  }));
@@ -329,14 +328,14 @@ const C = "sort-array-includes", ne = I({
329
328
  }
330
329
  })
331
330
  });
332
- class Y {
331
+ class V {
333
332
  static get() {
334
333
  return this._instance ? this._instance : (this._instance = te.parse(
335
334
  re.readFileSync(ae.resolve(process.cwd(), "tsconfig.json"), "utf8")
336
335
  ), this._instance);
337
336
  }
338
337
  }
339
- K(Y, "_instance");
338
+ F(V, "_instance");
340
339
  const _ = "sort-imports", le = I({
341
340
  name: _,
342
341
  meta: {
@@ -416,13 +415,13 @@ const _ = "sort-imports", le = I({
416
415
  groups: []
417
416
  }), a = [];
418
417
  if (n["read-tsconfig"]) {
419
- let o = Y.get();
418
+ let o = V.get();
420
419
  (s = o.compilerOptions) != null && s.paths && Object.keys(o.compilerOptions.paths).forEach((c) => {
421
420
  a.push(c);
422
421
  });
423
422
  }
424
423
  let l = r.getSourceCode(), p = [], t = (o) => {
425
- let c, u = (h) => [
424
+ let c, f = (h) => [
426
425
  "./index.d.js",
427
426
  "./index.d.ts",
428
427
  "./index.js",
@@ -430,15 +429,15 @@ const _ = "sort-imports", le = I({
430
429
  "./index",
431
430
  "./",
432
431
  "."
433
- ].includes(h), m = (h) => h.indexOf("..") === 0, y = (h) => h.indexOf("./") === 0, g = (h) => {
432
+ ].includes(h), g = (h) => h.indexOf("..") === 0, m = (h) => h.indexOf("./") === 0, y = (h) => {
434
433
  !c && n.groups.flat().includes(h) && (c = h);
435
434
  }, x = (h) => n["internal-pattern"].length && n["internal-pattern"].some(
436
435
  (b) => R(h.source.value, b)
437
436
  ) || a.some((b) => R(h.source.value, b));
438
- return o.importKind === "type" && (o.type === f.ImportDeclaration && (x(o) ? g("internal-type") : u(o.source.value) ? g("index-type") : m(o.source.value) ? g("parent-type") : y(o.source.value) && g("sibling-type")), g("type")), !c && o.type === f.ImportDeclaration && (ee(o.source.value) ? g("builtin") : x(o) ? g("internal") : u(o.source.value) ? g("index") : m(o.source.value) ? g("parent") : y(o.source.value) ? g("sibling") : g("external")), c ?? "unknown";
437
+ return o.importKind === "type" && (o.type === u.ImportDeclaration && (x(o) ? y("internal-type") : f(o.source.value) ? y("index-type") : g(o.source.value) ? y("parent-type") : m(o.source.value) && y("sibling-type")), y("type")), !c && o.type === u.ImportDeclaration && (ee(o.source.value) ? y("builtin") : x(o) ? y("internal") : f(o.source.value) ? y("index") : g(o.source.value) ? y("parent") : m(o.source.value) ? y("sibling") : y("external")), c ?? "unknown";
439
438
  }, e = (o) => {
440
439
  let c;
441
- o.type === f.ImportDeclaration ? c = o.source.value : o.moduleReference.type === f.TSExternalModuleReference && o.moduleReference.expression.type === f.Literal ? c = `${o.moduleReference.expression.value}` : c = l.text.slice(...o.moduleReference.range), p.push({
440
+ o.type === u.ImportDeclaration ? c = o.source.value : o.moduleReference.type === u.TSExternalModuleReference && o.moduleReference.expression.type === u.Literal ? c = `${o.moduleReference.expression.value}` : c = l.text.slice(...o.moduleReference.range), p.push({
442
441
  size: S(o.range),
443
442
  group: t(o),
444
443
  name: c,
@@ -449,18 +448,18 @@ const _ = "sort-imports", le = I({
449
448
  TSImportEqualsDeclaration: e,
450
449
  ImportDeclaration: e,
451
450
  "Program:exit": () => {
452
- let o = (m) => {
453
- for (let y = 0, g = n.groups.length; y < g; y++) {
454
- let x = n.groups[y];
455
- if (m.group === x || Array.isArray(x) && x.includes(m.group))
456
- return y;
451
+ let o = (g) => {
452
+ for (let m = 0, y = n.groups.length; m < y; m++) {
453
+ let x = n.groups[m];
454
+ if (g.group === x || Array.isArray(x) && x.includes(g.group))
455
+ return m;
457
456
  }
458
457
  return n.groups.length;
459
- }, c = (m, y) => l.lines.slice(
460
- m.node.loc.end.line,
461
- y.node.loc.start.line - 1
462
- ).filter((x) => !x.trim().length).length, u = (m) => {
463
- let y = [], g = p.reduce(
458
+ }, c = (g, m) => l.lines.slice(
459
+ g.node.loc.end.line,
460
+ m.node.loc.start.line - 1
461
+ ).filter((x) => !x.trim().length).length, f = (g) => {
462
+ let m = [], y = p.reduce(
464
463
  (h, b) => {
465
464
  let P = o(b);
466
465
  return P in h ? h[P] = w(
@@ -469,16 +468,16 @@ const _ = "sort-imports", le = I({
469
468
  ) : h[P] = [b], h;
470
469
  },
471
470
  {}
472
- ), x = Object.keys(g).sort().reduce(
471
+ ), x = Object.keys(y).sort().reduce(
473
472
  (h, b) => [
474
473
  ...h,
475
- ...g[b]
474
+ ...y[b]
476
475
  ],
477
476
  []
478
477
  );
479
478
  return x.forEach((h, b) => {
480
- if (y.push(
481
- m.replaceTextRange(
479
+ if (m.push(
480
+ g.replaceTextRange(
482
481
  k(p.at(b).node, l),
483
482
  l.text.slice(...k(h.node, l))
484
483
  )
@@ -489,13 +488,13 @@ const _ = "sort-imports", le = I({
489
488
  p.at(b),
490
489
  p.at(b + 1)
491
490
  );
492
- (n["newlines-between"] === "always" && o(h) === o(P) && T !== 0 || n["newlines-between"] === "never" && T > 0) && y.push(
493
- m.removeRange([
491
+ (n["newlines-between"] === "always" && o(h) === o(P) && T !== 0 || n["newlines-between"] === "never" && T > 0) && m.push(
492
+ g.removeRange([
494
493
  k(p.at(b).node, l).at(1),
495
494
  k(p.at(b + 1).node, l).at(0) - 1
496
495
  ])
497
- ), n["newlines-between"] === "always" && T > 1 && y.push(
498
- m.replaceTextRange(
496
+ ), n["newlines-between"] === "always" && T > 1 && m.push(
497
+ g.replaceTextRange(
499
498
  [
500
499
  k(p.at(b).node, l).at(1),
501
500
  k(p.at(b + 1).node, l).at(0) - 1
@@ -503,8 +502,8 @@ const _ = "sort-imports", le = I({
503
502
  `
504
503
  `
505
504
  )
506
- ), n["newlines-between"] === "always" && o(h) !== o(P) && T === 0 && y.push(
507
- m.insertTextAfterRange(
505
+ ), n["newlines-between"] === "always" && o(h) !== o(P) && T === 0 && m.push(
506
+ g.insertTextAfterRange(
508
507
  k(p.at(b).node, l),
509
508
  `
510
509
  `
@@ -512,42 +511,42 @@ const _ = "sort-imports", le = I({
512
511
  );
513
512
  }
514
513
  }
515
- }), y;
514
+ }), m;
516
515
  };
517
- N(p, (m, y) => {
518
- let g = o(m), x = o(y), h = c(m, y);
519
- (g > x || g === x && O(m, y, n)) && r.report({
516
+ N(p, (g, m) => {
517
+ let y = o(g), x = o(m), h = c(g, m);
518
+ (y > x || y === x && O(g, m, n)) && r.report({
520
519
  messageId: "unexpectedImportsOrder",
521
520
  data: {
522
- first: m.name,
523
- second: y.name
521
+ first: g.name,
522
+ second: m.name
524
523
  },
525
- node: y.node,
526
- fix: u
524
+ node: m.node,
525
+ fix: f
527
526
  }), n["newlines-between"] === "never" && h > 0 && r.report({
528
527
  messageId: "extraSpacingBetweenImports",
529
528
  data: {
530
- first: m.name,
531
- second: y.name
529
+ first: g.name,
530
+ second: m.name
532
531
  },
533
- node: y.node,
534
- fix: u
535
- }), n["newlines-between"] === "always" && (g < x && h === 0 ? r.report({
532
+ node: m.node,
533
+ fix: f
534
+ }), n["newlines-between"] === "always" && (y < x && h === 0 ? r.report({
536
535
  messageId: "missedSpacingBetweenImports",
537
536
  data: {
538
- first: m.name,
539
- second: y.name
537
+ first: g.name,
538
+ second: m.name
540
539
  },
541
- node: y.node,
542
- fix: u
543
- }) : (h > 1 || g === x && h > 0) && r.report({
540
+ node: m.node,
541
+ fix: f
542
+ }) : (h > 1 || y === x && h > 0) && r.report({
544
543
  messageId: "extraSpacingBetweenImports",
545
544
  data: {
546
- first: m.name,
547
- second: y.name
545
+ first: g.name,
546
+ second: m.name
548
547
  },
549
- node: y.node,
550
- fix: u
548
+ node: m.node,
549
+ fix: f
551
550
  }));
552
551
  });
553
552
  }
@@ -602,56 +601,58 @@ const _ = "sort-imports", le = I({
602
601
  ],
603
602
  create: (r) => ({
604
603
  TSInterfaceDeclaration: (n) => {
605
- let a = j(r.options.at(0), {
606
- type: i.alphabetical,
607
- "ignore-case": !1,
608
- order: d.asc,
609
- "ignore-pattern": []
610
- });
611
- if (!a["ignore-pattern"].some(
612
- (l) => R(n.id.name, l)
613
- ) && n.body.body.length > 1) {
614
- let l = r.getSourceCode(), p = n.body.body.map((t) => {
615
- var s, o, c;
616
- let e;
617
- if (t.type === f.TSPropertySignature)
618
- if (t.key.type === f.Identifier)
619
- ({ name: e } = t.key);
620
- else if (t.key.type === f.Literal)
621
- e = `${t.key.value}`;
622
- else {
623
- let u = ((s = t.typeAnnotation) == null ? void 0 : s.range.at(0)) ?? t.range.at(1) - (t.optional ? 1 : 0);
624
- e = l.text.slice(t.range.at(0), u);
625
- }
626
- else if (t.type === f.TSIndexSignature) {
627
- let u = ((o = t.typeAnnotation) == null ? void 0 : o.range.at(0)) ?? t.range.at(1);
628
- e = l.text.slice(t.range.at(0), u);
629
- } else {
630
- let u = ((c = t.returnType) == null ? void 0 : c.range.at(0)) ?? t.range.at(1);
631
- e = l.text.slice(t.range.at(0), u);
632
- }
633
- return {
634
- size: S(t.range),
635
- node: t,
636
- name: e
637
- };
604
+ if (n.body.body.length > 1) {
605
+ let a = j(r.options.at(0), {
606
+ type: i.alphabetical,
607
+ "ignore-case": !1,
608
+ order: d.asc,
609
+ "ignore-pattern": []
638
610
  });
639
- N(p, (t, e) => {
640
- O(t, e, a) && r.report({
641
- messageId: "unexpectedInterfacePropertiesOrder",
642
- data: {
643
- first: E(t.name),
644
- second: E(e.name)
645
- },
646
- node: e.node,
647
- fix: (s) => A(s, p, w(p, a), l)
611
+ if (!a["ignore-pattern"].some(
612
+ (l) => R(n.id.name, l)
613
+ )) {
614
+ let l = r.getSourceCode(), p = n.body.body.map((t) => {
615
+ var s, o, c;
616
+ let e;
617
+ if (t.type === u.TSPropertySignature)
618
+ if (t.key.type === u.Identifier)
619
+ ({ name: e } = t.key);
620
+ else if (t.key.type === u.Literal)
621
+ e = `${t.key.value}`;
622
+ else {
623
+ let f = ((s = t.typeAnnotation) == null ? void 0 : s.range.at(0)) ?? t.range.at(1) - (t.optional ? 1 : 0);
624
+ e = l.text.slice(t.range.at(0), f);
625
+ }
626
+ else if (t.type === u.TSIndexSignature) {
627
+ let f = ((o = t.typeAnnotation) == null ? void 0 : o.range.at(0)) ?? t.range.at(1);
628
+ e = l.text.slice(t.range.at(0), f);
629
+ } else {
630
+ let f = ((c = t.returnType) == null ? void 0 : c.range.at(0)) ?? t.range.at(1);
631
+ e = l.text.slice(t.range.at(0), f);
632
+ }
633
+ return {
634
+ size: S(t.range),
635
+ node: t,
636
+ name: e
637
+ };
648
638
  });
649
- });
639
+ N(p, (t, e) => {
640
+ O(t, e, a) && r.report({
641
+ messageId: "unexpectedInterfacePropertiesOrder",
642
+ data: {
643
+ first: E(t.name),
644
+ second: E(e.name)
645
+ },
646
+ node: e.node,
647
+ fix: (s) => A(s, p, w(p, a), l)
648
+ });
649
+ });
650
+ }
650
651
  }
651
652
  }
652
653
  })
653
654
  });
654
- let q = (r, n) => r.reduce((a, l) => {
655
+ let Y = (r, n) => r.reduce((a, l) => {
655
656
  let p = n(l);
656
657
  return p in a || (a[p] = []), a[p].push(l), a;
657
658
  }, {});
@@ -728,79 +729,81 @@ const z = "sort-jsx-props", de = I({
728
729
  ],
729
730
  create: (r) => ({
730
731
  JSXElement: (n) => {
731
- let a = j(r.options.at(0), {
732
- type: i.alphabetical,
733
- shorthand: "ignore",
734
- multiline: "ignore",
735
- callback: "ignore",
736
- "always-on-top": [],
737
- "ignore-case": !1,
738
- order: d.asc
739
- }), l = r.getSourceCode();
740
- n.openingElement.attributes.reduce(
741
- (t, e) => {
742
- if (e.type === f.JSXSpreadAttribute)
743
- return t.push([]), t;
744
- let s = "ignore";
745
- e.name.type === f.JSXIdentifier && a["always-on-top"].includes(e.name.name) ? s = "exception" : (a.shorthand !== "ignore" && e.value === null && (s = a.shorthand), a.callback !== "ignore" && e.name.type === f.JSXIdentifier && e.name.name.indexOf("on") === 0 && e.value !== null ? s = a.callback : a.multiline !== "ignore" && e.loc.start.line !== e.loc.end.line && (s = a.multiline));
746
- let o = {
747
- name: e.name.type === f.JSXNamespacedName ? `${e.name.namespace.name}:${e.name.name.name}` : e.name.name,
748
- size: S(e.range),
749
- node: e,
750
- position: s
751
- };
752
- return t.at(-1).push(o), t;
753
- },
754
- [[]]
755
- ).forEach((t) => {
756
- N(t, (e, s) => {
757
- let o;
758
- if (e.position === "exception" && s.position === "exception")
759
- o = a["always-on-top"].indexOf(e.name) > a["always-on-top"].indexOf(s.name);
760
- else if (e.position === s.position)
761
- o = O(e, s, a);
762
- else {
763
- let c = {
764
- exception: 2,
765
- first: 1,
766
- ignore: 0,
767
- last: -1
732
+ if (n.openingElement.attributes.length > 1) {
733
+ let a = j(r.options.at(0), {
734
+ type: i.alphabetical,
735
+ shorthand: "ignore",
736
+ multiline: "ignore",
737
+ callback: "ignore",
738
+ "always-on-top": [],
739
+ "ignore-case": !1,
740
+ order: d.asc
741
+ }), l = r.getSourceCode();
742
+ n.openingElement.attributes.reduce(
743
+ (t, e) => {
744
+ if (e.type === u.JSXSpreadAttribute)
745
+ return t.push([]), t;
746
+ let s = "ignore";
747
+ e.name.type === u.JSXIdentifier && a["always-on-top"].includes(e.name.name) ? s = "exception" : (a.shorthand !== "ignore" && e.value === null && (s = a.shorthand), a.callback !== "ignore" && e.name.type === u.JSXIdentifier && e.name.name.indexOf("on") === 0 && e.value !== null ? s = a.callback : a.multiline !== "ignore" && e.loc.start.line !== e.loc.end.line && (s = a.multiline));
748
+ let o = {
749
+ name: e.name.type === u.JSXNamespacedName ? `${e.name.namespace.name}:${e.name.name.name}` : e.name.name,
750
+ size: S(e.range),
751
+ node: e,
752
+ position: s
768
753
  };
769
- o = c[e.position] < c[s.position];
770
- }
771
- o && r.report({
772
- messageId: "unexpectedJSXPropsOrder",
773
- data: {
774
- first: e.name,
775
- second: s.name
776
- },
777
- node: s.node,
778
- fix: (c) => {
779
- let u = q(t, ({ position: g }) => g), m = (g) => g in u ? u[g] : [], y = [
780
- m(
781
- "exception"
782
- /* exception */
783
- ).sort(
784
- (g, x) => a["always-on-top"].indexOf(g.name) - a["always-on-top"].indexOf(x.name)
785
- ),
786
- w(m(
787
- "first"
788
- /* first */
789
- ), a),
790
- w(m(
791
- "ignore"
792
- /* ignore */
793
- ), a),
794
- w(m(
795
- "last"
796
- /* last */
797
- ), a)
798
- ].flat();
799
- return A(c, t, y, l);
754
+ return t.at(-1).push(o), t;
755
+ },
756
+ [[]]
757
+ ).forEach((t) => {
758
+ N(t, (e, s) => {
759
+ let o;
760
+ if (e.position === "exception" && s.position === "exception")
761
+ o = a["always-on-top"].indexOf(e.name) > a["always-on-top"].indexOf(s.name);
762
+ else if (e.position === s.position)
763
+ o = O(e, s, a);
764
+ else {
765
+ let c = {
766
+ exception: 2,
767
+ first: 1,
768
+ ignore: 0,
769
+ last: -1
770
+ };
771
+ o = c[e.position] < c[s.position];
800
772
  }
773
+ o && r.report({
774
+ messageId: "unexpectedJSXPropsOrder",
775
+ data: {
776
+ first: e.name,
777
+ second: s.name
778
+ },
779
+ node: s.node,
780
+ fix: (c) => {
781
+ let f = Y(t, ({ position: y }) => y), g = (y) => y in f ? f[y] : [], m = [
782
+ g(
783
+ "exception"
784
+ /* exception */
785
+ ).sort(
786
+ (y, x) => a["always-on-top"].indexOf(y.name) - a["always-on-top"].indexOf(x.name)
787
+ ),
788
+ w(g(
789
+ "first"
790
+ /* first */
791
+ ), a),
792
+ w(g(
793
+ "ignore"
794
+ /* ignore */
795
+ ), a),
796
+ w(g(
797
+ "last"
798
+ /* last */
799
+ ), a)
800
+ ].flat();
801
+ return A(c, t, m, l);
802
+ }
803
+ });
801
804
  });
802
805
  });
803
- });
806
+ }
804
807
  }
805
808
  })
806
809
  }), B = "sort-map-elements", ce = I({
@@ -848,41 +851,44 @@ const z = "sort-jsx-props", de = I({
848
851
  ],
849
852
  create: (r) => ({
850
853
  NewExpression: (n) => {
851
- if (n.callee.type === f.Identifier && n.callee.name === "Map" && n.arguments[0].type === f.ArrayExpression) {
852
- let a = j(r.options.at(0), {
853
- type: i.alphabetical,
854
- "ignore-case": !1,
855
- order: d.asc
856
- }), [{ elements: l }] = n.arguments, p = r.getSourceCode();
857
- l.length > 1 && l.reduce(
858
- (e, s) => (s === null || s.type === f.SpreadElement ? e.push([]) : e.at(-1).push(s), e),
859
- [[]]
860
- ).forEach((e) => {
861
- let s = e.map((o) => {
862
- let c;
863
- if (o.type === f.ArrayExpression) {
864
- let [u] = o.elements;
865
- u ? u.type === f.Literal ? c = u.raw : c = p.text.slice(...u.range) : c = `${u}`;
866
- } else
867
- c = p.text.slice(...o.range);
868
- return {
869
- size: S(o.range),
870
- node: o,
871
- name: c
872
- };
873
- });
874
- N(s, (o, c) => {
875
- O(o, c, a) && r.report({
876
- messageId: "unexpectedMapElementsOrder",
877
- data: {
878
- first: E(o.name),
879
- second: E(c.name)
880
- },
881
- node: c.node,
882
- fix: (u) => A(u, s, w(s, a), p)
854
+ if (n.callee.type === u.Identifier && n.callee.name === "Map" && n.arguments[0].type === u.ArrayExpression) {
855
+ let [{ elements: a }] = n.arguments;
856
+ if (a.length > 1) {
857
+ let l = j(r.options.at(0), {
858
+ type: i.alphabetical,
859
+ "ignore-case": !1,
860
+ order: d.asc
861
+ }), p = r.getSourceCode();
862
+ a.reduce(
863
+ (e, s) => (s === null || s.type === u.SpreadElement ? e.push([]) : e.at(-1).push(s), e),
864
+ [[]]
865
+ ).forEach((e) => {
866
+ let s = e.map((o) => {
867
+ let c;
868
+ if (o.type === u.ArrayExpression) {
869
+ let [f] = o.elements;
870
+ f ? f.type === u.Literal ? c = f.raw : c = p.text.slice(...f.range) : c = `${f}`;
871
+ } else
872
+ c = p.text.slice(...o.range);
873
+ return {
874
+ size: S(o.range),
875
+ node: o,
876
+ name: c
877
+ };
878
+ });
879
+ N(s, (o, c) => {
880
+ O(o, c, l) && r.report({
881
+ messageId: "unexpectedMapElementsOrder",
882
+ data: {
883
+ first: E(o.name),
884
+ second: E(c.name)
885
+ },
886
+ node: c.node,
887
+ fix: (f) => A(f, s, w(s, l), p)
888
+ });
883
889
  });
884
890
  });
885
- });
891
+ }
886
892
  }
887
893
  }
888
894
  })
@@ -931,13 +937,12 @@ const z = "sort-jsx-props", de = I({
931
937
  ],
932
938
  create: (r) => ({
933
939
  ExportNamedDeclaration: (n) => {
934
- let a = j(r.options.at(0), {
935
- type: i.alphabetical,
936
- "ignore-case": !1,
937
- order: d.asc
938
- });
939
940
  if (n.specifiers.length > 1) {
940
- let l = r.getSourceCode(), p = n.specifiers.map((t) => ({
941
+ let a = j(r.options.at(0), {
942
+ type: i.alphabetical,
943
+ "ignore-case": !1,
944
+ order: d.asc
945
+ }), l = r.getSourceCode(), p = n.specifiers.map((t) => ({
941
946
  size: S(t.range),
942
947
  name: t.local.name,
943
948
  node: t
@@ -1001,13 +1006,12 @@ const z = "sort-jsx-props", de = I({
1001
1006
  ],
1002
1007
  create: (r) => ({
1003
1008
  ImportDeclaration: (n) => {
1004
- let a = j(r.options.at(0), {
1005
- type: i.alphabetical,
1006
- "ignore-case": !1,
1007
- order: d.asc
1008
- });
1009
1009
  if (n.specifiers.length > 1) {
1010
- let l = r.getSourceCode(), p = n.specifiers.map((t) => ({
1010
+ let a = j(r.options.at(0), {
1011
+ type: i.alphabetical,
1012
+ "ignore-case": !1,
1013
+ order: d.asc
1014
+ }), l = r.getSourceCode(), p = n.specifiers.map((t) => ({
1011
1015
  size: S(t.range),
1012
1016
  name: t.local.name,
1013
1017
  node: t
@@ -1077,20 +1081,21 @@ const z = "sort-jsx-props", de = I({
1077
1081
  "ignore-case": !1,
1078
1082
  order: d.asc
1079
1083
  }), l = r.getSourceCode(), p = n.members.map((t) => {
1080
- var s, o;
1081
- let e;
1082
- if (t.type === f.TSPropertySignature)
1083
- t.key.type === f.Identifier ? { name: e } = t.key : t.key.type === f.Literal ? e = `${t.key.value}` : e = l.text.slice(
1084
+ var f, g;
1085
+ let e, s = l.text.slice(t.range.at(0), t.range.at(1));
1086
+ if (t.type === u.TSPropertySignature)
1087
+ t.key.type === u.Identifier ? { name: e } = t.key : t.key.type === u.Literal ? e = `${t.key.value}` : e = l.text.slice(
1084
1088
  t.range.at(0),
1085
- (s = t.typeAnnotation) == null ? void 0 : s.range.at(0)
1089
+ (f = t.typeAnnotation) == null ? void 0 : f.range.at(0)
1086
1090
  );
1087
- else if (t.type === f.TSIndexSignature) {
1088
- let c = ((o = t.typeAnnotation) == null ? void 0 : o.range.at(0)) ?? t.range.at(1);
1089
- e = l.text.slice(t.range.at(0), c);
1091
+ else if (t.type === u.TSIndexSignature) {
1092
+ let m = ((g = t.typeAnnotation) == null ? void 0 : g.range.at(0)) ?? t.range.at(1);
1093
+ e = l.text.slice(t.range.at(0), m);
1090
1094
  } else
1091
1095
  e = l.text.slice(t.range.at(0), t.range.at(1));
1096
+ let c = s.endsWith(";") || s.endsWith(",") ? 1 : 0;
1092
1097
  return {
1093
- size: S(t.range),
1098
+ size: S(t.range) - c,
1094
1099
  node: t,
1095
1100
  name: e
1096
1101
  };
@@ -1167,17 +1172,17 @@ const z = "sort-jsx-props", de = I({
1167
1172
  }), l = r.getSourceCode();
1168
1173
  ((t) => t.reduce(
1169
1174
  (e, s) => {
1170
- if (s.type === f.SpreadElement)
1175
+ if (s.type === u.SpreadElement)
1171
1176
  return e.push([]), e;
1172
1177
  let o, c = "ignore";
1173
- s.key.type === f.Identifier ? { name: o } = s.key : s.key.type === f.Literal ? o = `${s.key.value}` : o = l.text.slice(...s.key.range), s.key.type === f.Identifier && a["always-on-top"].includes(s.key.name) && (c = "exception");
1174
- let u = {
1178
+ s.key.type === u.Identifier ? { name: o } = s.key : s.key.type === u.Literal ? o = `${s.key.value}` : o = l.text.slice(...s.key.range), s.key.type === u.Identifier && a["always-on-top"].includes(s.key.name) && (c = "exception");
1179
+ let f = {
1175
1180
  size: S(s.range),
1176
1181
  node: s,
1177
1182
  position: c,
1178
1183
  name: o
1179
1184
  };
1180
- return e.at(-1).push(u), e;
1185
+ return e.at(-1).push(f), e;
1181
1186
  },
1182
1187
  [[]]
1183
1188
  ))(n.properties).forEach((t) => {
@@ -1202,19 +1207,19 @@ const z = "sort-jsx-props", de = I({
1202
1207
  },
1203
1208
  node: s.node,
1204
1209
  fix: (c) => {
1205
- let u = q(t, ({ position: g }) => g), m = (g) => g in u ? u[g] : [], y = [
1206
- m(
1210
+ let f = Y(t, ({ position: y }) => y), g = (y) => y in f ? f[y] : [], m = [
1211
+ g(
1207
1212
  "exception"
1208
1213
  /* exception */
1209
1214
  ).sort(
1210
- (g, x) => a["always-on-top"].indexOf(g.name) - a["always-on-top"].indexOf(x.name)
1215
+ (y, x) => a["always-on-top"].indexOf(y.name) - a["always-on-top"].indexOf(x.name)
1211
1216
  ),
1212
- w(m(
1217
+ w(g(
1213
1218
  "ignore"
1214
1219
  /* ignore */
1215
1220
  ), a)
1216
1221
  ].flat();
1217
- return A(c, t, y, l);
1222
+ return A(c, t, m, l);
1218
1223
  }
1219
1224
  });
1220
1225
  });
@@ -1222,8 +1227,8 @@ const z = "sort-jsx-props", de = I({
1222
1227
  }
1223
1228
  }
1224
1229
  })
1225
- }), F = "sort-union-types", ye = I({
1226
- name: F,
1230
+ }), W = "sort-union-types", ye = I({
1231
+ name: W,
1227
1232
  meta: {
1228
1233
  type: "suggestion",
1229
1234
  docs: {
@@ -1352,7 +1357,7 @@ let v = (r) => {
1352
1357
  "always-on-top": []
1353
1358
  }
1354
1359
  ],
1355
- [F]: ["error"]
1360
+ [W]: ["error"]
1356
1361
  };
1357
1362
  return {
1358
1363
  plugins: ["perfectionist"],
@@ -1378,7 +1383,7 @@ const ke = {
1378
1383
  [J]: ue,
1379
1384
  [X]: ge,
1380
1385
  [G]: me,
1381
- [F]: ye
1386
+ [W]: ye
1382
1387
  },
1383
1388
  configs: {
1384
1389
  "recommended-alphabetical": v({
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "eslint-plugin-perfectionist",
3
3
  "description": "ESLint plugin for sorting various data such as objects, imports, types, enums, JSX props, etc.",
4
- "version": "0.11.1",
4
+ "version": "0.11.2",
5
5
  "homepage": "https://eslint-plugin-perfectionist.azat.io",
6
6
  "repository": "azat-io/eslint-plugin-perfectionist",
7
7
  "author": "Azat S. <to@azat.io>",