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