eslint-plugin-perfectionist 1.5.1 → 2.0.0
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/configs/recommended-alphabetical.js +1 -1
- package/dist/configs/recommended-alphabetical.mjs +8 -11
- package/dist/configs/recommended-line-length.js +1 -1
- package/dist/configs/recommended-line-length.mjs +8 -11
- package/dist/configs/recommended-natural.js +1 -1
- package/dist/configs/recommended-natural.mjs +5 -8
- package/dist/index.js +3 -3
- package/dist/index.mjs +1293 -934
- package/package.json +21 -6
- package/readme.md +19 -17
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";const e=require("../index.js");require("
|
|
1
|
+
"use strict";const e=require("../index.js");require("path");require("@typescript-eslint/utils");require("minimatch");require("natural-compare-lite");require("module");const r={rules:e.configs["recommended-alphabetical"].rules,plugins:{perfectionist:e}};module.exports=r;
|
|
@@ -1,18 +1,15 @@
|
|
|
1
|
-
import
|
|
2
|
-
import "
|
|
1
|
+
import e from "../index.mjs";
|
|
2
|
+
import "path";
|
|
3
3
|
import "@typescript-eslint/utils";
|
|
4
|
-
import "natural-compare-lite";
|
|
5
4
|
import "minimatch";
|
|
6
|
-
import "
|
|
7
|
-
import "
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
const s = {
|
|
11
|
-
rules: o.configs["recommended-alphabetical"].rules,
|
|
5
|
+
import "natural-compare-lite";
|
|
6
|
+
import "module";
|
|
7
|
+
const l = {
|
|
8
|
+
rules: e.configs["recommended-alphabetical"].rules,
|
|
12
9
|
plugins: {
|
|
13
|
-
perfectionist:
|
|
10
|
+
perfectionist: e
|
|
14
11
|
}
|
|
15
12
|
};
|
|
16
13
|
export {
|
|
17
|
-
|
|
14
|
+
l as default
|
|
18
15
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";const e=require("../index.js");require("
|
|
1
|
+
"use strict";const e=require("../index.js");require("path");require("@typescript-eslint/utils");require("minimatch");require("natural-compare-lite");require("module");const r={rules:e.configs["recommended-line-length"].rules,plugins:{perfectionist:e}};module.exports=r;
|
|
@@ -1,18 +1,15 @@
|
|
|
1
|
-
import
|
|
2
|
-
import "
|
|
1
|
+
import e from "../index.mjs";
|
|
2
|
+
import "path";
|
|
3
3
|
import "@typescript-eslint/utils";
|
|
4
|
-
import "natural-compare-lite";
|
|
5
4
|
import "minimatch";
|
|
6
|
-
import "
|
|
7
|
-
import "
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
const l = {
|
|
11
|
-
rules: o.configs["recommended-line-length"].rules,
|
|
5
|
+
import "natural-compare-lite";
|
|
6
|
+
import "module";
|
|
7
|
+
const p = {
|
|
8
|
+
rules: e.configs["recommended-line-length"].rules,
|
|
12
9
|
plugins: {
|
|
13
|
-
perfectionist:
|
|
10
|
+
perfectionist: e
|
|
14
11
|
}
|
|
15
12
|
};
|
|
16
13
|
export {
|
|
17
|
-
|
|
14
|
+
p as default
|
|
18
15
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";const e=require("../index.js");require("
|
|
1
|
+
"use strict";const e=require("../index.js");require("path");require("@typescript-eslint/utils");require("minimatch");require("natural-compare-lite");require("module");const r={rules:e.configs["recommended-natural"].rules,plugins:{perfectionist:e}};module.exports=r;
|
|
@@ -1,18 +1,15 @@
|
|
|
1
1
|
import r from "../index.mjs";
|
|
2
|
-
import "
|
|
2
|
+
import "path";
|
|
3
3
|
import "@typescript-eslint/utils";
|
|
4
|
-
import "natural-compare-lite";
|
|
5
4
|
import "minimatch";
|
|
6
|
-
import "
|
|
7
|
-
import "
|
|
8
|
-
|
|
9
|
-
import "fs";
|
|
10
|
-
const d = {
|
|
5
|
+
import "natural-compare-lite";
|
|
6
|
+
import "module";
|
|
7
|
+
const n = {
|
|
11
8
|
rules: r.configs["recommended-natural"].rules,
|
|
12
9
|
plugins: {
|
|
13
10
|
perfectionist: r
|
|
14
11
|
}
|
|
15
12
|
};
|
|
16
13
|
export {
|
|
17
|
-
|
|
14
|
+
n as default
|
|
18
15
|
};
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
"use strict";var ne=Object.defineProperty;var oe=(t,n,a)=>n in t?ne(t,n,{enumerable:!0,configurable:!0,writable:!0,value:a}):t[n]=a;var ee=(t,n,a)=>(oe(t,typeof n!="symbol"?n+"":n,a),a);const u=require("@typescript-eslint/types"),M=require("@typescript-eslint/utils"),ie=require("natural-compare-lite"),v=require("minimatch"),te=require("is-core-module"),le=require("json5"),pe=require("path"),ce=require("fs");let w=M.ESLintUtils.RuleCreator(t=>`https://eslint-plugin-perfectionist.azat.io/rules/${t}`),A=t=>t.replaceAll(/\s\s+/g," ").trim(),D=t=>{let[n,a]=t;return a-n};var p=(t=>(t.alphabetical="alphabetical",t["line-length"]="line-length",t.natural="natural",t))(p||{}),d=(t=>(t.desc="desc",t.asc="asc",t))(d||{});let P=(t,n,a)=>{var e,s;if((e=n.dependencies)!=null&&e.includes(t.name))return!1;if((s=t.dependencies)!=null&&s.includes(n.name))return!0;let r=a.order==="asc"?1:-1,c,o=l=>a["ignore-case"]?l.toLowerCase():l;return a.type===p.alphabetical?c=(l,i)=>o(l.name).localeCompare(o(i.name)):a.type===p.natural?c=(l,i)=>ie(o(l.name),o(i.name)):c=(l,i)=>l.size-i.size,r*c(t,n)>0},N=(t,n)=>[...t].sort((a,r)=>Number(P(a,r,n))||-1),de=(t,n)=>{let a=n.getTokenAfter(t,{filter:({value:r,type:c})=>!(c===u.AST_TOKEN_TYPES.Punctuator&&[",",";"].includes(r)),includeComments:!0});return((a==null?void 0:a.type)===u.AST_TOKEN_TYPES.Block||(a==null?void 0:a.type)===u.AST_TOKEN_TYPES.Line)&&t.loc.end.line===a.loc.end.line?a:null},ae=(t,n)=>Array.isArray(t)&&t.some(a=>v.minimatch(n.trim(),a))||typeof t=="string"&&v.minimatch(n.trim(),t)||t===!0,B=(t,n)=>{let[a,r]=n.getTokensBefore(t,{filter:({value:c,type:o})=>!(o===u.AST_TOKEN_TYPES.Punctuator&&[",",";"].includes(c)),includeComments:!0,count:2});return((r==null?void 0:r.type)===u.AST_TOKEN_TYPES.Block||(r==null?void 0:r.type)===u.AST_TOKEN_TYPES.Line)&&t.loc.start.line-r.loc.end.line<=1&&(a==null?void 0:a.loc.end.line)!==r.loc.start.line?r:null},j=(t,n,a)=>{var s;let r=t.range.at(0),c=t.range.at(1),o=n.text.slice(r,c);if(M.ASTUtils.isParenthesized(t,n)){let l=n.getTokenBefore(t,M.ASTUtils.isOpeningParenToken),i=n.getTokenAfter(t,M.ASTUtils.isClosingParenToken);r=l.range.at(0),c=i.range.at(1)}let e=B(t,n);if(o.endsWith(";")||o.endsWith(",")){let l=n.getTokensAfter(t,{includeComments:!0,count:2});t.loc.start.line===((s=l.at(1))==null?void 0:s.loc.start.line)&&(c-=1)}return e&&!ae((a==null?void 0:a.partitionComment)??!1,e.value)&&(r=e.range.at(0)),[r,c]},k=(t,n,a,r,c)=>{let o=[];for(let e=0,s=n.length;e<s;e++){let{node:l}=n.at(e);o.push(t.replaceTextRange(j(l,r,c),r.text.slice(...j(a.at(e).node,r,c))));let i=de(a.at(e).node,r);if(i){let E=[r.getTokenBefore(i).range.at(1),i.range.at(1)];o.push(t.replaceTextRange(E,""));let h=r.getTokenAfter(l);o.push(t.insertTextAfter((h==null?void 0:h.loc.end.line)===l.loc.end.line?h:l,r.text.slice(...E)))}}return o},I=(t={},n)=>Object.assign(n,t),Y=(t,n)=>{if(t.length>1)for(let a=1;a<t.length;a++){let r=t.at(a-1),c=t.at(a);r&&c&&n(r,c,a-1)}};const U="sort-array-includes",fe=w({name:U,meta:{type:"suggestion",docs:{description:"enforce sorted arrays before include method",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[p.alphabetical,p.natural,p["line-length"]],default:p.alphabetical},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:p.alphabetical,order:d.asc}],create:t=>({MemberExpression:n=>{if((n.object.type===u.AST_NODE_TYPES.ArrayExpression||n.object.type===u.AST_NODE_TYPES.NewExpression)&&n.property.type===u.AST_NODE_TYPES.Identifier&&n.property.name==="includes"){let a=n.object.type===u.AST_NODE_TYPES.ArrayExpression?n.object.elements:n.object.arguments;if(a.length>1){let r=I(t.options.at(0),{type:p.alphabetical,order:d.asc,"ignore-case":!1,"spread-last":!1}),c=t.getSourceCode(),o=a.reduce((e,s)=>(s!==null&&e.at(0).push({name:s.type===u.AST_NODE_TYPES.Literal?s.raw:c.text.slice(...s.range),size:D(s.range),type:s.type,node:s}),e),[[],[]]).flat();Y(o,(e,s)=>{let l;r["spread-last"]&&e.node.type===u.AST_NODE_TYPES.Literal&&s.node.type===u.AST_NODE_TYPES.SpreadElement?l=!1:r["spread-last"]&&e.node.type===u.AST_NODE_TYPES.SpreadElement&&s.node.type===u.AST_NODE_TYPES.Literal?l=!0:l=P(e,s,r),l&&t.report({messageId:"unexpectedArrayIncludesOrder",data:{left:A(e.name),right:A(s.name)},node:s.node,fix:i=>{let m=N(o,r);if(r["spread-last"])for(let E=0,h=m.length;E<h;E++)m.at(E).node.type===u.AST_NODE_TYPES.SpreadElement&&m.push(m.splice(E,1).at(0));return k(i,o,m,c)}})})}}}})}),z="sort-named-exports",ue=w({name:z,meta:{type:"suggestion",docs:{description:"enforce sorted named exports",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[p.alphabetical,p.natural,p["line-length"]],default:p.alphabetical},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:p.alphabetical,order:d.asc}],create:t=>({ExportNamedDeclaration:n=>{if(n.specifiers.length>1){let a=I(t.options.at(0),{type:p.alphabetical,"ignore-case":!1,order:d.asc}),r=t.getSourceCode(),c=n.specifiers.map(o=>({size:D(o.range),name:o.local.name,node:o}));Y(c,(o,e)=>{P(o,e,a)&&t.report({messageId:"unexpectedNamedExportsOrder",data:{left:o.name,right:e.name},node:e.node,fix:s=>k(s,c,N(c,a),r)})})}}})}),q="sort-named-imports",me=w({name:q,meta:{type:"suggestion",docs:{description:"enforce sorted named imports",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[p.alphabetical,p.natural,p["line-length"]],default:p.alphabetical},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:p.alphabetical,order:d.asc}],create:t=>({ImportDeclaration:n=>{let a=n.specifiers.filter(({type:r})=>r===u.AST_NODE_TYPES.ImportSpecifier);if(a.length>1){let r=I(t.options.at(0),{type:p.alphabetical,"ignore-case":!1,order:d.asc}),c=t.getSourceCode(),o=a.map(e=>({size:D(e.range),name:e.local.name,node:e}));Y(o,(e,s)=>{P(e,s,r)&&t.report({messageId:"unexpectedNamedImportsOrder",data:{left:e.name,right:s.name},node:s.node,fix:l=>k(l,o,N(o,r),c)})})}}})}),J="sort-object-types",ge=w({name:J,meta:{type:"suggestion",docs:{description:"enforce sorted object types",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[p.alphabetical,p.natural,p["line-length"]],default:p.alphabetical},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:p.alphabetical,order:d.asc}],create:t=>({TSTypeLiteral:n=>{if(n.members.length>1){let a=I(t.options.at(0),{type:p.alphabetical,"ignore-case":!1,order:d.asc}),r=t.getSourceCode(),c=n.members.map(o=>{var E,h;let e,s=r.text.slice(o.range.at(0),o.range.at(1)),l=T=>T.replace(/(,|;)$/,"");if(o.type===u.AST_NODE_TYPES.TSPropertySignature)o.key.type===u.AST_NODE_TYPES.Identifier?{name:e}=o.key:o.key.type===u.AST_NODE_TYPES.Literal?e=`${o.key.value}`:e=r.text.slice(o.range.at(0),(E=o.typeAnnotation)==null?void 0:E.range.at(0));else if(o.type===u.AST_NODE_TYPES.TSIndexSignature){let T=((h=o.typeAnnotation)==null?void 0:h.range.at(0))??o.range.at(1);e=l(r.text.slice(o.range.at(0),T))}else e=l(r.text.slice(o.range.at(0),o.range.at(1)));let m=s.endsWith(";")||s.endsWith(",")?1:0;return{size:D(o.range)-m,node:o,name:e}});Y(c,(o,e)=>{P(o,e,a)&&t.report({messageId:"unexpectedObjectTypesOrder",data:{left:A(o.name),right:A(e.name)},node:e.node,fix:s=>k(s,c,N(c,a),r)})})}}})}),G="sort-map-elements",ye=w({name:G,meta:{type:"suggestion",docs:{description:"enforce sorted Map elements",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[p.alphabetical,p.natural,p["line-length"]],default:p.alphabetical},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:p.alphabetical,order:d.asc}],create:t=>({NewExpression:n=>{if(n.callee.type===u.AST_NODE_TYPES.Identifier&&n.callee.name==="Map"&&n.arguments.length&&n.arguments[0].type===u.AST_NODE_TYPES.ArrayExpression){let[{elements:a}]=n.arguments;if(a.length>1){let r=I(t.options.at(0),{type:p.alphabetical,"ignore-case":!1,order:d.asc}),c=t.getSourceCode(),o=a.reduce((e,s)=>(s===null||s.type===u.AST_NODE_TYPES.SpreadElement?e.push([]):e.at(-1).push(s),e),[[]]);for(let e of o){let s=e.map(l=>{let i;if(l.type===u.AST_NODE_TYPES.ArrayExpression){let[m]=l.elements;m?m.type===u.AST_NODE_TYPES.Literal?i=m.raw:i=c.text.slice(...m.range):i=`${m}`}else i=c.text.slice(...l.range);return{size:D(l.range),node:l,name:i}});Y(s,(l,i)=>{P(l,i,r)&&t.report({messageId:"unexpectedMapElementsOrder",data:{left:A(l.name),right:A(i.name)},node:i.node,fix:m=>k(m,s,N(s,r),c)})})}}}}})}),W="sort-union-types",Ee=w({name:W,meta:{type:"suggestion",docs:{description:"enforce sorted union types",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[p.alphabetical,p.natural,p["line-length"]],default:p.alphabetical},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:p.alphabetical,order:d.asc}],create:t=>({TSUnionType:n=>{let a=t.getSourceCode(),r=I(t.options.at(0),{type:p.alphabetical,"ignore-case":!1,order:d.asc}),c=n.types.map(o=>({name:a.text.slice(...o.range),size:D(o.range),node:o}));Y(c,(o,e)=>{P(o,e,r)&&t.report({messageId:"unexpectedUnionTypesOrder",data:{left:A(o.name),right:A(e.name)},node:e.node,fix:s=>k(s,c,N(c,r),a)})})}})}),X="sort-interfaces",he=w({name:X,meta:{type:"suggestion",docs:{description:"enforce sorted interface properties",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[p.alphabetical,p.natural,p["line-length"]],default:p.alphabetical},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:p.alphabetical,order:d.asc}],create:t=>({TSInterfaceDeclaration:n=>{if(n.body.body.length>1){let a=I(t.options.at(0),{type:p.alphabetical,"ignore-case":!1,order:d.asc,"ignore-pattern":[]});if(!a["ignore-pattern"].some(r=>v.minimatch(n.id.name,r))){let r=t.getSourceCode(),c=n.body.body.reduce((o,e)=>{var l,i,m;if(e.type===u.AST_NODE_TYPES.TSCallSignatureDeclaration)return o.push([]),o;let s;if(e.type===u.AST_NODE_TYPES.TSPropertySignature)if(e.key.type===u.AST_NODE_TYPES.Identifier)({name:s}=e.key);else if(e.key.type===u.AST_NODE_TYPES.Literal)s=`${e.key.value}`;else{let E=((l=e.typeAnnotation)==null?void 0:l.range.at(0))??e.range.at(1)-(e.optional?1:0);s=r.text.slice(e.range.at(0),E)}else if(e.type===u.AST_NODE_TYPES.TSIndexSignature){let E=((i=e.typeAnnotation)==null?void 0:i.range.at(0))??e.range.at(1);s=r.text.slice(e.range.at(0),E)}else{let E=((m=e.returnType)==null?void 0:m.range.at(0))??e.range.at(1);s=r.text.slice(e.range.at(0),E)}return o.at(-1).push({size:D(e.range),node:e,name:s}),o},[[]]);for(let o of c)Y(o,(e,s)=>{P(e,s,a)&&t.report({messageId:"unexpectedInterfacePropertiesOrder",data:{left:A(e.name),right:A(s.name)},node:s.node,fix:l=>k(l,o,N(o,a),r)})})}}}})});let re=(t,n)=>t.reduce((a,r)=>{let c=n(r);return c in a||(a[c]=[]),a[c].push(r),a},{});const K="sort-jsx-props",Se=w({name:K,meta:{type:"suggestion",docs:{description:"enforce sorted JSX props",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[p.alphabetical,p.natural,p["line-length"]],default:p.alphabetical},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:p.alphabetical,order:d.asc}],create:t=>({JSXElement:n=>{if(n.openingElement.attributes.length>1){let a=I(t.options.at(0),{type:p.alphabetical,shorthand:"ignore",multiline:"ignore",callback:"ignore","always-on-top":[],"ignore-case":!1,order:d.asc}),r=t.getSourceCode(),c=n.openingElement.attributes.reduce((o,e)=>{if(e.type===u.AST_NODE_TYPES.JSXSpreadAttribute)return o.push([]),o;let s="ignore";e.name.type===u.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===u.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 l={name:e.name.type===u.AST_NODE_TYPES.JSXNamespacedName?`${e.name.namespace.name}:${e.name.name.name}`:e.name.name,size:D(e.range),node:e,position:s};return o.at(-1).push(l),o},[[]]);for(let o of c)Y(o,(e,s)=>{let l;if(e.position==="exception"&&s.position==="exception")l=a["always-on-top"].indexOf(e.name)>a["always-on-top"].indexOf(s.name);else if(e.position===s.position)l=P(e,s,a);else{let i={exception:2,first:1,ignore:0,last:-1};l=i[e.position]<i[s.position]}l&&t.report({messageId:"unexpectedJSXPropsOrder",data:{left:e.name,right:s.name},node:s.node,fix:i=>{let m=re(o,({position:T})=>T),E=T=>T in m?m[T]:[],h=[E("exception").sort((T,x)=>a["always-on-top"].indexOf(T.name)-a["always-on-top"].indexOf(x.name)),N(E("first"),a),N(E("ignore"),a),N(E("last"),a)].flat();return k(i,o,h,r)}})})}}})});class se{static get(){return this._instance?this._instance:(this._instance=le.parse(ce.readFileSync(pe.resolve(process.cwd(),"tsconfig.json"),"utf8")),this._instance)}}ee(se,"_instance");const V="sort-imports",Te=w({name:V,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:[p.alphabetical,p.natural,p["line-length"]],default:p.alphabetical},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:p.alphabetical,order:d.asc}],create:t=>{var l;let n=I(t.options.at(0),{"newlines-between":"always","custom-groups":{type:{},value:{}},"internal-pattern":["~/**"],type:p.alphabetical,"read-tsconfig":!1,order:d.asc,"ignore-case":!1,groups:[]}),a=[];if(n["read-tsconfig"]){let i=se.get();if((l=i.compilerOptions)!=null&&l.paths)for(let m of Object.keys(i.compilerOptions.paths))a.push(m)}let r=t.getSourceCode(),c=[],o=i=>i.type===u.AST_NODE_TYPES.ImportDeclaration&&i.specifiers.length===0,e=i=>{let m,E=y=>[".less",".scss",".sass",".styl",".pcss",".css",".sss"].some(O=>y.endsWith(O)),h=y=>["./index.d.js","./index.d.ts","./index.js","./index.ts","./index","./","."].includes(y),T=y=>y.indexOf("..")===0,x=y=>y.indexOf("./")===0,f=y=>{!m&&n.groups.flat().includes(y)&&(m=y)},g=y=>n["internal-pattern"].length&&n["internal-pattern"].some(O=>v.minimatch(y.source.value,O))||a.some(O=>v.minimatch(y.source.value,O)),S=(y,O)=>{for(let[b,_]of Object.entries(n["custom-groups"][y]??{}))Array.isArray(_)&&_.some(L=>v.minimatch(O,L))&&f(b),typeof _=="string"&&v.minimatch(O,_)&&f(b)};return i.importKind==="type"&&(i.type===u.AST_NODE_TYPES.ImportDeclaration&&(S("type",i.source.value),te(i.source.value)&&f("builtin-type"),g(i)&&f("internal-type"),h(i.source.value)&&f("index-type"),T(i.source.value)&&f("parent-type"),x(i.source.value)&&f("sibling-type")),f("external-type"),f("type")),!m&&i.type===u.AST_NODE_TYPES.ImportDeclaration&&(S("value",i.source.value),te(i.source.value)&&f("builtin"),g(i)&&f("internal"),E(i.source.value)&&f("style"),o(i)&&f("side-effect"),h(i.source.value)&&f("index"),T(i.source.value)&&f("parent"),x(i.source.value)&&f("sibling"),f("external")),m??"unknown"},s=i=>{let m;i.type===u.AST_NODE_TYPES.ImportDeclaration?m=i.source.value:i.moduleReference.type===u.AST_NODE_TYPES.TSExternalModuleReference&&i.moduleReference.expression.type===u.AST_NODE_TYPES.Literal?m=`${i.moduleReference.expression.value}`:m=r.text.slice(...i.moduleReference.range),c.push({size:D(i.range),group:e(i),name:m,node:i})};return{TSImportEqualsDeclaration:s,ImportDeclaration:s,"Program:exit":()=>{var x;let i=f=>{for(let g=0,S=n.groups.length;g<S;g++){let y=n.groups[g];if(f.group===y||Array.isArray(y)&&y.includes(f.group))return g}return n.groups.length},m=(f,g)=>!!r.getTokensBetween(f.node,B(g.node,r)||g.node,{includeComments:!0}).length,E=(f,g)=>r.lines.slice(f.node.loc.end.line,g.node.loc.start.line-1).filter(y=>!y.trim().length).length,h=(f,g)=>{let S=[],y={};for(let b of g){let _=i(b);_ in y?y[_]=N([...y[_],b],n):y[_]=[b]}let O=Object.keys(y).sort().reduce((b,_)=>[...b,...y[_]],[]);for(let b=0,_=O.length;b<_;b++){let L=O.at(b);if(S.push(f.replaceTextRange(j(g.at(b).node,r),r.text.slice(...j(L.node,r)))),n["newlines-between"]!=="ignore"){let R=O.at(b+1);if(R){let C=E(g.at(b),g.at(b+1));(n["newlines-between"]==="always"&&i(L)===i(R)&&C!==0||n["newlines-between"]==="never"&&C>0)&&S.push(f.removeRange([j(g.at(b).node,r).at(1),j(g.at(b+1).node,r).at(0)-1])),n["newlines-between"]==="always"&&i(L)!==i(R)&&C>1&&S.push(f.replaceTextRange([j(g.at(b).node,r).at(1),j(g.at(b+1).node,r).at(0)-1],`
|
|
2
|
-
`)),
|
|
3
|
-
`))}}}return
|
|
1
|
+
"use strict";const D=require("path"),B=require("@typescript-eslint/utils"),$=require("minimatch"),le=require("natural-compare-lite"),oe=require("module");let v=B.ESLintUtils.RuleCreator(s=>`https://eslint-plugin-perfectionist.azat.io/rules/${s}`),x=(s,l)=>{for(let n=0,a=s.length;n<a;n++){let u=s[n];if(l.group===u||Array.isArray(u)&&typeof l.group=="string"&&u.includes(l.group))return n}return s.length},w=s=>{let[l,n]=s;return n-l},j=s=>s>0;var i=(s=>(s.alphabetical="alphabetical",s["line-length"]="line-length",s.natural="natural",s))(i||{}),f=(s=>(s.desc="desc",s.asc="asc",s))(f||{});let G=s=>{let l,n=u=>{!l&&s.flat().includes(u)&&(l=u)};return{getGroup:()=>l??"unknown",setCustomGroups:(u,t)=>{if(u)for(let[e,o]of Object.entries(u))Array.isArray(o)&&o.some(r=>$.minimatch(t,r))&&n(e),typeof o=="string"&&$.minimatch(t,o)&&n(e)},defineGroup:n}},I=(s,l,n)=>{var e,o;if((e=l.dependencies)!=null&&e.includes(s.name))return-1;if((o=s.dependencies)!=null&&o.includes(l.name))return 1;let a=n.order==="asc"?1:-1,u,t=r=>n["ignore-case"]?r.toLowerCase():r;return n.type===i.alphabetical?u=(r,c)=>t(r.name).localeCompare(t(c.name)):n.type===i.natural?u=(r,c)=>le(t(r.name),t(c.name)):u=(r,c)=>r.size-c.size,a*u(s,l)},A=(s,l)=>[...s].sort((n,a)=>I(n,a,l)),ie=(s,l)=>{let n=l.getTokenAfter(s,{filter:({value:a,type:u})=>!(u==="Punctuator"&&[",",";"].includes(a)),includeComments:!0});return((n==null?void 0:n.type)==="Block"||(n==null?void 0:n.type)==="Line")&&s.loc.end.line===n.loc.end.line?n:null},ne=(s,l)=>Array.isArray(s)&&s.some(n=>$.minimatch(l.trim(),n))||typeof s=="string"&&$.minimatch(l.trim(),s)||s===!0,_=(s,l)=>{let[n,a]=l.getTokensBefore(s,{filter:({value:u,type:t})=>!(t==="Punctuator"&&[",",";"].includes(u)),includeComments:!0,count:2});return((a==null?void 0:a.type)==="Block"||(a==null?void 0:a.type)==="Line")&&s.loc.start.line-a.loc.end.line<=1&&(n==null?void 0:n.loc.end.line)!==a.loc.start.line?a:null},L=(s,l,n)=>{var o;let a=s.range.at(0),u=s.range.at(1),t=l.text.slice(a,u);if(B.ASTUtils.isParenthesized(s,l)){let r=l.getTokenBefore(s,B.ASTUtils.isOpeningParenToken),c=l.getTokenAfter(s,B.ASTUtils.isClosingParenToken);a=r.range.at(0),u=c.range.at(1)}let e=_(s,l);if(t.endsWith(";")||t.endsWith(",")){let r=l.getTokensAfter(s,{includeComments:!0,count:2});s.loc.start.line===((o=r.at(1))==null?void 0:o.loc.start.line)&&(u-=1)}return e&&!ne((n==null?void 0:n.partitionComment)??!1,e.value)&&(a=e.range.at(0)),[a,u]},P=(s,l,n,a,u)=>{var o,r;let t=[],e=((o=l.at(0))==null?void 0:o.node.loc.start.line)===((r=l.at(-1))==null?void 0:r.node.loc.end.line);for(let c=0,d=l.length;c<d;c++){let{node:y}=l.at(c);t.push(s.replaceTextRange(L(y,a,u),a.text.slice(...L(n.at(c).node,a,u))));let m=ie(n.at(c).node,a);if(m&&!e){let p=[a.getTokenBefore(m).range.at(1),m.range.at(1)];t.push(s.replaceTextRange(p,""));let h=a.getTokenAfter(y);t.push(s.insertTextAfter((h==null?void 0:h.loc.end.line)===y.loc.end.line?h:y,a.text.slice(...p)))}}return t},k=(s={},l)=>Object.assign(l,s),C=(s,l)=>{if(s.length>1)for(let n=1;n<s.length;n++){let a=s.at(n-1),u=s.at(n);a&&u&&l(a,u,n-1)}};const V="sort-svelte-attributes",pe=v({name:V,meta:{type:"suggestion",docs:{description:"enforce sorted Svelte attributes"},fixable:"code",schema:[{type:"object",properties:{"custom-groups":{type:"object"},type:{enum:[i.alphabetical,i.natural,i["line-length"]],default:i.alphabetical,type:"string"},order:{enum:[f.asc,f.desc],default:f.asc,type:"string"},"ignore-case":{type:"boolean",default:!1},groups:{type:"array"}},additionalProperties:!1}],messages:{unexpectedSvelteAttributesOrder:'Expected "{{right}}" to come before "{{left}}"'}},defaultOptions:[{type:i.alphabetical,order:f.asc}],create:s=>D.extname(s.getFilename())!==".svelte"?{}:{SvelteStartTag:l=>{if(l.attributes.length>1){let n=k(s.options.at(0),{type:i.alphabetical,order:f.asc,"ignore-case":!1,"custom-groups":{},groups:[]}),a=s.getSourceCode(),u=l.attributes.reduce((t,e)=>{if(e.type==="SvelteSpreadAttribute")return t.push([]),t;let o,{getGroup:r,defineGroup:c,setCustomGroups:d}=G(n.groups);return e.key.type==="SvelteSpecialDirectiveKey"?o=a.text.slice(...e.key.range):typeof e.key.name=="string"?{name:o}=e.key:o=a.text.slice(...e.key.range),d(n["custom-groups"],o),e.type==="SvelteShorthandAttribute"&&(c("svelte-shorthand"),c("shorthand")),(!("value"in e)||Array.isArray(e.value)&&!e.value.at(0))&&c("shorthand"),e.loc.start.line!==e.loc.end.line&&c("multiline"),t.at(-1).push({size:w(e.range),node:e,group:r(),name:o}),t},[[]]);for(let t of u)C(t,(e,o)=>{let r=x(n.groups,e),c=x(n.groups,o);(r>c||r===c&&j(I(e,o,n)))&&s.report({messageId:"unexpectedSvelteAttributesOrder",data:{left:e.name,right:o.name},node:o.node,fix:d=>{let y={};for(let g of t){let p=x(n.groups,g);p in y?y[p]=A([...y[p],g],n):y[p]=[g]}let m=[];for(let g of Object.keys(y).sort())m.push(...A(y[g],n));return P(d,t,m,a)}})})}}}}),W="sort-astro-attributes",ue=v({name:W,meta:{type:"suggestion",docs:{description:"enforce sorted Astro attributes"},fixable:"code",schema:[{type:"object",properties:{"custom-groups":{type:"object"},type:{enum:[i.alphabetical,i.natural,i["line-length"]],default:i.alphabetical,type:"string"},order:{enum:[f.asc,f.desc],default:f.asc,type:"string"},"ignore-case":{type:"boolean",default:!1},groups:{type:"array"}},additionalProperties:!1}],messages:{unexpectedAstroAttributesOrder:'Expected "{{right}}" to come before "{{left}}"'}},defaultOptions:[{type:i.alphabetical,order:f.asc}],create:s=>D.extname(s.getFilename())!==".astro"?{}:{JSXElement:l=>{let{attributes:n}=l.openingElement;if(n.length>1){let a=k(s.options.at(0),{type:i.alphabetical,order:f.asc,"ignore-case":!1,"custom-groups":{},groups:[]}),u=s.getSourceCode(),t=n.reduce((e,o)=>{if(o.type==="JSXSpreadAttribute")return e.push([]),e;let r=typeof o.name.name=="string"?o.name.name:u.text.slice(...o.name.range),{getGroup:c,defineGroup:d,setCustomGroups:y}=G(a.groups);return y(a["custom-groups"],r),o.type==="AstroShorthandAttribute"&&(d("astro-shorthand"),d("shorthand")),o.value===null&&d("shorthand"),o.loc.start.line!==o.loc.end.line&&d("multiline"),e.at(-1).push({size:w(o.range),node:o,group:c(),name:r}),e},[[]]);for(let e of t)C(e,(o,r)=>{let c=x(a.groups,o),d=x(a.groups,r);(c>d||c===d&&j(I(o,r,a)))&&s.report({messageId:"unexpectedAstroAttributesOrder",data:{left:o.name,right:r.name},node:r.node,fix:y=>{let m={};for(let p of e){let h=x(a.groups,p);h in m?m[h]=A([...m[h],p],a):m[h]=[p]}let g=[];for(let p of Object.keys(m).sort())g.push(...A(m[p],a));return P(y,e,g,u)}})})}}}});let O=s=>s.replaceAll(/\s\s+/g," ").trim();const J="sort-array-includes",ce=v({name:J,meta:{type:"suggestion",docs:{description:"enforce sorted arrays before include method"},fixable:"code",schema:[{type:"object",properties:{type:{enum:[i.alphabetical,i.natural,i["line-length"]],default:i.alphabetical,type:"string"},order:{enum:[f.asc,f.desc],default:f.asc,type:"string"},"ignore-case":{type:"boolean",default:!1},"spread-last":{type:"boolean",default:!1}},additionalProperties:!1}],messages:{unexpectedArrayIncludesOrder:'Expected "{{right}}" to come before "{{left}}"'}},defaultOptions:[{type:i.alphabetical,order:f.asc}],create:s=>({MemberExpression:l=>{if((l.object.type==="ArrayExpression"||l.object.type==="NewExpression")&&l.property.type==="Identifier"&&l.property.name==="includes"){let n=l.object.type==="ArrayExpression"?l.object.elements:l.object.arguments;if(n.length>1){let a=k(s.options.at(0),{type:i.alphabetical,order:f.asc,"ignore-case":!1,"spread-last":!1}),u=s.getSourceCode(),t=n.reduce((e,o)=>(o!==null&&e.at(0).push({name:o.type==="Literal"?`${o.value}`:u.text.slice(...o.range),size:w(o.range),type:o.type,node:o}),e),[[],[]]).flat();C(t,(e,o)=>{let r;a["spread-last"]&&e.node.type==="Literal"&&o.node.type==="SpreadElement"?r=!1:a["spread-last"]&&e.node.type==="SpreadElement"&&o.node.type==="Literal"?r=!0:r=j(I(e,o,a)),r&&s.report({messageId:"unexpectedArrayIncludesOrder",data:{left:O(e.name),right:O(o.name)},node:o.node,fix:c=>{let d=A(t,a);if(a["spread-last"])for(let y=0,m=d.length;y<m;y++)d.at(y).node.type==="SpreadElement"&&d.push(d.splice(y,1).at(0));return P(c,t,d,u)}})})}}}})}),X="sort-vue-attributes",de=v({name:X,meta:{type:"suggestion",docs:{description:"enforce sorted Vue attributes"},fixable:"code",schema:[{type:"object",properties:{"custom-groups":{type:"object"},type:{enum:[i.alphabetical,i.natural,i["line-length"]],default:i.alphabetical,type:"string"},order:{enum:[f.asc,f.desc],default:f.asc,type:"string"},"ignore-case":{type:"boolean",default:!1},groups:{type:"array",default:[]}},additionalProperties:!1}],messages:{unexpectedVueAttributesOrder:'Expected "{{right}}" to come before "{{left}}"'}},defaultOptions:[{type:i.alphabetical,order:f.asc}],create:s=>{if(D.extname(s.getFilename())!==".vue")return{};if(!("defineTemplateBodyVisitor"in s.parserServices))return{};let{defineTemplateBodyVisitor:l}=s.parserServices;return l({VStartTag:n=>{if(n.attributes.length>1){let a=k(s.options.at(0),{type:i.alphabetical,order:f.asc,"ignore-case":!1,"custom-groups":{},groups:[]}),u=s.getSourceCode(),t=n.attributes.reduce((e,o)=>{if(o.key.type==="VDirectiveKey"&&o.key.name.rawName==="bind")return e.push([]),e;let r,{getGroup:c,defineGroup:d,setCustomGroups:y}=G(a.groups);return typeof o.key.name=="string"&&o.key.type!=="VDirectiveKey"?r=o.key.rawName:r=u.text.slice(...o.key.range),y(a["custom-groups"],r),o.value===null&&d("shorthand"),o.loc.start.line!==o.loc.end.line&&d("multiline"),e.at(-1).push({size:w(o.range),node:o,group:c(),name:r}),e},[[]]);for(let e of t)C(e,(o,r)=>{let c=x(a.groups,o),d=x(a.groups,r);(c>d||c===d&&j(I(o,r,a)))&&s.report({messageId:"unexpectedVueAttributesOrder",data:{left:o.name,right:r.name},node:r.node,fix:y=>{let m={};for(let p of e){let h=x(a.groups,p);h in m?m[h]=A([...m[h],p],a):m[h]=[p]}let g=[];for(let p of Object.keys(m).sort())g.push(...A(m[p],a));return P(y,e,g,u)}})})}}})}}),q="sort-named-exports",fe=v({name:q,meta:{type:"suggestion",docs:{description:"enforce sorted named exports"},fixable:"code",schema:[{type:"object",properties:{type:{enum:[i.alphabetical,i.natural,i["line-length"]],default:i.alphabetical,type:"string"},order:{enum:[f.asc,f.desc],default:f.asc,type:"string"},"ignore-case":{type:"boolean",default:!1}},additionalProperties:!1}],messages:{unexpectedNamedExportsOrder:'Expected "{{right}}" to come before "{{left}}"'}},defaultOptions:[{type:i.alphabetical,order:f.asc}],create:s=>({ExportNamedDeclaration:l=>{if(l.specifiers.length>1){let n=k(s.options.at(0),{type:i.alphabetical,"ignore-case":!1,order:f.asc}),a=s.getSourceCode(),u=l.specifiers.map(t=>({size:w(t.range),name:t.local.name,node:t}));C(u,(t,e)=>{j(I(t,e,n))&&s.report({messageId:"unexpectedNamedExportsOrder",data:{left:t.name,right:e.name},node:e.node,fix:o=>P(o,u,A(u,n),a)})})}}})}),K="sort-named-imports",ge=v({name:K,meta:{type:"suggestion",docs:{description:"enforce sorted named imports"},fixable:"code",schema:[{type:"object",properties:{type:{enum:[i.alphabetical,i.natural,i["line-length"]],default:i.alphabetical,type:"string"},order:{enum:[f.asc,f.desc],default:f.asc,type:"string"},"ignore-case":{type:"boolean",default:!1}},additionalProperties:!1}],messages:{unexpectedNamedImportsOrder:'Expected "{{right}}" to come before "{{left}}"'}},defaultOptions:[{type:i.alphabetical,order:f.asc}],create:s=>({ImportDeclaration:l=>{let n=l.specifiers.filter(({type:a})=>a==="ImportSpecifier");if(n.length>1){let a=k(s.options.at(0),{type:i.alphabetical,"ignore-case":!1,order:f.asc}),u=s.getSourceCode(),t=n.map(e=>({size:w(e.range),name:e.local.name,node:e}));C(t,(e,o)=>{j(I(e,o,a))&&s.report({messageId:"unexpectedNamedImportsOrder",data:{left:e.name,right:o.name},node:o.node,fix:r=>P(r,t,A(t,a),u)})})}}})}),F="sort-object-types",ye=v({name:F,meta:{type:"suggestion",docs:{description:"enforce sorted object types"},fixable:"code",schema:[{type:"object",properties:{"custom-groups":{type:"object"},type:{enum:[i.alphabetical,i.natural,i["line-length"]],default:i.alphabetical,type:"string"},order:{enum:[f.asc,f.desc],default:f.asc,type:"string"},"ignore-case":{type:"boolean",default:!1},groups:{type:"array",default:[]}},additionalProperties:!1}],messages:{unexpectedObjectTypesOrder:'Expected "{{right}}" to come before "{{left}}"'}},defaultOptions:[{type:i.alphabetical,order:f.asc}],create:s=>({TSTypeLiteral:l=>{if(l.members.length>1){let n=k(s.options.at(0),{type:i.alphabetical,"ignore-case":!1,order:f.asc,"custom-groups":{},groups:[]}),a=s.getSourceCode(),u=l.members.map(t=>{var p,h;let e,o=a.text.slice(t.range.at(0),t.range.at(1)),{getGroup:r,defineGroup:c,setCustomGroups:d}=G(n.groups),y=S=>S.replace(/(,|;)$/,"");if(t.type==="TSPropertySignature")t.key.type==="Identifier"?{name:e}=t.key:t.key.type==="Literal"?e=`${t.key.value}`:e=a.text.slice(t.range.at(0),(p=t.typeAnnotation)==null?void 0:p.range.at(0));else if(t.type==="TSIndexSignature"){let S=((h=t.typeAnnotation)==null?void 0:h.range.at(0))??t.range.at(1);e=y(a.text.slice(t.range.at(0),S))}else e=y(a.text.slice(t.range.at(0),t.range.at(1)));d(n["custom-groups"],e),t.loc.start.line!==t.loc.end.line&&c("multiline");let g=o.endsWith(";")||o.endsWith(",")?1:0;return{size:w(t.range)-g,group:r(),node:t,name:e}});C(u,(t,e)=>{let o=x(n.groups,t),r=x(n.groups,e);(o>r||o===r&&j(I(t,e,n)))&&s.report({messageId:"unexpectedObjectTypesOrder",data:{left:O(t.name),right:O(e.name)},node:e.node,fix:c=>{let d={};for(let m of u){let g=x(n.groups,m);g in d?d[g]=A([...d[g],m],n):d[g]=[m]}let y=[];for(let m of Object.keys(d).sort())y.push(...A(d[m],n));return P(c,u,y,a)}})})}}})}),H="sort-union-types",me=v({name:H,meta:{type:"suggestion",docs:{description:"enforce sorted union types"},fixable:"code",schema:[{type:"object",properties:{type:{enum:[i.alphabetical,i.natural,i["line-length"]],default:i.alphabetical,type:"string"},order:{enum:[f.asc,f.desc],default:f.asc,type:"string"},"ignore-case":{type:"boolean",default:!1},"nullable-last":{type:"boolean",default:!1}},additionalProperties:!1}],messages:{unexpectedUnionTypesOrder:'Expected "{{right}}" to come before "{{left}}"'}},defaultOptions:[{type:i.alphabetical,order:f.asc}],create:s=>({TSUnionType:l=>{let n=s.getSourceCode(),a=k(s.options.at(0),{type:i.alphabetical,"nullable-last":!1,"ignore-case":!1,order:f.asc}),u=l.types.map(t=>({group:t.type==="TSNullKeyword"||t.type==="TSUndefinedKeyword"?"nullable":"unknown",name:n.text.slice(...t.range),size:w(t.range),node:t}));C(u,(t,e)=>{let o=j(I(t,e,a));a["nullable-last"]&&(t.group==="nullable"&&e.group==="unknown"?o=!0:t.group==="unknown"&&e.group==="nullable"&&(o=!1)),o&&s.report({messageId:"unexpectedUnionTypesOrder",data:{left:O(t.name),right:O(e.name)},node:e.node,fix:r=>{let c=[];if(a["nullable-last"]){let d=[],y=u.filter(m=>m.group==="nullable"?(d.push(m),!1):!0);c=[...A(y,a),...A(d,a)]}else c=A(u,a);return P(r,u,c,n)}})})}})}),Q="sort-interfaces",he=v({name:Q,meta:{type:"suggestion",docs:{description:"enforce sorted interface properties"},fixable:"code",schema:[{type:"object",properties:{"custom-groups":{type:"object"},type:{enum:[i.alphabetical,i.natural,i["line-length"]],default:i.alphabetical,type:"string"},order:{enum:[f.asc,f.desc],default:f.asc,type:"string"},"ignore-case":{type:"boolean",default:!1},"ignore-pattern":{items:{type:"string"},type:"array"},groups:{type:"array",default:[]}},additionalProperties:!1}],messages:{unexpectedInterfacePropertiesOrder:'Expected "{{right}}" to come before "{{left}}"'}},defaultOptions:[{type:i.alphabetical,order:f.asc}],create:s=>({TSInterfaceDeclaration:l=>{if(l.body.body.length>1){let n=k(s.options.at(0),{type:i.alphabetical,"ignore-case":!1,order:f.asc,"ignore-pattern":[],"custom-groups":{},groups:[]});if(!n["ignore-pattern"].some(a=>$.minimatch(l.id.name,a))){let a=s.getSourceCode(),u=l.body.body.reduce((t,e)=>{var y,m,g;if(e.type==="TSCallSignatureDeclaration")return t.push([]),t;let o,{getGroup:r,defineGroup:c,setCustomGroups:d}=G(n.groups);if(e.type==="TSPropertySignature")if(e.key.type==="Identifier")({name:o}=e.key);else if(e.key.type==="Literal")o=`${e.key.value}`;else{let p=((y=e.typeAnnotation)==null?void 0:y.range.at(0))??e.range.at(1)-(e.optional?1:0);o=a.text.slice(e.range.at(0),p)}else if(e.type==="TSIndexSignature"){let p=((m=e.typeAnnotation)==null?void 0:m.range.at(0))??e.range.at(1);o=a.text.slice(e.range.at(0),p)}else{let p=((g=e.returnType)==null?void 0:g.range.at(0))??e.range.at(1);o=a.text.slice(e.range.at(0),p)}return d(n["custom-groups"],o),e.loc.start.line!==e.loc.end.line&&c("multiline"),t.at(-1).push({size:w(e.range),group:r(),node:e,name:o}),t},[[]]);for(let t of u)C(t,(e,o)=>{let r=x(n.groups,e),c=x(n.groups,o);(r>c||r===c&&j(I(e,o,n)))&&s.report({messageId:"unexpectedInterfacePropertiesOrder",data:{left:O(e.name),right:O(o.name)},node:o.node,fix:d=>{let y={};for(let g of t){let p=x(n.groups,g);p in y?y[p]=A([...y[p],g],n):y[p]=[g]}let m=[];for(let g of Object.keys(y).sort())m.push(...A(y[g],n));return P(d,t,m,a)}})})}}}})}),Y="sort-jsx-props",be=v({name:Y,meta:{type:"suggestion",docs:{description:"enforce sorted JSX props"},fixable:"code",schema:[{type:"object",properties:{"custom-groups":{type:"object"},type:{enum:[i.alphabetical,i.natural,i["line-length"]],default:i.alphabetical,type:"string"},order:{enum:[f.asc,f.desc],default:f.asc,type:"string"},groups:{type:"array"},"ignore-case":{type:"boolean",default:!1}},additionalProperties:!1}],messages:{unexpectedJSXPropsOrder:'Expected "{{right}}" to come before "{{left}}"'}},defaultOptions:[{type:i.alphabetical,order:f.asc}],create:s=>[".svelte",".astro",".vue"].includes(D.extname(s.getFilename()))?{}:{JSXElement:l=>{if(l.openingElement.attributes.length>1){let n=k(s.options.at(0),{type:i.alphabetical,"ignore-case":!1,order:f.asc,"custom-groups":{},groups:[]}),a=s.getSourceCode(),u=l.openingElement.attributes.reduce((t,e)=>{if(e.type==="JSXSpreadAttribute")return t.push([]),t;let o=e.name.type==="JSXNamespacedName"?`${e.name.namespace.name}:${e.name.name.name}`:e.name.name,{getGroup:r,defineGroup:c,setCustomGroups:d}=G(n.groups);d(n["custom-groups"],o),e.value===null&&c("shorthand"),e.loc.start.line!==e.loc.end.line&&c("multiline");let y={size:w(e.range),group:r(),node:e,name:o};return t.at(-1).push(y),t},[[]]);for(let t of u)C(t,(e,o)=>{let r=x(n.groups,e),c=x(n.groups,o);(r>c||r===c&&j(I(e,o,n)))&&s.report({messageId:"unexpectedJSXPropsOrder",data:{left:e.name,right:o.name},node:o.node,fix:d=>{let y={};for(let g of t){let p=x(n.groups,g);p in y?y[p]=A([...y[p],g],n):y[p]=[g]}let m=[];for(let g of Object.keys(y).sort())m.push(...A(y[g],n));return P(d,t,m,a)}})})}}}}),Z="sort-exports",xe=v({name:Z,meta:{type:"suggestion",docs:{description:"enforce sorted exports"},fixable:"code",schema:[{type:"object",properties:{type:{enum:[i.alphabetical,i.natural,i["line-length"]],default:i.alphabetical,type:"string"},order:{enum:[f.asc,f.desc],default:f.asc,type:"string"},"ignore-case":{type:"boolean",default:!1}},additionalProperties:!1}],messages:{unexpectedExportsOrder:'Expected "{{right}}" to come before "{{left}}"'}},defaultOptions:[{type:i.alphabetical,order:f.asc}],create:s=>{let l=k(s.options.at(0),{type:i.alphabetical,order:f.asc,"ignore-case":!1}),n=[[]],a=u=>{u.type==="ExportAllDeclaration"&&u.exported===null?n.push([]):n.at(-1).push({size:w(u.range),name:u.source.value,node:u})};return{ExportAllDeclaration:a,ExportNamedDeclaration:u=>{u.source!==null&&a(u)},"Program:exit":()=>{let u=s.getSourceCode();for(let t of n)C(t,(e,o)=>{j(I(e,o,l))&&s.report({messageId:"unexpectedExportsOrder",data:{left:e.name,right:o.name},node:o.node,fix:r=>P(r,t,A(t,l),u)})})}}}}),ee="sort-imports",Ee=v({name:ee,meta:{type:"suggestion",docs:{description:"enforce sorted imports"},fixable:"code",schema:[{type:"object",properties:{"custom-groups":{type:"object",properties:{type:{type:"object"},value:{type:"object"}},additionalProperties:!1},type:{enum:[i.alphabetical,i.natural,i["line-length"]],default:i.alphabetical,type:"string"},order:{enum:[f.asc,f.desc],default:f.asc,type:"string"},"ignore-case":{type:"boolean",default:!1},groups:{type:"array",default:[]},"internal-pattern":{items:{type:"string"},type:"array"},"newlines-between":{enum:["ignore","always","never"],default:"always",type:"string"}},additionalProperties:!1}],messages:{unexpectedImportsOrder:'Expected "{{right}}" to come before "{{left}}"',missedSpacingBetweenImports:'Missed spacing between "{{left}}" and "{{right}}" imports',extraSpacingBetweenImports:'Extra spacing between "{{left}}" and "{{right}}" imports'}},defaultOptions:[{type:i.alphabetical,order:f.asc}],create:s=>{let l=k(s.options.at(0),{"newlines-between":"always","custom-groups":{type:{},value:{}},"internal-pattern":["~/**"],type:i.alphabetical,order:f.asc,"ignore-case":!1,groups:[]}),n=[],a=s.getSourceCode(),u=[],t=r=>r.type==="ImportDeclaration"&&r.specifiers.length===0,e=r=>{let c=b=>[".less",".scss",".sass",".styl",".pcss",".css",".sss"].some(E=>b.endsWith(E)),d=b=>["./index.d.js","./index.d.ts","./index.js","./index.ts","./index","./","."].includes(b),y=b=>b.indexOf("..")===0,m=b=>b.indexOf("./")===0,{getGroup:g,defineGroup:p,setCustomGroups:h}=G(l.groups),S=b=>l["internal-pattern"].length&&l["internal-pattern"].some(E=>$.minimatch(b.source.value,E))||n.some(E=>$.minimatch(b.source.value,E)),N=b=>oe.builtinModules.includes(b)||oe.builtinModules.includes(`node:${b}`);return r.importKind==="type"&&(r.type==="ImportDeclaration"&&(h(l["custom-groups"].type,r.source.value),N(r.source.value)&&p("builtin-type"),S(r)&&p("internal-type"),d(r.source.value)&&p("index-type"),y(r.source.value)&&p("parent-type"),m(r.source.value)&&p("sibling-type")),p("external-type"),p("type")),r.type==="ImportDeclaration"&&(h(l["custom-groups"].value,r.source.value),N(r.source.value)&&p("builtin"),S(r)&&p("internal"),c(r.source.value)&&p("style"),t(r)&&p("side-effect"),d(r.source.value)&&p("index"),y(r.source.value)&&p("parent"),m(r.source.value)&&p("sibling"),p("external")),g()},o=r=>{let c;r.type==="ImportDeclaration"?c=r.source.value:r.moduleReference.type==="TSExternalModuleReference"&&r.moduleReference.expression.type==="Literal"?c=`${r.moduleReference.expression.value}`:c=a.text.slice(...r.moduleReference.range),u.push({size:w(r.range),group:e(r),name:c,node:r})};return{TSImportEqualsDeclaration:o,ImportDeclaration:o,"Program:exit":()=>{var m;let r=(g,p)=>!!a.getTokensBetween(g.node,_(p.node,a)||p.node,{includeComments:!0}).length,c=(g,p)=>a.lines.slice(g.node.loc.end.line,p.node.loc.start.line-1).filter(S=>!S.trim().length).length,d=(g,p)=>{let h=[],S={};for(let b of p){let E=x(l.groups,b);E in S?S[E]=A([...S[E],b],l):S[E]=[b]}let N=Object.keys(S).sort().reduce((b,E)=>[...b,...S[E]],[]);for(let b=0,E=N.length;b<E;b++){let M=N.at(b);if(h.push(g.replaceTextRange(L(p.at(b).node,a),a.text.slice(...L(M.node,a)))),l["newlines-between"]!=="ignore"){let T=N.at(b+1);if(T){let R=c(p.at(b),p.at(b+1));(l["newlines-between"]==="always"&&x(l.groups,M)===x(l.groups,T)&&R!==0||l["newlines-between"]==="never"&&R>0)&&h.push(g.removeRange([L(p.at(b).node,a).at(1),L(p.at(b+1).node,a).at(0)-1])),l["newlines-between"]==="always"&&x(l.groups,M)!==x(l.groups,T)&&R>1&&h.push(g.replaceTextRange([L(p.at(b).node,a).at(1),L(p.at(b+1).node,a).at(0)-1],`
|
|
2
|
+
`)),l["newlines-between"]==="always"&&x(l.groups,M)!==x(l.groups,T)&&R===0&&h.push(g.insertTextAfterRange(L(p.at(b).node,a),`
|
|
3
|
+
`))}}}return h},y=[[]];for(let g of u){let p=(m=y.at(-1))==null?void 0:m.at(-1);p&&r(p,g)?y.push([g]):y.at(-1).push(g)}for(let g of y)C(g,(p,h)=>{let S=x(l.groups,p),N=x(l.groups,h),b=c(p,h);!(t(p.node)&&t(h.node))&&!r(p,h)&&(S>N||S===N&&j(I(p,h,l)))&&s.report({messageId:"unexpectedImportsOrder",data:{left:p.name,right:h.name},node:h.node,fix:E=>d(E,g)}),l["newlines-between"]==="never"&&b>0&&s.report({messageId:"extraSpacingBetweenImports",data:{left:p.name,right:h.name},node:h.node,fix:E=>d(E,g)}),l["newlines-between"]==="always"&&(S<N&&b===0?s.report({messageId:"missedSpacingBetweenImports",data:{left:p.name,right:h.name},node:h.node,fix:E=>d(E,g)}):(b>1||S===N&&b>0)&&s.report({messageId:"extraSpacingBetweenImports",data:{left:p.name,right:h.name},node:h.node,fix:E=>d(E,g)}))})}}}}),te="sort-objects",Se=v({name:te,meta:{type:"suggestion",docs:{description:"enforce sorted objects"},fixable:"code",schema:[{type:"object",properties:{"custom-groups":{type:"object"},"partition-by-comment":{type:["boolean","string","array"],default:!1},"styled-components":{type:"boolean",default:!0},type:{enum:[i.alphabetical,i.natural,i["line-length"]],default:i.alphabetical,type:"string"},order:{enum:[f.asc,f.desc],default:f.asc,type:"string"},"ignore-case":{type:"boolean",default:!1},groups:{type:"array"}},additionalProperties:!1}],messages:{unexpectedObjectsOrder:'Expected "{{right}}" to come before "{{left}}"'}},defaultOptions:[{type:i.alphabetical,order:f.asc}],create:s=>{let l=n=>{if(n.properties.length>1){let a=k(s.options.at(0),{"partition-by-comment":!1,type:i.alphabetical,"styled-components":!0,"ignore-case":!1,order:f.asc,"custom-groups":{},groups:[]}),u=r=>r.type==="Identifier"&&r.name==="styled",t=r=>r!==void 0&&r.type==="CallExpression"&&(r.callee.type==="MemberExpression"&&u(r.callee.object)||r.callee.type==="CallExpression"&&u(r.callee.callee));if(!a["styled-components"]&&(t(n.parent)||n.parent.type==="ArrowFunctionExpression"&&t(n.parent.parent)))return;let e=s.getSourceCode(),o=r=>r.reduce((c,d)=>{if(d.type==="SpreadElement"||d.type==="RestElement")return c.push([]),c;let y=_(d,e);a["partition-by-comment"]&&y&&ne(a["partition-by-comment"],y.value)&&c.push([]);let m,g="ignore",p=[],{getGroup:h,setCustomGroups:S}=G(a.groups);d.key.type==="Identifier"?{name:m}=d.key:d.key.type==="Literal"?m=`${d.key.value}`:m=e.text.slice(...d.key.range),d.value.type==="AssignmentPattern"&&(E=>{E.right.type==="Identifier"&&p.push(E.right.name);let M=T=>{let R=[];switch(T.type){case"ArrowFunctionExpression":R.push(T.body);break;case"ConditionalExpression":R.push(T.consequent,T.alternate);break;case"LogicalExpression":case"BinaryExpression":R.push(T.left,T.right);break;case"CallExpression":R.push(...T.arguments);break}R.forEach(U=>{U.type==="Identifier"&&p.push(U.name),(U.type==="BinaryExpression"||U.type==="ConditionalExpression")&&M(U)})};switch(E.right.type){case"ArrowFunctionExpression":case"ConditionalExpression":case"LogicalExpression":case"BinaryExpression":case"CallExpression":M(E.right);break}})(d.value),S(a["custom-groups"],m);let N={size:w(d.range),group:h(),dependencies:p,node:d,position:g,name:m};return c.at(-1).push(N),c},[[]]);for(let r of o(n.properties))C(r,(c,d)=>{let y=x(a.groups,c),m=x(a.groups,d);if(y>m||y===m&&j(I(c,d,a))){let g=p=>{let h={};for(let N of r){let b=x(a.groups,N);b in h?h[b]=A([...h[b],N],a):h[b]=[N]}let S=[];for(let N of Object.keys(h).sort())S.push(...A(h[N],a));return P(p,r,S,e,{partitionComment:a["partition-by-comment"]})};s.report({messageId:"unexpectedObjectsOrder",data:{left:O(c.name),right:O(d.name)},node:d.node,fix:g})}})}};return{ObjectExpression:l,ObjectPattern:l}}}),re="sort-classes",Ae=v({name:re,meta:{type:"suggestion",docs:{description:"enforce sorted classes"},fixable:"code",schema:[{type:"object",properties:{type:{enum:[i.alphabetical,i.natural,i["line-length"]],default:i.alphabetical,type:"string"},"ignore-case":{type:"boolean",default:!1},order:{enum:[f.asc,f.desc],default:f.asc,type:"string"},groups:{type:"array",default:[]}},additionalProperties:!1}],messages:{unexpectedClassesOrder:'Expected "{{right}}" to come before "{{left}}"'}},defaultOptions:[{type:i.alphabetical,order:f.asc}],create:s=>({ClassBody:l=>{if(l.body.length>1){let n=k(s.options.at(0),{type:i.alphabetical,order:f.asc,"ignore-case":!1,groups:["property","constructor","method","unknown"]}),a=s.getSourceCode(),u=l.body.map(t=>{var d;let e,{getGroup:o,defineGroup:r}=G(n.groups);t.type==="StaticBlock"?e="static":t.type==="TSIndexSignature"?e=a.text.slice(t.range.at(0),((d=t.typeAnnotation)==null?void 0:d.range.at(0))??t.range.at(1)):t.key.type==="Identifier"?{name:e}=t.key:e=a.text.slice(...t.key.range);let c=e.startsWith("_")||e.startsWith("#");return t.type==="MethodDefinition"?(t.kind==="constructor"&&r("constructor"),(t.accessibility==="private"||c)&&r("private-method"),t.static&&r("static-method"),t.kind==="get"&&r("get-method"),t.kind==="set"&&r("set-method"),r("method")):t.type==="TSIndexSignature"?r("index-signature"):t.type==="PropertyDefinition"&&((t.accessibility==="private"||c)&&r("private-property"),t.static&&r("static-property"),r("property")),{size:w(t.range),group:o(),node:t,name:e}});C(u,(t,e)=>{let o=x(n.groups,t),r=x(n.groups,e);t.name!==e.name&&(o>r||o===r&&j(I(t,e,n)))&&s.report({messageId:"unexpectedClassesOrder",data:{left:O(t.name),right:O(e.name)},node:e.node,fix:c=>{let d=[],y=u.reduce((g,p)=>{let h=x(n.groups,p);return h in g?g[h]=A([...g[h],p],n):g[h]=[p],g},{}),m=Object.keys(y).sort().reduce((g,p)=>[...g,...y[p]],[]);for(let g=0,p=m.length;g<p;g++)d.push(c.replaceTextRange(L(u.at(g).node,a),a.text.slice(...L(m.at(g).node,a))));return d}})})}}})}),se="sort-enums",Ne=v({name:se,meta:{type:"suggestion",docs:{description:"enforce sorted TypeScript enums"},fixable:"code",schema:[{type:"object",properties:{type:{enum:[i.alphabetical,i.natural,i["line-length"]],default:i.alphabetical,type:"string"},"ignore-case":{type:"boolean",default:!1},order:{enum:[f.asc,f.desc],default:f.asc,type:"string"}},additionalProperties:!1}],messages:{unexpectedEnumsOrder:'Expected "{{right}}" to come before "{{left}}"'}},defaultOptions:[{type:i.alphabetical,order:f.asc}],create:s=>({TSEnumDeclaration:l=>{if(l.members.length>1&&l.members.every(({initializer:n})=>n)){let n=k(s.options.at(0),{type:i.alphabetical,order:f.asc,"ignore-case":!1}),a=s.getSourceCode(),u=l.members.map(t=>({name:t.id.type==="Literal"?`${t.id.value}`:`${a.text.slice(...t.id.range)}`,size:w(t.range),node:t}));C(u,(t,e)=>{j(I(t,e,n))&&s.report({messageId:"unexpectedEnumsOrder",data:{left:O(t.name),right:O(e.name)},node:e.node,fix:o=>P(o,u,A(u,n),a)})})}}})}),ae="sort-maps",Ie=v({name:ae,meta:{type:"suggestion",docs:{description:"enforce sorted Map elements"},fixable:"code",schema:[{type:"object",properties:{type:{enum:[i.alphabetical,i.natural,i["line-length"]],default:i.alphabetical,type:"string"},order:{enum:[f.asc,f.desc],default:f.asc,type:"string"},"ignore-case":{type:"boolean",default:!1}},additionalProperties:!1}],messages:{unexpectedMapElementsOrder:'Expected "{{right}}" to come before "{{left}}"'}},defaultOptions:[{type:i.alphabetical,order:f.asc}],create:s=>({NewExpression:l=>{if(l.callee.type==="Identifier"&&l.callee.name==="Map"&&l.arguments.length&&l.arguments[0].type==="ArrayExpression"){let[{elements:n}]=l.arguments;if(n.length>1){let a=k(s.options.at(0),{type:i.alphabetical,"ignore-case":!1,order:f.asc}),u=s.getSourceCode(),t=n.reduce((e,o)=>(o===null||o.type==="SpreadElement"?e.push([]):e.at(-1).push(o),e),[[]]);for(let e of t){let o=e.map(r=>{let c;if(r.type==="ArrayExpression"){let[d]=r.elements;d?d.type==="Literal"?c=d.raw:c=u.text.slice(...d.range):c=`${d}`}else c=u.text.slice(...r.range);return{size:w(r.range),node:r,name:c}});C(o,(r,c)=>{j(I(r,c,a))&&s.report({messageId:"unexpectedMapElementsOrder",data:{left:O(r.name),right:O(c.name)},node:c.node,fix:d=>P(d,o,A(o,a),u)})})}}}}})}),Oe="eslint-plugin-perfectionist";let z=s=>{let l={[ee]:["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":["~/**"]}],[re]:["error",{groups:["index-signature","static-property","private-property","property","constructor","static-method","private-method","method",["get-method","set-method"],"unknown"]}],[te]:["error",{"partition-by-comment":!1}],[J]:["error",{"spread-last":!0}],[V]:["error"],[W]:["error"],[X]:["error"],[q]:["error"],[K]:["error"],[F]:["error"],[H]:["error"],[Q]:["error"],[Y]:["error"],[Z]:["error"],[se]:["error"],[ae]:["error"]};return{rules:Object.fromEntries(Object.entries(l).map(([n,[a,u={}]])=>[`perfectionist/${n}`,[a,Object.assign(u,s)]])),plugins:["perfectionist"]}};const ve={rules:{[J]:ce,[W]:ue,[re]:Ae,[se]:Ne,[Z]:xe,[ee]:Ee,[Q]:he,[Y]:be,[ae]:Ie,[q]:fe,[K]:ge,[F]:ye,[te]:Se,[V]:pe,[H]:me,[X]:de},configs:{"recommended-alphabetical":z({type:i.alphabetical,order:f.asc,"ignore-case":!1}),"recommended-natural":z({type:i.natural,order:f.asc,"ignore-case":!1}),"recommended-line-length":z({type:i["line-length"],order:f.desc})},name:Oe};module.exports=ve;
|