eslint-plugin-perfectionist 0.8.0 → 0.9.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 +4 -2
- package/dist/configs/recommended-line-length.js +1 -1
- package/dist/configs/recommended-line-length.mjs +4 -2
- package/dist/configs/recommended-natural.js +1 -1
- package/dist/configs/recommended-natural.mjs +4 -2
- package/dist/index.js +3 -1
- package/dist/index.mjs +661 -388
- package/package.json +3 -1
- package/readme.md +105 -11
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";const e=require("../index.js");require("@typescript-eslint/types");require("@typescript-eslint/utils");require("natural-compare-lite");const r={plugins:{perfectionist:e},rules:e.configs["recommended-alphabetical"].rules};module.exports=r;
|
|
1
|
+
"use strict";const e=require("../index.js");require("@typescript-eslint/types");require("@typescript-eslint/utils");require("natural-compare-lite");require("is-core-module");require("minimatch");const r={plugins:{perfectionist:e},rules:e.configs["recommended-alphabetical"].rules};module.exports=r;
|
|
@@ -2,12 +2,14 @@ import e from "../index.mjs";
|
|
|
2
2
|
import "@typescript-eslint/types";
|
|
3
3
|
import "@typescript-eslint/utils";
|
|
4
4
|
import "natural-compare-lite";
|
|
5
|
-
|
|
5
|
+
import "is-core-module";
|
|
6
|
+
import "minimatch";
|
|
7
|
+
const l = {
|
|
6
8
|
plugins: {
|
|
7
9
|
perfectionist: e
|
|
8
10
|
},
|
|
9
11
|
rules: e.configs["recommended-alphabetical"].rules
|
|
10
12
|
};
|
|
11
13
|
export {
|
|
12
|
-
|
|
14
|
+
l as default
|
|
13
15
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";const e=require("../index.js");require("@typescript-eslint/types");require("@typescript-eslint/utils");require("natural-compare-lite");const r={plugins:{perfectionist:e},rules:e.configs["recommended-line-length"].rules};module.exports=r;
|
|
1
|
+
"use strict";const e=require("../index.js");require("@typescript-eslint/types");require("@typescript-eslint/utils");require("natural-compare-lite");require("is-core-module");require("minimatch");const r={plugins:{perfectionist:e},rules:e.configs["recommended-line-length"].rules};module.exports=r;
|
|
@@ -2,12 +2,14 @@ import e from "../index.mjs";
|
|
|
2
2
|
import "@typescript-eslint/types";
|
|
3
3
|
import "@typescript-eslint/utils";
|
|
4
4
|
import "natural-compare-lite";
|
|
5
|
-
|
|
5
|
+
import "is-core-module";
|
|
6
|
+
import "minimatch";
|
|
7
|
+
const p = {
|
|
6
8
|
plugins: {
|
|
7
9
|
perfectionist: e
|
|
8
10
|
},
|
|
9
11
|
rules: e.configs["recommended-line-length"].rules
|
|
10
12
|
};
|
|
11
13
|
export {
|
|
12
|
-
|
|
14
|
+
p as default
|
|
13
15
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";const e=require("../index.js");require("@typescript-eslint/types");require("@typescript-eslint/utils");require("natural-compare-lite");const r={plugins:{perfectionist:e},rules:e.configs["recommended-natural"].rules};module.exports=r;
|
|
1
|
+
"use strict";const e=require("../index.js");require("@typescript-eslint/types");require("@typescript-eslint/utils");require("natural-compare-lite");require("is-core-module");require("minimatch");const r={plugins:{perfectionist:e},rules:e.configs["recommended-natural"].rules};module.exports=r;
|
|
@@ -2,12 +2,14 @@ import r from "../index.mjs";
|
|
|
2
2
|
import "@typescript-eslint/types";
|
|
3
3
|
import "@typescript-eslint/utils";
|
|
4
4
|
import "natural-compare-lite";
|
|
5
|
-
|
|
5
|
+
import "is-core-module";
|
|
6
|
+
import "minimatch";
|
|
7
|
+
const n = {
|
|
6
8
|
plugins: {
|
|
7
9
|
perfectionist: r
|
|
8
10
|
},
|
|
9
11
|
rules: r.configs["recommended-natural"].rules
|
|
10
12
|
};
|
|
11
13
|
export {
|
|
12
|
-
|
|
14
|
+
n as default
|
|
13
15
|
};
|
package/dist/index.js
CHANGED
|
@@ -1 +1,3 @@
|
|
|
1
|
-
"use strict";const f=require("@typescript-eslint/types"),L=require("@typescript-eslint/utils"),C=require("natural-compare-lite"),$="eslint-plugin-perfectionist",M="azat-io/eslint-plugin-perfectionist";let g=L.ESLintUtils.RuleCreator(s=>`https://github.com/${M}/blob/main/docs/rules/${s}.md`),y=s=>{let[l,d]=s;return d-l};var r=(s=>(s.alphabetical="alphabetical",s.natural="natural",s["line-length"]="line-length",s))(r||{}),n=(s=>(s.asc="asc",s.desc="desc",s))(n||{});let m=(s,l,d)=>{let c=d.order==="asc"?1:-1,o;return d.type===r.alphabetical?o=(e,t)=>e.name.localeCompare(t.name):d.type===r.natural?o=(e,t)=>C(e.name,t.name):o=(e,t)=>e.size-t.size,c*o(s,l)>0},E=(s,l,d,c)=>{let o=[...d].sort((t,a)=>Number(m(t,a,c))||-1);c.spreadLast&&o.forEach((t,a)=>{t.node.type===f.AST_NODE_TYPES.SpreadElement&&o.push(o.splice(a,1).at(0))});let e=t=>[t.range.at(0),t.range.at(1)];return d.map(({node:t},a)=>s.replaceTextRange(e(t),l.slice(...e(o[a].node))))},h=(s={},l)=>Object.assign(l,s);const T="sort-array-includes",R=g({name:T,meta:{type:"suggestion",docs:{description:"Enforce sorted arrays before include method",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[r.alphabetical,r.natural,r["line-length"]],default:r.natural},order:{enum:[n.asc,n.desc],default:n.asc},spreadLast:{type:"boolean",default:!1}},additionalProperties:!1}],messages:{unexpectedArrayIncludesOrder:'Expected "{{second}}" to come before "{{first}}"'}},defaultOptions:[{type:r.alphabetical,order:n.asc}],create:s=>({MemberExpression:l=>{if((l.object.type===f.AST_NODE_TYPES.ArrayExpression||l.object.type===f.AST_NODE_TYPES.NewExpression)&&l.property.type===f.AST_NODE_TYPES.Identifier&&l.property.name==="includes"){let d=h(s.options.at(0),{type:r.alphabetical,order:n.asc,spreadLast:!1}),c=l.object.type===f.AST_NODE_TYPES.ArrayExpression?l.object.elements:l.object.arguments;if(c.length>1){let o=s.getSourceCode().text,e=c.reduce((t,a)=>(a===null||t.at(0).push({name:a.type===f.AST_NODE_TYPES.Literal?a.raw:o.slice(...a.range),size:y(a.range),type:a.type,node:a}),t),[[],[]]).flat();for(let t=1;t<e.length;t++){let a=e.at(t-1),i=e.at(t),p;d.spreadLast&&a.node.type===f.AST_NODE_TYPES.Literal&&i.node.type===f.AST_NODE_TYPES.SpreadElement?p=!1:d.spreadLast&&a.node.type===f.AST_NODE_TYPES.SpreadElement&&i.node.type===f.AST_NODE_TYPES.Literal?p=!0:p=m(a,i,d),p&&s.report({messageId:"unexpectedArrayIncludesOrder",data:{first:a.name,second:i.name},node:i.node,fix:u=>{let b=s.getSourceCode(),{text:S}=b;return E(u,S,e,d)}})}}}}})}),_="sort-enums",U=g({name:_,meta:{type:"suggestion",docs:{description:"Enforce sorted TypeScript enums",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[r.alphabetical,r.natural,r["line-length"]],default:r.natural},order:{enum:[n.asc,n.desc],default:n.asc}},additionalProperties:!1}],messages:{unexpectedEnumsOrder:'Expected "{{second}}" to come before "{{first}}"'}},defaultOptions:[{type:r.alphabetical,order:n.asc}],create:s=>({TSEnumDeclaration:l=>{let d=h(s.options.at(0),{type:r.alphabetical,order:n.asc});if(l.members.length>1){let c=s.getSourceCode().text,o=l.members.map(e=>{let t;return e.id.type===f.AST_NODE_TYPES.Literal?t=`${e.id.value}`:t=`${c.slice(...e.id.range)}`,{size:y(e.range),node:e,name:t}});for(let e=1;e<o.length;e++){let t=o.at(e-1),a=o.at(e);m(t,a,d)&&s.report({messageId:"unexpectedEnumsOrder",data:{first:t.name,second:a.name},node:a.node,fix:i=>E(i,c,o,d)})}}}})}),x="sort-interfaces",z=g({name:x,meta:{type:"suggestion",docs:{description:"Enforce sorted interface properties",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[r.alphabetical,r.natural,r["line-length"]],default:r.natural},order:{enum:[n.asc,n.desc],default:n.asc}},additionalProperties:!1}],messages:{unexpectedInterfacePropertiesOrder:'Expected "{{second}}" to come before "{{first}}"'}},defaultOptions:[{type:r.alphabetical,order:n.asc}],create:s=>({TSInterfaceBody:l=>{let d=h(s.options.at(0),{type:r.alphabetical,order:n.asc});if(l.body.length>1){let c=s.getSourceCode().text,o=l.body.map(e=>{var a,i,p;let t;if(e.type===f.AST_NODE_TYPES.TSPropertySignature)if(e.key.type===f.AST_NODE_TYPES.Identifier)({name:t}=e.key);else if(e.key.type===f.AST_NODE_TYPES.Literal)t=`${e.key.value}`;else{let u;if((a=e.typeAnnotation)!=null&&a.range.at(0))u=e.typeAnnotation.range.at(0);else{let b=e.optional?1:0;u=e.range.at(1)-b}t=c.slice(e.range.at(0),u)}else e.type===f.AST_NODE_TYPES.TSIndexSignature?t=c.slice(e.range.at(0),((i=e.typeAnnotation)==null?void 0:i.range.at(0))??e.range.at(1)):t=c.slice(e.range.at(0),((p=e.returnType)==null?void 0:p.range.at(0))??e.range.at(1));return{size:y(e.range),node:e,name:t}});for(let e=1;e<o.length;e++){let t=o.at(e-1),a=o.at(e);if(m(t,a,d)){let i=l.body[e];s.report({messageId:"unexpectedInterfacePropertiesOrder",data:{first:t.name,second:a.name},node:i,fix:p=>E(p,c,o,d)})}}}}})}),N="sort-jsx-props",k=g({name:N,meta:{type:"suggestion",docs:{description:"Enforce sorted JSX props",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[r.alphabetical,r.natural,r["line-length"]],default:r.natural},order:{enum:[n.asc,n.desc],default:n.asc}},additionalProperties:!1}],messages:{unexpectedJSXPropsOrder:'Expected "{{second}}" to come before "{{first}}"'}},defaultOptions:[{type:r.alphabetical,order:n.asc}],create:s=>({JSXElement:l=>{let d=h(s.options.at(0),{type:r.alphabetical,order:n.asc});l.openingElement.attributes.reduce((o,e)=>(e.type==="JSXAttribute"?o.at(-1).push(e):o.push([]),o),[[]]).forEach(o=>{let e=o.map(t=>({name:t.name.type===f.AST_NODE_TYPES.JSXNamespacedName?`${t.name.namespace.name}:${t.name.name.name}`:t.name.name,size:y(t.range),node:t}));for(let t=1;t<e.length;t++){let a=e.at(t-1),i=e.at(t);m(a,i,d)&&s.report({messageId:"unexpectedJSXPropsOrder",data:{first:a.name,second:i.name},node:i.node,fix:p=>{let u=s.getSourceCode().text;return E(p,u,e,d)}})}})}})}),A="sort-map-elements",v=g({name:A,meta:{type:"suggestion",docs:{description:"Enforce sorted Map elements",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[r.alphabetical,r.natural,r["line-length"]],default:r.natural},order:{enum:[n.asc,n.desc],default:n.asc}},additionalProperties:!1}],messages:{unexpectedMapElementsOrder:'Expected "{{second}}" to come before "{{first}}"'}},defaultOptions:[{type:r.alphabetical,order:n.asc}],create:s=>({NewExpression:l=>{if(l.callee.type===f.AST_NODE_TYPES.Identifier&&l.callee.name==="Map"&&l.arguments[0].type===f.AST_NODE_TYPES.ArrayExpression){let d=h(s.options.at(0),{type:r.alphabetical,order:n.asc}),[{elements:c}]=l.arguments,o=s.getSourceCode().text;c.length>1&&c.reduce((t,a)=>(a===null||a.type===f.AST_NODE_TYPES.SpreadElement?t.push([]):t.at(-1).push(a),t),[[]]).forEach(t=>{let a=t.map(i=>{let p;if(i.type===f.AST_NODE_TYPES.ArrayExpression){let[u]=i.elements;u?u.type===f.AST_NODE_TYPES.Literal?p=u.raw:p=o.slice(...u.range):p=`${u}`}else p=o.slice(...i.range);return{size:y(i.range),node:i,name:p}});for(let i=1;i<a.length;i++){let p=a.at(i-1),u=a.at(i);m(p,u,d)&&s.report({messageId:"unexpectedMapElementsOrder",data:{first:p.name,second:u.name},node:u.node,fix:b=>{let S=s.getSourceCode(),{text:Y}=S;return E(b,Y,a,d)}})}})}}})}),P="sort-named-exports",J=g({name:P,meta:{type:"suggestion",docs:{description:"Enforce sorted named exports",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[r.alphabetical,r.natural,r["line-length"]],default:r.natural},order:{enum:[n.asc,n.desc],default:n.asc}},additionalProperties:!1}],messages:{unexpectedNamedExportsOrder:'Expected "{{second}}" to come before "{{first}}"'}},defaultOptions:[{type:r.alphabetical,order:n.asc}],create:s=>({ExportNamedDeclaration:l=>{let d=h(s.options.at(0),{type:r.alphabetical,order:n.asc});if(l.specifiers.length>1){let c=l.specifiers.map(o=>{let{range:e,local:{name:t}}=o;return{size:y(e),node:o,name:t}});for(let o=1;o<c.length;o++){let e=c.at(o-1),t=c.at(o);if(m(e,t,d)){let a=l.specifiers[o];s.report({messageId:"unexpectedNamedExportsOrder",data:{first:e.name,second:t.name},node:a,fix:i=>{let p=s.getSourceCode(),{text:u}=p;return E(i,u,c,d)}})}}}}})}),D="sort-named-imports",X=g({name:D,meta:{type:"suggestion",docs:{description:"Enforce sorted named imports",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[r.alphabetical,r.natural,r["line-length"]],default:r.natural},order:{enum:[n.asc,n.desc],default:n.asc}},additionalProperties:!1}],messages:{unexpectedNamedImportsOrder:'Expected "{{second}}" to come before "{{first}}"'}},defaultOptions:[{type:r.alphabetical,order:n.asc}],create:s=>({ImportDeclaration:l=>{let d=h(s.options.at(0),{type:r.alphabetical,order:n.asc});if(l.specifiers.length>1){let c=l.specifiers.map(o=>{let{range:e,local:{name:t}}=o;return{size:y(e),node:o,name:t}});for(let o=1;o<c.length;o++){let e=c.at(o-1),t=c.at(o);if(m(e,t,d)){let a=l.specifiers[o];s.report({messageId:"unexpectedNamedImportsOrder",data:{first:e.name,second:t.name},node:a,fix:i=>{let p=s.getSourceCode(),{text:u}=p;return E(i,u,c,d)}})}}}}})}),j="sort-object-keys",w=g({name:j,meta:{type:"suggestion",docs:{description:"Enforce sorted object keys",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[r.alphabetical,r.natural,r["line-length"]],default:r.natural},order:{enum:[n.asc,n.desc],default:n.asc}},additionalProperties:!1}],messages:{unexpectedObjectKeysOrder:'Expected "{{second}}" to come before "{{first}}"'}},defaultOptions:[{type:r.alphabetical,order:n.asc}],create:s=>({ObjectExpression:l=>{if(l.properties.length>1){let d=h(s.options.at(0),{type:r.alphabetical,order:n.asc}),c=s.getSourceCode().text;(e=>e.reduce((t,a)=>{if(a.type===f.AST_NODE_TYPES.SpreadElement)return t.push([]),t;let i;a.key.type===f.AST_NODE_TYPES.Identifier?{name:i}=a.key:a.key.type===f.AST_NODE_TYPES.Literal?i=`${a.key.value}`:i=c.slice(...a.key.range);let p={size:y(a.range),node:a,name:i};return t.at(-1).push(p),t},[[]]))(l.properties).forEach(e=>{if(e.length>1)for(let t=1;t<e.length;t++){let a=e.at(t-1),i=e.at(t);m(a,i,d)&&s.report({messageId:"unexpectedObjectKeysOrder",data:{first:a.name,second:i.name},node:i.node,fix:p=>E(p,c,e,d)})}})}}})}),I="sort-union-types",q=g({name:I,meta:{type:"suggestion",docs:{description:"Enforce sorted union types",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[r.alphabetical,r.natural,r["line-length"]],default:r.natural},order:{enum:[n.asc,n.desc],default:n.asc}},additionalProperties:!1}],messages:{unexpectedUnionTypesOrder:'Expected "{{second}}" to come before "{{first}}"'}},defaultOptions:[{type:r.alphabetical,order:n.asc}],create:s=>({TSUnionType:l=>{let d=s.getSourceCode().text,c=h(s.options.at(0),{type:r.alphabetical,order:n.asc}),o=l.types.map(e=>{let{range:t}=e,a=d.slice(...t);return{size:y(t),node:e,name:a}});for(let e=1;e<o.length;e++){let t=o.at(e-1),a=o.at(e);if(m(t,a,c)){let i=l.types[e];s.report({messageId:"unexpectedUnionTypesOrder",data:{first:t.name,second:a.name},node:i,fix:p=>{let u=s.getSourceCode(),{text:b}=u;return E(p,b,o,c)}})}}}})});let O=s=>{let l={[T]:["error",{spreadLast:!0}],[_]:["error"],[x]:["error"],[N]:["error"],[A]:["error"],[P]:["error"],[D]:["error"],[j]:["error"],[I]:["error"]};return{plugins:["perfectionist"],rules:Object.fromEntries(Object.entries(l).map(([d,[c,o={}]])=>[`perfectionist/${d}`,[c,Object.assign(o,s)]]))}};const K={name:$,rules:{[T]:R,[_]:U,[x]:z,[N]:k,[A]:v,[P]:J,[D]:X,[j]:w,[I]:q},configs:{"recommended-alphabetical":O({type:r.alphabetical,order:n.asc}),"recommended-natural":O({type:r.natural,order:n.asc}),"recommended-line-length":O({type:r["line-length"],order:n.desc})}};module.exports=K;
|
|
1
|
+
"use strict";const m=require("@typescript-eslint/types"),U=require("@typescript-eslint/utils"),k=require("natural-compare-lite"),z=require("is-core-module"),B=require("minimatch");let h=U.ESLintUtils.RuleCreator(a=>`https://eslint-plugin-perfectionist.azat.io/rules/${a}`),S=a=>{let[o,s]=a;return s-o};var n=(a=>(a.alphabetical="alphabetical",a.natural="natural",a["line-length"]="line-length",a))(n||{}),i=(a=>(a.asc="asc",a.desc="desc",a))(i||{});let J=(a,o)=>{let s=o.getTokenBefore(a,{includeComments:!0});return(s==null?void 0:s.type)===m.AST_TOKEN_TYPES.Block||(s==null?void 0:s.type)===m.AST_TOKEN_TYPES.Line?s:null},_=(a,o)=>{let s=J(a,o);return[(s==null?void 0:s.range.at(0))??a.range.at(0),a.range.at(1)]},b=(a,o,s)=>{let l=s.order==="asc"?1:-1,p;return s.type===n.alphabetical?p=(t,e)=>t.name.localeCompare(e.name):s.type===n.natural?p=(t,e)=>k(t.name,e.name):p=(t,e)=>t.size-e.size,l*p(a,o)>0},O=(a,{options:o,source:s,nodes:l})=>{let p=[...l].sort((t,e)=>Number(b(t,e,o))||-1);return o.spreadLast&&p.forEach((t,e)=>{t.node.type===m.AST_NODE_TYPES.SpreadElement&&p.push(p.splice(e,1).at(0))}),l.map(({node:t},e)=>a.replaceTextRange(_(t,s),s.text.slice(..._(p[e].node,s))))},x=(a={},o)=>Object.assign(o,a),T=(a,o)=>{if(a.length>1)for(let s=1;s<a.length;s++){let l=a.at(s-1),p=a.at(s);l&&p&&o(l,p)}};const w="sort-array-includes",q=h({name:w,meta:{type:"suggestion",docs:{description:"Enforce sorted arrays before include method",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[n.alphabetical,n.natural,n["line-length"]],default:n.natural},order:{enum:[i.asc,i.desc],default:i.asc},spreadLast:{type:"boolean",default:!1}},additionalProperties:!1}],messages:{unexpectedArrayIncludesOrder:'Expected "{{second}}" to come before "{{first}}"'}},defaultOptions:[{type:n.alphabetical,order:i.asc}],create:a=>({MemberExpression:o=>{if((o.object.type===m.AST_NODE_TYPES.ArrayExpression||o.object.type===m.AST_NODE_TYPES.NewExpression)&&o.property.type===m.AST_NODE_TYPES.Identifier&&o.property.name==="includes"){let s=x(a.options.at(0),{type:n.alphabetical,order:i.asc,spreadLast:!1}),l=o.object.type===m.AST_NODE_TYPES.ArrayExpression?o.object.elements:o.object.arguments;if(l.length>1){let p=a.getSourceCode(),t=l.reduce((e,r)=>(r===null||e.at(0).push({name:r.type===m.AST_NODE_TYPES.Literal?r.raw:p.text.slice(...r.range),size:S(r.range),type:r.type,node:r}),e),[[],[]]).flat();T(t,(e,r)=>{let u;s.spreadLast&&e.node.type===m.AST_NODE_TYPES.Literal&&r.node.type===m.AST_NODE_TYPES.SpreadElement?u=!1:s.spreadLast&&e.node.type===m.AST_NODE_TYPES.SpreadElement&&r.node.type===m.AST_NODE_TYPES.Literal?u=!0:u=b(e,r,s),u&&a.report({messageId:"unexpectedArrayIncludesOrder",data:{first:e.name,second:r.name},node:r.node,fix:d=>O(d,{options:s,source:p,nodes:t})})})}}}})}),I="sort-enums",K=h({name:I,meta:{type:"suggestion",docs:{description:"Enforce sorted TypeScript enums",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[n.alphabetical,n.natural,n["line-length"]],default:n.natural},order:{enum:[i.asc,i.desc],default:i.asc}},additionalProperties:!1}],messages:{unexpectedEnumsOrder:'Expected "{{second}}" to come before "{{first}}"'}},defaultOptions:[{type:n.alphabetical,order:i.asc}],create:a=>({TSEnumDeclaration:o=>{let s=x(a.options.at(0),{type:n.alphabetical,order:i.asc});if(o.members.length>1){let l=a.getSourceCode(),p=o.members.map(t=>{let e;return t.id.type===m.AST_NODE_TYPES.Literal?e=`${t.id.value}`:e=`${l.text.slice(...t.id.range)}`,{size:S(t.range),node:t,name:e}});T(p,(t,e)=>{b(t,e,s)&&a.report({messageId:"unexpectedEnumsOrder",data:{first:t.name,second:e.name},node:e.node,fix:r=>O(r,{options:s,source:l,nodes:p})})})}}})}),D="sort-imports",X=h({name:D,meta:{type:"suggestion",docs:{description:"Enforce sorted imports",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[n.alphabetical,n.natural,n["line-length"]],default:n.natural},order:{enum:[i.asc,i.desc],default:i.asc},groups:{type:"array",default:[]},"internal-pattern":{type:"array",default:["~/**"]},"newlines-between":{enum:["ignore","always","never"],default:"always"}},additionalProperties:!1}],messages:{unexpectedImportsOrder:'Expected "{{second}}" to come before "{{first}}"',missedSpacingBetweenImports:'Missed spacing between "{{first}}" and "{{second}}" imports',extraSpacingBetweenImports:'Extra spacing between "{{first}}" and "{{second}}" imports'}},defaultOptions:[{type:n.alphabetical,order:i.asc}],create:a=>{let o=x(a.options.at(0),{"newlines-between":"always","internal-pattern":["~/**"],type:n.alphabetical,order:i.asc,groups:["unknown"]}),s=a.getSourceCode(),l=[],p=e=>{let r,u=f=>["./index.d.js","./index.d.ts","./index.js","./index.ts","./index","./","."].includes(f),d=f=>/^\.\.$|^\.\.[/\\]/.test(f),c=f=>/^\.[/\\]/.test(f),g=f=>{!r&&o.groups.flat().includes(f)&&(r=f)},E=f=>o["internal-pattern"].length&&o["internal-pattern"].some(y=>B.minimatch(f.source.value,y));return e.importKind==="type"&&(e.type===m.AST_NODE_TYPES.ImportDeclaration&&(E(e)?g("internal-type"):u(e.source.value)?g("index-type"):d(e.source.value)?g("parent-type"):c(e.source.value)&&g("sibling-type")),g("type")),!r&&e.type===m.AST_NODE_TYPES.ImportDeclaration&&(z(e.source.value)?g("builtin"):E(e)?g("internal"):u(e.source.value)?g("index"):d(e.source.value)?g("parent"):c(e.source.value)?g("sibling"):g("external")),r??"unknown"},t=e=>{let r;e.type===m.AST_NODE_TYPES.ImportDeclaration?r=e.source.value:e.moduleReference.type===m.AST_NODE_TYPES.TSExternalModuleReference&&e.moduleReference.expression.type===m.AST_NODE_TYPES.Literal?r=`${e.moduleReference.expression.value}`:r=s.text.slice(...e.moduleReference.range),l.push({size:S(e.range),group:p(e),name:r,node:e})};return{TSImportEqualsDeclaration:t,ImportDeclaration:t,"Program:exit":()=>{let e=d=>{for(let c=0,g=o.groups.length;c<g;c++){let E=o.groups[c];if(d.group===E||Array.isArray(E)&&E.includes(d.group))return c}return o.groups.length},r=(d,c)=>s.lines.slice(d.node.loc.end.line,c.node.loc.start.line-1).filter(E=>!E.trim().length).length,u=d=>{let c=[],g=l.reduce((f,y)=>{let N=e(y);return N in f?f[N]=[...f[N],y].sort((A,C)=>Number(b(A,C,o))||-1):f[N]=[y],f},{}),E=Object.keys(g).sort().reduce((f,y)=>[...f,...g[y]],[]);return E.forEach((f,y)=>{if(c.push(d.replaceTextRange(_(l.at(y).node,s),s.text.slice(..._(f.node,s)))),o["newlines-between"]!=="ignore"){let N=E.at(y+1);if(N){let A=r(l.at(y),l.at(y+1));(o["newlines-between"]==="always"&&e(f)===e(N)&&A!==0||o["newlines-between"]==="never"&&A>0)&&c.push(d.removeRange([_(l.at(y).node,s).at(1),_(l.at(y+1).node,s).at(0)-1])),o["newlines-between"]==="always"&&A>1&&c.push(d.replaceTextRange([_(l.at(y).node,s).at(1),_(l.at(y+1).node,s).at(0)-1],`
|
|
2
|
+
`)),o["newlines-between"]==="always"&&e(f)!==e(N)&&A===0&&c.push(d.insertTextAfterRange(_(l.at(y).node,s),`
|
|
3
|
+
`))}}}),c};T(l,(d,c)=>{let g=e(d),E=e(c),f=r(d,c);(g>E||g===E&&b(d,c,o))&&a.report({messageId:"unexpectedImportsOrder",data:{first:d.name,second:c.name},node:c.node,fix:u}),o["newlines-between"]==="never"&&f>0&&a.report({messageId:"extraSpacingBetweenImports",data:{first:d.name,second:c.name},node:c.node,fix:u}),o["newlines-between"]==="always"&&(g<E&&f===0?a.report({messageId:"missedSpacingBetweenImports",data:{first:d.name,second:c.name},node:c.node,fix:u}):(f>1||g===E&&f>0)&&a.report({messageId:"extraSpacingBetweenImports",data:{first:d.name,second:c.name},node:c.node,fix:u}))})}}}}),Y="sort-interfaces",G=h({name:Y,meta:{type:"suggestion",docs:{description:"Enforce sorted interface properties",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[n.alphabetical,n.natural,n["line-length"]],default:n.natural},order:{enum:[i.asc,i.desc],default:i.asc}},additionalProperties:!1}],messages:{unexpectedInterfacePropertiesOrder:'Expected "{{second}}" to come before "{{first}}"'}},defaultOptions:[{type:n.alphabetical,order:i.asc}],create:a=>({TSInterfaceBody:o=>{let s=x(a.options.at(0),{type:n.alphabetical,order:i.asc});if(o.body.length>1){let l=a.getSourceCode(),p=o.body.map(t=>{var r,u,d;let e;if(t.type===m.AST_NODE_TYPES.TSPropertySignature)if(t.key.type===m.AST_NODE_TYPES.Identifier)({name:e}=t.key);else if(t.key.type===m.AST_NODE_TYPES.Literal)e=`${t.key.value}`;else{let c;if((r=t.typeAnnotation)!=null&&r.range.at(0))c=t.typeAnnotation.range.at(0);else{let g=t.optional?1:0;c=t.range.at(1)-g}e=l.text.slice(t.range.at(0),c)}else t.type===m.AST_NODE_TYPES.TSIndexSignature?e=l.text.slice(t.range.at(0),((u=t.typeAnnotation)==null?void 0:u.range.at(0))??t.range.at(1)):e=l.text.slice(t.range.at(0),((d=t.returnType)==null?void 0:d.range.at(0))??t.range.at(1));return{size:S(t.range),node:t,name:e}});T(p,(t,e)=>{b(t,e,s)&&a.report({messageId:"unexpectedInterfacePropertiesOrder",data:{first:t.name,second:e.name},node:e.node,fix:r=>O(r,{options:s,source:l,nodes:p})})})}}})}),j="sort-jsx-props",F=h({name:j,meta:{type:"suggestion",docs:{description:"Enforce sorted JSX props",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[n.alphabetical,n.natural,n["line-length"]],default:n.natural},order:{enum:[i.asc,i.desc],default:i.asc}},additionalProperties:!1}],messages:{unexpectedJSXPropsOrder:'Expected "{{second}}" to come before "{{first}}"'}},defaultOptions:[{type:n.alphabetical,order:i.asc}],create:a=>({JSXElement:o=>{let s=x(a.options.at(0),{type:n.alphabetical,order:i.asc}),l=a.getSourceCode();o.openingElement.attributes.reduce((t,e)=>(e.type==="JSXAttribute"?t.at(-1).push(e):t.push([]),t),[[]]).forEach(t=>{let e=t.map(r=>({name:r.name.type===m.AST_NODE_TYPES.JSXNamespacedName?`${r.name.namespace.name}:${r.name.name.name}`:r.name.name,size:S(r.range),node:r}));T(e,(r,u)=>{b(r,u,s)&&a.report({messageId:"unexpectedJSXPropsOrder",data:{first:r.name,second:u.name},node:u.node,fix:d=>O(d,{options:s,source:l,nodes:e})})})})}})}),L="sort-map-elements",V=h({name:L,meta:{type:"suggestion",docs:{description:"Enforce sorted Map elements",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[n.alphabetical,n.natural,n["line-length"]],default:n.natural},order:{enum:[i.asc,i.desc],default:i.asc}},additionalProperties:!1}],messages:{unexpectedMapElementsOrder:'Expected "{{second}}" to come before "{{first}}"'}},defaultOptions:[{type:n.alphabetical,order:i.asc}],create:a=>({NewExpression:o=>{if(o.callee.type===m.AST_NODE_TYPES.Identifier&&o.callee.name==="Map"&&o.arguments[0].type===m.AST_NODE_TYPES.ArrayExpression){let s=x(a.options.at(0),{type:n.alphabetical,order:i.asc}),[{elements:l}]=o.arguments,p=a.getSourceCode();l.length>1&&l.reduce((e,r)=>(r===null||r.type===m.AST_NODE_TYPES.SpreadElement?e.push([]):e.at(-1).push(r),e),[[]]).forEach(e=>{let r=e.map(u=>{let d;if(u.type===m.AST_NODE_TYPES.ArrayExpression){let[c]=u.elements;c?c.type===m.AST_NODE_TYPES.Literal?d=c.raw:d=p.text.slice(...c.range):d=`${c}`}else d=p.text.slice(...u.range);return{size:S(u.range),node:u,name:d}});T(r,(u,d)=>{b(u,d,s)&&a.report({messageId:"unexpectedMapElementsOrder",data:{first:u.name,second:d.name},node:d.node,fix:c=>O(c,{options:s,source:p,nodes:r})})})})}}})}),R="sort-named-exports",W=h({name:R,meta:{type:"suggestion",docs:{description:"Enforce sorted named exports",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[n.alphabetical,n.natural,n["line-length"]],default:n.natural},order:{enum:[i.asc,i.desc],default:i.asc}},additionalProperties:!1}],messages:{unexpectedNamedExportsOrder:'Expected "{{second}}" to come before "{{first}}"'}},defaultOptions:[{type:n.alphabetical,order:i.asc}],create:a=>({ExportNamedDeclaration:o=>{let s=x(a.options.at(0),{type:n.alphabetical,order:i.asc});if(o.specifiers.length>1){let l=a.getSourceCode(),p=o.specifiers.map(t=>{let{range:e,local:{name:r}}=t;return{size:S(e),node:t,name:r}});T(p,(t,e)=>{b(t,e,s)&&a.report({messageId:"unexpectedNamedExportsOrder",data:{first:t.name,second:e.name},node:e.node,fix:r=>O(r,{options:s,source:l,nodes:p})})})}}})}),$="sort-named-imports",H=h({name:$,meta:{type:"suggestion",docs:{description:"Enforce sorted named imports",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[n.alphabetical,n.natural,n["line-length"]],default:n.natural},order:{enum:[i.asc,i.desc],default:i.asc}},additionalProperties:!1}],messages:{unexpectedNamedImportsOrder:'Expected "{{second}}" to come before "{{first}}"'}},defaultOptions:[{type:n.alphabetical,order:i.asc}],create:a=>({ImportDeclaration:o=>{let s=x(a.options.at(0),{type:n.alphabetical,order:i.asc});if(o.specifiers.length>1){let l=a.getSourceCode(),p=o.specifiers.map(t=>{let{range:e,local:{name:r}}=t;return{size:S(e),node:t,name:r}});T(p,(t,e)=>{b(t,e,s)&&a.report({messageId:"unexpectedNamedImportsOrder",data:{first:t.name,second:e.name},node:e.node,fix:r=>O(r,{options:s,source:l,nodes:p})})})}}})}),M="sort-object-keys",Q=h({name:M,meta:{type:"suggestion",docs:{description:"Enforce sorted object keys",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[n.alphabetical,n.natural,n["line-length"]],default:n.natural},order:{enum:[i.asc,i.desc],default:i.asc}},additionalProperties:!1}],messages:{unexpectedObjectKeysOrder:'Expected "{{second}}" to come before "{{first}}"'}},defaultOptions:[{type:n.alphabetical,order:i.asc}],create:a=>({ObjectExpression:o=>{if(o.properties.length>1){let s=x(a.options.at(0),{type:n.alphabetical,order:i.asc}),l=a.getSourceCode();(t=>t.reduce((e,r)=>{if(r.type===m.AST_NODE_TYPES.SpreadElement)return e.push([]),e;let u;r.key.type===m.AST_NODE_TYPES.Identifier?{name:u}=r.key:r.key.type===m.AST_NODE_TYPES.Literal?u=`${r.key.value}`:u=l.text.slice(...r.key.range);let d={size:S(r.range),node:r,name:u};return e.at(-1).push(d),e},[[]]))(o.properties).forEach(t=>{T(t,(e,r)=>{b(e,r,s)&&a.report({messageId:"unexpectedObjectKeysOrder",data:{first:e.name,second:r.name},node:r.node,fix:u=>O(u,{options:s,source:l,nodes:t})})})})}}})}),v="sort-union-types",Z=h({name:v,meta:{type:"suggestion",docs:{description:"Enforce sorted union types",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[n.alphabetical,n.natural,n["line-length"]],default:n.natural},order:{enum:[i.asc,i.desc],default:i.asc}},additionalProperties:!1}],messages:{unexpectedUnionTypesOrder:'Expected "{{second}}" to come before "{{first}}"'}},defaultOptions:[{type:n.alphabetical,order:i.asc}],create:a=>({TSUnionType:o=>{let s=a.getSourceCode(),l=x(a.options.at(0),{type:n.alphabetical,order:i.asc}),p=o.types.map(t=>({size:S(t.range),node:t,name:s.text.slice(...t.range)}));T(p,(t,e)=>{b(t,e,l)&&a.report({messageId:"unexpectedUnionTypesOrder",data:{first:t.name,second:e.name},node:e.node,fix:r=>O(r,{options:l,source:s,nodes:p})})})}})}),ee="eslint-plugin-perfectionist";let P=a=>{let o={[w]:["error",{spreadLast:!0}],[I]:["error"],[D]:["error",{order:["type",["builtin","external"],"internal-type","internal",["parent-type","sibling-type","index-type"],["parent","sibling","index"],"object","unknown"],"newlines-between":"always","internal-pattern":["~/**"]}],[Y]:["error"],[j]:["error"],[L]:["error"],[R]:["error"],[$]:["error"],[M]:["error"],[v]:["error"]};return{plugins:["perfectionist"],rules:Object.fromEntries(Object.entries(o).map(([s,[l,p={}]])=>[`perfectionist/${s}`,[l,Object.assign(p,a)]]))}};const te={name:ee,rules:{[w]:q,[I]:K,[D]:X,[Y]:G,[j]:F,[L]:V,[R]:W,[$]:H,[M]:Q,[v]:Z},configs:{"recommended-alphabetical":P({type:n.alphabetical,order:i.asc}),"recommended-natural":P({type:n.natural,order:i.asc}),"recommended-line-length":P({type:n["line-length"],order:i.desc})}};module.exports=te;
|