eslint-plugin-perfectionist
Advanced tools
Comparing version 1.4.0 to 1.5.0
@@ -1,3 +0,3 @@ | ||
"use strict";var ae=Object.defineProperty;var re=(a,o,r)=>o in a?ae(a,o,{enumerable:!0,configurable:!0,writable:!0,value:r}):a[o]=r;var Q=(a,o,r)=>(re(a,typeof o!="symbol"?o+"":o,r),r);const u=require("@typescript-eslint/types"),R=require("@typescript-eslint/utils"),se=require("natural-compare-lite"),k=require("minimatch"),Z=require("is-core-module"),ne=require("json5"),oe=require("path"),ie=require("fs");let P=R.ESLintUtils.RuleCreator(a=>`https://eslint-plugin-perfectionist.azat.io/rules/${a}`),_=a=>a.replaceAll(/\s\s+/g," ").trim(),w=a=>{let[o,r]=a;return r-o};var l=(a=>(a.alphabetical="alphabetical",a["line-length"]="line-length",a.natural="natural",a))(l||{}),c=(a=>(a.desc="desc",a.asc="asc",a))(c||{});let N=(a,o,r)=>{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 n=r.order==="asc"?1:-1,p,s=t=>r["ignore-case"]?t.toLowerCase():t;return r.type===l.alphabetical?p=(t,d)=>s(t.name).localeCompare(s(d.name)):r.type===l.natural?p=(t,d)=>se(s(t.name),s(d.name)):p=(t,d)=>t.size-d.size,n*p(a,o)>0},O=(a,o)=>[...a].sort((r,n)=>Number(N(r,n,o))||-1),le=(a,o)=>{let r=o.getTokenAfter(a,{filter:({type:n,value:p})=>!(n===u.AST_TOKEN_TYPES.Punctuator&&[",",";"].includes(p)),includeComments:!0});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.end.line===r.loc.end.line?r:null},F=(a,o)=>Array.isArray(a)&&a.some(r=>k.minimatch(o.trim(),r))||typeof a=="string"&&k.minimatch(o.trim(),a)||a===!0,L=(a,o)=>{let[r,n]=o.getTokensBefore(a,{filter:({type:p,value:s})=>!(p===u.AST_TOKEN_TYPES.Punctuator&&[",",";"].includes(s)),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)&&a.loc.start.line-n.loc.end.line<=1&&(r==null?void 0:r.loc.end.line)!==n.loc.start.line?n:null},Y=(a,o,r)=>{var i;let n=a.range.at(0),p=a.range.at(1),s=o.text.slice(n,p);if(R.ASTUtils.isParenthesized(a,o)){let t=o.getTokenBefore(a,R.ASTUtils.isOpeningParenToken),d=o.getTokenAfter(a,R.ASTUtils.isClosingParenToken);n=t.range.at(0),p=d.range.at(1)}let e=L(a,o);if(s.endsWith(";")||s.endsWith(",")){let t=o.getTokensAfter(a,{includeComments:!0,count:2});a.loc.start.line===((i=t.at(1))==null?void 0:i.loc.start.line)&&(p-=1)}return e&&!F((r==null?void 0:r.partitionComment)??!1,e.value)&&(n=e.range.at(0)),[n,p]},j=(a,o,r,n,p)=>{let s=[];return o.forEach(({node:e},i)=>{s.push(a.replaceTextRange(Y(e,n,p),n.text.slice(...Y(r[i].node,n,p))));let t=le(r[i].node,n);if(t){let g=[n.getTokenBefore(t).range.at(1),t.range.at(1)];s.push(a.replaceTextRange(g,""));let E=n.getTokenAfter(e);s.push(a.insertTextAfter((E==null?void 0:E.loc.end.line)===e.loc.end.line?E:e,n.text.slice(...g)))}}),s},D=(a={},o)=>Object.assign(o,a),I=(a,o)=>{if(a.length>1)for(let r=1;r<a.length;r++){let n=a.at(r-1),p=a.at(r);n&&p&&o(n,p,r-1)}};const $="sort-array-includes",pe=P({name:$,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:[c.asc,c.desc],default:c.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:c.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 r=o.object.type===u.AST_NODE_TYPES.ArrayExpression?o.object.elements:o.object.arguments;if(r.length>1){let n=D(a.options.at(0),{type:l.alphabetical,order:c.asc,"ignore-case":!1,"spread-last":!1}),p=a.getSourceCode(),s=r.reduce((e,i)=>(i!==null&&e.at(0).push({name:i.type===u.AST_NODE_TYPES.Literal?i.raw:p.text.slice(...i.range),size:w(i.range),type:i.type,node:i}),e),[[],[]]).flat();I(s,(e,i)=>{let t;n["spread-last"]&&e.node.type===u.AST_NODE_TYPES.Literal&&i.node.type===u.AST_NODE_TYPES.SpreadElement?t=!1:n["spread-last"]&&e.node.type===u.AST_NODE_TYPES.SpreadElement&&i.node.type===u.AST_NODE_TYPES.Literal?t=!0:t=N(e,i,n),t&&a.report({messageId:"unexpectedArrayIncludesOrder",data:{left:_(e.name),right:_(i.name)},node:i.node,fix:d=>{let g=O(s,n);return n["spread-last"]&&g.forEach((E,b)=>{E.node.type===u.AST_NODE_TYPES.SpreadElement&&g.push(g.splice(b,1).at(0))}),j(d,s,g,p)}})})}}}})}),M="sort-named-exports",ce=P({name:M,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:[c.asc,c.desc],default:c.asc},"ignore-case":{type:"boolean",default:!1}},additionalProperties:!1}],messages:{unexpectedNamedExportsOrder:'Expected "{{right}}" to come before "{{left}}"'}},defaultOptions:[{type:l.alphabetical,order:c.asc}],create:a=>({ExportNamedDeclaration:o=>{if(o.specifiers.length>1){let r=D(a.options.at(0),{type:l.alphabetical,"ignore-case":!1,order:c.asc}),n=a.getSourceCode(),p=o.specifiers.map(s=>({size:w(s.range),name:s.local.name,node:s}));I(p,(s,e)=>{N(s,e,r)&&a.report({messageId:"unexpectedNamedExportsOrder",data:{left:s.name,right:e.name},node:e.node,fix:i=>j(i,p,O(p,r),n)})})}}})}),B="sort-named-imports",de=P({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:[c.asc,c.desc],default:c.asc},"ignore-case":{type:"boolean",default:!1}},additionalProperties:!1}],messages:{unexpectedNamedImportsOrder:'Expected "{{right}}" to come before "{{left}}"'}},defaultOptions:[{type:l.alphabetical,order:c.asc}],create:a=>({ImportDeclaration:o=>{let r=o.specifiers.filter(({type:n})=>n===u.AST_NODE_TYPES.ImportSpecifier);if(r.length>1){let n=D(a.options.at(0),{type:l.alphabetical,"ignore-case":!1,order:c.asc}),p=a.getSourceCode(),s=r.map(e=>({size:w(e.range),name:e.local.name,node:e}));I(s,(e,i)=>{N(e,i,n)&&a.report({messageId:"unexpectedNamedImportsOrder",data:{left:e.name,right:i.name},node:i.node,fix:t=>j(t,s,O(s,n),p)})})}}})}),U="sort-object-types",fe=P({name:U,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:[c.asc,c.desc],default:c.asc},"ignore-case":{type:"boolean",default:!1}},additionalProperties:!1}],messages:{unexpectedObjectTypesOrder:'Expected "{{right}}" to come before "{{left}}"'}},defaultOptions:[{type:l.alphabetical,order:c.asc}],create:a=>({TSTypeLiteral:o=>{if(o.members.length>1){let r=D(a.options.at(0),{type:l.alphabetical,"ignore-case":!1,order:c.asc}),n=a.getSourceCode(),p=o.members.map(s=>{var E,b;let e,i=n.text.slice(s.range.at(0),s.range.at(1)),t=y=>y.replace(/(,|;)$/,"");if(s.type===u.AST_NODE_TYPES.TSPropertySignature)s.key.type===u.AST_NODE_TYPES.Identifier?{name:e}=s.key:s.key.type===u.AST_NODE_TYPES.Literal?e=`${s.key.value}`:e=n.text.slice(s.range.at(0),(E=s.typeAnnotation)==null?void 0:E.range.at(0));else if(s.type===u.AST_NODE_TYPES.TSIndexSignature){let y=((b=s.typeAnnotation)==null?void 0:b.range.at(0))??s.range.at(1);e=t(n.text.slice(s.range.at(0),y))}else e=t(n.text.slice(s.range.at(0),s.range.at(1)));let g=i.endsWith(";")||i.endsWith(",")?1:0;return{size:w(s.range)-g,node:s,name:e}});I(p,(s,e)=>{N(s,e,r)&&a.report({messageId:"unexpectedObjectTypesOrder",data:{left:_(s.name),right:_(e.name)},node:e.node,fix:i=>j(i,p,O(p,r),n)})})}}})}),z="sort-map-elements",ue=P({name:z,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:[c.asc,c.desc],default:c.asc},"ignore-case":{type:"boolean",default:!1}},additionalProperties:!1}],messages:{unexpectedMapElementsOrder:'Expected "{{right}}" to come before "{{left}}"'}},defaultOptions:[{type:l.alphabetical,order:c.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:r}]=o.arguments;if(r.length>1){let n=D(a.options.at(0),{type:l.alphabetical,"ignore-case":!1,order:c.asc}),p=a.getSourceCode();r.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 d;if(t.type===u.AST_NODE_TYPES.ArrayExpression){let[g]=t.elements;g?g.type===u.AST_NODE_TYPES.Literal?d=g.raw:d=p.text.slice(...g.range):d=`${g}`}else d=p.text.slice(...t.range);return{size:w(t.range),node:t,name:d}});I(i,(t,d)=>{N(t,d,n)&&a.report({messageId:"unexpectedMapElementsOrder",data:{left:_(t.name),right:_(d.name)},node:d.node,fix:g=>j(g,i,O(i,n),p)})})})}}}})}),q="sort-union-types",me=P({name:q,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:[c.asc,c.desc],default:c.asc},"ignore-case":{type:"boolean",default:!1}},additionalProperties:!1}],messages:{unexpectedUnionTypesOrder:'Expected "{{right}}" to come before "{{left}}"'}},defaultOptions:[{type:l.alphabetical,order:c.asc}],create:a=>({TSUnionType:o=>{let r=a.getSourceCode(),n=D(a.options.at(0),{type:l.alphabetical,"ignore-case":!1,order:c.asc}),p=o.types.map(s=>({name:r.text.slice(...s.range),size:w(s.range),node:s}));I(p,(s,e)=>{N(s,e,n)&&a.report({messageId:"unexpectedUnionTypesOrder",data:{left:_(s.name),right:_(e.name)},node:e.node,fix:i=>j(i,p,O(p,n),r)})})}})}),J="sort-interfaces",ge=P({name:J,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:[c.asc,c.desc],default:c.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:c.asc}],create:a=>({TSInterfaceDeclaration:o=>{if(o.body.body.length>1){let r=D(a.options.at(0),{type:l.alphabetical,"ignore-case":!1,order:c.asc,"ignore-pattern":[]});if(!r["ignore-pattern"].some(n=>k.minimatch(o.id.name,n))){let n=a.getSourceCode(),p=o.body.body.map(s=>{var i,t,d;let e;if(s.type===u.AST_NODE_TYPES.TSPropertySignature)if(s.key.type===u.AST_NODE_TYPES.Identifier)({name:e}=s.key);else if(s.key.type===u.AST_NODE_TYPES.Literal)e=`${s.key.value}`;else{let g=((i=s.typeAnnotation)==null?void 0:i.range.at(0))??s.range.at(1)-(s.optional?1:0);e=n.text.slice(s.range.at(0),g)}else if(s.type===u.AST_NODE_TYPES.TSIndexSignature){let g=((t=s.typeAnnotation)==null?void 0:t.range.at(0))??s.range.at(1);e=n.text.slice(s.range.at(0),g)}else{let g=((d=s.returnType)==null?void 0:d.range.at(0))??s.range.at(1);e=n.text.slice(s.range.at(0),g)}return{size:w(s.range),node:s,name:e}});I(p,(s,e)=>{N(s,e,r)&&a.report({messageId:"unexpectedInterfacePropertiesOrder",data:{left:_(s.name),right:_(e.name)},node:e.node,fix:i=>j(i,p,O(p,r),n)})})}}}})});let ee=(a,o)=>a.reduce((r,n)=>{let p=o(n);return p in r||(r[p]=[]),r[p].push(n),r},{});const G="sort-jsx-props",ye=P({name:G,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:[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 "{{right}}" to come before "{{left}}"'}},defaultOptions:[{type:l.alphabetical,order:c.asc}],create:a=>({JSXElement:o=>{if(o.openingElement.attributes.length>1){let r=D(a.options.at(0),{type:l.alphabetical,shorthand:"ignore",multiline:"ignore",callback:"ignore","always-on-top":[],"ignore-case":!1,order:c.asc}),n=a.getSourceCode();o.openingElement.attributes.reduce((s,e)=>{if(e.type===u.AST_NODE_TYPES.JSXSpreadAttribute)return s.push([]),s;let i="ignore";e.name.type===u.AST_NODE_TYPES.JSXIdentifier&&r["always-on-top"].includes(e.name.name)?i="exception":(r.shorthand!=="ignore"&&e.value===null&&(i=r.shorthand),r.callback!=="ignore"&&e.name.type===u.AST_NODE_TYPES.JSXIdentifier&&e.name.name.indexOf("on")===0&&e.value!==null?i=r.callback:r.multiline!=="ignore"&&e.loc.start.line!==e.loc.end.line&&(i=r.multiline));let t={name:e.name.type===u.AST_NODE_TYPES.JSXNamespacedName?`${e.name.namespace.name}:${e.name.name.name}`:e.name.name,size:w(e.range),node:e,position:i};return s.at(-1).push(t),s},[[]]).forEach(s=>{I(s,(e,i)=>{let t;if(e.position==="exception"&&i.position==="exception")t=r["always-on-top"].indexOf(e.name)>r["always-on-top"].indexOf(i.name);else if(e.position===i.position)t=N(e,i,r);else{let d={exception:2,first:1,ignore:0,last:-1};t=d[e.position]<d[i.position]}t&&a.report({messageId:"unexpectedJSXPropsOrder",data:{left:e.name,right:i.name},node:i.node,fix:d=>{let g=ee(s,({position:y})=>y),E=y=>y in g?g[y]:[],b=[E("exception").sort((y,f)=>r["always-on-top"].indexOf(y.name)-r["always-on-top"].indexOf(f.name)),O(E("first"),r),O(E("ignore"),r),O(E("last"),r)].flat();return j(d,s,b,n)}})})})}}})});class te{static get(){return this._instance?this._instance:(this._instance=ne.parse(ie.readFileSync(oe.resolve(process.cwd(),"tsconfig.json"),"utf8")),this._instance)}}Q(te,"_instance");const W="sort-imports",Ee=P({name:W,meta:{type:"suggestion",docs:{description:"enforce sorted imports",recommended:!1},fixable:"code",schema:[{type:"object",properties:{"custom-groups":{type:"object",properties:{type:{type:"object"},value:{type:"object"}},additionalProperties:!1},type:{enum:[l.alphabetical,l.natural,l["line-length"]],default:l.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 "{{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:c.asc}],create:a=>{var i;let o=D(a.options.at(0),{"newlines-between":"always","custom-groups":{type:{},value:{}},"internal-pattern":["~/**"],type:l.alphabetical,"read-tsconfig":!1,order:c.asc,"ignore-case":!1,groups:[]}),r=[];if(o["read-tsconfig"]){let t=te.get();(i=t.compilerOptions)!=null&&i.paths&&Object.keys(t.compilerOptions.paths).forEach(d=>{r.push(d)})}let n=a.getSourceCode(),p=[],s=t=>{let d,g=x=>[".less",".scss",".sass",".styl",".pcss",".css",".sss"].some(h=>x.endsWith(h)),E=x=>["./index.d.js","./index.d.ts","./index.js","./index.ts","./index","./","."].includes(x),b=x=>x.indexOf("..")===0,y=x=>x.indexOf("./")===0,f=x=>{!d&&o.groups.flat().includes(x)&&(d=x)},m=x=>o["internal-pattern"].length&&o["internal-pattern"].some(h=>k.minimatch(x.source.value,h))||r.some(h=>k.minimatch(x.source.value,h)),S=(x,h)=>{Object.entries(o["custom-groups"][x]??{}).forEach(([T,A])=>{Array.isArray(A)&&A.some(v=>k.minimatch(h,v))&&f(T),typeof A=="string"&&k.minimatch(h,A)&&f(T)})};return t.importKind==="type"&&(t.type===u.AST_NODE_TYPES.ImportDeclaration&&(S("type",t.source.value),Z(t.source.value)&&f("builtin-type"),m(t)&&f("internal-type"),E(t.source.value)&&f("index-type"),b(t.source.value)&&f("parent-type"),y(t.source.value)&&f("sibling-type")),f("type")),!d&&t.type===u.AST_NODE_TYPES.ImportDeclaration&&(S("value",t.source.value),Z(t.source.value)&&f("builtin"),m(t)&&f("internal"),g(t.source.value)&&f("style"),t.specifiers.length===0&&f("side-effect"),E(t.source.value)&&f("index"),b(t.source.value)&&f("parent"),y(t.source.value)&&f("sibling"),f("external")),d??"unknown"},e=t=>{let d;t.type===u.AST_NODE_TYPES.ImportDeclaration?d=t.source.value:t.moduleReference.type===u.AST_NODE_TYPES.TSExternalModuleReference&&t.moduleReference.expression.type===u.AST_NODE_TYPES.Literal?d=`${t.moduleReference.expression.value}`:d=n.text.slice(...t.moduleReference.range),p.push({size:w(t.range),group:s(t),name:d,node:t})};return{TSImportEqualsDeclaration:e,ImportDeclaration:e,"Program:exit":()=>{let t=y=>{for(let f=0,m=o.groups.length;f<m;f++){let S=o.groups[f];if(y.group===S||Array.isArray(S)&&S.includes(y.group))return f}return o.groups.length},d=(y,f)=>!!n.getTokensBetween(y.node,L(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(S=>!S.trim().length).length,E=(y,f)=>{let m=[],S=f.reduce((h,T)=>{let A=t(T);return A in h?h[A]=O([...h[A],T],o):h[A]=[T],h},{}),x=Object.keys(S).sort().reduce((h,T)=>[...h,...S[T]],[]);return x.forEach((h,T)=>{if(m.push(y.replaceTextRange(Y(f.at(T).node,n),n.text.slice(...Y(h.node,n)))),o["newlines-between"]!=="ignore"){let A=x.at(T+1);if(A){let v=g(f.at(T),f.at(T+1));(o["newlines-between"]==="always"&&t(h)===t(A)&&v!==0||o["newlines-between"]==="never"&&v>0)&&m.push(y.removeRange([Y(f.at(T).node,n).at(1),Y(f.at(T+1).node,n).at(0)-1])),o["newlines-between"]==="always"&&t(h)!==t(A)&&v>1&&m.push(y.replaceTextRange([Y(f.at(T).node,n).at(1),Y(f.at(T+1).node,n).at(0)-1],` | ||
`)),o["newlines-between"]==="always"&&t(h)!==t(A)&&v===0&&m.push(y.insertTextAfterRange(Y(f.at(T).node,n),` | ||
`))}}}),m};p.reduce((y,f)=>{var S;let m=(S=y.at(-1))==null?void 0:S.at(-1);return m&&d(m,f)?y.push([f]):y.at(-1).push(f),y},[[]]).forEach(y=>{I(y,(f,m)=>{let S=t(f),x=t(m),h=g(f,m);!d(f,m)&&(S>x||S===x&&N(f,m,o))&&a.report({messageId:"unexpectedImportsOrder",data:{left:f.name,right:m.name},node:m.node,fix:T=>E(T,y)}),o["newlines-between"]==="never"&&h>0&&a.report({messageId:"extraSpacingBetweenImports",data:{left:f.name,right:m.name},node:m.node,fix:T=>E(T,y)}),o["newlines-between"]==="always"&&(S<x&&h===0?a.report({messageId:"missedSpacingBetweenImports",data:{left:f.name,right:m.name},node:m.node,fix:T=>E(T,y)}):(h>1||S===x&&h>0)&&a.report({messageId:"extraSpacingBetweenImports",data:{left:f.name,right:m.name},node:m.node,fix:T=>E(T,y)}))})})}}}}),X="sort-classes",he=P({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:[c.asc,c.desc],default:c.asc},groups:{type:"array",default:["property","constructor","method","unknown"]}},additionalProperties:!1}],messages:{unexpectedClassesOrder:'Expected "{{right}}" to come before "{{left}}"'}},defaultOptions:[{type:l.alphabetical,order:c.asc}],create:a=>({ClassBody:o=>{if(o.body.length>1){let r=D(a.options.at(0),{type:l.alphabetical,order:c.asc,"ignore-case":!1,groups:["property","constructor","method","unknown"]}),n=a.getSourceCode(),p=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=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:t}=e.key:t=n.text.slice(...e.key.range);let d=E=>{!i&&r.groups.flat().includes(E)&&(i=E)};return e.type===u.AST_NODE_TYPES.MethodDefinition?(e.kind==="constructor"&&d("constructor"),e.static&&d("static-method"),e.accessibility==="private"&&d("private-method"),d("method")):e.type===u.AST_NODE_TYPES.PropertyDefinition&&(e.static&&d("static-property"),e.accessibility==="private"&&d("private-property"),d("property")),{size:w(e.range),group:i??"unknown",node:e,name:t}}),s=e=>{for(let i=0,t=r.groups.length;i<t;i++){let d=r.groups[i];if(e.group===d||Array.isArray(d)&&d.includes(e.group))return i}return r.groups.length};I(p,(e,i)=>{let t=s(e),d=s(i);(t>d||t===d&&N(e,i,r))&&a.report({messageId:"unexpectedClassesOrder",data:{left:_(e.name),right:_(i.name)},node:i.node,fix:g=>{let E=[],b=p.reduce((f,m)=>{let S=s(m);return S in f?f[S]=O([...f[S],m],r):f[S]=[m],f},{});return Object.keys(b).sort().reduce((f,m)=>[...f,...b[m]],[]).forEach((f,m)=>{E.push(g.replaceTextRange(Y(p.at(m).node,n),n.text.slice(...Y(f.node,n))))}),E}})})}}})}),K="sort-objects",Se=P({name:K,meta:{type:"suggestion",docs:{description:"enforce sorted objects",recommended:!1},fixable:"code",schema:[{type:"object",properties:{"partition-by-comment":{type:["boolean","string","array"],default:!1},type:{enum:[l.alphabetical,l.natural,l["line-length"]],default:l.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 "{{right}}" to come before "{{left}}"'}},defaultOptions:[{type:l.alphabetical,order:c.asc}],create:a=>{let o=r=>{if(r.properties.length>1){let n=D(a.options.at(0),{"partition-by-comment":!1,type:l.alphabetical,"ignore-case":!1,order:c.asc,"always-on-top":[]}),p=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 d=L(t,p);n["partition-by-comment"]&&d&&F(n["partition-by-comment"],d.value)&&i.push([]);let g,E="ignore",b=[];if(t.key.type===u.AST_NODE_TYPES.Identifier?{name:g}=t.key:t.key.type===u.AST_NODE_TYPES.Literal?g=`${t.key.value}`:g=p.text.slice(...t.key.range),t.key.type===u.AST_NODE_TYPES.Identifier&&n["always-on-top"].includes(t.key.name)&&(E="exception"),t.value.type===u.AST_NODE_TYPES.AssignmentPattern){let f=(m,S)=>{m.right.type===u.AST_NODE_TYPES.Identifier&&b.push(m.right.name),!S&&m.left.type===u.AST_NODE_TYPES.Identifier&&b.push(m.left.name);let x=h=>{h.right.type===u.AST_NODE_TYPES.Identifier&&b.push(h.right.name),h.left.type===u.AST_NODE_TYPES.BinaryExpression&&f(h.left,!1)};m.right.type===u.AST_NODE_TYPES.BinaryExpression&&x(m.right)};f(t.value,!0)}let y={size:w(t.range),dependencies:b,node:t,position:E,name:g};return i.at(-1).push(y),i},[[]]))(r.properties).forEach(e=>{I(e,(i,t)=>{let d;if(i.position==="exception"&&t.position==="exception")d=n["always-on-top"].indexOf(i.name)>n["always-on-top"].indexOf(t.name);else if(i.position===t.position)d=N(i,t,n);else{let g={exception:1,ignore:0};d=g[i.position]<g[t.position]}if(d){let g=E=>{let b=ee(e,({position:m})=>m),y=m=>m in b?b[m]:[],f=[y("exception").sort((m,S)=>n["always-on-top"].indexOf(m.name)-n["always-on-top"].indexOf(S.name)),O(y("ignore"),n)].flat();return j(E,e,f,p,{partitionComment:n["partition-by-comment"]})};a.report({messageId:"unexpectedObjectsOrder",data:{left:_(i.name),right:_(t.name)},node:t.node,fix:g})}})})}};return{ObjectExpression:o,ObjectPattern:o}}}),V="sort-exports",Te=P({name:V,meta:{type:"suggestion",docs:{description:"enforce sorted exports",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[l.alphabetical,l.natural,l["line-length"]],default:l.natural},order:{enum:[c.asc,c.desc],default:c.asc},"ignore-case":{type:"boolean",default:!1}},additionalProperties:!1}],messages:{unexpectedExportsOrder:'Expected "{{right}}" to come before "{{left}}"'}},defaultOptions:[{type:l.alphabetical,order:c.asc}],create:a=>{let o=D(a.options.at(0),{type:l.alphabetical,order:c.asc,"ignore-case":!1}),r=[[]],n=p=>{p.type===u.AST_NODE_TYPES.ExportAllDeclaration&&p.exported===null?r.push([]):r.at(-1).push({size:w(p.range),name:p.source.value,node:p})};return{ExportAllDeclaration:n,ExportNamedDeclaration:p=>{p.source!==null&&n(p)},"Program:exit":()=>{let p=a.getSourceCode();r.forEach(s=>{I(s,(e,i)=>{N(e,i,o)&&a.report({messageId:"unexpectedExportsOrder",data:{left:e.name,right:i.name},node:i.node,fix:t=>j(t,s,O(s,o),p)})})})}}}}),H="sort-enums",xe=P({name:H,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:[c.asc,c.desc],default:c.asc}},additionalProperties:!1}],messages:{unexpectedEnumsOrder:'Expected "{{right}}" to come before "{{left}}"'}},defaultOptions:[{type:l.alphabetical,order:c.asc}],create:a=>({TSEnumDeclaration:o=>{if(o.members.length>1&&o.members.some(({initializer:r})=>r)){let r=D(a.options.at(0),{type:l.alphabetical,order:c.asc,"ignore-case":!1}),n=a.getSourceCode(),p=o.members.map(s=>({name:s.id.type===u.AST_NODE_TYPES.Literal?`${s.id.value}`:`${n.text.slice(...s.id.range)}`,size:w(s.range),node:s}));I(p,(s,e)=>{N(s,e,r)&&a.report({messageId:"unexpectedEnumsOrder",data:{left:_(s.name),right:_(e.name)},node:e.node,fix:i=>j(i,p,O(p,r),n)})})}}})}),be="eslint-plugin-perfectionist";let C=a=>{let o={[W]:["error",{groups:["type",["builtin","external"],"internal-type","internal",["parent-type","sibling-type","index-type"],["parent","sibling","index"],"object","unknown"],"custom-groups":{value:{},type:{}},"newlines-between":"always","internal-pattern":["~/**"],"read-tsconfig":!1}],[X]:["error",{groups:["static-property","private-property","property","constructor","static-method","private-method","method","unknown"]}],[G]:["error",{"always-on-top":[],shorthand:"ignore",multiline:"first",callback:"ignore"}],[K]:["error",{"partition-by-comment":!1,"always-on-top":[]}],[$]:["error",{"spread-last":!0}],[M]:["error"],[B]:["error"],[z]:["error"],[U]:["error"],[J]:["error"],[q]:["error"],[V]:["error"],[H]:["error"]};return{rules:Object.fromEntries(Object.entries(o).map(([r,[n,p={}]])=>[`perfectionist/${r}`,[n,Object.assign(p,a)]])),plugins:["perfectionist"]}};const _e={rules:{[$]:pe,[X]:he,[H]:xe,[V]:Te,[W]:Ee,[J]:ge,[G]:ye,[z]:ue,[M]:ce,[B]:de,[U]:fe,[K]:Se,[q]:me},configs:{"recommended-alphabetical":C({type:l.alphabetical,order:c.asc,"ignore-case":!1}),"recommended-natural":C({type:l.natural,order:c.asc,"ignore-case":!1}),"recommended-line-length":C({type:l["line-length"],order:c.desc})},name:be};module.exports=_e; | ||
"use strict";var se=Object.defineProperty;var ne=(a,o,r)=>o in a?se(a,o,{enumerable:!0,configurable:!0,writable:!0,value:r}):a[o]=r;var F=(a,o,r)=>(ne(a,typeof o!="symbol"?o+"":o,r),r);const u=require("@typescript-eslint/types"),C=require("@typescript-eslint/utils"),oe=require("natural-compare-lite"),k=require("minimatch"),ee=require("is-core-module"),ie=require("json5"),le=require("path"),pe=require("fs");let P=C.ESLintUtils.RuleCreator(a=>`https://eslint-plugin-perfectionist.azat.io/rules/${a}`),O=a=>a.replaceAll(/\s\s+/g," ").trim(),w=a=>{let[o,r]=a;return r-o};var l=(a=>(a.alphabetical="alphabetical",a["line-length"]="line-length",a.natural="natural",a))(l||{}),d=(a=>(a.desc="desc",a.asc="asc",a))(d||{});let N=(a,o,r)=>{var e,n;if((e=o.dependencies)!=null&&e.includes(a.name))return!1;if((n=a.dependencies)!=null&&n.includes(o.name))return!0;let s=r.order==="asc"?1:-1,p,i=t=>r["ignore-case"]?t.toLowerCase():t;return r.type===l.alphabetical?p=(t,c)=>i(t.name).localeCompare(i(c.name)):r.type===l.natural?p=(t,c)=>oe(i(t.name),i(c.name)):p=(t,c)=>t.size-c.size,s*p(a,o)>0},A=(a,o)=>[...a].sort((r,s)=>Number(N(r,s,o))||-1),ce=(a,o)=>{let r=o.getTokenAfter(a,{filter:({value:s,type:p})=>!(p===u.AST_TOKEN_TYPES.Punctuator&&[",",";"].includes(s)),includeComments:!0});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.end.line===r.loc.end.line?r:null},te=(a,o)=>Array.isArray(a)&&a.some(r=>k.minimatch(o.trim(),r))||typeof a=="string"&&k.minimatch(o.trim(),a)||a===!0,$=(a,o)=>{let[r,s]=o.getTokensBefore(a,{filter:({value:p,type:i})=>!(i===u.AST_TOKEN_TYPES.Punctuator&&[",",";"].includes(p)),includeComments:!0,count:2});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.start.line-s.loc.end.line<=1&&(r==null?void 0:r.loc.end.line)!==s.loc.start.line?s:null},Y=(a,o,r)=>{var n;let s=a.range.at(0),p=a.range.at(1),i=o.text.slice(s,p);if(C.ASTUtils.isParenthesized(a,o)){let t=o.getTokenBefore(a,C.ASTUtils.isOpeningParenToken),c=o.getTokenAfter(a,C.ASTUtils.isClosingParenToken);s=t.range.at(0),p=c.range.at(1)}let e=$(a,o);if(i.endsWith(";")||i.endsWith(",")){let t=o.getTokensAfter(a,{includeComments:!0,count:2});a.loc.start.line===((n=t.at(1))==null?void 0:n.loc.start.line)&&(p-=1)}return e&&!te((r==null?void 0:r.partitionComment)??!1,e.value)&&(s=e.range.at(0)),[s,p]},j=(a,o,r,s,p)=>{let i=[];for(let e=0,n=o.length;e<n;e++){let{node:t}=o.at(e);i.push(a.replaceTextRange(Y(t,s,p),s.text.slice(...Y(r.at(e).node,s,p))));let c=ce(r.at(e).node,s);if(c){let y=[s.getTokenBefore(c).range.at(1),c.range.at(1)];i.push(a.replaceTextRange(y,""));let h=s.getTokenAfter(t);i.push(a.insertTextAfter((h==null?void 0:h.loc.end.line)===t.loc.end.line?h:t,s.text.slice(...y)))}}return i},D=(a={},o)=>Object.assign(o,a),I=(a,o)=>{if(a.length>1)for(let r=1;r<a.length;r++){let s=a.at(r-1),p=a.at(r);s&&p&&o(s,p,r-1)}};const B="sort-array-includes",de=P({name:B,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:[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:l.alphabetical,order:d.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 r=o.object.type===u.AST_NODE_TYPES.ArrayExpression?o.object.elements:o.object.arguments;if(r.length>1){let s=D(a.options.at(0),{type:l.alphabetical,order:d.asc,"ignore-case":!1,"spread-last":!1}),p=a.getSourceCode(),i=r.reduce((e,n)=>(n!==null&&e.at(0).push({name:n.type===u.AST_NODE_TYPES.Literal?n.raw:p.text.slice(...n.range),size:w(n.range),type:n.type,node:n}),e),[[],[]]).flat();I(i,(e,n)=>{let t;s["spread-last"]&&e.node.type===u.AST_NODE_TYPES.Literal&&n.node.type===u.AST_NODE_TYPES.SpreadElement?t=!1:s["spread-last"]&&e.node.type===u.AST_NODE_TYPES.SpreadElement&&n.node.type===u.AST_NODE_TYPES.Literal?t=!0:t=N(e,n,s),t&&a.report({messageId:"unexpectedArrayIncludesOrder",data:{left:O(e.name),right:O(n.name)},node:n.node,fix:c=>{let g=A(i,s);if(s["spread-last"])for(let y=0,h=g.length;y<h;y++)g.at(y).node.type===u.AST_NODE_TYPES.SpreadElement&&g.push(g.splice(y,1).at(0));return j(c,i,g,p)}})})}}}})}),U="sort-named-exports",fe=P({name:U,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:[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:l.alphabetical,order:d.asc}],create:a=>({ExportNamedDeclaration:o=>{if(o.specifiers.length>1){let r=D(a.options.at(0),{type:l.alphabetical,"ignore-case":!1,order:d.asc}),s=a.getSourceCode(),p=o.specifiers.map(i=>({size:w(i.range),name:i.local.name,node:i}));I(p,(i,e)=>{N(i,e,r)&&a.report({messageId:"unexpectedNamedExportsOrder",data:{left:i.name,right:e.name},node:e.node,fix:n=>j(n,p,A(p,r),s)})})}}})}),z="sort-named-imports",ue=P({name:z,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:[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:l.alphabetical,order:d.asc}],create:a=>({ImportDeclaration:o=>{let r=o.specifiers.filter(({type:s})=>s===u.AST_NODE_TYPES.ImportSpecifier);if(r.length>1){let s=D(a.options.at(0),{type:l.alphabetical,"ignore-case":!1,order:d.asc}),p=a.getSourceCode(),i=r.map(e=>({size:w(e.range),name:e.local.name,node:e}));I(i,(e,n)=>{N(e,n,s)&&a.report({messageId:"unexpectedNamedImportsOrder",data:{left:e.name,right:n.name},node:n.node,fix:t=>j(t,i,A(i,s),p)})})}}})}),q="sort-object-types",me=P({name:q,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:[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:l.alphabetical,order:d.asc}],create:a=>({TSTypeLiteral:o=>{if(o.members.length>1){let r=D(a.options.at(0),{type:l.alphabetical,"ignore-case":!1,order:d.asc}),s=a.getSourceCode(),p=o.members.map(i=>{var y,h;let e,n=s.text.slice(i.range.at(0),i.range.at(1)),t=x=>x.replace(/(,|;)$/,"");if(i.type===u.AST_NODE_TYPES.TSPropertySignature)i.key.type===u.AST_NODE_TYPES.Identifier?{name:e}=i.key:i.key.type===u.AST_NODE_TYPES.Literal?e=`${i.key.value}`:e=s.text.slice(i.range.at(0),(y=i.typeAnnotation)==null?void 0:y.range.at(0));else if(i.type===u.AST_NODE_TYPES.TSIndexSignature){let x=((h=i.typeAnnotation)==null?void 0:h.range.at(0))??i.range.at(1);e=t(s.text.slice(i.range.at(0),x))}else e=t(s.text.slice(i.range.at(0),i.range.at(1)));let g=n.endsWith(";")||n.endsWith(",")?1:0;return{size:w(i.range)-g,node:i,name:e}});I(p,(i,e)=>{N(i,e,r)&&a.report({messageId:"unexpectedObjectTypesOrder",data:{left:O(i.name),right:O(e.name)},node:e.node,fix:n=>j(n,p,A(p,r),s)})})}}})}),J="sort-map-elements",ge=P({name:J,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:[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:l.alphabetical,order:d.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:r}]=o.arguments;if(r.length>1){let s=D(a.options.at(0),{type:l.alphabetical,"ignore-case":!1,order:d.asc}),p=a.getSourceCode(),i=r.reduce((e,n)=>(n===null||n.type===u.AST_NODE_TYPES.SpreadElement?e.push([]):e.at(-1).push(n),e),[[]]);for(let e of i){let n=e.map(t=>{let c;if(t.type===u.AST_NODE_TYPES.ArrayExpression){let[g]=t.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(...t.range);return{size:w(t.range),node:t,name:c}});I(n,(t,c)=>{N(t,c,s)&&a.report({messageId:"unexpectedMapElementsOrder",data:{left:O(t.name),right:O(c.name)},node:c.node,fix:g=>j(g,n,A(n,s),p)})})}}}}})}),G="sort-union-types",ye=P({name:G,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:[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:l.alphabetical,order:d.asc}],create:a=>({TSUnionType:o=>{let r=a.getSourceCode(),s=D(a.options.at(0),{type:l.alphabetical,"ignore-case":!1,order:d.asc}),p=o.types.map(i=>({name:r.text.slice(...i.range),size:w(i.range),node:i}));I(p,(i,e)=>{N(i,e,s)&&a.report({messageId:"unexpectedUnionTypesOrder",data:{left:O(i.name),right:O(e.name)},node:e.node,fix:n=>j(n,p,A(p,s),r)})})}})}),W="sort-interfaces",Ee=P({name:W,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:[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:l.alphabetical,order:d.asc}],create:a=>({TSInterfaceDeclaration:o=>{if(o.body.body.length>1){let r=D(a.options.at(0),{type:l.alphabetical,"ignore-case":!1,order:d.asc,"ignore-pattern":[]});if(!r["ignore-pattern"].some(s=>k.minimatch(o.id.name,s))){let s=a.getSourceCode(),p=o.body.body.reduce((i,e)=>{var t,c,g;if(e.type===u.AST_NODE_TYPES.TSCallSignatureDeclaration)return i.push([]),i;let n;if(e.type===u.AST_NODE_TYPES.TSPropertySignature)if(e.key.type===u.AST_NODE_TYPES.Identifier)({name:n}=e.key);else if(e.key.type===u.AST_NODE_TYPES.Literal)n=`${e.key.value}`;else{let y=((t=e.typeAnnotation)==null?void 0:t.range.at(0))??e.range.at(1)-(e.optional?1:0);n=s.text.slice(e.range.at(0),y)}else if(e.type===u.AST_NODE_TYPES.TSIndexSignature){let y=((c=e.typeAnnotation)==null?void 0:c.range.at(0))??e.range.at(1);n=s.text.slice(e.range.at(0),y)}else{let y=((g=e.returnType)==null?void 0:g.range.at(0))??e.range.at(1);n=s.text.slice(e.range.at(0),y)}return i.at(-1).push({size:w(e.range),node:e,name:n}),i},[[]]);for(let i of p)I(i,(e,n)=>{N(e,n,r)&&a.report({messageId:"unexpectedInterfacePropertiesOrder",data:{left:O(e.name),right:O(n.name)},node:n.node,fix:t=>j(t,i,A(i,r),s)})})}}}})});let ae=(a,o)=>a.reduce((r,s)=>{let p=o(s);return p in r||(r[p]=[]),r[p].push(s),r},{});const X="sort-jsx-props",Se=P({name:X,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:[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:l.alphabetical,order:d.asc}],create:a=>({JSXElement:o=>{if(o.openingElement.attributes.length>1){let r=D(a.options.at(0),{type:l.alphabetical,shorthand:"ignore",multiline:"ignore",callback:"ignore","always-on-top":[],"ignore-case":!1,order:d.asc}),s=a.getSourceCode(),p=o.openingElement.attributes.reduce((i,e)=>{if(e.type===u.AST_NODE_TYPES.JSXSpreadAttribute)return i.push([]),i;let n="ignore";e.name.type===u.AST_NODE_TYPES.JSXIdentifier&&r["always-on-top"].includes(e.name.name)?n="exception":(r.shorthand!=="ignore"&&e.value===null&&(n=r.shorthand),r.callback!=="ignore"&&e.name.type===u.AST_NODE_TYPES.JSXIdentifier&&e.name.name.indexOf("on")===0&&e.value!==null?n=r.callback:r.multiline!=="ignore"&&e.loc.start.line!==e.loc.end.line&&(n=r.multiline));let t={name:e.name.type===u.AST_NODE_TYPES.JSXNamespacedName?`${e.name.namespace.name}:${e.name.name.name}`:e.name.name,size:w(e.range),node:e,position:n};return i.at(-1).push(t),i},[[]]);for(let i of p)I(i,(e,n)=>{let t;if(e.position==="exception"&&n.position==="exception")t=r["always-on-top"].indexOf(e.name)>r["always-on-top"].indexOf(n.name);else if(e.position===n.position)t=N(e,n,r);else{let c={exception:2,first:1,ignore:0,last:-1};t=c[e.position]<c[n.position]}t&&a.report({messageId:"unexpectedJSXPropsOrder",data:{left:e.name,right:n.name},node:n.node,fix:c=>{let g=ae(i,({position:x})=>x),y=x=>x in g?g[x]:[],h=[y("exception").sort((x,f)=>r["always-on-top"].indexOf(x.name)-r["always-on-top"].indexOf(f.name)),A(y("first"),r),A(y("ignore"),r),A(y("last"),r)].flat();return j(c,i,h,s)}})})}}})});class re{static get(){return this._instance?this._instance:(this._instance=ie.parse(pe.readFileSync(le.resolve(process.cwd(),"tsconfig.json"),"utf8")),this._instance)}}F(re,"_instance");const K="sort-imports",he=P({name:K,meta:{type:"suggestion",docs:{description:"enforce sorted imports",recommended:!1},fixable:"code",schema:[{type:"object",properties:{"custom-groups":{type:"object",properties:{type:{type:"object"},value:{type:"object"}},additionalProperties:!1},type:{enum:[l.alphabetical,l.natural,l["line-length"]],default:l.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:l.alphabetical,order:d.asc}],create:a=>{var n;let o=D(a.options.at(0),{"newlines-between":"always","custom-groups":{type:{},value:{}},"internal-pattern":["~/**"],type:l.alphabetical,"read-tsconfig":!1,order:d.asc,"ignore-case":!1,groups:[]}),r=[];if(o["read-tsconfig"]){let t=re.get();if((n=t.compilerOptions)!=null&&n.paths)for(let c of Object.keys(t.compilerOptions.paths))r.push(c)}let s=a.getSourceCode(),p=[],i=t=>{let c,g=E=>[".less",".scss",".sass",".styl",".pcss",".css",".sss"].some(b=>E.endsWith(b)),y=E=>["./index.d.js","./index.d.ts","./index.js","./index.ts","./index","./","."].includes(E),h=E=>E.indexOf("..")===0,x=E=>E.indexOf("./")===0,f=E=>{!c&&o.groups.flat().includes(E)&&(c=E)},m=E=>o["internal-pattern"].length&&o["internal-pattern"].some(b=>k.minimatch(E.source.value,b))||r.some(b=>k.minimatch(E.source.value,b)),S=(E,b)=>{for(let[T,_]of Object.entries(o["custom-groups"][E]??{}))Array.isArray(_)&&_.some(v=>k.minimatch(b,v))&&f(T),typeof _=="string"&&k.minimatch(b,_)&&f(T)};return t.importKind==="type"&&(t.type===u.AST_NODE_TYPES.ImportDeclaration&&(S("type",t.source.value),ee(t.source.value)&&f("builtin-type"),m(t)&&f("internal-type"),y(t.source.value)&&f("index-type"),h(t.source.value)&&f("parent-type"),x(t.source.value)&&f("sibling-type")),f("external-type"),f("type")),!c&&t.type===u.AST_NODE_TYPES.ImportDeclaration&&(S("value",t.source.value),ee(t.source.value)&&f("builtin"),m(t)&&f("internal"),g(t.source.value)&&f("style"),t.specifiers.length===0&&f("side-effect"),y(t.source.value)&&f("index"),h(t.source.value)&&f("parent"),x(t.source.value)&&f("sibling"),f("external")),c??"unknown"},e=t=>{let c;t.type===u.AST_NODE_TYPES.ImportDeclaration?c=t.source.value:t.moduleReference.type===u.AST_NODE_TYPES.TSExternalModuleReference&&t.moduleReference.expression.type===u.AST_NODE_TYPES.Literal?c=`${t.moduleReference.expression.value}`:c=s.text.slice(...t.moduleReference.range),p.push({size:w(t.range),group:i(t),name:c,node:t})};return{TSImportEqualsDeclaration:e,ImportDeclaration:e,"Program:exit":()=>{var x;let t=f=>{for(let m=0,S=o.groups.length;m<S;m++){let E=o.groups[m];if(f.group===E||Array.isArray(E)&&E.includes(f.group))return m}return o.groups.length},c=(f,m)=>!!s.getTokensBetween(f.node,$(m.node,s)||m.node,{includeComments:!0}).length,g=(f,m)=>s.lines.slice(f.node.loc.end.line,m.node.loc.start.line-1).filter(E=>!E.trim().length).length,y=(f,m)=>{let S=[],E={};for(let T of m){let _=t(T);_ in E?E[_]=A([...E[_],T],o):E[_]=[T]}let b=Object.keys(E).sort().reduce((T,_)=>[...T,...E[_]],[]);for(let T=0,_=b.length;T<_;T++){let v=b.at(T);if(S.push(f.replaceTextRange(Y(m.at(T).node,s),s.text.slice(...Y(v.node,s)))),o["newlines-between"]!=="ignore"){let L=b.at(T+1);if(L){let R=g(m.at(T),m.at(T+1));(o["newlines-between"]==="always"&&t(v)===t(L)&&R!==0||o["newlines-between"]==="never"&&R>0)&&S.push(f.removeRange([Y(m.at(T).node,s).at(1),Y(m.at(T+1).node,s).at(0)-1])),o["newlines-between"]==="always"&&t(v)!==t(L)&&R>1&&S.push(f.replaceTextRange([Y(m.at(T).node,s).at(1),Y(m.at(T+1).node,s).at(0)-1],` | ||
`)),o["newlines-between"]==="always"&&t(v)!==t(L)&&R===0&&S.push(f.insertTextAfterRange(Y(m.at(T).node,s),` | ||
`))}}}return S},h=[[]];for(let f of p){let m=(x=h.at(-1))==null?void 0:x.at(-1);m&&c(m,f)?h.push([f]):h.at(-1).push(f)}for(let f of h)I(f,(m,S)=>{let E=t(m),b=t(S),T=g(m,S);!c(m,S)&&(E>b||E===b&&N(m,S,o))&&a.report({messageId:"unexpectedImportsOrder",data:{left:m.name,right:S.name},node:S.node,fix:_=>y(_,f)}),o["newlines-between"]==="never"&&T>0&&a.report({messageId:"extraSpacingBetweenImports",data:{left:m.name,right:S.name},node:S.node,fix:_=>y(_,f)}),o["newlines-between"]==="always"&&(E<b&&T===0?a.report({messageId:"missedSpacingBetweenImports",data:{left:m.name,right:S.name},node:S.node,fix:_=>y(_,f)}):(T>1||E===b&&T>0)&&a.report({messageId:"extraSpacingBetweenImports",data:{left:m.name,right:S.name},node:S.node,fix:_=>y(_,f)}))})}}}}),V="sort-classes",Te=P({name:V,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:[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:l.alphabetical,order:d.asc}],create:a=>({ClassBody:o=>{if(o.body.length>1){let r=D(a.options.at(0),{type:l.alphabetical,order:d.asc,"ignore-case":!1,groups:["property","constructor","method","unknown"]}),s=a.getSourceCode(),p=o.body.map(e=>{var g;let n,t;e.type===u.AST_NODE_TYPES.StaticBlock?t="static":e.type===u.AST_NODE_TYPES.TSIndexSignature?t=s.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=s.text.slice(...e.key.range);let c=y=>{!n&&r.groups.flat().includes(y)&&(n=y)};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:w(e.range),group:n??"unknown",node:e,name:t}}),i=e=>{for(let n=0,t=r.groups.length;n<t;n++){let c=r.groups[n];if(e.group===c||Array.isArray(c)&&c.includes(e.group))return n}return r.groups.length};I(p,(e,n)=>{let t=i(e),c=i(n);(t>c||t===c&&N(e,n,r))&&a.report({messageId:"unexpectedClassesOrder",data:{left:O(e.name),right:O(n.name)},node:n.node,fix:g=>{let y=[],h=p.reduce((f,m)=>{let S=i(m);return S in f?f[S]=A([...f[S],m],r):f[S]=[m],f},{}),x=Object.keys(h).sort().reduce((f,m)=>[...f,...h[m]],[]);for(let f=0,m=x.length;f<m;f++)y.push(g.replaceTextRange(Y(p.at(f).node,s),s.text.slice(...Y(x.at(f).node,s))));return y}})})}}})}),H="sort-objects",xe=P({name:H,meta:{type:"suggestion",docs:{description:"enforce sorted objects",recommended:!1},fixable:"code",schema:[{type:"object",properties:{"partition-by-comment":{type:["boolean","string","array"],default:!1},type:{enum:[l.alphabetical,l.natural,l["line-length"]],default:l.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:l.alphabetical,order:d.asc}],create:a=>{let o=r=>{if(r.properties.length>1){let s=D(a.options.at(0),{"partition-by-comment":!1,type:l.alphabetical,"ignore-case":!1,order:d.asc,"always-on-top":[]}),p=a.getSourceCode(),i=e=>e.reduce((n,t)=>{if(t.type===u.AST_NODE_TYPES.SpreadElement||t.type===u.AST_NODE_TYPES.RestElement)return n.push([]),n;let c=$(t,p);s["partition-by-comment"]&&c&&te(s["partition-by-comment"],c.value)&&n.push([]);let g,y="ignore",h=[];if(t.key.type===u.AST_NODE_TYPES.Identifier?{name:g}=t.key:t.key.type===u.AST_NODE_TYPES.Literal?g=`${t.key.value}`:g=p.text.slice(...t.key.range),t.key.type===u.AST_NODE_TYPES.Identifier&&s["always-on-top"].includes(t.key.name)&&(y="exception"),t.value.type===u.AST_NODE_TYPES.AssignmentPattern){let f=(m,S)=>{m.right.type===u.AST_NODE_TYPES.Identifier&&h.push(m.right.name),!S&&m.left.type===u.AST_NODE_TYPES.Identifier&&h.push(m.left.name);let E=b=>{b.right.type===u.AST_NODE_TYPES.Identifier&&h.push(b.right.name),b.left.type===u.AST_NODE_TYPES.BinaryExpression&&f(b.left,!1)};m.right.type===u.AST_NODE_TYPES.BinaryExpression&&E(m.right)};f(t.value,!0)}let x={size:w(t.range),dependencies:h,node:t,position:y,name:g};return n.at(-1).push(x),n},[[]]);for(let e of i(r.properties))I(e,(n,t)=>{let c;if(n.position==="exception"&&t.position==="exception")c=s["always-on-top"].indexOf(n.name)>s["always-on-top"].indexOf(t.name);else if(n.position===t.position)c=N(n,t,s);else{let g={exception:1,ignore:0};c=g[n.position]<g[t.position]}if(c){let g=y=>{let h=ae(e,({position:m})=>m),x=m=>m in h?h[m]:[],f=[x("exception").sort((m,S)=>s["always-on-top"].indexOf(m.name)-s["always-on-top"].indexOf(S.name)),A(x("ignore"),s)].flat();return j(y,e,f,p,{partitionComment:s["partition-by-comment"]})};a.report({messageId:"unexpectedObjectsOrder",data:{left:O(n.name),right:O(t.name)},node:t.node,fix:g})}})}};return{ObjectExpression:o,ObjectPattern:o}}}),Q="sort-exports",be=P({name:Q,meta:{type:"suggestion",docs:{description:"enforce sorted exports",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[l.alphabetical,l.natural,l["line-length"]],default:l.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:l.alphabetical,order:d.asc}],create:a=>{let o=D(a.options.at(0),{type:l.alphabetical,order:d.asc,"ignore-case":!1}),r=[[]],s=p=>{p.type===u.AST_NODE_TYPES.ExportAllDeclaration&&p.exported===null?r.push([]):r.at(-1).push({size:w(p.range),name:p.source.value,node:p})};return{ExportAllDeclaration:s,ExportNamedDeclaration:p=>{p.source!==null&&s(p)},"Program:exit":()=>{let p=a.getSourceCode();for(let i of r)I(i,(e,n)=>{N(e,n,o)&&a.report({messageId:"unexpectedExportsOrder",data:{left:e.name,right:n.name},node:n.node,fix:t=>j(t,i,A(i,o),p)})})}}}}),Z="sort-enums",_e=P({name:Z,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:[d.asc,d.desc],default:d.asc}},additionalProperties:!1}],messages:{unexpectedEnumsOrder:'Expected "{{right}}" to come before "{{left}}"'}},defaultOptions:[{type:l.alphabetical,order:d.asc}],create:a=>({TSEnumDeclaration:o=>{if(o.members.length>1&&o.members.some(({initializer:r})=>r)){let r=D(a.options.at(0),{type:l.alphabetical,order:d.asc,"ignore-case":!1}),s=a.getSourceCode(),p=o.members.map(i=>({name:i.id.type===u.AST_NODE_TYPES.Literal?`${i.id.value}`:`${s.text.slice(...i.id.range)}`,size:w(i.range),node:i}));I(p,(i,e)=>{N(i,e,r)&&a.report({messageId:"unexpectedEnumsOrder",data:{left:O(i.name),right:O(e.name)},node:e.node,fix:n=>j(n,p,A(p,r),s)})})}}})}),Oe="eslint-plugin-perfectionist";let M=a=>{let o={[K]:["error",{groups:["type",["builtin","external"],"internal-type","internal",["parent-type","sibling-type","index-type"],["parent","sibling","index"],"object","unknown"],"custom-groups":{value:{},type:{}},"newlines-between":"always","internal-pattern":["~/**"],"read-tsconfig":!1}],[V]:["error",{groups:["static-property","private-property","property","constructor","static-method","private-method","method","unknown"]}],[X]:["error",{"always-on-top":[],shorthand:"ignore",multiline:"first",callback:"ignore"}],[H]:["error",{"partition-by-comment":!1,"always-on-top":[]}],[B]:["error",{"spread-last":!0}],[U]:["error"],[z]:["error"],[J]:["error"],[q]:["error"],[W]:["error"],[G]:["error"],[Q]:["error"],[Z]:["error"]};return{rules:Object.fromEntries(Object.entries(o).map(([r,[s,p={}]])=>[`perfectionist/${r}`,[s,Object.assign(p,a)]])),plugins:["perfectionist"]}};const Ae={rules:{[B]:de,[V]:Te,[Z]:_e,[Q]:be,[K]:he,[W]:Ee,[X]:Se,[J]:ge,[U]:fe,[z]:ue,[q]:me,[H]:xe,[G]:ye},configs:{"recommended-alphabetical":M({type:l.alphabetical,order:d.asc,"ignore-case":!1}),"recommended-natural":M({type:l.natural,order:d.asc,"ignore-case":!1}),"recommended-line-length":M({type:l["line-length"],order:d.desc})},name:Oe};module.exports=Ae; |
{ | ||
"name": "eslint-plugin-perfectionist", | ||
"description": "ESLint plugin for sorting various data such as objects, imports, types, enums, JSX props, etc.", | ||
"version": "1.4.0", | ||
"version": "1.5.0", | ||
"homepage": "https://eslint-plugin-perfectionist.azat.io", | ||
@@ -44,9 +44,9 @@ "repository": "https://github.com/azat-io/eslint-plugin-perfectionist", | ||
"dependencies": { | ||
"@typescript-eslint/types": "^5.59.11", | ||
"@typescript-eslint/utils": "^5.59.11", | ||
"@typescript-eslint/types": "^5.62.0", | ||
"@typescript-eslint/utils": "^5.62.0", | ||
"is-core-module": "^2.12.1", | ||
"json5": "^2.2.3", | ||
"minimatch": "^9.0.1", | ||
"minimatch": "^9.0.3", | ||
"natural-compare-lite": "^1.4.0" | ||
} | ||
} |
@@ -16,3 +16,3 @@ # ESLint Plugin Perfectionist | ||
This plugin defines rules for sorting various data, such as objects, imports, TypeScript types, enums, JSX props, etc. | ||
This plugin defines rules for sorting various data, such as objects, imports, TypeScript types, enums, JSX props, etc. alphabetically, naturally, or by line length | ||
@@ -160,2 +160,17 @@ All rules are automatically fixable. It's safe! | ||
## ⁉️ FAQ | ||
### Can I automatically fix problems in the editor? | ||
Yes. To do this, you need to enable autofix in ESLint when you save the file in your editor. Instructions for your editor can be found [here](https://eslint-plugin-perfectionist.azat.io/guide/integrations). | ||
### Is it safety? | ||
On the whole, yes. We are very careful to make sure that the work of the plugin does not negatively affect the work of the code. For example, the plugin takes into account spread operators in JSX and objects, comments to the code, exports with `*`. Safety is our priority. If you encounter any problem, you can create an [issue](https://github.com/azat-io/eslint-plugin-perfectionist/issues/new/choose). | ||
### Why not Prettier? | ||
I love Prettier. However, this is not his area of responsibility. Prettier is used for formatting, and ESLint is also used for styling. For example, changing the order of imports can affect how the code works (console.log calls, fetch, style loading). Prettier should not change the AST. There is a cool article about this: ["The Blurry Line Between Formatting and Style"](https://blog.joshuakgoldberg.com/the-blurry-line-between-formatting-and-style) by **@joshuakgoldberg**. | ||
## ⚠️ Troubleshooting | ||
@@ -162,0 +177,0 @@ |
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
87098
1780
271
Updatedminimatch@^9.0.3