eslint-plugin-perfectionist
Advanced tools
Comparing version 0.3.0 to 0.4.0
@@ -1,1 +0,1 @@ | ||
"use strict";const x=require("@typescript-eslint/types"),R=require("@typescript-eslint/utils"),j=require("natural-compare-lite"),C="eslint-plugin-perfectionist",D="azat-io/eslint-plugin-perfectionist";let E=R.ESLintUtils.RuleCreator(t=>`https://github.com/${D}/blob/main/docs/rules/${t}.md`),S=t=>{let[s,a]=t;return a-s};var d=(t=>(t.natural="natural",t["line-length"]="line-length",t))(d||{}),c=(t=>(t.asc="asc",t.desc="desc",t))(c||{});let I=(t,s,a)=>(a.order==="asc"?1:-1)*(a.type===d.natural?j(t.name,s.name):t.size-s.size)>0,O=(t,s,a,l)=>{let e=[...a].sort((r,o)=>Number(I(r,o,l))||-1),n=r=>[r.range.at(0),r.range.at(1)];return a.map(({node:r},o)=>t.replaceTextRange(n(r),s.slice(...n(e[o].node))))},N=(t={},s)=>Object.assign(s,t);const T="sort-interfaces",z=E({name:T,meta:{type:"suggestion",docs:{description:"Enforce sorted interface properties",recommended:!1},messages:{unexpectedInterfacePropertiesOrder:'Expected "{{second}}" to come before "{{first}}"'},fixable:"code",schema:[{type:"object",properties:{type:{enum:[d.natural,d["line-length"]]},order:{enum:[c.asc,c.desc]}},additionalProperties:!1}]},defaultOptions:[{type:d.natural,order:c.asc}],create:t=>({TSInterfaceBody:s=>{let a=N(t.options.at(0),{type:d.natural,order:c.asc}),l=s.body.map(e=>{let n="",r=0,o=i=>{r+=i},p=i=>{o(S(i.key.range))},g=i=>{let{length:f}="[]";o(i.parameters.reduce((h,{range:A})=>h+S(A),f))},m=i=>{if(i.optional){let{length:f}="?";o(f)}},y=i=>{var h;let f=(h=i.typeAnnotation)==null?void 0:h.range;f&&o(S(f))},u=i=>{if(i.type===x.AST_NODE_TYPES.Identifier)({name:n}=i);else if(i.type===x.AST_NODE_TYPES.TSIndexSignature){let f=i.parameters.at(0);f&&u(f)}};return e.type===x.AST_NODE_TYPES.TSPropertySignature?(u(e.key),p(e),m(e),y(e)):e.type===x.AST_NODE_TYPES.TSIndexSignature&&(u(e),g(e),y(e)),{node:e,name:n,size:r}});for(let e=1;e<l.length;e++){let n=e-1,r=e,o=l.at(n),p=l.at(r);if(I(o,p,a)){let g=s.body[r];t.report({messageId:"unexpectedInterfacePropertiesOrder",data:{first:o.name,second:p.name},node:g,fix:m=>{let y=t.getSourceCode(),{text:u}=y;return O(m,u,l,a)}})}}}})}),b="sort-jsx-props",J=E({name:b,meta:{type:"suggestion",docs:{description:"Enforce sorted interface properties",recommended:!1},messages:{unexpectedJSXPropsOrder:'Expected "{{second}}" to come before "{{first}}"'},fixable:"code",schema:[{type:"object",properties:{type:{enum:[d.natural,d["line-length"]]},order:{enum:[c.asc,c.desc]}},additionalProperties:!1}]},defaultOptions:[{type:d.natural,order:c.asc}],create:t=>({JSXElement:s=>{let a=N(t.options.at(0),{type:d.natural,order:c.asc});s.openingElement.attributes.reduce((e,n)=>(n.type==="JSXAttribute"?e.at(-1).push(n):e.push([]),e),[[]]).forEach(e=>{let n=e.map(r=>({name:r.name.type===x.AST_NODE_TYPES.JSXNamespacedName?`${r.name.namespace.name}:${r.name.name.name}`:r.name.name,size:S(r.range),node:r}));for(let r=1;r<n.length;r++){let o=r-1,p=r,g=n.at(o),m=n.at(p);I(g,m,a)&&t.report({messageId:"unexpectedJSXPropsOrder",data:{first:g.name,second:m.name},node:m.node,fix:y=>{let u=t.getSourceCode(),{text:i}=u;return O(y,i,n,a)}})}})}})}),P="sort-named-imports",$=E({name:P,meta:{type:"suggestion",docs:{description:"Enforce sorted interface properties",recommended:!1},messages:{unexpectedNamedImportsOrder:'Expected "{{second}}" to come before "{{first}}"'},fixable:"code",schema:[{type:"object",properties:{type:{enum:[d.natural,d["line-length"]]},order:{enum:[c.asc,c.desc]}},additionalProperties:!1}]},defaultOptions:[{type:d.natural,order:c.asc}],create:t=>({ImportDeclaration:s=>{let a=N(t.options.at(0),{type:d.natural,order:c.asc});if(s.specifiers.length>1){let l=s.specifiers.map(e=>{let{range:n,local:{name:r}}=e;return{size:S(n),node:e,name:r}});for(let e=1;e<l.length;e++){let n=e-1,r=e,o=l.at(n),p=l.at(r);if(I(o,p,a)){let g=s.specifiers[r];t.report({messageId:"unexpectedNamedImportsOrder",data:{first:o.name,second:p.name},node:g,fix:m=>{let y=t.getSourceCode(),{text:u}=y;return O(m,u,l,a)}})}}}}})});let _=t=>{let s={[T]:["error"],[b]:["error"],[P]:["error"]};return Object.fromEntries(Object.entries(s).map(([a,[l,e]])=>[a,[l,Object.assign(e,t)]]))};const v={name:C,rules:{[T]:z,[b]:J,[P]:$},configs:{"recommended-natural":_({type:"natural",order:"asc"}),"recommended-line-length":_({type:"line-length",order:"desc"})}};module.exports=v; | ||
"use strict";const h=require("@typescript-eslint/types"),j=require("@typescript-eslint/utils"),U=require("natural-compare-lite"),z="eslint-plugin-perfectionist",D="azat-io/eslint-plugin-perfectionist";let S=j.ESLintUtils.RuleCreator(t=>`https://github.com/${D}/blob/main/docs/rules/${t}.md`),x=t=>{let[n,a]=t;return a-n};var i=(t=>(t.natural="natural",t["line-length"]="line-length",t))(i||{}),l=(t=>(t.asc="asc",t.desc="desc",t))(l||{});let E=(t,n,a)=>(a.order==="asc"?1:-1)*(a.type===i.natural?U(t.name,n.name):t.size-n.size)>0,I=(t,n,a,d)=>{let e=[...a].sort((r,o)=>Number(E(r,o,d))||-1),s=r=>[r.range.at(0),r.range.at(1)];return a.map(({node:r},o)=>t.replaceTextRange(s(r),n.slice(...s(e[o].node))))},N=(t={},n)=>Object.assign(n,t);const T="sort-interfaces",$=S({name:T,meta:{type:"suggestion",docs:{description:"Enforce sorted interface properties",recommended:!1},messages:{unexpectedInterfacePropertiesOrder:'Expected "{{second}}" to come before "{{first}}"'},fixable:"code",schema:[{type:"object",properties:{type:{enum:[i.natural,i["line-length"]]},order:{enum:[l.asc,l.desc]}},additionalProperties:!1}]},defaultOptions:[{type:i.natural,order:l.asc}],create:t=>({TSInterfaceBody:n=>{let a=N(t.options.at(0),{type:i.natural,order:l.asc}),d=n.body.map(e=>{let s="",r=0,o=c=>{r+=c},p=c=>{o(x(c.key.range))},m=c=>{let{length:y}="[]";o(c.parameters.reduce((O,{range:R})=>O+x(R),y))},u=c=>{if(c.optional){let{length:y}="?";o(y)}},g=c=>{var O;let y=(O=c.typeAnnotation)==null?void 0:O.range;y&&o(x(y))},f=c=>{if(c.type===h.AST_NODE_TYPES.Identifier)({name:s}=c);else if(c.type===h.AST_NODE_TYPES.TSIndexSignature){let y=c.parameters.at(0);y&&f(y)}};return e.type===h.AST_NODE_TYPES.TSPropertySignature?(f(e.key),p(e),u(e),g(e)):e.type===h.AST_NODE_TYPES.TSIndexSignature&&(f(e),m(e),g(e)),{node:e,name:s,size:r}});for(let e=1;e<d.length;e++){let s=e-1,r=e,o=d.at(s),p=d.at(r);if(E(o,p,a)){let m=n.body[r];t.report({messageId:"unexpectedInterfacePropertiesOrder",data:{first:o.name,second:p.name},node:m,fix:u=>{let g=t.getSourceCode(),{text:f}=g;return I(u,f,d,a)}})}}}})}),b="sort-jsx-props",v=S({name:b,meta:{type:"suggestion",docs:{description:"Enforce sorted interface properties",recommended:!1},messages:{unexpectedJSXPropsOrder:'Expected "{{second}}" to come before "{{first}}"'},fixable:"code",schema:[{type:"object",properties:{type:{enum:[i.natural,i["line-length"]]},order:{enum:[l.asc,l.desc]}},additionalProperties:!1}]},defaultOptions:[{type:i.natural,order:l.asc}],create:t=>({JSXElement:n=>{let a=N(t.options.at(0),{type:i.natural,order:l.asc});n.openingElement.attributes.reduce((e,s)=>(s.type==="JSXAttribute"?e.at(-1).push(s):e.push([]),e),[[]]).forEach(e=>{let s=e.map(r=>({name:r.name.type===h.AST_NODE_TYPES.JSXNamespacedName?`${r.name.namespace.name}:${r.name.name.name}`:r.name.name,size:x(r.range),node:r}));for(let r=1;r<s.length;r++){let o=r-1,p=r,m=s.at(o),u=s.at(p);E(m,u,a)&&t.report({messageId:"unexpectedJSXPropsOrder",data:{first:m.name,second:u.name},node:u.node,fix:g=>{let f=t.getSourceCode(),{text:c}=f;return I(g,c,s,a)}})}})}})}),P="sort-named-exports",J=S({name:P,meta:{type:"suggestion",docs:{description:"Enforce sorted interface properties",recommended:!1},messages:{unexpectedNamedExportsOrder:'Expected "{{second}}" to come before "{{first}}"'},fixable:"code",schema:[{type:"object",properties:{type:{enum:[i.natural,i["line-length"]]},order:{enum:[l.asc,l.desc]}},additionalProperties:!1}]},defaultOptions:[{type:i.natural,order:l.asc}],create:t=>({ExportNamedDeclaration:n=>{let a=N(t.options.at(0),{type:i.natural,order:l.asc});if(n.specifiers.length>1){let d=n.specifiers.map(e=>{let{range:s,local:{name:r}}=e;return{size:x(s),node:e,name:r}});for(let e=1;e<d.length;e++){let s=e-1,r=e,o=d.at(s),p=d.at(r);if(E(o,p,a)){let m=n.specifiers[r];t.report({messageId:"unexpectedNamedExportsOrder",data:{first:o.name,second:p.name},node:m,fix:u=>{let g=t.getSourceCode(),{text:f}=g;return I(u,f,d,a)}})}}}}})}),_="sort-named-imports",L=S({name:_,meta:{type:"suggestion",docs:{description:"Enforce sorted interface properties",recommended:!1},messages:{unexpectedNamedImportsOrder:'Expected "{{second}}" to come before "{{first}}"'},fixable:"code",schema:[{type:"object",properties:{type:{enum:[i.natural,i["line-length"]]},order:{enum:[l.asc,l.desc]}},additionalProperties:!1}]},defaultOptions:[{type:i.natural,order:l.asc}],create:t=>({ImportDeclaration:n=>{let a=N(t.options.at(0),{type:i.natural,order:l.asc});if(n.specifiers.length>1){let d=n.specifiers.map(e=>{let{range:s,local:{name:r}}=e;return{size:x(s),node:e,name:r}});for(let e=1;e<d.length;e++){let s=e-1,r=e,o=d.at(s),p=d.at(r);if(E(o,p,a)){let m=n.specifiers[r];t.report({messageId:"unexpectedNamedImportsOrder",data:{first:o.name,second:p.name},node:m,fix:u=>{let g=t.getSourceCode(),{text:f}=g;return I(u,f,d,a)}})}}}}})}),A="sort-union-types",M=S({name:A,meta:{type:"suggestion",docs:{description:"Enforce sorted union types",recommended:!1},messages:{unexpectedUnionTypesOrder:'Expected "{{second}}" to come before "{{first}}"'},fixable:"code",schema:[{type:"object",properties:{type:{enum:[i.natural,i["line-length"]]},order:{enum:[l.asc,l.desc]}},additionalProperties:!1}]},defaultOptions:[{type:i.natural,order:l.asc}],create:t=>({TSUnionType:n=>{let a=t.getSourceCode().text,d=N(t.options.at(0),{type:i.natural,order:l.asc}),e=n.types.map(s=>{let{range:r}=s,o=a.slice(...r);return{size:x(r),node:s,name:o}});for(let s=1;s<e.length;s++){let r=s-1,o=s,p=e.at(r),m=e.at(o);if(E(p,m,d)){let u=n.types[o];t.report({messageId:"unexpectedUnionTypesOrder",data:{first:p.name,second:m.name},node:u,fix:g=>{let f=t.getSourceCode(),{text:c}=f;return I(g,c,e,d)}})}}}})});let C=t=>{let n={[T]:["error"],[b]:["error"],[P]:["error"],[_]:["error"],[A]:["error"]};return Object.fromEntries(Object.entries(n).map(([a,[d,e={}]])=>[a,[d,Object.assign(e,t)]]))};const X={name:z,rules:{[T]:$,[b]:v,[P]:J,[_]:L,[A]:M},configs:{"recommended-natural":C({type:"natural",order:"asc"}),"recommended-line-length":C({type:"line-length",order:"desc"})}};module.exports=X; |
{ | ||
"name": "eslint-plugin-perfectionist", | ||
"description": "ESLint plugin for code formatting in perfectionist style", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"homepage": "https://eslint-plugin-perfectionist.azat.io", | ||
@@ -6,0 +6,0 @@ "repository": "azat-io/eslint-plugin-perfectionist", |
@@ -29,2 +29,3 @@ # ESLint Plugin Perfectionist | ||
| [sort-jsx-props](https://eslint-plugin-perfectionist.azat.io/rules/sort-jsx-props) | Enforce sorted JSX props | ✅ | 🔧 | | ||
| [sort-named-exports](https://eslint-plugin-perfectionist.azat.io/rules/sort-named-exports) | Enforce sorted named exports | ✅ | 🔧 | | ||
| [sort-named-imports](https://eslint-plugin-perfectionist.azat.io/rules/sort-named-imports) | Enforce sorted named imports | ✅ | 🔧 | | ||
@@ -31,0 +32,0 @@ | [sort-union-types](https://eslint-plugin-perfectionist.azat.io/rules/sort-union-types) | Enforce sorted union types | ✅ | 🔧 | |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
18932
439
40