@sabzidev/eslint-rules 0.0.0-stage → 1.0.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/LICENSE +21 -0
- package/README.md +163 -2
- package/dist/index.js +553 -0
- package/docs/images/logo.webp +0 -0
- package/package.json +41 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Abolfazl Sabzmohammadi | ابوالفضل سبزمحمدی
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,164 @@
|
|
|
1
|
-
#
|
|
1
|
+
# 🛠️ @sabzidev/eslint-rules
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A collection of custom ESLint rules for improving code quality, consistency, and maintainability.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## 👀 Overview
|
|
8
|
+
|
|
9
|
+
<div align="center">
|
|
10
|
+
<img src="https://img.shields.io/npm/dm/@sabzidev/eslint-rules?style=flat-square&&color=16A34A&label=DOWNLOADS" alt="DOWNLOADS"/>
|
|
11
|
+
<img src="https://img.shields.io/npm/v/@sabzidev/eslint-rules?style=flat-square&color=16A34A&label=VERSION" alt="VERSION"/>
|
|
12
|
+
<br />
|
|
13
|
+
<img src="./docs/images/logo.webp" alt="SabziDev logo" height="300" width="70%"/>
|
|
14
|
+
|
|
15
|
+
[GITHUB](https://github.com/SabziDev/eslint-rules) | [NPM](https://npmjs.com/package/@sabzidev/eslint-rules)
|
|
16
|
+
</div>
|
|
17
|
+
|
|
18
|
+
---
|
|
19
|
+
|
|
20
|
+
## ✨ Features
|
|
21
|
+
|
|
22
|
+
- Recommended configuration for ESLint Flat Config.
|
|
23
|
+
- Easy integration into existing ESLint setups.
|
|
24
|
+
|
|
25
|
+
---
|
|
26
|
+
|
|
27
|
+
## 📦 Installation
|
|
28
|
+
|
|
29
|
+
Install the package as a development dependency:
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
pnpm add -D @sabzidev/eslint-rules
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
---
|
|
36
|
+
|
|
37
|
+
## 🚀 Usage
|
|
38
|
+
|
|
39
|
+
Choose **one** of the following configuration methods.
|
|
40
|
+
|
|
41
|
+
### ● Recommended Configuration
|
|
42
|
+
|
|
43
|
+
Add the recommended configuration to your existing `eslint.config.js` file:
|
|
44
|
+
|
|
45
|
+
```js
|
|
46
|
+
import sabzidev from "@sabzidev/eslint-rules";
|
|
47
|
+
import { defineConfig } from "eslint/config";
|
|
48
|
+
|
|
49
|
+
export default defineConfig([
|
|
50
|
+
// Your existing configurations
|
|
51
|
+
sabzidev.configs.recommended,
|
|
52
|
+
]);
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
This enables all rules included in the recommended configuration with their predefined severity levels.
|
|
56
|
+
|
|
57
|
+
### ● Custom Configuration
|
|
58
|
+
|
|
59
|
+
Alternatively, register the plugin and configure each rule individually in your existing `eslint.config.js` file:
|
|
60
|
+
|
|
61
|
+
```js
|
|
62
|
+
import sabzidev from "@sabzidev/eslint-rules";
|
|
63
|
+
import { defineConfig } from "eslint/config";
|
|
64
|
+
|
|
65
|
+
export default defineConfig([
|
|
66
|
+
{
|
|
67
|
+
plugins: {
|
|
68
|
+
"@sabzidev": sabzidev,
|
|
69
|
+
},
|
|
70
|
+
rules: {
|
|
71
|
+
"@sabzidev/merge-duplicate-id-and-classname-props": "warn",
|
|
72
|
+
"@sabzidev/merge-exports": "warn",
|
|
73
|
+
"@sabzidev/no-invalid-id-and-classname-value": "warn",
|
|
74
|
+
"@sabzidev/no-useless-empty-type": "warn",
|
|
75
|
+
"@sabzidev/no-useless-template-literal": "warn",
|
|
76
|
+
"@sabzidev/padding-before-jump-statement": "warn",
|
|
77
|
+
"@sabzidev/sort-comments": "warn",
|
|
78
|
+
"@sabzidev/sort-jsx-props": "warn",
|
|
79
|
+
"@sabzidev/sort-object-props": "warn",
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
]);
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
---
|
|
86
|
+
|
|
87
|
+
## ⭐ Recommended Tooling Setup
|
|
88
|
+
|
|
89
|
+
For a more comprehensive linting setup, combine `@sabzidev/eslint-rules` with [`@fullstacksjs/eslint-config`](https://www.npmjs.com/package/@fullstacksjs/eslint-config), [`eslint-plugin-sonarjs`](https://www.npmjs.com/package/eslint-plugin-sonarjs), and [`eslint-plugin-unicorn`](https://www.npmjs.com/package/eslint-plugin-unicorn).
|
|
90
|
+
|
|
91
|
+
This combination brings together code-quality rules, bug detection, maintainability checks, and custom linting rules.
|
|
92
|
+
|
|
93
|
+
```js
|
|
94
|
+
import { defineConfig } from "@fullstacksjs/eslint-config";
|
|
95
|
+
import sabzidev from "@sabzidev/eslint-rules";
|
|
96
|
+
import sonarjs from "eslint-plugin-sonarjs";
|
|
97
|
+
import unicorn from "eslint-plugin-unicorn";
|
|
98
|
+
|
|
99
|
+
const baseRules = {
|
|
100
|
+
"func-style": ["warn", "expression"],
|
|
101
|
+
quotes: [
|
|
102
|
+
"error",
|
|
103
|
+
"double",
|
|
104
|
+
{ avoidEscape: true, allowTemplateLiterals: false },
|
|
105
|
+
],
|
|
106
|
+
eqeqeq: ["error", "always"],
|
|
107
|
+
"no-console": "warn",
|
|
108
|
+
};
|
|
109
|
+
const plugins = [
|
|
110
|
+
sabzidev.configs.recommended,
|
|
111
|
+
sonarjs.configs.recommended,
|
|
112
|
+
unicorn.configs.recommended,
|
|
113
|
+
];
|
|
114
|
+
const pluginsRules = {
|
|
115
|
+
"unicorn/filename-case": "off",
|
|
116
|
+
"unicorn/prefer-global-this": "off",
|
|
117
|
+
"unicorn/name-replacements": "off",
|
|
118
|
+
"unicorn/no-array-sort": "off",
|
|
119
|
+
"unicorn/no-null": "off",
|
|
120
|
+
"unicorn/default-export-style": "off",
|
|
121
|
+
|
|
122
|
+
"jsx-a11y/click-events-have-key-events": "off",
|
|
123
|
+
"jsx-a11y/no-noninteractive-element-interactions": "off",
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
const config = defineConfig(
|
|
127
|
+
{
|
|
128
|
+
tailwind: { entryPoint: "./src/input.css" },
|
|
129
|
+
rules: baseRules,
|
|
130
|
+
},
|
|
131
|
+
|
|
132
|
+
plugins,
|
|
133
|
+
{ rules: pluginsRules },
|
|
134
|
+
);
|
|
135
|
+
|
|
136
|
+
export default config;
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
**Note:** Customize the rules to match your project's requirements and verify compatibility with your installed plugin versions.
|
|
140
|
+
|
|
141
|
+
---
|
|
142
|
+
|
|
143
|
+
## 📋 Available Rules
|
|
144
|
+
|
|
145
|
+
| Rule | Description |
|
|
146
|
+
| :--------------------------------------- | :------------------------------------------- |
|
|
147
|
+
| `merge-duplicate-id-and-classname-props` | Merges duplicate `id` and `className` props. |
|
|
148
|
+
| `merge-exports` | Merges or organizes export declarations. |
|
|
149
|
+
| `no-invalid-id-and-classname-value` | Detects invalid `id` and `className` values. |
|
|
150
|
+
| `no-useless-empty-type` | Detects unnecessary empty types. |
|
|
151
|
+
| `no-useless-template-literal` | Detects unnecessary template literals. |
|
|
152
|
+
| `padding-before-jump-statement` | Enforces padding before jump statements. |
|
|
153
|
+
| `sort-comments` | Sorts comments. |
|
|
154
|
+
| `sort-jsx-props` | Sorts JSX props. |
|
|
155
|
+
| `sort-object-props` | Sorts object properties. |
|
|
156
|
+
|
|
157
|
+
---
|
|
158
|
+
|
|
159
|
+
## 👨💻 Developer
|
|
160
|
+
|
|
161
|
+
Developed by **Abolfazl Sabzmohammadi**.
|
|
162
|
+
|
|
163
|
+
- GitHub: [SabziDev](https://github.com/SabziDev)
|
|
164
|
+
- Website: [Sabzi.Dev](https://Sabzi.Dev)
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,553 @@
|
|
|
1
|
+
var e = {
|
|
2
|
+
name: "@sabzidev/eslint-rules",
|
|
3
|
+
description: "A collection of ESLint rules for better code quality, consistency, and maintainability.",
|
|
4
|
+
repository: {
|
|
5
|
+
type: "git",
|
|
6
|
+
url: "https://github.com/SabziDev/eslint-rules"
|
|
7
|
+
},
|
|
8
|
+
license: "MIT",
|
|
9
|
+
keywords: [
|
|
10
|
+
"SabziDev",
|
|
11
|
+
"eslint",
|
|
12
|
+
"eslint-plugin",
|
|
13
|
+
"eslint-rules",
|
|
14
|
+
"code-quality",
|
|
15
|
+
"code-style",
|
|
16
|
+
"linting",
|
|
17
|
+
"developer-tools"
|
|
18
|
+
],
|
|
19
|
+
author: "Abolfazl Sabzmohammadi <SabziDev@gmail.com> (https://Sabzi.Dev)",
|
|
20
|
+
version: "1.0.0",
|
|
21
|
+
type: "module",
|
|
22
|
+
main: "./dist/index.js",
|
|
23
|
+
exports: "./dist/index.js",
|
|
24
|
+
files: [
|
|
25
|
+
"dist",
|
|
26
|
+
"docs",
|
|
27
|
+
"README.md",
|
|
28
|
+
"LICENSE"
|
|
29
|
+
],
|
|
30
|
+
publishConfig: { access: "public" },
|
|
31
|
+
scripts: { build: "vite build" },
|
|
32
|
+
devDependencies: {
|
|
33
|
+
eslint: "^10.12.0",
|
|
34
|
+
prettier: "^3.9.9",
|
|
35
|
+
"@fullstacksjs/eslint-config": "^15.3.0",
|
|
36
|
+
"eslint-plugin-sonarjs": "^4.2.2",
|
|
37
|
+
"eslint-plugin-unicorn": "^77.0.0",
|
|
38
|
+
vite: "^8.3.0"
|
|
39
|
+
}
|
|
40
|
+
}, t = {
|
|
41
|
+
meta: {
|
|
42
|
+
type: "suggestion",
|
|
43
|
+
fixable: "code",
|
|
44
|
+
messages: { merge: "Duplicate '{{prop}}' props should be merged into one" }
|
|
45
|
+
},
|
|
46
|
+
create(e) {
|
|
47
|
+
let t = (e) => {
|
|
48
|
+
let { value: t } = e;
|
|
49
|
+
return t ? t.type === "Literal" ? t.value : t.type === "JSXExpressionContainer" && t.expression.type === "Literal" ? t.expression.value : null : null;
|
|
50
|
+
};
|
|
51
|
+
return { JSXOpeningElement(n) {
|
|
52
|
+
for (let r of ["className", "id"]) {
|
|
53
|
+
let i = n.attributes.filter((e) => e.type === "JSXAttribute" && e.name?.name === r);
|
|
54
|
+
if (i.length < 2) continue;
|
|
55
|
+
let a = [];
|
|
56
|
+
for (let e of i) {
|
|
57
|
+
let n = t(e);
|
|
58
|
+
if (typeof n != "string") return;
|
|
59
|
+
a.push(n);
|
|
60
|
+
}
|
|
61
|
+
e.report({
|
|
62
|
+
data: { prop: r },
|
|
63
|
+
node: i[1],
|
|
64
|
+
messageId: "merge",
|
|
65
|
+
fix(e) {
|
|
66
|
+
let t = [...new Set(a)].join(" ").trim(), n = [e.replaceText(i[0], `${r}="${t}"`)];
|
|
67
|
+
for (let t = 1; t < i.length; t++) n.push(e.remove(i[t]));
|
|
68
|
+
return n;
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
} };
|
|
73
|
+
}
|
|
74
|
+
}, n = {
|
|
75
|
+
meta: {
|
|
76
|
+
type: "suggestion",
|
|
77
|
+
fixable: "code",
|
|
78
|
+
messages: { mergeExports: "Combine and move exports into a single export block at the end of the file" }
|
|
79
|
+
},
|
|
80
|
+
create(e) {
|
|
81
|
+
let { sourceCode: t } = e, n = (e) => e?.type === "ExportNamedDeclaration" && !e.source && !e.declaration && e.specifiers?.length > 0, r = (e) => e?.type === "ExportDefaultDeclaration", i = (e) => n(e) || r(e), a = (e) => e.exported.name ?? e.exported.value;
|
|
82
|
+
return { Program(o) {
|
|
83
|
+
let { body: s } = o, c = s.filter(i);
|
|
84
|
+
if (c.length === 0) return;
|
|
85
|
+
let l = c.filter(n), u = c.find(r), d = l.flatMap(({ specifiers: e }) => e).map((e) => ({
|
|
86
|
+
name: a(e),
|
|
87
|
+
text: t.getText(e)
|
|
88
|
+
})).sort((e, t) => e.name.localeCompare(t.name)), f = [d.length > 0 ? `export { ${d.map(({ text: e }) => e).join(", ")} };` : "", u ? t.getText(u) : ""].filter(Boolean).join("\n"), p = c.map(({ range: e }) => e).sort((e, t) => t[0] - e[0]), m = t.text;
|
|
89
|
+
for (let [e, t] of p) m = m.slice(0, e) + m.slice(t);
|
|
90
|
+
let h = `${m.replace(/\s+$/, "")}\n\n${f}\n`;
|
|
91
|
+
t.text !== h && e.report({
|
|
92
|
+
node: c[0],
|
|
93
|
+
messageId: "mergeExports",
|
|
94
|
+
fix(e) {
|
|
95
|
+
return e.replaceText(o, h);
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
} };
|
|
99
|
+
}
|
|
100
|
+
}, r = {
|
|
101
|
+
meta: {
|
|
102
|
+
type: "problem",
|
|
103
|
+
fixable: "code",
|
|
104
|
+
messages: {
|
|
105
|
+
invalid: "{{attribute}} cannot be {{value}}!",
|
|
106
|
+
singleItemArray: "{{attribute}} should not use an array with only one item!"
|
|
107
|
+
}
|
|
108
|
+
},
|
|
109
|
+
create(e) {
|
|
110
|
+
let t = /* @__PURE__ */ new Set(["className", "id"]), n = e.sourceCode || e.getSourceCode(), r = (e) => {
|
|
111
|
+
if (e.length === 0) return {
|
|
112
|
+
invalidValue: "empty",
|
|
113
|
+
index: -2
|
|
114
|
+
};
|
|
115
|
+
for (let [t, n] of e.entries()) if (n.type === "Literal") {
|
|
116
|
+
if (n.value === "") return {
|
|
117
|
+
invalidValue: "empty",
|
|
118
|
+
index: t
|
|
119
|
+
};
|
|
120
|
+
if ([
|
|
121
|
+
!1,
|
|
122
|
+
null,
|
|
123
|
+
!0
|
|
124
|
+
].includes(n.value)) return {
|
|
125
|
+
invalidValue: String(n.value),
|
|
126
|
+
index: t
|
|
127
|
+
};
|
|
128
|
+
} else if (n.type === "Identifier" && n.name === "undefined") return {
|
|
129
|
+
invalidValue: "undefined",
|
|
130
|
+
index: t
|
|
131
|
+
};
|
|
132
|
+
return null;
|
|
133
|
+
};
|
|
134
|
+
return { JSXAttribute(i) {
|
|
135
|
+
let a = i.name.name;
|
|
136
|
+
if (!t.has(a) || !i.value) return;
|
|
137
|
+
let o, s = -1, c = !1;
|
|
138
|
+
if (i.value.type === "Literal" && i.value.value === "") o = "empty";
|
|
139
|
+
else if (i.value.type === "JSXExpressionContainer") {
|
|
140
|
+
let { expression: t } = i.value;
|
|
141
|
+
if (t.type === "Literal" && [
|
|
142
|
+
!1,
|
|
143
|
+
null,
|
|
144
|
+
!0
|
|
145
|
+
].includes(t.value)) o = String(t.value);
|
|
146
|
+
else if (t.type === "Identifier" && t.name === "undefined") o = "undefined";
|
|
147
|
+
else if (t.type === "ArrayExpression") {
|
|
148
|
+
c = !0;
|
|
149
|
+
let e = r(t.elements);
|
|
150
|
+
e && ({invalidValue: o, index: s} = e);
|
|
151
|
+
} else if (t.type === "ObjectExpression") t.properties.length === 0 && (o = "empty");
|
|
152
|
+
else if (t.type === "CallExpression" && t.callee.name === "clsx" && t.arguments.length === 1) {
|
|
153
|
+
let n = t.arguments[0];
|
|
154
|
+
if (n.type === "ArrayExpression") {
|
|
155
|
+
c = !0;
|
|
156
|
+
let l = r(n.elements);
|
|
157
|
+
if (l && ({invalidValue: o, index: s} = l), c && t.type === "CallExpression" && t.callee.name === "clsx") {
|
|
158
|
+
let n = t.arguments[0];
|
|
159
|
+
if (n?.type === "ArrayExpression" && n.elements.length === 1 && n.elements[0]?.type === "Literal") {
|
|
160
|
+
e.report({
|
|
161
|
+
data: {
|
|
162
|
+
value: "single-item-array",
|
|
163
|
+
attribute: a
|
|
164
|
+
},
|
|
165
|
+
node: i,
|
|
166
|
+
messageId: "singleItemArray",
|
|
167
|
+
fix: (e) => e.replaceText(i.value, `{${n.elements[0].raw}}`)
|
|
168
|
+
});
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
} else n.type === "Literal" && n.value === "" ? o = "empty" : n.type === "Literal" && [
|
|
173
|
+
!1,
|
|
174
|
+
null,
|
|
175
|
+
!0
|
|
176
|
+
].includes(n.value) ? o = String(n.value) : n.type === "Identifier" && n.name === "undefined" ? o = "undefined" : n.type === "ObjectExpression" && n.properties.length === 0 && (o = "empty");
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
o && e.report({
|
|
180
|
+
data: {
|
|
181
|
+
value: o,
|
|
182
|
+
attribute: a
|
|
183
|
+
},
|
|
184
|
+
node: i,
|
|
185
|
+
messageId: o === "single-item-array" ? "singleItemArray" : "invalid",
|
|
186
|
+
fix: (e) => {
|
|
187
|
+
if (s === -2 || s === -1 || !c) return e.remove(i);
|
|
188
|
+
let { elements: t } = i.value.expression.arguments ? i.value.expression.arguments[0] : i.value.expression;
|
|
189
|
+
if (t.length === 1) return e.remove(i);
|
|
190
|
+
let r = t[s], a = t[s + 1], o = t[s - 1], l;
|
|
191
|
+
l = a ? [r.range[0], a.range[0]] : o ? [o.range[1], r.range[1]] : r.range;
|
|
192
|
+
let u = n.getText().slice(0, l[0]) + n.getText().slice(l[1]);
|
|
193
|
+
return e.replaceTextRange([0, n.getText().length], u);
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
} };
|
|
197
|
+
}
|
|
198
|
+
}, i = {
|
|
199
|
+
meta: {
|
|
200
|
+
type: "problem",
|
|
201
|
+
fixable: "code",
|
|
202
|
+
messages: {
|
|
203
|
+
emptyType: "Empty type alias is not allowed.",
|
|
204
|
+
emptyInterface: "Empty interface is not allowed."
|
|
205
|
+
},
|
|
206
|
+
schema: []
|
|
207
|
+
},
|
|
208
|
+
create(e) {
|
|
209
|
+
return {
|
|
210
|
+
TSTypeAliasDeclaration(t) {
|
|
211
|
+
t.typeAnnotation.type === "TSTypeLiteral" && t.typeAnnotation.members.length === 0 && e.report({
|
|
212
|
+
node: t,
|
|
213
|
+
messageId: "emptyType",
|
|
214
|
+
fix(e) {
|
|
215
|
+
return e.remove(t);
|
|
216
|
+
}
|
|
217
|
+
});
|
|
218
|
+
},
|
|
219
|
+
TSInterfaceDeclaration(t) {
|
|
220
|
+
t.body.body.length === 0 && e.report({
|
|
221
|
+
node: t,
|
|
222
|
+
messageId: "emptyInterface",
|
|
223
|
+
fix(e) {
|
|
224
|
+
return e.remove(t);
|
|
225
|
+
}
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
}, a = {
|
|
231
|
+
meta: {
|
|
232
|
+
type: "suggestion",
|
|
233
|
+
fixable: "code",
|
|
234
|
+
messages: { remove: "This template literal has a single expression and can be replaced with the expression itself" }
|
|
235
|
+
},
|
|
236
|
+
create(e) {
|
|
237
|
+
let t = e.sourceCode || e.getSourceCode();
|
|
238
|
+
return { TemplateLiteral(n) {
|
|
239
|
+
if (n.expressions.length !== 1) return;
|
|
240
|
+
let r = n.quasis[0].value.raw === "", i = n.quasis[1].value.raw === "";
|
|
241
|
+
if (!(r && i)) return;
|
|
242
|
+
let a = n.expressions[0], o = t.getText(a);
|
|
243
|
+
e.report({
|
|
244
|
+
node: n,
|
|
245
|
+
messageId: "remove",
|
|
246
|
+
fix: (e) => e.replaceText(n, o)
|
|
247
|
+
});
|
|
248
|
+
} };
|
|
249
|
+
}
|
|
250
|
+
}, o = [
|
|
251
|
+
"ReturnStatement",
|
|
252
|
+
"BreakStatement",
|
|
253
|
+
"ContinueStatement",
|
|
254
|
+
"ThrowStatement"
|
|
255
|
+
], s = {
|
|
256
|
+
meta: {
|
|
257
|
+
type: "layout",
|
|
258
|
+
fixable: "code",
|
|
259
|
+
messages: { missingBlankLine: "Expected a blank line before statement" },
|
|
260
|
+
schema: []
|
|
261
|
+
},
|
|
262
|
+
create(e) {
|
|
263
|
+
let { sourceCode: t = e.getSourceCode() } = e, n = new Set(o), r = (e) => n.has(e), i = (e, t) => {
|
|
264
|
+
let n = t.indexOf(e);
|
|
265
|
+
if (n <= 0) return !1;
|
|
266
|
+
let { type: i } = t[n - 1];
|
|
267
|
+
return !r(i);
|
|
268
|
+
}, a = (e) => {
|
|
269
|
+
let { parent: t } = e;
|
|
270
|
+
if (t?.type === "BlockStatement") {
|
|
271
|
+
let { body: n } = t;
|
|
272
|
+
if (n.length === 1 && n[0] === e || n.length === 1 && r(n[0].type)) return !0;
|
|
273
|
+
}
|
|
274
|
+
if (t?.type === "ArrowFunctionExpression" && t.body === e) {
|
|
275
|
+
let { body: n } = t;
|
|
276
|
+
return r(n.type) && n === e;
|
|
277
|
+
}
|
|
278
|
+
return !1;
|
|
279
|
+
}, s = (e) => {
|
|
280
|
+
let n = t.getTokenBefore(e, { includeComments: !0 });
|
|
281
|
+
if (!n) return !0;
|
|
282
|
+
let { end: r } = n.loc, { start: i } = e.loc;
|
|
283
|
+
return i.line - r.line > 1;
|
|
284
|
+
}, c = (e) => {
|
|
285
|
+
let n = t.getTokenBefore(e, { includeComments: !0 });
|
|
286
|
+
for (; n && (n.type === "Line" || n.type === "Block");) n = t.getTokenBefore(n, { includeComments: !0 });
|
|
287
|
+
return n;
|
|
288
|
+
}, l = (e) => {
|
|
289
|
+
let t = c(e);
|
|
290
|
+
if (!t) return !1;
|
|
291
|
+
let { end: n } = t.loc, { start: r } = e.loc;
|
|
292
|
+
return r.line - n.line === 1;
|
|
293
|
+
}, u = (e) => {
|
|
294
|
+
let { parent: t } = e;
|
|
295
|
+
for (; t;) {
|
|
296
|
+
let { type: e } = t;
|
|
297
|
+
if (e === "BlockStatement" || e === "Program") return { parentBody: t.body };
|
|
298
|
+
t = t.parent;
|
|
299
|
+
}
|
|
300
|
+
return { parentBody: null };
|
|
301
|
+
}, d = (n) => {
|
|
302
|
+
if (a(n)) return;
|
|
303
|
+
let { parentBody: o } = u(n);
|
|
304
|
+
if (!o || !i(n, o)) return;
|
|
305
|
+
let c = t.getTokenBefore(n, { includeComments: !0 });
|
|
306
|
+
if (c && (c.type === "Line" || c.type === "Block")) {
|
|
307
|
+
l(c) && e.report({
|
|
308
|
+
node: n,
|
|
309
|
+
messageId: "missingBlankLine",
|
|
310
|
+
fix: (e) => e.insertTextBefore(c, "\n")
|
|
311
|
+
});
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
if (s(n)) return;
|
|
315
|
+
let { type: d } = o[o.indexOf(n) - 1] || {};
|
|
316
|
+
r(d) || e.report({
|
|
317
|
+
node: n,
|
|
318
|
+
messageId: "missingBlankLine",
|
|
319
|
+
fix: (e) => e.insertTextBefore(n, "\n")
|
|
320
|
+
});
|
|
321
|
+
};
|
|
322
|
+
return Object.fromEntries(o.map((e) => [e, d]));
|
|
323
|
+
}
|
|
324
|
+
}, c = {
|
|
325
|
+
meta: {
|
|
326
|
+
type: "suggestion",
|
|
327
|
+
fixable: "code",
|
|
328
|
+
messages: { reorganize: "Comments should be ordered!" }
|
|
329
|
+
},
|
|
330
|
+
create(e) {
|
|
331
|
+
let { sourceCode: t } = e, { text: n } = t, r = t.getAllComments(), i = [];
|
|
332
|
+
for (let e of r) {
|
|
333
|
+
let t = e.value.trim();
|
|
334
|
+
e.type === "Block" && /^eslint-disable\s+/.test(t) && i.push({
|
|
335
|
+
text: `/* ${t} */`,
|
|
336
|
+
line: e.loc.start.line,
|
|
337
|
+
range: e.range
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
return i.length === 0 ? {} : { Program(t) {
|
|
341
|
+
i.sort((e, t) => e.line - t.line);
|
|
342
|
+
let r = "";
|
|
343
|
+
for (let e = 0; e < i.length; e++) r += i[e].text, e < i.length - 1 && (r += "\n");
|
|
344
|
+
r += "\n\n";
|
|
345
|
+
let a = n;
|
|
346
|
+
for (let e = i.length - 1; e >= 0; e--) {
|
|
347
|
+
let t = i[e];
|
|
348
|
+
a = a.slice(0, t.range[0]) + a.slice(t.range[1]);
|
|
349
|
+
}
|
|
350
|
+
for (; a.startsWith("\n");) a = a.slice(1);
|
|
351
|
+
let o = r + a;
|
|
352
|
+
n !== o && e.report({
|
|
353
|
+
node: t,
|
|
354
|
+
messageId: "reorganize",
|
|
355
|
+
fix: (e) => e.replaceTextRange([0, n.length], o)
|
|
356
|
+
});
|
|
357
|
+
} };
|
|
358
|
+
}
|
|
359
|
+
}, l = /* @__PURE__ */ "children.key.id.ref.data.title.desc.htmlFor.type.inputMode.name.value.placeholder.autoComplete.min.max.minLength.maxLength.src.alt.loading.poster.preload.controls.href.target.rel.to.replace.viewTransition".split("."), u = [
|
|
360
|
+
"onClick",
|
|
361
|
+
"onChange",
|
|
362
|
+
"onKeyUp"
|
|
363
|
+
], d = {
|
|
364
|
+
"merge-duplicate-id-and-classname-props": t,
|
|
365
|
+
"merge-exports": n,
|
|
366
|
+
"no-invalid-id-and-classname-value": r,
|
|
367
|
+
"no-useless-empty-type": i,
|
|
368
|
+
"no-useless-template-literal": a,
|
|
369
|
+
"padding-before-jump-statement": s,
|
|
370
|
+
"sort-comments": c,
|
|
371
|
+
"sort-jsx-props": {
|
|
372
|
+
meta: {
|
|
373
|
+
type: "suggestion",
|
|
374
|
+
fixable: "code",
|
|
375
|
+
messages: { wrongOrder: "Props should be ordered!" }
|
|
376
|
+
},
|
|
377
|
+
create(e) {
|
|
378
|
+
let { sourceCode: t } = e, n = new Set(l), r = (e) => /^on[A-Z]/.test(e), i = (e) => {
|
|
379
|
+
let t = u.indexOf(e);
|
|
380
|
+
return t === -1 ? 999 : t;
|
|
381
|
+
};
|
|
382
|
+
return { JSXOpeningElement(a) {
|
|
383
|
+
let o = a.attributes, s = o.filter((e) => e.type === "JSXSpreadAttribute"), c = o.filter((e) => e.type === "JSXAttribute");
|
|
384
|
+
if (c.length === 0 && s.length === 0) return;
|
|
385
|
+
let u = l.flatMap((e) => c.filter((t) => t.name.name === e)), d = c.filter((e) => r(e.name.name)).sort((e, t) => {
|
|
386
|
+
let n = i(e.name.name), r = i(t.name.name);
|
|
387
|
+
return n === 999 && r === 999 ? 0 : n - r;
|
|
388
|
+
}), f = c.filter((e) => ["className", "style"].includes(e.name.name)).sort((e, t) => e.name.name === "className" && t.name.name === "style" ? -1 : +(e.name.name === "style" && t.name.name === "className")), p = c.filter((e) => {
|
|
389
|
+
let { name: t } = e.name;
|
|
390
|
+
return !n.has(t) && !r(t) && !["className", "style"].includes(t);
|
|
391
|
+
}), m = [
|
|
392
|
+
...u,
|
|
393
|
+
...p,
|
|
394
|
+
...d,
|
|
395
|
+
...f,
|
|
396
|
+
...s
|
|
397
|
+
], h = !1, g = [];
|
|
398
|
+
for (let e of o) (e.type === "JSXAttribute" || e.type === "JSXSpreadAttribute") && g.push(e);
|
|
399
|
+
if (g.length === m.length) {
|
|
400
|
+
for (let [e, t] of g.entries()) if (t !== m[e]) {
|
|
401
|
+
h = !0;
|
|
402
|
+
break;
|
|
403
|
+
}
|
|
404
|
+
} else h = !0;
|
|
405
|
+
if (!h) return;
|
|
406
|
+
let _ = `<${t.getText(a.name)} ${m.map((e) => t.getText(e)).join(" ")}${a.selfClosing ? " />" : ">"}`;
|
|
407
|
+
e.report({
|
|
408
|
+
node: a,
|
|
409
|
+
messageId: "wrongOrder",
|
|
410
|
+
fix: (e) => e.replaceTextRange([a.range[0], a.range[1]], _)
|
|
411
|
+
});
|
|
412
|
+
} };
|
|
413
|
+
}
|
|
414
|
+
},
|
|
415
|
+
"sort-object-props": {
|
|
416
|
+
meta: {
|
|
417
|
+
type: "suggestion",
|
|
418
|
+
fixable: "code",
|
|
419
|
+
messages: {
|
|
420
|
+
wrongOrder: "Object properties should be ordered!",
|
|
421
|
+
wrongDestructure: "Destructured properties should be ordered!",
|
|
422
|
+
wrongParams: "Function parameters should be ordered!",
|
|
423
|
+
wrongTypeProperties: "Type properties should be ordered!"
|
|
424
|
+
}
|
|
425
|
+
},
|
|
426
|
+
create(e) {
|
|
427
|
+
let { sourceCode: t } = e, n = new Set(l), r = (e) => /^on[A-Z]/.test(e), i = (e) => {
|
|
428
|
+
let t = u.indexOf(e);
|
|
429
|
+
return t === -1 ? 999 : t;
|
|
430
|
+
}, a = (e) => e ? e.type === "Property" || e.type === "TSPropertySignature" || e.type === "TSMethodSignature" || e.type === "TSCallSignatureDeclaration" || e.type === "TSConstructSignatureDeclaration" ? e.key?.name || e.key?.value || "" : e.type === "Identifier" ? e.name : "" : "", o = (e) => e.type === "SpreadElement" || e.type === "RestElement" || e.type === "ExperimentalRestProperty", s = (e) => e.type === "TSPropertySignature" || e.type === "TSMethodSignature" || e.type === "TSCallSignatureDeclaration" || e.type === "TSConstructSignatureDeclaration" || e.type === "TSIndexSignature", c = (e) => {
|
|
431
|
+
if (e.length === 0) return e;
|
|
432
|
+
let t = l.flatMap((t) => e.filter((e) => a(e) === t)), o = e.filter((e) => {
|
|
433
|
+
let t = a(e);
|
|
434
|
+
return !n.has(t) && !r(t) && !["className", "style"].includes(t);
|
|
435
|
+
}), s = e.filter((e) => {
|
|
436
|
+
let t = a(e);
|
|
437
|
+
return r(t);
|
|
438
|
+
}).sort((e, t) => {
|
|
439
|
+
let n = i(a(e)), r = i(a(t));
|
|
440
|
+
return n === 999 && r === 999 ? 0 : n - r;
|
|
441
|
+
}), c = e.filter((e) => {
|
|
442
|
+
let t = a(e);
|
|
443
|
+
return ["className", "style"].includes(t);
|
|
444
|
+
}).sort((e, t) => {
|
|
445
|
+
let n = a(e), r = a(t);
|
|
446
|
+
return n === "className" && r === "style" ? -1 : +(n === "style" && r === "className");
|
|
447
|
+
});
|
|
448
|
+
return [
|
|
449
|
+
...t,
|
|
450
|
+
...o,
|
|
451
|
+
...s,
|
|
452
|
+
...c
|
|
453
|
+
];
|
|
454
|
+
}, d = (e) => {
|
|
455
|
+
let t = [], n = [];
|
|
456
|
+
for (let r of e) if (o(r)) {
|
|
457
|
+
if (n.length > 0) {
|
|
458
|
+
let e = c(n);
|
|
459
|
+
t.push(...e), n = [];
|
|
460
|
+
}
|
|
461
|
+
t.push(r);
|
|
462
|
+
} else n.push(r);
|
|
463
|
+
if (n.length > 0) {
|
|
464
|
+
let e = c(n);
|
|
465
|
+
t.push(...e);
|
|
466
|
+
}
|
|
467
|
+
return t;
|
|
468
|
+
}, f = (e) => {
|
|
469
|
+
if (e.length <= 1) return e;
|
|
470
|
+
let t = e.filter((e) => !o(e) && s(e)), n = e.filter((e) => !t.includes(e));
|
|
471
|
+
return [...c(t), ...n];
|
|
472
|
+
}, p = (e, t) => {
|
|
473
|
+
if (e.length !== t.length) return !0;
|
|
474
|
+
for (let [n, r] of e.entries()) if (r !== t[n]) return !0;
|
|
475
|
+
return !1;
|
|
476
|
+
}, m = (e) => `{ ${e.map((e) => t.getText(e)).join(", ")} }`, h = (e) => e.map((e) => t.getText(e)).join("\n"), g = (n, r) => {
|
|
477
|
+
let { properties: i } = n;
|
|
478
|
+
if (!i || i.length <= 1) return;
|
|
479
|
+
let a = d(i);
|
|
480
|
+
if (!p(i, a)) return;
|
|
481
|
+
let o = i[0], s = i.at(-1), c = o.range[0], l = s.range[1];
|
|
482
|
+
e.report({
|
|
483
|
+
node: n,
|
|
484
|
+
messageId: r,
|
|
485
|
+
fix(e) {
|
|
486
|
+
let n = a.map((e) => t.getText(e)).join(", ");
|
|
487
|
+
return e.replaceTextRange([c, l], n);
|
|
488
|
+
}
|
|
489
|
+
});
|
|
490
|
+
}, _ = (e) => {
|
|
491
|
+
let { params: t } = e;
|
|
492
|
+
if (t && t.length !== 0) for (let e of t) e.type === "ObjectPattern" && g(e, "wrongParams");
|
|
493
|
+
}, v = (t) => {
|
|
494
|
+
let n = t.body ?? t.members;
|
|
495
|
+
if (!n || n.length <= 1) return;
|
|
496
|
+
let r = f(n);
|
|
497
|
+
if (!p(n, r)) return;
|
|
498
|
+
let i = n[0], a = n.at(-1), o = i.range[0], s = a.range[1];
|
|
499
|
+
e.report({
|
|
500
|
+
node: t,
|
|
501
|
+
messageId: "wrongTypeProperties",
|
|
502
|
+
fix(e) {
|
|
503
|
+
let t = h(r);
|
|
504
|
+
return e.replaceTextRange([o, s], t);
|
|
505
|
+
}
|
|
506
|
+
});
|
|
507
|
+
};
|
|
508
|
+
return {
|
|
509
|
+
ObjectExpression(t) {
|
|
510
|
+
let { properties: n } = t;
|
|
511
|
+
if (n.length <= 1) return;
|
|
512
|
+
let r = d(n);
|
|
513
|
+
p(n, r) && e.report({
|
|
514
|
+
node: t,
|
|
515
|
+
messageId: "wrongOrder",
|
|
516
|
+
fix(e) {
|
|
517
|
+
let n = m(r);
|
|
518
|
+
return e.replaceText(t, n);
|
|
519
|
+
}
|
|
520
|
+
});
|
|
521
|
+
},
|
|
522
|
+
ObjectPattern(e) {
|
|
523
|
+
g(e, "wrongDestructure");
|
|
524
|
+
},
|
|
525
|
+
TSTypeLiteral(e) {
|
|
526
|
+
v(e);
|
|
527
|
+
},
|
|
528
|
+
TSInterfaceBody(e) {
|
|
529
|
+
v(e);
|
|
530
|
+
},
|
|
531
|
+
TSTypeAliasDeclaration(e) {
|
|
532
|
+
e.typeAnnotation?.type === "TSTypeLiteral" && v(e.typeAnnotation);
|
|
533
|
+
},
|
|
534
|
+
FunctionDeclaration: _,
|
|
535
|
+
FunctionExpression: _,
|
|
536
|
+
ArrowFunctionExpression: _
|
|
537
|
+
};
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
}, f = {
|
|
541
|
+
meta: {
|
|
542
|
+
name: e.name,
|
|
543
|
+
version: e.version
|
|
544
|
+
},
|
|
545
|
+
rules: d,
|
|
546
|
+
configs: {}
|
|
547
|
+
};
|
|
548
|
+
f.configs.recommended = {
|
|
549
|
+
plugins: { "@sabzidev": f },
|
|
550
|
+
rules: Object.fromEntries(Object.keys(d).map((e) => [`@sabzidev/${e}`, "warn"]))
|
|
551
|
+
};
|
|
552
|
+
//#endregion
|
|
553
|
+
export { f as default };
|
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,6 +1,44 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sabzidev/eslint-rules",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
|
|
3
|
+
"description": "A collection of ESLint rules for better code quality, consistency, and maintainability.",
|
|
4
|
+
"repository": {
|
|
5
|
+
"type": "git",
|
|
6
|
+
"url": "https://github.com/SabziDev/eslint-rules"
|
|
7
|
+
},
|
|
8
|
+
"license": "MIT",
|
|
9
|
+
"keywords": [
|
|
10
|
+
"SabziDev",
|
|
11
|
+
"eslint",
|
|
12
|
+
"eslint-plugin",
|
|
13
|
+
"eslint-rules",
|
|
14
|
+
"code-quality",
|
|
15
|
+
"code-style",
|
|
16
|
+
"linting",
|
|
17
|
+
"developer-tools"
|
|
18
|
+
],
|
|
19
|
+
"author": "Abolfazl Sabzmohammadi <SabziDev@gmail.com> (https://Sabzi.Dev)",
|
|
20
|
+
"version": "1.0.0",
|
|
21
|
+
"type": "module",
|
|
22
|
+
"main": "./dist/index.js",
|
|
23
|
+
"exports": "./dist/index.js",
|
|
24
|
+
"files": [
|
|
25
|
+
"dist",
|
|
26
|
+
"docs",
|
|
27
|
+
"README.md",
|
|
28
|
+
"LICENSE"
|
|
29
|
+
],
|
|
30
|
+
"publishConfig": {
|
|
31
|
+
"access": "public"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"eslint": "^10.12.0",
|
|
35
|
+
"prettier": "^3.9.9",
|
|
36
|
+
"@fullstacksjs/eslint-config": "^15.3.0",
|
|
37
|
+
"eslint-plugin-sonarjs": "^4.2.2",
|
|
38
|
+
"eslint-plugin-unicorn": "^77.0.0",
|
|
39
|
+
"vite": "^8.3.0"
|
|
40
|
+
},
|
|
41
|
+
"scripts": {
|
|
42
|
+
"build": "vite build"
|
|
43
|
+
}
|
|
6
44
|
}
|