eslint-plugin-perfectionist 0.5.0 → 0.6.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 -0
- package/dist/configs/recommended-alphabetical.mjs +13 -0
- package/dist/configs/recommended-line-length.js +1 -0
- package/dist/configs/recommended-line-length.mjs +13 -0
- package/dist/configs/recommended-natural.js +1 -0
- package/dist/configs/recommended-natural.mjs +13 -0
- package/dist/index.js +1 -1
- package/dist/index.mjs +313 -230
- package/package.json +15 -3
- package/readme.md +1 -0
|
@@ -0,0 +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;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import e from "../index.mjs";
|
|
2
|
+
import "@typescript-eslint/types";
|
|
3
|
+
import "@typescript-eslint/utils";
|
|
4
|
+
import "natural-compare-lite";
|
|
5
|
+
const m = {
|
|
6
|
+
plugins: {
|
|
7
|
+
perfectionist: e
|
|
8
|
+
},
|
|
9
|
+
rules: e.configs["recommended-alphabetical"].rules
|
|
10
|
+
};
|
|
11
|
+
export {
|
|
12
|
+
m as default
|
|
13
|
+
};
|
|
@@ -0,0 +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;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import e from "../index.mjs";
|
|
2
|
+
import "@typescript-eslint/types";
|
|
3
|
+
import "@typescript-eslint/utils";
|
|
4
|
+
import "natural-compare-lite";
|
|
5
|
+
const m = {
|
|
6
|
+
plugins: {
|
|
7
|
+
perfectionist: e
|
|
8
|
+
},
|
|
9
|
+
rules: e.configs["recommended-line-length"].rules
|
|
10
|
+
};
|
|
11
|
+
export {
|
|
12
|
+
m as default
|
|
13
|
+
};
|
|
@@ -0,0 +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;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import r from "../index.mjs";
|
|
2
|
+
import "@typescript-eslint/types";
|
|
3
|
+
import "@typescript-eslint/utils";
|
|
4
|
+
import "natural-compare-lite";
|
|
5
|
+
const i = {
|
|
6
|
+
plugins: {
|
|
7
|
+
perfectionist: r
|
|
8
|
+
},
|
|
9
|
+
rules: r.configs["recommended-natural"].rules
|
|
10
|
+
};
|
|
11
|
+
export {
|
|
12
|
+
i as default
|
|
13
|
+
};
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";const f=require("@typescript-eslint/types"),Y=require("@typescript-eslint/utils"),L=require("natural-compare-lite"),j="eslint-plugin-perfectionist",C="azat-io/eslint-plugin-perfectionist";let y=Y.ESLintUtils.RuleCreator(r=>`https://github.com/${C}/blob/main/docs/rules/${r}.md`),E=r=>{let[o,l]=r;return l-o};var n=(r=>(r.natural="natural",r["line-length"]="line-length",r))(n||{}),d=(r=>(r.asc="asc",r.desc="desc",r))(d||{});let m=(r,o,l)=>(l.order==="asc"?1:-1)*(l.type===n.natural?L(r.name,o.name):r.size-o.size)>0,S=(r,o,l,p)=>{let s=[...l].sort((t,a)=>Number(m(t,a,p))||-1);p.spreadLast&&s.forEach((t,a)=>{t.node.type===f.AST_NODE_TYPES.SpreadElement&&s.push(s.splice(a,1).at(0))});let e=t=>[t.range.at(0),t.range.at(1)];return l.map(({node:t},a)=>r.replaceTextRange(e(t),o.slice(...e(s[a].node))))},T=(r={},o)=>Object.assign(o,r);const _="sort-array-includes",M=y({name:_,meta:{type:"suggestion",docs:{description:"Enforce sorted arrays before include method",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[n.natural,n["line-length"]],default:n.natural},order:{enum:[d.asc,d.desc],default:d.asc},spreadLast:{type:"boolean",default:!1}},additionalProperties:!1}],messages:{unexpectedArrayIncludesOrder:'Expected "{{second}}" to come before "{{first}}"'}},defaultOptions:[{type:n.natural,order:d.asc}],create:r=>({MemberExpression:o=>{if((o.object.type===f.AST_NODE_TYPES.ArrayExpression||o.object.type===f.AST_NODE_TYPES.NewExpression)&&o.property.type===f.AST_NODE_TYPES.Identifier&&o.property.name==="includes"){let l=T(r.options.at(0),{type:n.natural,order:d.asc,spreadLast:!1}),p=o.object.type===f.AST_NODE_TYPES.ArrayExpression?o.object.elements:o.object.arguments;if(p.length>1){let s=r.getSourceCode().text,e=p.reduce((t,a)=>(a===null||t.at(0).push({name:a.type===f.AST_NODE_TYPES.Literal?a.raw:s.slice(...a.range),size:E(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),c;l.spreadLast&&a.node.type===f.AST_NODE_TYPES.Literal&&i.node.type===f.AST_NODE_TYPES.SpreadElement?c=!1:l.spreadLast&&a.node.type===f.AST_NODE_TYPES.SpreadElement&&i.node.type===f.AST_NODE_TYPES.Literal?c=!0:c=m(a,i,l),c&&r.report({messageId:"unexpectedArrayIncludesOrder",data:{first:a.name,second:i.name},node:i.node,fix:u=>{let g=r.getSourceCode(),{text:x}=g;return S(u,x,e,l)}})}}}}})}),N="sort-interfaces",R=y({name:N,meta:{type:"suggestion",docs:{description:"Enforce sorted interface properties",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[n.natural,n["line-length"]],default:n.natural},order:{enum:[d.asc,d.desc],default:d.asc}},additionalProperties:!1}],messages:{unexpectedInterfacePropertiesOrder:'Expected "{{second}}" to come before "{{first}}"'}},defaultOptions:[{type:n.natural,order:d.asc}],create:r=>({TSInterfaceBody:o=>{let l=T(r.options.at(0),{type:n.natural,order:d.asc});if(o.body.length>1){let p=r.getSourceCode().text,s=o.body.map(e=>{var a,i,c;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 g=e.optional?1:0;u=e.range.at(1)-g}t=p.slice(e.range.at(0),u)}else e.type===f.AST_NODE_TYPES.TSIndexSignature?t=p.slice(e.range.at(0),((i=e.typeAnnotation)==null?void 0:i.range.at(0))??e.range.at(1)):t=p.slice(e.range.at(0),((c=e.returnType)==null?void 0:c.range.at(0))??e.range.at(1));return{size:E(e.range),node:e,name:t}});for(let e=1;e<s.length;e++){let t=s.at(e-1),a=s.at(e);if(m(t,a,l)){let i=o.body[e];r.report({messageId:"unexpectedInterfacePropertiesOrder",data:{first:t.name,second:a.name},node:i,fix:c=>S(c,p,s,l)})}}}}})}),O="sort-jsx-props",U=y({name:O,meta:{type:"suggestion",docs:{description:"Enforce sorted JSX props",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[n.natural,n["line-length"]],default:n.natural},order:{enum:[d.asc,d.desc],default:d.asc}},additionalProperties:!1}],messages:{unexpectedJSXPropsOrder:'Expected "{{second}}" to come before "{{first}}"'}},defaultOptions:[{type:n.natural,order:d.asc}],create:r=>({JSXElement:o=>{let l=T(r.options.at(0),{type:n.natural,order:d.asc});o.openingElement.attributes.reduce((s,e)=>(e.type==="JSXAttribute"?s.at(-1).push(e):s.push([]),s),[[]]).forEach(s=>{let e=s.map(t=>({name:t.name.type===f.AST_NODE_TYPES.JSXNamespacedName?`${t.name.namespace.name}:${t.name.name.name}`:t.name.name,size:E(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,l)&&r.report({messageId:"unexpectedJSXPropsOrder",data:{first:a.name,second:i.name},node:i.node,fix:c=>{let u=r.getSourceCode().text;return S(c,u,e,l)}})}})}})}),h="sort-map-elements",$=y({name:h,meta:{type:"suggestion",docs:{description:"Enforce sorted Map elements",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[n.natural,n["line-length"]],default:n.natural},order:{enum:[d.asc,d.desc],default:d.asc}},additionalProperties:!1}],messages:{unexpectedMapElementsOrder:'Expected "{{second}}" to come before "{{first}}"'}},defaultOptions:[{type:n.natural,order:d.asc}],create:r=>({NewExpression:o=>{if(o.callee.type===f.AST_NODE_TYPES.Identifier&&o.callee.name==="Map"&&o.arguments[0].type===f.AST_NODE_TYPES.ArrayExpression){let l=T(r.options.at(0),{type:n.natural,order:d.asc}),[{elements:p}]=o.arguments,s=r.getSourceCode().text;p.length>1&&p.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 c;if(i.type===f.AST_NODE_TYPES.ArrayExpression){let[u]=i.elements;u?u.type===f.AST_NODE_TYPES.Literal?c=u.raw:c=s.slice(...u.range):c=`${u}`}else c=s.slice(...i.range);return{size:E(i.range),node:i,name:c}});for(let i=1;i<a.length;i++){let c=a.at(i-1),u=a.at(i);m(c,u,l)&&r.report({messageId:"unexpectedMapElementsOrder",data:{first:c.name,second:u.name},node:u.node,fix:g=>{let x=r.getSourceCode(),{text:I}=x;return S(g,I,a,l)}})}})}}})}),A="sort-named-exports",v=y({name:A,meta:{type:"suggestion",docs:{description:"Enforce sorted named exports",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[n.natural,n["line-length"]],default:n.natural},order:{enum:[d.asc,d.desc],default:d.asc}},additionalProperties:!1}],messages:{unexpectedNamedExportsOrder:'Expected "{{second}}" to come before "{{first}}"'}},defaultOptions:[{type:n.natural,order:d.asc}],create:r=>({ExportNamedDeclaration:o=>{let l=T(r.options.at(0),{type:n.natural,order:d.asc});if(o.specifiers.length>1){let p=o.specifiers.map(s=>{let{range:e,local:{name:t}}=s;return{size:E(e),node:s,name:t}});for(let s=1;s<p.length;s++){let e=p.at(s-1),t=p.at(s);if(m(e,t,l)){let a=o.specifiers[s];r.report({messageId:"unexpectedNamedExportsOrder",data:{first:e.name,second:t.name},node:a,fix:i=>{let c=r.getSourceCode(),{text:u}=c;return S(i,u,p,l)}})}}}}})}),b="sort-named-imports",z=y({name:b,meta:{type:"suggestion",docs:{description:"Enforce sorted named imports",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[n.natural,n["line-length"]],default:n.natural},order:{enum:[d.asc,d.desc],default:d.asc}},additionalProperties:!1}],messages:{unexpectedNamedImportsOrder:'Expected "{{second}}" to come before "{{first}}"'}},defaultOptions:[{type:n.natural,order:d.asc}],create:r=>({ImportDeclaration:o=>{let l=T(r.options.at(0),{type:n.natural,order:d.asc});if(o.specifiers.length>1){let p=o.specifiers.map(s=>{let{range:e,local:{name:t}}=s;return{size:E(e),node:s,name:t}});for(let s=1;s<p.length;s++){let e=p.at(s-1),t=p.at(s);if(m(e,t,l)){let a=o.specifiers[s];r.report({messageId:"unexpectedNamedImportsOrder",data:{first:e.name,second:t.name},node:a,fix:i=>{let c=r.getSourceCode(),{text:u}=c;return S(i,u,p,l)}})}}}}})}),P="sort-union-types",J=y({name:P,meta:{type:"suggestion",docs:{description:"Enforce sorted union types",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[n.natural,n["line-length"]],default:n.natural},order:{enum:[d.asc,d.desc],default:d.asc}},additionalProperties:!1}],messages:{unexpectedUnionTypesOrder:'Expected "{{second}}" to come before "{{first}}"'}},defaultOptions:[{type:n.natural,order:d.asc}],create:r=>({TSUnionType:o=>{let l=r.getSourceCode().text,p=T(r.options.at(0),{type:n.natural,order:d.asc}),s=o.types.map(e=>{let{range:t}=e,a=l.slice(...t);return{size:E(t),node:e,name:a}});for(let e=1;e<s.length;e++){let t=s.at(e-1),a=s.at(e);if(m(t,a,p)){let i=o.types[e];r.report({messageId:"unexpectedUnionTypesOrder",data:{first:t.name,second:a.name},node:i,fix:c=>{let u=r.getSourceCode(),{text:g}=u;return S(c,g,s,p)}})}}}})});let D=r=>{let o={[_]:["error",{spreadLast:!0}],[N]:["error"],[O]:["error"],[h]:["error"],[A]:["error"],[b]:["error"],[P]:["error"]};return Object.fromEntries(Object.entries(o).map(([l,[p,s={}]])=>[l,[p,Object.assign(s,r)]]))};const X={name:j,rules:{[_]:M,[N]:R,[O]:U,[h]:$,[A]:v,[b]:z,[P]:J},configs:{"recommended-natural":D({type:n.natural,order:d.asc}),"recommended-line-length":D({type:n["line-length"],order:d.desc})}};module.exports=X;
|
|
1
|
+
"use strict";const u=require("@typescript-eslint/types"),Y=require("@typescript-eslint/utils"),L=require("natural-compare-lite"),C="eslint-plugin-perfectionist",M="azat-io/eslint-plugin-perfectionist";let g=Y.ESLintUtils.RuleCreator(a=>`https://github.com/${M}/blob/main/docs/rules/${a}.md`),y=a=>{let[l,d]=a;return d-l};var s=(a=>(a.alphabetical="alphabetical",a.natural="natural",a["line-length"]="line-length",a))(s||{}),n=(a=>(a.asc="asc",a.desc="desc",a))(n||{});let m=(a,l,d)=>{let p=d.order==="asc"?1:-1,o;return d.type===s.alphabetical?o=(t,e)=>t.name.localeCompare(e.name):d.type===s.natural?o=(t,e)=>L(t.name,e.name):o=(t,e)=>t.size-e.size,p*o(a,l)>0},E=(a,l,d,p)=>{let o=[...d].sort((e,r)=>Number(m(e,r,p))||-1);p.spreadLast&&o.forEach((e,r)=>{e.node.type===u.AST_NODE_TYPES.SpreadElement&&o.push(o.splice(r,1).at(0))});let t=e=>[e.range.at(0),e.range.at(1)];return d.map(({node:e},r)=>a.replaceTextRange(t(e),l.slice(...t(o[r].node))))},h=(a={},l)=>Object.assign(l,a);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:[s.alphabetical,s.natural,s["line-length"]],default:s.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:s.alphabetical,order:n.asc}],create:a=>({MemberExpression:l=>{if((l.object.type===u.AST_NODE_TYPES.ArrayExpression||l.object.type===u.AST_NODE_TYPES.NewExpression)&&l.property.type===u.AST_NODE_TYPES.Identifier&&l.property.name==="includes"){let d=h(a.options.at(0),{type:s.alphabetical,order:n.asc,spreadLast:!1}),p=l.object.type===u.AST_NODE_TYPES.ArrayExpression?l.object.elements:l.object.arguments;if(p.length>1){let o=a.getSourceCode().text,t=p.reduce((e,r)=>(r===null||e.at(0).push({name:r.type===u.AST_NODE_TYPES.Literal?r.raw:o.slice(...r.range),size:y(r.range),type:r.type,node:r}),e),[[],[]]).flat();for(let e=1;e<t.length;e++){let r=t.at(e-1),i=t.at(e),c;d.spreadLast&&r.node.type===u.AST_NODE_TYPES.Literal&&i.node.type===u.AST_NODE_TYPES.SpreadElement?c=!1:d.spreadLast&&r.node.type===u.AST_NODE_TYPES.SpreadElement&&i.node.type===u.AST_NODE_TYPES.Literal?c=!0:c=m(r,i,d),c&&a.report({messageId:"unexpectedArrayIncludesOrder",data:{first:r.name,second:i.name},node:i.node,fix:f=>{let b=a.getSourceCode(),{text:S}=b;return E(f,S,t,d)}})}}}}})}),_="sort-interfaces",$=g({name:_,meta:{type:"suggestion",docs:{description:"Enforce sorted interface properties",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[s.alphabetical,s.natural,s["line-length"]],default:s.natural},order:{enum:[n.asc,n.desc],default:n.asc}},additionalProperties:!1}],messages:{unexpectedInterfacePropertiesOrder:'Expected "{{second}}" to come before "{{first}}"'}},defaultOptions:[{type:s.alphabetical,order:n.asc}],create:a=>({TSInterfaceBody:l=>{let d=h(a.options.at(0),{type:s.alphabetical,order:n.asc});if(l.body.length>1){let p=a.getSourceCode().text,o=l.body.map(t=>{var r,i,c;let e;if(t.type===u.AST_NODE_TYPES.TSPropertySignature)if(t.key.type===u.AST_NODE_TYPES.Identifier)({name:e}=t.key);else if(t.key.type===u.AST_NODE_TYPES.Literal)e=`${t.key.value}`;else{let f;if((r=t.typeAnnotation)!=null&&r.range.at(0))f=t.typeAnnotation.range.at(0);else{let b=t.optional?1:0;f=t.range.at(1)-b}e=p.slice(t.range.at(0),f)}else t.type===u.AST_NODE_TYPES.TSIndexSignature?e=p.slice(t.range.at(0),((i=t.typeAnnotation)==null?void 0:i.range.at(0))??t.range.at(1)):e=p.slice(t.range.at(0),((c=t.returnType)==null?void 0:c.range.at(0))??t.range.at(1));return{size:y(t.range),node:t,name:e}});for(let t=1;t<o.length;t++){let e=o.at(t-1),r=o.at(t);if(m(e,r,d)){let i=l.body[t];a.report({messageId:"unexpectedInterfacePropertiesOrder",data:{first:e.name,second:r.name},node:i,fix:c=>E(c,p,o,d)})}}}}})}),x="sort-jsx-props",U=g({name:x,meta:{type:"suggestion",docs:{description:"Enforce sorted JSX props",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[s.alphabetical,s.natural,s["line-length"]],default:s.natural},order:{enum:[n.asc,n.desc],default:n.asc}},additionalProperties:!1}],messages:{unexpectedJSXPropsOrder:'Expected "{{second}}" to come before "{{first}}"'}},defaultOptions:[{type:s.alphabetical,order:n.asc}],create:a=>({JSXElement:l=>{let d=h(a.options.at(0),{type:s.alphabetical,order:n.asc});l.openingElement.attributes.reduce((o,t)=>(t.type==="JSXAttribute"?o.at(-1).push(t):o.push([]),o),[[]]).forEach(o=>{let t=o.map(e=>({name:e.name.type===u.AST_NODE_TYPES.JSXNamespacedName?`${e.name.namespace.name}:${e.name.name.name}`:e.name.name,size:y(e.range),node:e}));for(let e=1;e<t.length;e++){let r=t.at(e-1),i=t.at(e);m(r,i,d)&&a.report({messageId:"unexpectedJSXPropsOrder",data:{first:r.name,second:i.name},node:i.node,fix:c=>{let f=a.getSourceCode().text;return E(c,f,t,d)}})}})}})}),N="sort-map-elements",k=g({name:N,meta:{type:"suggestion",docs:{description:"Enforce sorted Map elements",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[s.alphabetical,s.natural,s["line-length"]],default:s.natural},order:{enum:[n.asc,n.desc],default:n.asc}},additionalProperties:!1}],messages:{unexpectedMapElementsOrder:'Expected "{{second}}" to come before "{{first}}"'}},defaultOptions:[{type:s.alphabetical,order:n.asc}],create:a=>({NewExpression:l=>{if(l.callee.type===u.AST_NODE_TYPES.Identifier&&l.callee.name==="Map"&&l.arguments[0].type===u.AST_NODE_TYPES.ArrayExpression){let d=h(a.options.at(0),{type:s.alphabetical,order:n.asc}),[{elements:p}]=l.arguments,o=a.getSourceCode().text;p.length>1&&p.reduce((e,r)=>(r===null||r.type===u.AST_NODE_TYPES.SpreadElement?e.push([]):e.at(-1).push(r),e),[[]]).forEach(e=>{let r=e.map(i=>{let c;if(i.type===u.AST_NODE_TYPES.ArrayExpression){let[f]=i.elements;f?f.type===u.AST_NODE_TYPES.Literal?c=f.raw:c=o.slice(...f.range):c=`${f}`}else c=o.slice(...i.range);return{size:y(i.range),node:i,name:c}});for(let i=1;i<r.length;i++){let c=r.at(i-1),f=r.at(i);m(c,f,d)&&a.report({messageId:"unexpectedMapElementsOrder",data:{first:c.name,second:f.name},node:f.node,fix:b=>{let S=a.getSourceCode(),{text:I}=S;return E(b,I,r,d)}})}})}}})}),A="sort-named-exports",z=g({name:A,meta:{type:"suggestion",docs:{description:"Enforce sorted named exports",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[s.alphabetical,s.natural,s["line-length"]],default:s.natural},order:{enum:[n.asc,n.desc],default:n.asc}},additionalProperties:!1}],messages:{unexpectedNamedExportsOrder:'Expected "{{second}}" to come before "{{first}}"'}},defaultOptions:[{type:s.alphabetical,order:n.asc}],create:a=>({ExportNamedDeclaration:l=>{let d=h(a.options.at(0),{type:s.alphabetical,order:n.asc});if(l.specifiers.length>1){let p=l.specifiers.map(o=>{let{range:t,local:{name:e}}=o;return{size:y(t),node:o,name:e}});for(let o=1;o<p.length;o++){let t=p.at(o-1),e=p.at(o);if(m(t,e,d)){let r=l.specifiers[o];a.report({messageId:"unexpectedNamedExportsOrder",data:{first:t.name,second:e.name},node:r,fix:i=>{let c=a.getSourceCode(),{text:f}=c;return E(i,f,p,d)}})}}}}})}),P="sort-named-imports",v=g({name:P,meta:{type:"suggestion",docs:{description:"Enforce sorted named imports",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[s.alphabetical,s.natural,s["line-length"]],default:s.natural},order:{enum:[n.asc,n.desc],default:n.asc}},additionalProperties:!1}],messages:{unexpectedNamedImportsOrder:'Expected "{{second}}" to come before "{{first}}"'}},defaultOptions:[{type:s.alphabetical,order:n.asc}],create:a=>({ImportDeclaration:l=>{let d=h(a.options.at(0),{type:s.alphabetical,order:n.asc});if(l.specifiers.length>1){let p=l.specifiers.map(o=>{let{range:t,local:{name:e}}=o;return{size:y(t),node:o,name:e}});for(let o=1;o<p.length;o++){let t=p.at(o-1),e=p.at(o);if(m(t,e,d)){let r=l.specifiers[o];a.report({messageId:"unexpectedNamedImportsOrder",data:{first:t.name,second:e.name},node:r,fix:i=>{let c=a.getSourceCode(),{text:f}=c;return E(i,f,p,d)}})}}}}})}),D="sort-object-keys",J=g({name:D,meta:{type:"suggestion",docs:{description:"Enforce sorted object keys",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[s.alphabetical,s.natural,s["line-length"]],default:s.natural},order:{enum:[n.asc,n.desc],default:n.asc}},additionalProperties:!1}],messages:{unexpectedObjectKeysOrder:'Expected "{{second}}" to come before "{{first}}"'}},defaultOptions:[{type:s.alphabetical,order:n.asc}],create:a=>({ObjectExpression:l=>{if(l.properties.length>1){let d=h(a.options.at(0),{type:s.alphabetical,order:n.asc}),p=a.getSourceCode().text;(t=>t.reduce((e,r)=>{if(r.type===u.AST_NODE_TYPES.SpreadElement)return e.push([]),e;let i;r.key.type===u.AST_NODE_TYPES.Identifier?{name:i}=r.key:r.key.type===u.AST_NODE_TYPES.Literal?i=`${r.key.value}`:i=p.slice(...r.key.range);let c={size:y(r.range),node:r,name:i};return e.at(-1).push(c),e},[[]]))(l.properties).forEach(t=>{if(t.length>1)for(let e=1;e<t.length;e++){let r=t.at(e-1),i=t.at(e);m(r,i,d)&&a.report({messageId:"unexpectedObjectKeysOrder",data:{first:r.name,second:i.name},node:i.node,fix:c=>E(c,p,t,d)})}})}}})}),j="sort-union-types",X=g({name:j,meta:{type:"suggestion",docs:{description:"Enforce sorted union types",recommended:!1},fixable:"code",schema:[{type:"object",properties:{type:{enum:[s.alphabetical,s.natural,s["line-length"]],default:s.natural},order:{enum:[n.asc,n.desc],default:n.asc}},additionalProperties:!1}],messages:{unexpectedUnionTypesOrder:'Expected "{{second}}" to come before "{{first}}"'}},defaultOptions:[{type:s.alphabetical,order:n.asc}],create:a=>({TSUnionType:l=>{let d=a.getSourceCode().text,p=h(a.options.at(0),{type:s.alphabetical,order:n.asc}),o=l.types.map(t=>{let{range:e}=t,r=d.slice(...e);return{size:y(e),node:t,name:r}});for(let t=1;t<o.length;t++){let e=o.at(t-1),r=o.at(t);if(m(e,r,p)){let i=l.types[t];a.report({messageId:"unexpectedUnionTypesOrder",data:{first:e.name,second:r.name},node:i,fix:c=>{let f=a.getSourceCode(),{text:b}=f;return E(c,b,o,p)}})}}}})});let O=a=>{let l={[T]:["error",{spreadLast:!0}],[_]:["error"],[x]:["error"],[N]:["error"],[A]:["error"],[P]:["error"],[D]:["error"],[j]:["error"]};return Object.fromEntries(Object.entries(l).map(([d,[p,o={}]])=>[d,[p,Object.assign(o,a)]]))};const w={name:C,rules:{[T]:R,[_]:$,[x]:U,[N]:k,[A]:z,[P]:v,[D]:J,[j]:X},configs:{"recommended-alphabetical":O({type:s.alphabetical,order:n.asc}),"recommended-natural":O({type:s.natural,order:n.asc}),"recommended-line-length":O({type:s["line-length"],order:n.desc})}};module.exports=w;
|
package/dist/index.mjs
CHANGED
|
@@ -1,25 +1,28 @@
|
|
|
1
|
-
import { AST_NODE_TYPES as
|
|
2
|
-
import { ESLintUtils as
|
|
3
|
-
import
|
|
4
|
-
const
|
|
5
|
-
let
|
|
6
|
-
(r) => `https://github.com/${
|
|
7
|
-
),
|
|
8
|
-
let [
|
|
9
|
-
return
|
|
1
|
+
import { AST_NODE_TYPES as u } from "@typescript-eslint/types";
|
|
2
|
+
import { ESLintUtils as R } from "@typescript-eslint/utils";
|
|
3
|
+
import $ from "natural-compare-lite";
|
|
4
|
+
const U = "eslint-plugin-perfectionist", k = "azat-io/eslint-plugin-perfectionist";
|
|
5
|
+
let g = R.RuleCreator(
|
|
6
|
+
(r) => `https://github.com/${k}/blob/main/docs/rules/${r}.md`
|
|
7
|
+
), y = (r) => {
|
|
8
|
+
let [l, d] = r;
|
|
9
|
+
return d - l;
|
|
10
10
|
};
|
|
11
|
-
var
|
|
12
|
-
let m = (r,
|
|
13
|
-
let
|
|
14
|
-
|
|
15
|
-
|
|
11
|
+
var s = /* @__PURE__ */ ((r) => (r.alphabetical = "alphabetical", r.natural = "natural", r["line-length"] = "line-length", r))(s || {}), n = /* @__PURE__ */ ((r) => (r.asc = "asc", r.desc = "desc", r))(n || {});
|
|
12
|
+
let m = (r, l, d) => {
|
|
13
|
+
let p = d.order === "asc" ? 1 : -1, o;
|
|
14
|
+
return d.type === s.alphabetical ? o = (t, e) => t.name.localeCompare(e.name) : d.type === s.natural ? o = (t, e) => $(t.name, e.name) : o = (t, e) => t.size - e.size, p * o(r, l) > 0;
|
|
15
|
+
}, h = (r, l, d, p) => {
|
|
16
|
+
let o = [...d].sort((e, a) => Number(m(e, a, p)) || -1);
|
|
17
|
+
p.spreadLast && o.forEach((e, a) => {
|
|
18
|
+
e.node.type === u.SpreadElement && o.push(o.splice(a, 1).at(0));
|
|
16
19
|
});
|
|
17
|
-
let
|
|
18
|
-
return
|
|
19
|
-
({ node:
|
|
20
|
+
let t = (e) => [e.range.at(0), e.range.at(1)];
|
|
21
|
+
return d.map(
|
|
22
|
+
({ node: e }, a) => r.replaceTextRange(t(e), l.slice(...t(o[a].node)))
|
|
20
23
|
);
|
|
21
|
-
},
|
|
22
|
-
const N = "sort-array-includes",
|
|
24
|
+
}, b = (r = {}, l) => Object.assign(l, r);
|
|
25
|
+
const N = "sort-array-includes", z = g({
|
|
23
26
|
name: N,
|
|
24
27
|
meta: {
|
|
25
28
|
type: "suggestion",
|
|
@@ -33,12 +36,12 @@ const N = "sort-array-includes", $ = y({
|
|
|
33
36
|
type: "object",
|
|
34
37
|
properties: {
|
|
35
38
|
type: {
|
|
36
|
-
enum: [
|
|
37
|
-
default:
|
|
39
|
+
enum: [s.alphabetical, s.natural, s["line-length"]],
|
|
40
|
+
default: s.natural
|
|
38
41
|
},
|
|
39
42
|
order: {
|
|
40
|
-
enum: [
|
|
41
|
-
default:
|
|
43
|
+
enum: [n.asc, n.desc],
|
|
44
|
+
default: n.asc
|
|
42
45
|
},
|
|
43
46
|
spreadLast: {
|
|
44
47
|
type: "boolean",
|
|
@@ -54,40 +57,40 @@ const N = "sort-array-includes", $ = y({
|
|
|
54
57
|
},
|
|
55
58
|
defaultOptions: [
|
|
56
59
|
{
|
|
57
|
-
type:
|
|
58
|
-
order:
|
|
60
|
+
type: s.alphabetical,
|
|
61
|
+
order: n.asc
|
|
59
62
|
}
|
|
60
63
|
],
|
|
61
64
|
create: (r) => ({
|
|
62
|
-
MemberExpression: (
|
|
63
|
-
if ((
|
|
64
|
-
let
|
|
65
|
-
type:
|
|
66
|
-
order:
|
|
65
|
+
MemberExpression: (l) => {
|
|
66
|
+
if ((l.object.type === u.ArrayExpression || l.object.type === u.NewExpression) && l.property.type === u.Identifier && l.property.name === "includes") {
|
|
67
|
+
let d = b(r.options.at(0), {
|
|
68
|
+
type: s.alphabetical,
|
|
69
|
+
order: n.asc,
|
|
67
70
|
spreadLast: !1
|
|
68
|
-
}), p =
|
|
71
|
+
}), p = l.object.type === u.ArrayExpression ? l.object.elements : l.object.arguments;
|
|
69
72
|
if (p.length > 1) {
|
|
70
|
-
let
|
|
71
|
-
(
|
|
72
|
-
name: a.type ===
|
|
73
|
-
size:
|
|
73
|
+
let o = r.getSourceCode().text, t = p.reduce(
|
|
74
|
+
(e, a) => (a === null || e.at(0).push({
|
|
75
|
+
name: a.type === u.Literal ? a.raw : o.slice(...a.range),
|
|
76
|
+
size: y(a.range),
|
|
74
77
|
type: a.type,
|
|
75
78
|
node: a
|
|
76
|
-
}),
|
|
79
|
+
}), e),
|
|
77
80
|
[[], []]
|
|
78
81
|
).flat();
|
|
79
|
-
for (let
|
|
80
|
-
let a =
|
|
81
|
-
|
|
82
|
+
for (let e = 1; e < t.length; e++) {
|
|
83
|
+
let a = t.at(e - 1), i = t.at(e), c;
|
|
84
|
+
d.spreadLast && a.node.type === u.Literal && i.node.type === u.SpreadElement ? c = !1 : d.spreadLast && a.node.type === u.SpreadElement && i.node.type === u.Literal ? c = !0 : c = m(a, i, d), c && r.report({
|
|
82
85
|
messageId: "unexpectedArrayIncludesOrder",
|
|
83
86
|
data: {
|
|
84
87
|
first: a.name,
|
|
85
88
|
second: i.name
|
|
86
89
|
},
|
|
87
90
|
node: i.node,
|
|
88
|
-
fix: (
|
|
89
|
-
let
|
|
90
|
-
return
|
|
91
|
+
fix: (f) => {
|
|
92
|
+
let E = r.getSourceCode(), { text: x } = E;
|
|
93
|
+
return h(f, x, t, d);
|
|
91
94
|
}
|
|
92
95
|
});
|
|
93
96
|
}
|
|
@@ -95,7 +98,7 @@ const N = "sort-array-includes", $ = y({
|
|
|
95
98
|
}
|
|
96
99
|
}
|
|
97
100
|
})
|
|
98
|
-
}), S = "sort-interfaces",
|
|
101
|
+
}), S = "sort-interfaces", T = g({
|
|
99
102
|
name: S,
|
|
100
103
|
meta: {
|
|
101
104
|
type: "suggestion",
|
|
@@ -109,12 +112,12 @@ const N = "sort-array-includes", $ = y({
|
|
|
109
112
|
type: "object",
|
|
110
113
|
properties: {
|
|
111
114
|
type: {
|
|
112
|
-
enum: [
|
|
113
|
-
default:
|
|
115
|
+
enum: [s.alphabetical, s.natural, s["line-length"]],
|
|
116
|
+
default: s.natural
|
|
114
117
|
},
|
|
115
118
|
order: {
|
|
116
|
-
enum: [
|
|
117
|
-
default:
|
|
119
|
+
enum: [n.asc, n.desc],
|
|
120
|
+
default: n.asc
|
|
118
121
|
}
|
|
119
122
|
},
|
|
120
123
|
additionalProperties: !1
|
|
@@ -126,63 +129,63 @@ const N = "sort-array-includes", $ = y({
|
|
|
126
129
|
},
|
|
127
130
|
defaultOptions: [
|
|
128
131
|
{
|
|
129
|
-
type:
|
|
130
|
-
order:
|
|
132
|
+
type: s.alphabetical,
|
|
133
|
+
order: n.asc
|
|
131
134
|
}
|
|
132
135
|
],
|
|
133
136
|
create: (r) => ({
|
|
134
|
-
TSInterfaceBody: (
|
|
135
|
-
let
|
|
136
|
-
type:
|
|
137
|
-
order:
|
|
137
|
+
TSInterfaceBody: (l) => {
|
|
138
|
+
let d = b(r.options.at(0), {
|
|
139
|
+
type: s.alphabetical,
|
|
140
|
+
order: n.asc
|
|
138
141
|
});
|
|
139
|
-
if (
|
|
140
|
-
let p = r.getSourceCode().text,
|
|
142
|
+
if (l.body.length > 1) {
|
|
143
|
+
let p = r.getSourceCode().text, o = l.body.map((t) => {
|
|
141
144
|
var a, i, c;
|
|
142
|
-
let
|
|
143
|
-
if (
|
|
144
|
-
if (
|
|
145
|
-
({ name:
|
|
146
|
-
else if (
|
|
147
|
-
|
|
145
|
+
let e;
|
|
146
|
+
if (t.type === u.TSPropertySignature)
|
|
147
|
+
if (t.key.type === u.Identifier)
|
|
148
|
+
({ name: e } = t.key);
|
|
149
|
+
else if (t.key.type === u.Literal)
|
|
150
|
+
e = `${t.key.value}`;
|
|
148
151
|
else {
|
|
149
|
-
let
|
|
150
|
-
if ((a =
|
|
151
|
-
|
|
152
|
+
let f;
|
|
153
|
+
if ((a = t.typeAnnotation) != null && a.range.at(0))
|
|
154
|
+
f = t.typeAnnotation.range.at(0);
|
|
152
155
|
else {
|
|
153
|
-
let
|
|
154
|
-
|
|
156
|
+
let E = t.optional ? 1 : 0;
|
|
157
|
+
f = t.range.at(1) - E;
|
|
155
158
|
}
|
|
156
|
-
|
|
159
|
+
e = p.slice(t.range.at(0), f);
|
|
157
160
|
}
|
|
158
161
|
else
|
|
159
|
-
|
|
162
|
+
t.type === u.TSIndexSignature ? e = p.slice(t.range.at(0), ((i = t.typeAnnotation) == null ? void 0 : i.range.at(0)) ?? t.range.at(1)) : e = p.slice(t.range.at(0), ((c = t.returnType) == null ? void 0 : c.range.at(0)) ?? t.range.at(1));
|
|
160
163
|
return {
|
|
161
|
-
size:
|
|
162
|
-
node:
|
|
163
|
-
name:
|
|
164
|
+
size: y(t.range),
|
|
165
|
+
node: t,
|
|
166
|
+
name: e
|
|
164
167
|
};
|
|
165
168
|
});
|
|
166
|
-
for (let
|
|
167
|
-
let
|
|
168
|
-
if (m(
|
|
169
|
-
let i =
|
|
169
|
+
for (let t = 1; t < o.length; t++) {
|
|
170
|
+
let e = o.at(t - 1), a = o.at(t);
|
|
171
|
+
if (m(e, a, d)) {
|
|
172
|
+
let i = l.body[t];
|
|
170
173
|
r.report({
|
|
171
174
|
messageId: "unexpectedInterfacePropertiesOrder",
|
|
172
175
|
data: {
|
|
173
|
-
first:
|
|
176
|
+
first: e.name,
|
|
174
177
|
second: a.name
|
|
175
178
|
},
|
|
176
179
|
node: i,
|
|
177
|
-
fix: (c) =>
|
|
180
|
+
fix: (c) => h(c, p, o, d)
|
|
178
181
|
});
|
|
179
182
|
}
|
|
180
183
|
}
|
|
181
184
|
}
|
|
182
185
|
}
|
|
183
186
|
})
|
|
184
|
-
}),
|
|
185
|
-
name:
|
|
187
|
+
}), j = "sort-jsx-props", _ = g({
|
|
188
|
+
name: j,
|
|
186
189
|
meta: {
|
|
187
190
|
type: "suggestion",
|
|
188
191
|
docs: {
|
|
@@ -195,12 +198,12 @@ const N = "sort-array-includes", $ = y({
|
|
|
195
198
|
type: "object",
|
|
196
199
|
properties: {
|
|
197
200
|
type: {
|
|
198
|
-
enum: [
|
|
199
|
-
default:
|
|
201
|
+
enum: [s.alphabetical, s.natural, s["line-length"]],
|
|
202
|
+
default: s.natural
|
|
200
203
|
},
|
|
201
204
|
order: {
|
|
202
|
-
enum: [
|
|
203
|
-
default:
|
|
205
|
+
enum: [n.asc, n.desc],
|
|
206
|
+
default: n.asc
|
|
204
207
|
}
|
|
205
208
|
},
|
|
206
209
|
additionalProperties: !1
|
|
@@ -212,28 +215,28 @@ const N = "sort-array-includes", $ = y({
|
|
|
212
215
|
},
|
|
213
216
|
defaultOptions: [
|
|
214
217
|
{
|
|
215
|
-
type:
|
|
216
|
-
order:
|
|
218
|
+
type: s.alphabetical,
|
|
219
|
+
order: n.asc
|
|
217
220
|
}
|
|
218
221
|
],
|
|
219
222
|
create: (r) => ({
|
|
220
|
-
JSXElement: (
|
|
221
|
-
let
|
|
222
|
-
type:
|
|
223
|
-
order:
|
|
223
|
+
JSXElement: (l) => {
|
|
224
|
+
let d = b(r.options.at(0), {
|
|
225
|
+
type: s.alphabetical,
|
|
226
|
+
order: n.asc
|
|
224
227
|
});
|
|
225
|
-
|
|
226
|
-
(
|
|
228
|
+
l.openingElement.attributes.reduce(
|
|
229
|
+
(o, t) => (t.type === "JSXAttribute" ? o.at(-1).push(t) : o.push([]), o),
|
|
227
230
|
[[]]
|
|
228
|
-
).forEach((
|
|
229
|
-
let
|
|
230
|
-
name:
|
|
231
|
-
size:
|
|
232
|
-
node:
|
|
231
|
+
).forEach((o) => {
|
|
232
|
+
let t = o.map((e) => ({
|
|
233
|
+
name: e.name.type === u.JSXNamespacedName ? `${e.name.namespace.name}:${e.name.name.name}` : e.name.name,
|
|
234
|
+
size: y(e.range),
|
|
235
|
+
node: e
|
|
233
236
|
}));
|
|
234
|
-
for (let
|
|
235
|
-
let a =
|
|
236
|
-
m(a, i,
|
|
237
|
+
for (let e = 1; e < t.length; e++) {
|
|
238
|
+
let a = t.at(e - 1), i = t.at(e);
|
|
239
|
+
m(a, i, d) && r.report({
|
|
237
240
|
messageId: "unexpectedJSXPropsOrder",
|
|
238
241
|
data: {
|
|
239
242
|
first: a.name,
|
|
@@ -241,15 +244,15 @@ const N = "sort-array-includes", $ = y({
|
|
|
241
244
|
},
|
|
242
245
|
node: i.node,
|
|
243
246
|
fix: (c) => {
|
|
244
|
-
let
|
|
245
|
-
return
|
|
247
|
+
let f = r.getSourceCode().text;
|
|
248
|
+
return h(c, f, t, d);
|
|
246
249
|
}
|
|
247
250
|
});
|
|
248
251
|
}
|
|
249
252
|
});
|
|
250
253
|
}
|
|
251
254
|
})
|
|
252
|
-
}), I = "sort-map-elements",
|
|
255
|
+
}), I = "sort-map-elements", v = g({
|
|
253
256
|
name: I,
|
|
254
257
|
meta: {
|
|
255
258
|
type: "suggestion",
|
|
@@ -263,12 +266,12 @@ const N = "sort-array-includes", $ = y({
|
|
|
263
266
|
type: "object",
|
|
264
267
|
properties: {
|
|
265
268
|
type: {
|
|
266
|
-
enum: [
|
|
267
|
-
default:
|
|
269
|
+
enum: [s.alphabetical, s.natural, s["line-length"]],
|
|
270
|
+
default: s.natural
|
|
268
271
|
},
|
|
269
272
|
order: {
|
|
270
|
-
enum: [
|
|
271
|
-
default:
|
|
273
|
+
enum: [n.asc, n.desc],
|
|
274
|
+
default: n.asc
|
|
272
275
|
}
|
|
273
276
|
},
|
|
274
277
|
additionalProperties: !1
|
|
@@ -280,46 +283,46 @@ const N = "sort-array-includes", $ = y({
|
|
|
280
283
|
},
|
|
281
284
|
defaultOptions: [
|
|
282
285
|
{
|
|
283
|
-
type:
|
|
284
|
-
order:
|
|
286
|
+
type: s.alphabetical,
|
|
287
|
+
order: n.asc
|
|
285
288
|
}
|
|
286
289
|
],
|
|
287
290
|
create: (r) => ({
|
|
288
|
-
NewExpression: (
|
|
289
|
-
if (
|
|
290
|
-
let
|
|
291
|
-
type:
|
|
292
|
-
order:
|
|
293
|
-
}), [{ elements: p }] =
|
|
291
|
+
NewExpression: (l) => {
|
|
292
|
+
if (l.callee.type === u.Identifier && l.callee.name === "Map" && l.arguments[0].type === u.ArrayExpression) {
|
|
293
|
+
let d = b(r.options.at(0), {
|
|
294
|
+
type: s.alphabetical,
|
|
295
|
+
order: n.asc
|
|
296
|
+
}), [{ elements: p }] = l.arguments, o = r.getSourceCode().text;
|
|
294
297
|
p.length > 1 && p.reduce(
|
|
295
|
-
(
|
|
298
|
+
(e, a) => (a === null || a.type === u.SpreadElement ? e.push([]) : e.at(-1).push(a), e),
|
|
296
299
|
[[]]
|
|
297
|
-
).forEach((
|
|
298
|
-
let a =
|
|
300
|
+
).forEach((e) => {
|
|
301
|
+
let a = e.map((i) => {
|
|
299
302
|
let c;
|
|
300
|
-
if (i.type ===
|
|
301
|
-
let [
|
|
302
|
-
|
|
303
|
+
if (i.type === u.ArrayExpression) {
|
|
304
|
+
let [f] = i.elements;
|
|
305
|
+
f ? f.type === u.Literal ? c = f.raw : c = o.slice(...f.range) : c = `${f}`;
|
|
303
306
|
} else
|
|
304
|
-
c =
|
|
307
|
+
c = o.slice(...i.range);
|
|
305
308
|
return {
|
|
306
|
-
size:
|
|
309
|
+
size: y(i.range),
|
|
307
310
|
node: i,
|
|
308
311
|
name: c
|
|
309
312
|
};
|
|
310
313
|
});
|
|
311
314
|
for (let i = 1; i < a.length; i++) {
|
|
312
|
-
let c = a.at(i - 1),
|
|
313
|
-
m(c,
|
|
315
|
+
let c = a.at(i - 1), f = a.at(i);
|
|
316
|
+
m(c, f, d) && r.report({
|
|
314
317
|
messageId: "unexpectedMapElementsOrder",
|
|
315
318
|
data: {
|
|
316
319
|
first: c.name,
|
|
317
|
-
second:
|
|
320
|
+
second: f.name
|
|
318
321
|
},
|
|
319
|
-
node:
|
|
320
|
-
fix: (
|
|
321
|
-
let
|
|
322
|
-
return
|
|
322
|
+
node: f.node,
|
|
323
|
+
fix: (E) => {
|
|
324
|
+
let x = r.getSourceCode(), { text: M } = x;
|
|
325
|
+
return h(E, M, a, d);
|
|
323
326
|
}
|
|
324
327
|
});
|
|
325
328
|
}
|
|
@@ -327,8 +330,8 @@ const N = "sort-array-includes", $ = y({
|
|
|
327
330
|
}
|
|
328
331
|
}
|
|
329
332
|
})
|
|
330
|
-
}),
|
|
331
|
-
name:
|
|
333
|
+
}), L = "sort-named-exports", J = g({
|
|
334
|
+
name: L,
|
|
332
335
|
meta: {
|
|
333
336
|
type: "suggestion",
|
|
334
337
|
docs: {
|
|
@@ -341,12 +344,12 @@ const N = "sort-array-includes", $ = y({
|
|
|
341
344
|
type: "object",
|
|
342
345
|
properties: {
|
|
343
346
|
type: {
|
|
344
|
-
enum: [
|
|
345
|
-
default:
|
|
347
|
+
enum: [s.alphabetical, s.natural, s["line-length"]],
|
|
348
|
+
default: s.natural
|
|
346
349
|
},
|
|
347
350
|
order: {
|
|
348
|
-
enum: [
|
|
349
|
-
default:
|
|
351
|
+
enum: [n.asc, n.desc],
|
|
352
|
+
default: n.asc
|
|
350
353
|
}
|
|
351
354
|
},
|
|
352
355
|
additionalProperties: !1
|
|
@@ -358,42 +361,42 @@ const N = "sort-array-includes", $ = y({
|
|
|
358
361
|
},
|
|
359
362
|
defaultOptions: [
|
|
360
363
|
{
|
|
361
|
-
type:
|
|
362
|
-
order:
|
|
364
|
+
type: s.alphabetical,
|
|
365
|
+
order: n.asc
|
|
363
366
|
}
|
|
364
367
|
],
|
|
365
368
|
create: (r) => ({
|
|
366
|
-
ExportNamedDeclaration: (
|
|
367
|
-
let
|
|
368
|
-
type:
|
|
369
|
-
order:
|
|
369
|
+
ExportNamedDeclaration: (l) => {
|
|
370
|
+
let d = b(r.options.at(0), {
|
|
371
|
+
type: s.alphabetical,
|
|
372
|
+
order: n.asc
|
|
370
373
|
});
|
|
371
|
-
if (
|
|
372
|
-
let p =
|
|
374
|
+
if (l.specifiers.length > 1) {
|
|
375
|
+
let p = l.specifiers.map((o) => {
|
|
373
376
|
let {
|
|
374
|
-
range:
|
|
375
|
-
local: { name:
|
|
376
|
-
} =
|
|
377
|
+
range: t,
|
|
378
|
+
local: { name: e }
|
|
379
|
+
} = o;
|
|
377
380
|
return {
|
|
378
|
-
size:
|
|
379
|
-
node:
|
|
380
|
-
name:
|
|
381
|
+
size: y(t),
|
|
382
|
+
node: o,
|
|
383
|
+
name: e
|
|
381
384
|
};
|
|
382
385
|
});
|
|
383
|
-
for (let
|
|
384
|
-
let
|
|
385
|
-
if (m(
|
|
386
|
-
let a =
|
|
386
|
+
for (let o = 1; o < p.length; o++) {
|
|
387
|
+
let t = p.at(o - 1), e = p.at(o);
|
|
388
|
+
if (m(t, e, d)) {
|
|
389
|
+
let a = l.specifiers[o];
|
|
387
390
|
r.report({
|
|
388
391
|
messageId: "unexpectedNamedExportsOrder",
|
|
389
392
|
data: {
|
|
390
|
-
first:
|
|
391
|
-
second:
|
|
393
|
+
first: t.name,
|
|
394
|
+
second: e.name
|
|
392
395
|
},
|
|
393
396
|
node: a,
|
|
394
397
|
fix: (i) => {
|
|
395
|
-
let c = r.getSourceCode(), { text:
|
|
396
|
-
return
|
|
398
|
+
let c = r.getSourceCode(), { text: f } = c;
|
|
399
|
+
return h(i, f, p, d);
|
|
397
400
|
}
|
|
398
401
|
});
|
|
399
402
|
}
|
|
@@ -401,8 +404,8 @@ const N = "sort-array-includes", $ = y({
|
|
|
401
404
|
}
|
|
402
405
|
}
|
|
403
406
|
})
|
|
404
|
-
}),
|
|
405
|
-
name:
|
|
407
|
+
}), A = "sort-named-imports", X = g({
|
|
408
|
+
name: A,
|
|
406
409
|
meta: {
|
|
407
410
|
type: "suggestion",
|
|
408
411
|
docs: {
|
|
@@ -415,12 +418,12 @@ const N = "sort-array-includes", $ = y({
|
|
|
415
418
|
type: "object",
|
|
416
419
|
properties: {
|
|
417
420
|
type: {
|
|
418
|
-
enum: [
|
|
419
|
-
default:
|
|
421
|
+
enum: [s.alphabetical, s.natural, s["line-length"]],
|
|
422
|
+
default: s.natural
|
|
420
423
|
},
|
|
421
424
|
order: {
|
|
422
|
-
enum: [
|
|
423
|
-
default:
|
|
425
|
+
enum: [n.asc, n.desc],
|
|
426
|
+
default: n.asc
|
|
424
427
|
}
|
|
425
428
|
},
|
|
426
429
|
additionalProperties: !1
|
|
@@ -432,42 +435,42 @@ const N = "sort-array-includes", $ = y({
|
|
|
432
435
|
},
|
|
433
436
|
defaultOptions: [
|
|
434
437
|
{
|
|
435
|
-
type:
|
|
436
|
-
order:
|
|
438
|
+
type: s.alphabetical,
|
|
439
|
+
order: n.asc
|
|
437
440
|
}
|
|
438
441
|
],
|
|
439
442
|
create: (r) => ({
|
|
440
|
-
ImportDeclaration: (
|
|
441
|
-
let
|
|
442
|
-
type:
|
|
443
|
-
order:
|
|
443
|
+
ImportDeclaration: (l) => {
|
|
444
|
+
let d = b(r.options.at(0), {
|
|
445
|
+
type: s.alphabetical,
|
|
446
|
+
order: n.asc
|
|
444
447
|
});
|
|
445
|
-
if (
|
|
446
|
-
let p =
|
|
448
|
+
if (l.specifiers.length > 1) {
|
|
449
|
+
let p = l.specifiers.map((o) => {
|
|
447
450
|
let {
|
|
448
|
-
range:
|
|
449
|
-
local: { name:
|
|
450
|
-
} =
|
|
451
|
+
range: t,
|
|
452
|
+
local: { name: e }
|
|
453
|
+
} = o;
|
|
451
454
|
return {
|
|
452
|
-
size:
|
|
453
|
-
node:
|
|
454
|
-
name:
|
|
455
|
+
size: y(t),
|
|
456
|
+
node: o,
|
|
457
|
+
name: e
|
|
455
458
|
};
|
|
456
459
|
});
|
|
457
|
-
for (let
|
|
458
|
-
let
|
|
459
|
-
if (m(
|
|
460
|
-
let a =
|
|
460
|
+
for (let o = 1; o < p.length; o++) {
|
|
461
|
+
let t = p.at(o - 1), e = p.at(o);
|
|
462
|
+
if (m(t, e, d)) {
|
|
463
|
+
let a = l.specifiers[o];
|
|
461
464
|
r.report({
|
|
462
465
|
messageId: "unexpectedNamedImportsOrder",
|
|
463
466
|
data: {
|
|
464
|
-
first:
|
|
465
|
-
second:
|
|
467
|
+
first: t.name,
|
|
468
|
+
second: e.name
|
|
466
469
|
},
|
|
467
470
|
node: a,
|
|
468
471
|
fix: (i) => {
|
|
469
|
-
let c = r.getSourceCode(), { text:
|
|
470
|
-
return
|
|
472
|
+
let c = r.getSourceCode(), { text: f } = c;
|
|
473
|
+
return h(i, f, p, d);
|
|
471
474
|
}
|
|
472
475
|
});
|
|
473
476
|
}
|
|
@@ -475,8 +478,82 @@ const N = "sort-array-includes", $ = y({
|
|
|
475
478
|
}
|
|
476
479
|
}
|
|
477
480
|
})
|
|
478
|
-
}),
|
|
479
|
-
name:
|
|
481
|
+
}), C = "sort-object-keys", w = g({
|
|
482
|
+
name: C,
|
|
483
|
+
meta: {
|
|
484
|
+
type: "suggestion",
|
|
485
|
+
docs: {
|
|
486
|
+
description: "Enforce sorted object keys",
|
|
487
|
+
recommended: !1
|
|
488
|
+
},
|
|
489
|
+
fixable: "code",
|
|
490
|
+
schema: [
|
|
491
|
+
{
|
|
492
|
+
type: "object",
|
|
493
|
+
properties: {
|
|
494
|
+
type: {
|
|
495
|
+
enum: [s.alphabetical, s.natural, s["line-length"]],
|
|
496
|
+
default: s.natural
|
|
497
|
+
},
|
|
498
|
+
order: {
|
|
499
|
+
enum: [n.asc, n.desc],
|
|
500
|
+
default: n.asc
|
|
501
|
+
}
|
|
502
|
+
},
|
|
503
|
+
additionalProperties: !1
|
|
504
|
+
}
|
|
505
|
+
],
|
|
506
|
+
messages: {
|
|
507
|
+
unexpectedObjectKeysOrder: 'Expected "{{second}}" to come before "{{first}}"'
|
|
508
|
+
}
|
|
509
|
+
},
|
|
510
|
+
defaultOptions: [
|
|
511
|
+
{
|
|
512
|
+
type: s.alphabetical,
|
|
513
|
+
order: n.asc
|
|
514
|
+
}
|
|
515
|
+
],
|
|
516
|
+
create: (r) => ({
|
|
517
|
+
ObjectExpression: (l) => {
|
|
518
|
+
if (l.properties.length > 1) {
|
|
519
|
+
let d = b(r.options.at(0), {
|
|
520
|
+
type: s.alphabetical,
|
|
521
|
+
order: n.asc
|
|
522
|
+
}), p = r.getSourceCode().text;
|
|
523
|
+
((t) => t.reduce(
|
|
524
|
+
(e, a) => {
|
|
525
|
+
if (a.type === u.SpreadElement)
|
|
526
|
+
return e.push([]), e;
|
|
527
|
+
let i;
|
|
528
|
+
a.key.type === u.Identifier ? { name: i } = a.key : a.key.type === u.Literal ? i = `${a.key.value}` : i = p.slice(...a.key.range);
|
|
529
|
+
let c = {
|
|
530
|
+
size: y(a.range),
|
|
531
|
+
node: a,
|
|
532
|
+
name: i
|
|
533
|
+
};
|
|
534
|
+
return e.at(-1).push(c), e;
|
|
535
|
+
},
|
|
536
|
+
[[]]
|
|
537
|
+
))(l.properties).forEach((t) => {
|
|
538
|
+
if (t.length > 1)
|
|
539
|
+
for (let e = 1; e < t.length; e++) {
|
|
540
|
+
let a = t.at(e - 1), i = t.at(e);
|
|
541
|
+
m(a, i, d) && r.report({
|
|
542
|
+
messageId: "unexpectedObjectKeysOrder",
|
|
543
|
+
data: {
|
|
544
|
+
first: a.name,
|
|
545
|
+
second: i.name
|
|
546
|
+
},
|
|
547
|
+
node: i.node,
|
|
548
|
+
fix: (c) => h(c, p, t, d)
|
|
549
|
+
});
|
|
550
|
+
}
|
|
551
|
+
});
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
})
|
|
555
|
+
}), P = "sort-union-types", D = g({
|
|
556
|
+
name: P,
|
|
480
557
|
meta: {
|
|
481
558
|
type: "suggestion",
|
|
482
559
|
docs: {
|
|
@@ -489,12 +566,12 @@ const N = "sort-array-includes", $ = y({
|
|
|
489
566
|
type: "object",
|
|
490
567
|
properties: {
|
|
491
568
|
type: {
|
|
492
|
-
enum: [
|
|
493
|
-
default:
|
|
569
|
+
enum: [s.alphabetical, s.natural, s["line-length"]],
|
|
570
|
+
default: s.natural
|
|
494
571
|
},
|
|
495
572
|
order: {
|
|
496
|
-
enum: [
|
|
497
|
-
default:
|
|
573
|
+
enum: [n.asc, n.desc],
|
|
574
|
+
default: n.asc
|
|
498
575
|
}
|
|
499
576
|
},
|
|
500
577
|
additionalProperties: !1
|
|
@@ -506,37 +583,37 @@ const N = "sort-array-includes", $ = y({
|
|
|
506
583
|
},
|
|
507
584
|
defaultOptions: [
|
|
508
585
|
{
|
|
509
|
-
type:
|
|
510
|
-
order:
|
|
586
|
+
type: s.alphabetical,
|
|
587
|
+
order: n.asc
|
|
511
588
|
}
|
|
512
589
|
],
|
|
513
590
|
create: (r) => ({
|
|
514
|
-
TSUnionType: (
|
|
515
|
-
let
|
|
516
|
-
type:
|
|
517
|
-
order:
|
|
518
|
-
}),
|
|
519
|
-
let { range:
|
|
591
|
+
TSUnionType: (l) => {
|
|
592
|
+
let d = r.getSourceCode().text, p = b(r.options.at(0), {
|
|
593
|
+
type: s.alphabetical,
|
|
594
|
+
order: n.asc
|
|
595
|
+
}), o = l.types.map((t) => {
|
|
596
|
+
let { range: e } = t, a = d.slice(...e);
|
|
520
597
|
return {
|
|
521
|
-
size:
|
|
522
|
-
node:
|
|
598
|
+
size: y(e),
|
|
599
|
+
node: t,
|
|
523
600
|
name: a
|
|
524
601
|
};
|
|
525
602
|
});
|
|
526
|
-
for (let
|
|
527
|
-
let
|
|
528
|
-
if (m(
|
|
529
|
-
let i =
|
|
603
|
+
for (let t = 1; t < o.length; t++) {
|
|
604
|
+
let e = o.at(t - 1), a = o.at(t);
|
|
605
|
+
if (m(e, a, p)) {
|
|
606
|
+
let i = l.types[t];
|
|
530
607
|
r.report({
|
|
531
608
|
messageId: "unexpectedUnionTypesOrder",
|
|
532
609
|
data: {
|
|
533
|
-
first:
|
|
610
|
+
first: e.name,
|
|
534
611
|
second: a.name
|
|
535
612
|
},
|
|
536
613
|
node: i,
|
|
537
614
|
fix: (c) => {
|
|
538
|
-
let
|
|
539
|
-
return
|
|
615
|
+
let f = r.getSourceCode(), { text: E } = f;
|
|
616
|
+
return h(c, E, o, p);
|
|
540
617
|
}
|
|
541
618
|
});
|
|
542
619
|
}
|
|
@@ -544,42 +621,48 @@ const N = "sort-array-includes", $ = y({
|
|
|
544
621
|
}
|
|
545
622
|
})
|
|
546
623
|
});
|
|
547
|
-
let
|
|
548
|
-
let
|
|
624
|
+
let O = (r) => {
|
|
625
|
+
let l = {
|
|
549
626
|
[N]: ["error", { spreadLast: !0 }],
|
|
550
627
|
[S]: ["error"],
|
|
551
|
-
[
|
|
628
|
+
[j]: ["error"],
|
|
552
629
|
[I]: ["error"],
|
|
553
|
-
[A]: ["error"],
|
|
554
630
|
[L]: ["error"],
|
|
555
|
-
[
|
|
631
|
+
[A]: ["error"],
|
|
632
|
+
[C]: ["error"],
|
|
633
|
+
[P]: ["error"]
|
|
556
634
|
};
|
|
557
635
|
return Object.fromEntries(
|
|
558
|
-
Object.entries(
|
|
559
|
-
|
|
560
|
-
[p, Object.assign(
|
|
636
|
+
Object.entries(l).map(([d, [p, o = {}]]) => [
|
|
637
|
+
d,
|
|
638
|
+
[p, Object.assign(o, r)]
|
|
561
639
|
])
|
|
562
640
|
);
|
|
563
641
|
};
|
|
564
642
|
const V = {
|
|
565
|
-
name:
|
|
643
|
+
name: U,
|
|
566
644
|
rules: {
|
|
567
|
-
[N]:
|
|
568
|
-
[S]:
|
|
569
|
-
[
|
|
570
|
-
[I]:
|
|
571
|
-
[
|
|
572
|
-
[
|
|
573
|
-
[
|
|
645
|
+
[N]: z,
|
|
646
|
+
[S]: T,
|
|
647
|
+
[j]: _,
|
|
648
|
+
[I]: v,
|
|
649
|
+
[L]: J,
|
|
650
|
+
[A]: X,
|
|
651
|
+
[C]: w,
|
|
652
|
+
[P]: D
|
|
574
653
|
},
|
|
575
654
|
configs: {
|
|
576
|
-
"recommended-
|
|
577
|
-
type:
|
|
578
|
-
order:
|
|
655
|
+
"recommended-alphabetical": O({
|
|
656
|
+
type: s.alphabetical,
|
|
657
|
+
order: n.asc
|
|
658
|
+
}),
|
|
659
|
+
"recommended-natural": O({
|
|
660
|
+
type: s.natural,
|
|
661
|
+
order: n.asc
|
|
579
662
|
}),
|
|
580
|
-
"recommended-line-length":
|
|
581
|
-
type:
|
|
582
|
-
order:
|
|
663
|
+
"recommended-line-length": O({
|
|
664
|
+
type: s["line-length"],
|
|
665
|
+
order: n.desc
|
|
583
666
|
})
|
|
584
667
|
}
|
|
585
668
|
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eslint-plugin-perfectionist",
|
|
3
3
|
"description": "ESLint plugin for code formatting in perfectionist style",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.6.0",
|
|
5
5
|
"homepage": "https://eslint-plugin-perfectionist.azat.io",
|
|
6
6
|
"repository": "azat-io/eslint-plugin-perfectionist",
|
|
7
7
|
"author": "Azat S. <to@azat.io>",
|
|
@@ -22,14 +22,26 @@
|
|
|
22
22
|
"require": "./dist/index.js",
|
|
23
23
|
"import": "./dist/index.mjs"
|
|
24
24
|
},
|
|
25
|
+
"./configs/recommended-alphabetical": {
|
|
26
|
+
"require": "./dist/configs/recommended-alphabetical.js",
|
|
27
|
+
"import": "./dist/configs/recommended-alphabetical.mjs"
|
|
28
|
+
},
|
|
29
|
+
"./configs/recommended-line-length": {
|
|
30
|
+
"require": "./dist/configs/recommended-line-length.js",
|
|
31
|
+
"import": "./dist/configs/recommended-line-length.mjs"
|
|
32
|
+
},
|
|
33
|
+
"./configs/recommended-natural": {
|
|
34
|
+
"require": "./dist/configs/recommended-natural.js",
|
|
35
|
+
"import": "./dist/configs/recommended-natural.mjs"
|
|
36
|
+
},
|
|
25
37
|
"./package.json": "./package.json"
|
|
26
38
|
},
|
|
27
39
|
"peerDependencies": {
|
|
28
40
|
"eslint": "^8.39.0"
|
|
29
41
|
},
|
|
30
42
|
"dependencies": {
|
|
31
|
-
"@typescript-eslint/types": "^5.59.
|
|
32
|
-
"@typescript-eslint/utils": "^5.59.
|
|
43
|
+
"@typescript-eslint/types": "^5.59.7",
|
|
44
|
+
"@typescript-eslint/utils": "^5.59.7",
|
|
33
45
|
"natural-compare-lite": "^1.4.0"
|
|
34
46
|
}
|
|
35
47
|
}
|
package/readme.md
CHANGED
|
@@ -30,6 +30,7 @@ npm install --save-dev eslint-plugin-perfectionist
|
|
|
30
30
|
| [sort-map-elements](https://eslint-plugin-perfectionist.azat.io/rules/sort-map-elements) | Enforce sorted Map elements | ✅ | 🔧 |
|
|
31
31
|
| [sort-named-exports](https://eslint-plugin-perfectionist.azat.io/rules/sort-named-exports) | Enforce sorted named exports | ✅ | 🔧 |
|
|
32
32
|
| [sort-named-imports](https://eslint-plugin-perfectionist.azat.io/rules/sort-named-imports) | Enforce sorted named imports | ✅ | 🔧 |
|
|
33
|
+
| [sort-object-keys](https://eslint-plugin-perfectionist.azat.io/rules/sort-object-keys) | Enforce sorted object keys | ✅ | 🔧 |
|
|
33
34
|
| [sort-union-types](https://eslint-plugin-perfectionist.azat.io/rules/sort-union-types) | Enforce sorted union types | ✅ | 🔧 |
|
|
34
35
|
|
|
35
36
|
## See also
|