eslint-plugin-perfectionist 0.1.0 → 0.2.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 -0
- package/dist/index.mjs +193 -0
- package/package.json +1 -2
- package/readme.md +6 -4
- package/dist/index.cjs.js +0 -1
- package/dist/index.es.js +0 -102
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";const u=require("@typescript-eslint/types"),h=require("@typescript-eslint/utils"),y="eslint-plugin-perfectionist",_="azat-io/eslint-plugin-perfectionist";let S=h.ESLintUtils.RuleCreator(a=>`https://github.com/${_}/blob/main/docs/rules/${a}.md`),g=a=>{let[i,o]=a;return o-i},I=(a,i,o)=>{let e=[...o].sort((t,r)=>r.size-t.size),s=t=>[t.range.at(0),t.range.at(1)];return o.map(({node:t},r)=>a.replaceTextRange(s(t),i.slice(...s(e[r].node))))};const E="sort-interfaces",z=S({name:E,meta:{type:"suggestion",docs:{description:"Enforce sorted interface properties",recommended:!1},messages:{unexpectedInterfacePropertiesOrder:'Expected "{{second}}" to come before "{{first}}"'},fixable:"code",schema:[]},defaultOptions:[],create:a=>({TSInterfaceBody:i=>{let o=i.body.map(e=>{let s="",t=0,r=n=>{t+=n},d=n=>{r(g(n.key.range))},f=n=>{let{length:p}="[]";r(n.parameters.reduce((x,{range:O})=>x+g(O),p))},c=n=>{if(n.optional){let{length:p}="?";r(p)}},m=n=>{var x;let p=(x=n.typeAnnotation)==null?void 0:x.range;p&&r(g(p))},l=n=>{if(n.type===u.AST_NODE_TYPES.Identifier)({name:s}=n);else if(n.type===u.AST_NODE_TYPES.TSIndexSignature){let p=n.parameters.at(0);p&&l(p)}};return e.type===u.AST_NODE_TYPES.TSPropertySignature?(l(e.key),d(e),c(e),m(e)):e.type===u.AST_NODE_TYPES.TSIndexSignature&&(l(e),f(e),m(e)),{node:e,name:s,size:t}});for(let e=1;e<o.length;e++){let s=e-1,t=e,r=o.at(s),d=o.at(t);if(r.size<d.size){let f=i.body[t];a.report({messageId:"unexpectedInterfacePropertiesOrder",data:{first:r.name,second:d.name},node:f,fix:c=>{let m=a.getSourceCode(),{text:l}=m;return I(c,l,o)}})}}}})}),T="sort-jsx-props",A=S({name:T,meta:{type:"suggestion",docs:{description:"Enforce sorted interface properties",recommended:!1},messages:{unexpectedJSXPropsOrder:'Expected "{{second}}" to come before "{{first}}"'},fixable:"code",schema:[]},defaultOptions:[],create:a=>({JSXElement:i=>{i.openingElement.attributes.reduce((e,s)=>(s.type==="JSXAttribute"?e.at(-1).push(s):e.push([]),e),[[]]).forEach(e=>{let s=e.map(t=>({name:t.name.type===u.AST_NODE_TYPES.JSXNamespacedName?`${t.name.namespace.name}:${t.name.name.name}`:t.name.name,size:g(t.range),node:t}));for(let t=1;t<s.length;t++){let r=t-1,d=t,f=s.at(r),c=s.at(d);f.size<c.size&&a.report({messageId:"unexpectedJSXPropsOrder",data:{first:f.name,second:c.name},node:c.node,fix:m=>{let l=a.getSourceCode(),{text:n}=l;return I(m,n,s)}})}})}})}),N="sort-named-imports",P=S({name:N,meta:{type:"suggestion",docs:{description:"Enforce sorted interface properties",recommended:!1},messages:{unexpectedNamedImportsOrder:'Expected "{{second}}" to come before "{{first}}"'},fixable:"code",schema:[]},defaultOptions:[],create:a=>({ImportDeclaration:i=>{if(i.specifiers.length>1){let o=i.specifiers.map(e=>{let{range:s,local:{name:t}}=e;return{size:g(s),node:e,name:t}});for(let e=1;e<o.length;e++){let s=e-1,t=e,r=o.at(s),d=o.at(t);if(r.size<d.size){let f=i.specifiers[t];a.report({messageId:"unexpectedNamedImportsOrder",data:{first:r.name,second:d.name},node:f,fix:c=>{let m=a.getSourceCode(),{text:l}=m;return I(c,l,o)}})}}}}})}),R={name:y,rules:{[E]:z,[T]:A,[N]:P}};module.exports=R;
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import { AST_NODE_TYPES as g } from "@typescript-eslint/types";
|
|
2
|
+
import { ESLintUtils as O } from "@typescript-eslint/utils";
|
|
3
|
+
const E = "eslint-plugin-perfectionist", R = "azat-io/eslint-plugin-perfectionist";
|
|
4
|
+
let I = O.RuleCreator(
|
|
5
|
+
(a) => `https://github.com/${R}/blob/main/docs/rules/${a}.md`
|
|
6
|
+
), u = (a) => {
|
|
7
|
+
let [i, n] = a;
|
|
8
|
+
return n - i;
|
|
9
|
+
}, S = (a, i, n) => {
|
|
10
|
+
let e = [...n].sort((t, r) => r.size - t.size), s = (t) => [
|
|
11
|
+
t.range.at(0),
|
|
12
|
+
t.range.at(1)
|
|
13
|
+
];
|
|
14
|
+
return n.map(
|
|
15
|
+
({ node: t }, r) => a.replaceTextRange(
|
|
16
|
+
s(t),
|
|
17
|
+
i.slice(...s(e[r].node))
|
|
18
|
+
)
|
|
19
|
+
);
|
|
20
|
+
};
|
|
21
|
+
const h = "sort-interfaces", T = I({
|
|
22
|
+
name: h,
|
|
23
|
+
meta: {
|
|
24
|
+
type: "suggestion",
|
|
25
|
+
docs: {
|
|
26
|
+
description: "Enforce sorted interface properties",
|
|
27
|
+
recommended: !1
|
|
28
|
+
},
|
|
29
|
+
messages: {
|
|
30
|
+
unexpectedInterfacePropertiesOrder: 'Expected "{{second}}" to come before "{{first}}"'
|
|
31
|
+
},
|
|
32
|
+
fixable: "code",
|
|
33
|
+
schema: []
|
|
34
|
+
},
|
|
35
|
+
defaultOptions: [],
|
|
36
|
+
create: (a) => ({
|
|
37
|
+
TSInterfaceBody: (i) => {
|
|
38
|
+
let n = i.body.map((e) => {
|
|
39
|
+
let s = "", t = 0, r = (o) => {
|
|
40
|
+
t += o;
|
|
41
|
+
}, d = (o) => {
|
|
42
|
+
r(u(o.key.range));
|
|
43
|
+
}, f = (o) => {
|
|
44
|
+
let { length: p } = "[]";
|
|
45
|
+
r(o.parameters.reduce((x, { range: N }) => x + u(N), p));
|
|
46
|
+
}, c = (o) => {
|
|
47
|
+
if (o.optional) {
|
|
48
|
+
let { length: p } = "?";
|
|
49
|
+
r(p);
|
|
50
|
+
}
|
|
51
|
+
}, m = (o) => {
|
|
52
|
+
var x;
|
|
53
|
+
let p = (x = o.typeAnnotation) == null ? void 0 : x.range;
|
|
54
|
+
p && r(u(p));
|
|
55
|
+
}, l = (o) => {
|
|
56
|
+
if (o.type === g.Identifier)
|
|
57
|
+
({ name: s } = o);
|
|
58
|
+
else if (o.type === g.TSIndexSignature) {
|
|
59
|
+
let p = o.parameters.at(0);
|
|
60
|
+
p && l(p);
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
return e.type === g.TSPropertySignature ? (l(e.key), d(e), c(e), m(e)) : e.type === g.TSIndexSignature && (l(e), f(e), m(e)), {
|
|
64
|
+
node: e,
|
|
65
|
+
name: s,
|
|
66
|
+
size: t
|
|
67
|
+
};
|
|
68
|
+
});
|
|
69
|
+
for (let e = 1; e < n.length; e++) {
|
|
70
|
+
let s = e - 1, t = e, r = n.at(s), d = n.at(t);
|
|
71
|
+
if (r.size < d.size) {
|
|
72
|
+
let f = i.body[t];
|
|
73
|
+
a.report({
|
|
74
|
+
messageId: "unexpectedInterfacePropertiesOrder",
|
|
75
|
+
data: {
|
|
76
|
+
first: r.name,
|
|
77
|
+
second: d.name
|
|
78
|
+
},
|
|
79
|
+
node: f,
|
|
80
|
+
fix: (c) => {
|
|
81
|
+
let m = a.getSourceCode(), { text: l } = m;
|
|
82
|
+
return S(c, l, n);
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
})
|
|
89
|
+
}), y = "sort-jsx-props", A = I({
|
|
90
|
+
name: y,
|
|
91
|
+
meta: {
|
|
92
|
+
type: "suggestion",
|
|
93
|
+
docs: {
|
|
94
|
+
description: "Enforce sorted interface properties",
|
|
95
|
+
recommended: !1
|
|
96
|
+
},
|
|
97
|
+
messages: {
|
|
98
|
+
unexpectedJSXPropsOrder: 'Expected "{{second}}" to come before "{{first}}"'
|
|
99
|
+
},
|
|
100
|
+
fixable: "code",
|
|
101
|
+
schema: []
|
|
102
|
+
},
|
|
103
|
+
defaultOptions: [],
|
|
104
|
+
create: (a) => ({
|
|
105
|
+
JSXElement: (i) => {
|
|
106
|
+
i.openingElement.attributes.reduce(
|
|
107
|
+
(e, s) => (s.type === "JSXAttribute" ? e.at(-1).push(s) : e.push([]), e),
|
|
108
|
+
[[]]
|
|
109
|
+
).forEach((e) => {
|
|
110
|
+
let s = e.map((t) => ({
|
|
111
|
+
name: t.name.type === g.JSXNamespacedName ? `${t.name.namespace.name}:${t.name.name.name}` : t.name.name,
|
|
112
|
+
size: u(t.range),
|
|
113
|
+
node: t
|
|
114
|
+
}));
|
|
115
|
+
for (let t = 1; t < s.length; t++) {
|
|
116
|
+
let r = t - 1, d = t, f = s.at(r), c = s.at(d);
|
|
117
|
+
f.size < c.size && a.report({
|
|
118
|
+
messageId: "unexpectedJSXPropsOrder",
|
|
119
|
+
data: {
|
|
120
|
+
first: f.name,
|
|
121
|
+
second: c.name
|
|
122
|
+
},
|
|
123
|
+
node: c.node,
|
|
124
|
+
fix: (m) => {
|
|
125
|
+
let l = a.getSourceCode(), { text: o } = l;
|
|
126
|
+
return S(m, o, s);
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
})
|
|
133
|
+
}), z = "sort-named-imports", b = I({
|
|
134
|
+
name: z,
|
|
135
|
+
meta: {
|
|
136
|
+
type: "suggestion",
|
|
137
|
+
docs: {
|
|
138
|
+
description: "Enforce sorted interface properties",
|
|
139
|
+
recommended: !1
|
|
140
|
+
},
|
|
141
|
+
messages: {
|
|
142
|
+
unexpectedNamedImportsOrder: 'Expected "{{second}}" to come before "{{first}}"'
|
|
143
|
+
},
|
|
144
|
+
fixable: "code",
|
|
145
|
+
schema: []
|
|
146
|
+
},
|
|
147
|
+
defaultOptions: [],
|
|
148
|
+
create: (a) => ({
|
|
149
|
+
ImportDeclaration: (i) => {
|
|
150
|
+
if (i.specifiers.length > 1) {
|
|
151
|
+
let n = i.specifiers.map((e) => {
|
|
152
|
+
let {
|
|
153
|
+
range: s,
|
|
154
|
+
local: { name: t }
|
|
155
|
+
} = e;
|
|
156
|
+
return {
|
|
157
|
+
size: u(s),
|
|
158
|
+
node: e,
|
|
159
|
+
name: t
|
|
160
|
+
};
|
|
161
|
+
});
|
|
162
|
+
for (let e = 1; e < n.length; e++) {
|
|
163
|
+
let s = e - 1, t = e, r = n.at(s), d = n.at(t);
|
|
164
|
+
if (r.size < d.size) {
|
|
165
|
+
let f = i.specifiers[t];
|
|
166
|
+
a.report({
|
|
167
|
+
messageId: "unexpectedNamedImportsOrder",
|
|
168
|
+
data: {
|
|
169
|
+
first: r.name,
|
|
170
|
+
second: d.name
|
|
171
|
+
},
|
|
172
|
+
node: f,
|
|
173
|
+
fix: (c) => {
|
|
174
|
+
let m = a.getSourceCode(), { text: l } = m;
|
|
175
|
+
return S(c, l, n);
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
})
|
|
183
|
+
}), J = {
|
|
184
|
+
name: E,
|
|
185
|
+
rules: {
|
|
186
|
+
[h]: T,
|
|
187
|
+
[y]: A,
|
|
188
|
+
[z]: b
|
|
189
|
+
}
|
|
190
|
+
};
|
|
191
|
+
export {
|
|
192
|
+
J as default
|
|
193
|
+
};
|
package/package.json
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eslint-plugin-perfectionist",
|
|
3
3
|
"description": "ESLint plugin for code formatting in perfectionist style",
|
|
4
|
-
"version": "0.
|
|
5
|
-
"type": "module",
|
|
4
|
+
"version": "0.2.0",
|
|
6
5
|
"repository": "azat-io/eslint-plugin-perfectionist",
|
|
7
6
|
"author": "Azat S. <to@azat.io>",
|
|
8
7
|
"license": "MIT",
|
package/readme.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# ESLint Plugin Perfectionist
|
|
2
2
|
|
|
3
|
-
<img src="https://
|
|
3
|
+
<img src="https://raw.githubusercontent.com/azat-io/eslint-plugin-perfectionist/main/docs/public/logo.svg" alt="ESLint" align="right" width="120" height="120" />
|
|
4
4
|
|
|
5
5
|

|
|
6
6
|
|
|
@@ -22,9 +22,11 @@ npm install --save-dev eslint-plugin-perfectionist
|
|
|
22
22
|
|
|
23
23
|
## Rules
|
|
24
24
|
|
|
25
|
-
| Name
|
|
26
|
-
|
|
|
27
|
-
| sort-interfaces
|
|
25
|
+
| Name | Description | 💼 | 🛠 |
|
|
26
|
+
| :----------------------------------------------------------------------------------------- | :---------------------------------- | :-- | :-- |
|
|
27
|
+
| [sort-interfaces](https://eslint-plugin-perfectionist.azat.io/rules/sort-interfaces) | Enforce sorted interface properties | ✅ | 🔧 |
|
|
28
|
+
| [sort-jsx-props](https://eslint-plugin-perfectionist.azat.io/rules/sort-jsx-props) | Enforce sorted JSX props | ✅ | 🔧 |
|
|
29
|
+
| [sort-named-imports](https://eslint-plugin-perfectionist.azat.io/rules/sort-named-imports) | Enforce sorted named imports | ✅ | 🔧 |
|
|
28
30
|
|
|
29
31
|
## See also
|
|
30
32
|
|
package/dist/index.cjs.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";const g=require("@typescript-eslint/types"),y=require("@typescript-eslint/utils"),I="eslint-plugin-perfectionist",_="azat-io/eslint-plugin-perfectionist";let b=y.ESLintUtils.RuleCreator(f=>`https://github.com/${_}/blob/main/docs/rules/${f}.md`);const T="sort-interfaces",A=b({name:T,meta:{type:"suggestion",docs:{description:"Enforce sorted interface properties",recommended:!1},messages:{unexpectedInterfacePropertiesOrder:'Expected "{{second}}" to come before "{{first}}"'},fixable:"code",schema:[]},defaultOptions:[],create:f=>({TSInterfaceBody:S=>{let x=S.body.map(t=>{let a="",l=0,n=e=>{l+=e},i=e=>{let[r,o]=e;return o-r},u=e=>{n(i(e.key.range))},p=e=>{let{length:r}="[]";n(e.parameters.reduce((o,{range:m})=>o+i(m),r))},c=e=>{if(e.optional){let{length:r}="?";n(r)}},d=e=>{var o;let r=(o=e.typeAnnotation)==null?void 0:o.range;r&&n(i(r))},s=e=>{if(e.type===g.AST_NODE_TYPES.Identifier)({name:a}=e);else if(e.type===g.AST_NODE_TYPES.TSIndexSignature){let r=e.parameters.at(0);r&&s(r)}};return t.type===g.AST_NODE_TYPES.TSPropertySignature?(s(t.key),u(t),c(t),d(t)):t.type===g.AST_NODE_TYPES.TSIndexSignature&&(s(t),p(t),d(t)),{name:a,size:l}});for(let t=1;t<x.length;t++){let a=t-1,l=t,n=x.at(a),i=x.at(l);if(n.size<i.size){let u=f.getSourceCode(),p=S.body[a],c=S.body[l];f.report({messageId:"unexpectedInterfacePropertiesOrder",data:{first:n.name,second:i.name},node:c,fix:d=>{let s=r=>[r.range.at(0),r.range.at(1)];return[d.replaceTextRange(s(p),u.text.slice(...s(c))),d.replaceTextRange(s(c),u.text.slice(...s(p)))]}})}}}})}),O={name:I,rules:{[T]:A}};module.exports=O;
|
package/dist/index.es.js
DELETED
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
import { AST_NODE_TYPES as u } from "@typescript-eslint/types";
|
|
2
|
-
import { ESLintUtils as y } from "@typescript-eslint/utils";
|
|
3
|
-
const T = "eslint-plugin-perfectionist", b = "azat-io/eslint-plugin-perfectionist";
|
|
4
|
-
let R = y.RuleCreator(
|
|
5
|
-
(f) => `https://github.com/${b}/blob/main/docs/rules/${f}.md`
|
|
6
|
-
);
|
|
7
|
-
const S = "sort-interfaces", h = R({
|
|
8
|
-
name: S,
|
|
9
|
-
meta: {
|
|
10
|
-
type: "suggestion",
|
|
11
|
-
docs: {
|
|
12
|
-
description: "Enforce sorted interface properties",
|
|
13
|
-
recommended: !1
|
|
14
|
-
},
|
|
15
|
-
messages: {
|
|
16
|
-
unexpectedInterfacePropertiesOrder: 'Expected "{{second}}" to come before "{{first}}"'
|
|
17
|
-
},
|
|
18
|
-
fixable: "code",
|
|
19
|
-
schema: []
|
|
20
|
-
},
|
|
21
|
-
defaultOptions: [],
|
|
22
|
-
create: (f) => ({
|
|
23
|
-
TSInterfaceBody: (m) => {
|
|
24
|
-
let x = m.body.map((t) => {
|
|
25
|
-
let i = "", l = 0, n = (e) => {
|
|
26
|
-
l += e;
|
|
27
|
-
}, o = (e) => {
|
|
28
|
-
let [r, a] = e;
|
|
29
|
-
return a - r;
|
|
30
|
-
}, p = (e) => {
|
|
31
|
-
n(o(e.key.range));
|
|
32
|
-
}, g = (e) => {
|
|
33
|
-
let { length: r } = "[]";
|
|
34
|
-
n(
|
|
35
|
-
e.parameters.reduce(
|
|
36
|
-
(a, { range: I }) => a + o(I),
|
|
37
|
-
r
|
|
38
|
-
)
|
|
39
|
-
);
|
|
40
|
-
}, c = (e) => {
|
|
41
|
-
if (e.optional) {
|
|
42
|
-
let { length: r } = "?";
|
|
43
|
-
n(r);
|
|
44
|
-
}
|
|
45
|
-
}, d = (e) => {
|
|
46
|
-
var a;
|
|
47
|
-
let r = (a = e.typeAnnotation) == null ? void 0 : a.range;
|
|
48
|
-
r && n(o(r));
|
|
49
|
-
}, s = (e) => {
|
|
50
|
-
if (e.type === u.Identifier)
|
|
51
|
-
({ name: i } = e);
|
|
52
|
-
else if (e.type === u.TSIndexSignature) {
|
|
53
|
-
let r = e.parameters.at(0);
|
|
54
|
-
r && s(r);
|
|
55
|
-
}
|
|
56
|
-
};
|
|
57
|
-
return t.type === u.TSPropertySignature ? (s(t.key), p(t), c(t), d(t)) : t.type === u.TSIndexSignature && (s(t), g(t), d(t)), {
|
|
58
|
-
name: i,
|
|
59
|
-
size: l
|
|
60
|
-
};
|
|
61
|
-
});
|
|
62
|
-
for (let t = 1; t < x.length; t++) {
|
|
63
|
-
let i = t - 1, l = t, n = x.at(i), o = x.at(l);
|
|
64
|
-
if (n.size < o.size) {
|
|
65
|
-
let p = f.getSourceCode(), g = m.body[i], c = m.body[l];
|
|
66
|
-
f.report({
|
|
67
|
-
messageId: "unexpectedInterfacePropertiesOrder",
|
|
68
|
-
data: {
|
|
69
|
-
first: n.name,
|
|
70
|
-
second: o.name
|
|
71
|
-
},
|
|
72
|
-
node: c,
|
|
73
|
-
fix: (d) => {
|
|
74
|
-
let s = (r) => [
|
|
75
|
-
r.range.at(0),
|
|
76
|
-
r.range.at(1)
|
|
77
|
-
];
|
|
78
|
-
return [
|
|
79
|
-
d.replaceTextRange(
|
|
80
|
-
s(g),
|
|
81
|
-
p.text.slice(...s(c))
|
|
82
|
-
),
|
|
83
|
-
d.replaceTextRange(
|
|
84
|
-
s(c),
|
|
85
|
-
p.text.slice(...s(g))
|
|
86
|
-
)
|
|
87
|
-
];
|
|
88
|
-
}
|
|
89
|
-
});
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
})
|
|
94
|
-
}), z = {
|
|
95
|
-
name: T,
|
|
96
|
-
rules: {
|
|
97
|
-
[S]: h
|
|
98
|
-
}
|
|
99
|
-
};
|
|
100
|
-
export {
|
|
101
|
-
z as default
|
|
102
|
-
};
|