@utilfirst/eslint-plugin 0.1.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 +56 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +466 -0
- package/package.json +84 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Ayan Yenbekbay
|
|
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
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# @utilfirst/eslint-plugin
|
|
2
|
+
|
|
3
|
+
Shared ESLint rules for utilfirst projects. Flat config only, ESLint v9 / v10.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
pnpm add -D @utilfirst/eslint-plugin
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Use
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
// eslint.config.mjs
|
|
15
|
+
import utilfirst from "@utilfirst/eslint-plugin";
|
|
16
|
+
|
|
17
|
+
export default [
|
|
18
|
+
utilfirst.configs.recommended,
|
|
19
|
+
// ...your other configs
|
|
20
|
+
];
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Or wire rules individually:
|
|
24
|
+
|
|
25
|
+
```js
|
|
26
|
+
import utilfirst from "@utilfirst/eslint-plugin";
|
|
27
|
+
|
|
28
|
+
export default [
|
|
29
|
+
{
|
|
30
|
+
plugins: { utilfirst },
|
|
31
|
+
rules: {
|
|
32
|
+
"utilfirst/consistent-blank-lines": "error",
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
];
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Rules
|
|
39
|
+
|
|
40
|
+
| Rule | Description | Fixable |
|
|
41
|
+
| ------------------------------------------------------------------ | ------------------------------------------------------------------------------------ | ------- |
|
|
42
|
+
| [`consistent-blank-lines`](./docs/rules/consistent-blank-lines.md) | Insert blank lines between statement-list and JSXChild items that start new thoughts | yes |
|
|
43
|
+
|
|
44
|
+
## Develop
|
|
45
|
+
|
|
46
|
+
```sh
|
|
47
|
+
pnpm install
|
|
48
|
+
pnpm run setup-hooks # one-time: wire pre-commit via simple-git-hooks
|
|
49
|
+
pnpm test # vitest + @typescript-eslint/rule-tester
|
|
50
|
+
pnpm run build # tsdown → dist/
|
|
51
|
+
pnpm run lint # eslint + prettier + publint + tsc
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## License
|
|
55
|
+
|
|
56
|
+
MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { TSESLint } from "@typescript-eslint/utils";
|
|
2
|
+
|
|
3
|
+
//#region src/index.d.ts
|
|
4
|
+
declare const plugin: {
|
|
5
|
+
meta: {
|
|
6
|
+
readonly name: "@utilfirst/eslint-plugin";
|
|
7
|
+
readonly version: "0.1.0";
|
|
8
|
+
};
|
|
9
|
+
rules: {
|
|
10
|
+
"consistent-blank-lines": TSESLint.RuleModule<"extra" | "missing", [], unknown, TSESLint.RuleListener>;
|
|
11
|
+
};
|
|
12
|
+
configs: {
|
|
13
|
+
recommended: TSESLint.FlatConfig.Config;
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
//#endregion
|
|
17
|
+
export { plugin as default };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,466 @@
|
|
|
1
|
+
import { AST_NODE_TYPES } from "@typescript-eslint/utils";
|
|
2
|
+
|
|
3
|
+
//#region src/rules/consistent-blank-lines.ts
|
|
4
|
+
const SKIP_KEYS = new Set([
|
|
5
|
+
"parent",
|
|
6
|
+
"loc",
|
|
7
|
+
"range",
|
|
8
|
+
"start",
|
|
9
|
+
"end"
|
|
10
|
+
]);
|
|
11
|
+
const FN_DECL_TYPES = new Set([
|
|
12
|
+
AST_NODE_TYPES.FunctionDeclaration,
|
|
13
|
+
AST_NODE_TYPES.FunctionExpression,
|
|
14
|
+
AST_NODE_TYPES.ArrowFunctionExpression
|
|
15
|
+
]);
|
|
16
|
+
const IMPORT_SPECIFIER_TYPES = new Set([
|
|
17
|
+
AST_NODE_TYPES.ImportSpecifier,
|
|
18
|
+
AST_NODE_TYPES.ImportDefaultSpecifier,
|
|
19
|
+
AST_NODE_TYPES.ImportNamespaceSpecifier
|
|
20
|
+
]);
|
|
21
|
+
const OPAQUE_BODY_TYPES = new Set([
|
|
22
|
+
AST_NODE_TYPES.FunctionDeclaration,
|
|
23
|
+
AST_NODE_TYPES.ClassDeclaration,
|
|
24
|
+
AST_NODE_TYPES.ClassExpression
|
|
25
|
+
]);
|
|
26
|
+
const FLOW_PREV_TYPES = new Set([AST_NODE_TYPES.ExpressionStatement, AST_NODE_TYPES.IfStatement]);
|
|
27
|
+
const FLOW_NEXT_TYPES = new Set([
|
|
28
|
+
AST_NODE_TYPES.ExpressionStatement,
|
|
29
|
+
AST_NODE_TYPES.IfStatement,
|
|
30
|
+
AST_NODE_TYPES.ReturnStatement,
|
|
31
|
+
AST_NODE_TYPES.ThrowStatement,
|
|
32
|
+
AST_NODE_TYPES.BreakStatement,
|
|
33
|
+
AST_NODE_TYPES.ContinueStatement
|
|
34
|
+
]);
|
|
35
|
+
const HEAVY_NEXT_TYPES = new Set([
|
|
36
|
+
AST_NODE_TYPES.VariableDeclaration,
|
|
37
|
+
AST_NODE_TYPES.FunctionDeclaration,
|
|
38
|
+
AST_NODE_TYPES.ClassDeclaration,
|
|
39
|
+
AST_NODE_TYPES.TSInterfaceDeclaration,
|
|
40
|
+
AST_NODE_TYPES.ReturnStatement,
|
|
41
|
+
AST_NODE_TYPES.ThrowStatement
|
|
42
|
+
]);
|
|
43
|
+
const TERMINATING_STATEMENT_TYPES = new Set([
|
|
44
|
+
AST_NODE_TYPES.ReturnStatement,
|
|
45
|
+
AST_NODE_TYPES.ThrowStatement,
|
|
46
|
+
AST_NODE_TYPES.BreakStatement,
|
|
47
|
+
AST_NODE_TYPES.ContinueStatement
|
|
48
|
+
]);
|
|
49
|
+
function parentOf(node) {
|
|
50
|
+
return node.parent;
|
|
51
|
+
}
|
|
52
|
+
const consistentBlankLines = {
|
|
53
|
+
meta: {
|
|
54
|
+
type: "layout",
|
|
55
|
+
docs: {
|
|
56
|
+
description: "Insert blank lines between statement-list and JSXChild items that start a new thought.",
|
|
57
|
+
url: "https://github.com/utilfirst/utilfirst-eslint-plugin/blob/main/docs/rules/consistent-blank-lines.md"
|
|
58
|
+
},
|
|
59
|
+
fixable: "whitespace",
|
|
60
|
+
schema: [],
|
|
61
|
+
messages: {
|
|
62
|
+
extra: "Unexpected blank line between statements that continue the same paragraph.",
|
|
63
|
+
missing: "Expected a blank line between statements that start new paragraphs."
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
defaultOptions: [],
|
|
67
|
+
create(context) {
|
|
68
|
+
const sourceCode = context.sourceCode;
|
|
69
|
+
function checkBlock(statements) {
|
|
70
|
+
for (let i = 0; i < statements.length - 1; i++) {
|
|
71
|
+
const prev = statements[i];
|
|
72
|
+
const next = statements[i + 1];
|
|
73
|
+
if (prev && next) checkPair(prev, next, sourceCode.getCommentsBefore(next)[0] ?? next, () => sameParagraph(prev, next, sourceCode), () => isImportPair(prev, next) || isReExport(prev) && isReExport(next));
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
function checkJsxChildren(children) {
|
|
77
|
+
const siblings = [];
|
|
78
|
+
let pendingLeading = [];
|
|
79
|
+
for (const child of children) {
|
|
80
|
+
if (child.type === AST_NODE_TYPES.JSXText && child.value.trim() === "") continue;
|
|
81
|
+
if (isCommentOnlyContainer(child)) {
|
|
82
|
+
pendingLeading.push(child);
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
siblings.push({
|
|
86
|
+
leading: pendingLeading,
|
|
87
|
+
node: child
|
|
88
|
+
});
|
|
89
|
+
pendingLeading = [];
|
|
90
|
+
}
|
|
91
|
+
for (let i = 0; i < siblings.length - 1; i++) {
|
|
92
|
+
const prevSibling = siblings[i];
|
|
93
|
+
const nextSibling = siblings[i + 1];
|
|
94
|
+
if (prevSibling && nextSibling) {
|
|
95
|
+
const { node: prev } = prevSibling;
|
|
96
|
+
const { leading, node: next } = nextSibling;
|
|
97
|
+
checkPair(prev, next, leading[0] ?? next, () => sameJsxParagraph(prev, next, leading), () => false);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
function checkPair(prev, next, effectiveStart, isSameParagraph, shouldPreserveExtra) {
|
|
102
|
+
const prevEndLine = prev.loc.end.line;
|
|
103
|
+
const nextStartLine = effectiveStart.loc.start.line;
|
|
104
|
+
if (nextStartLine - prevEndLine < 1) return;
|
|
105
|
+
const isPaddingRequired = !isSameParagraph();
|
|
106
|
+
const paddingCount = nextStartLine - prevEndLine - 1;
|
|
107
|
+
const targetPadding = isPaddingRequired ? 1 : 0;
|
|
108
|
+
if (paddingCount === targetPadding) return;
|
|
109
|
+
if (paddingCount > targetPadding && shouldPreserveExtra()) return;
|
|
110
|
+
context.report({
|
|
111
|
+
node: next,
|
|
112
|
+
messageId: paddingCount < targetPadding ? "missing" : "extra",
|
|
113
|
+
fix(fixer) {
|
|
114
|
+
const start = sourceCode.getIndexFromLoc({
|
|
115
|
+
line: prevEndLine + 1,
|
|
116
|
+
column: 0
|
|
117
|
+
});
|
|
118
|
+
const end = sourceCode.getIndexFromLoc({
|
|
119
|
+
line: nextStartLine,
|
|
120
|
+
column: 0
|
|
121
|
+
});
|
|
122
|
+
return fixer.replaceTextRange([start, end], "\n".repeat(targetPadding));
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
return {
|
|
127
|
+
Program(node) {
|
|
128
|
+
checkBlock(node.body);
|
|
129
|
+
},
|
|
130
|
+
BlockStatement(node) {
|
|
131
|
+
checkBlock(node.body);
|
|
132
|
+
},
|
|
133
|
+
SwitchCase(node) {
|
|
134
|
+
checkBlock(node.consequent);
|
|
135
|
+
},
|
|
136
|
+
StaticBlock(node) {
|
|
137
|
+
checkBlock(node.body);
|
|
138
|
+
},
|
|
139
|
+
JSXElement(node) {
|
|
140
|
+
checkJsxChildren(node.children);
|
|
141
|
+
},
|
|
142
|
+
JSXFragment(node) {
|
|
143
|
+
checkJsxChildren(node.children);
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
function sameParagraph(prev, next, sourceCode) {
|
|
149
|
+
if (hasMultiLineLeadingComment(next, sourceCode)) return false;
|
|
150
|
+
const prevHook = isHookStatement(prev);
|
|
151
|
+
const nextHook = isHookStatement(next);
|
|
152
|
+
if (prevHook || nextHook) return prevHook && nextHook && isMatchingVarDeclPair(prev, next);
|
|
153
|
+
return sharesNameFlow(prev, next, sourceCode) || sameShape(prev, next);
|
|
154
|
+
}
|
|
155
|
+
function isHookStatement(stmt) {
|
|
156
|
+
if (stmt.type === AST_NODE_TYPES.VariableDeclaration && stmt.declarations.length === 1) return isHookCall(stmt.declarations[0].init);
|
|
157
|
+
if (stmt.type === AST_NODE_TYPES.ExpressionStatement) return isHookCall(stmt.expression);
|
|
158
|
+
return false;
|
|
159
|
+
}
|
|
160
|
+
function sharesNameFlow(prev, next, sourceCode) {
|
|
161
|
+
if (isMultiLine(prev)) return false;
|
|
162
|
+
const nextType = unwrapExport(next).type;
|
|
163
|
+
if (isMultiLine(next) && HEAVY_NEXT_TYPES.has(nextType)) return false;
|
|
164
|
+
if (nextType === AST_NODE_TYPES.TSTypeAliasDeclaration) return false;
|
|
165
|
+
const introduced = collectIntroducedOrAssignedNames(prev);
|
|
166
|
+
if (introduced.size === 0) return false;
|
|
167
|
+
const referenced = collectReferencedNames(next, sourceCode);
|
|
168
|
+
for (const name of introduced) if (referenced.has(name)) return true;
|
|
169
|
+
return false;
|
|
170
|
+
}
|
|
171
|
+
function isHookCall(node) {
|
|
172
|
+
return node?.type === AST_NODE_TYPES.CallExpression && node.callee.type === AST_NODE_TYPES.Identifier && /^use[A-Z]/.test(node.callee.name);
|
|
173
|
+
}
|
|
174
|
+
function sameShape(prev, next) {
|
|
175
|
+
if (isImportPair(prev, next)) return true;
|
|
176
|
+
if (isReExport(prev) && isReExport(next)) return true;
|
|
177
|
+
if (isMatchingVarDeclPair(prev, next)) return true;
|
|
178
|
+
if (isMatchingTypeAliasPair(prev, next)) return true;
|
|
179
|
+
if (FLOW_PREV_TYPES.has(prev.type) && FLOW_NEXT_TYPES.has(next.type)) {
|
|
180
|
+
if (HEAVY_NEXT_TYPES.has(next.type) && isMultiLine(next)) return false;
|
|
181
|
+
if (prev.type === AST_NODE_TYPES.IfStatement) {
|
|
182
|
+
if (next.type !== AST_NODE_TYPES.IfStatement) return false;
|
|
183
|
+
if (isGuardIf(prev) && !isGuardIf(next)) return false;
|
|
184
|
+
} else if (next.type === AST_NODE_TYPES.IfStatement && isGuardIf(next)) return false;
|
|
185
|
+
return true;
|
|
186
|
+
}
|
|
187
|
+
return false;
|
|
188
|
+
}
|
|
189
|
+
function isImportPair(prev, next) {
|
|
190
|
+
return prev.type === AST_NODE_TYPES.ImportDeclaration && next.type === AST_NODE_TYPES.ImportDeclaration;
|
|
191
|
+
}
|
|
192
|
+
function isReExport(stmt) {
|
|
193
|
+
return stmt.type === AST_NODE_TYPES.ExportNamedDeclaration && stmt.source !== null || stmt.type === AST_NODE_TYPES.ExportAllDeclaration;
|
|
194
|
+
}
|
|
195
|
+
function isMatchingVarDeclPair(prev, next) {
|
|
196
|
+
const p = unwrapExport(prev);
|
|
197
|
+
const n = unwrapExport(next);
|
|
198
|
+
if (p.type !== AST_NODE_TYPES.VariableDeclaration || n.type !== AST_NODE_TYPES.VariableDeclaration) return false;
|
|
199
|
+
if (!isConstOrLet(p) || !isConstOrLet(n)) return false;
|
|
200
|
+
if (isMultiLine(prev) || isMultiLine(next)) return false;
|
|
201
|
+
if (p.declarations.length !== 1 || n.declarations.length !== 1) return false;
|
|
202
|
+
if (!initsMatchByShape(p.declarations[0].init, n.declarations[0].init)) return false;
|
|
203
|
+
return prev.type === AST_NODE_TYPES.ExportNamedDeclaration === (next.type === AST_NODE_TYPES.ExportNamedDeclaration);
|
|
204
|
+
}
|
|
205
|
+
function isMatchingTypeAliasPair(prev, next) {
|
|
206
|
+
const p = unwrapExport(prev);
|
|
207
|
+
const n = unwrapExport(next);
|
|
208
|
+
if (p.type !== AST_NODE_TYPES.TSTypeAliasDeclaration || n.type !== AST_NODE_TYPES.TSTypeAliasDeclaration) return false;
|
|
209
|
+
if (isMultiLine(prev) || isMultiLine(next)) return false;
|
|
210
|
+
return prev.type === AST_NODE_TYPES.ExportNamedDeclaration === (next.type === AST_NODE_TYPES.ExportNamedDeclaration);
|
|
211
|
+
}
|
|
212
|
+
function unwrapExport(stmt) {
|
|
213
|
+
if (stmt.type === AST_NODE_TYPES.ExportNamedDeclaration && stmt.declaration) return stmt.declaration;
|
|
214
|
+
return stmt;
|
|
215
|
+
}
|
|
216
|
+
function isConstOrLet(decl) {
|
|
217
|
+
return decl.kind === "const" || decl.kind === "let";
|
|
218
|
+
}
|
|
219
|
+
function initsMatchByShape(prevInit, nextInit) {
|
|
220
|
+
const prevCall = asCall(prevInit);
|
|
221
|
+
const nextCall = asCall(nextInit);
|
|
222
|
+
if (prevCall && nextCall) {
|
|
223
|
+
if (calleesEqual(prevCall.callee, nextCall.callee)) return true;
|
|
224
|
+
return prevCall.arguments.length === 0 && nextCall.arguments.length === 0;
|
|
225
|
+
}
|
|
226
|
+
if (prevCall || nextCall) return false;
|
|
227
|
+
return true;
|
|
228
|
+
}
|
|
229
|
+
function asCall(expr) {
|
|
230
|
+
if (!expr) return null;
|
|
231
|
+
if (expr.type === AST_NODE_TYPES.CallExpression) return expr;
|
|
232
|
+
return null;
|
|
233
|
+
}
|
|
234
|
+
function calleesEqual(a, b) {
|
|
235
|
+
if (!a || a.type !== b?.type) return false;
|
|
236
|
+
if (a.type === AST_NODE_TYPES.Identifier && b.type === AST_NODE_TYPES.Identifier) return a.name === b.name;
|
|
237
|
+
if (a.type === AST_NODE_TYPES.ThisExpression) return true;
|
|
238
|
+
if (a.type === AST_NODE_TYPES.MemberExpression && b.type === AST_NODE_TYPES.MemberExpression) {
|
|
239
|
+
if (a.computed !== b.computed) return false;
|
|
240
|
+
if (!calleesEqual(a.object, b.object)) return false;
|
|
241
|
+
if (!a.computed && a.property.type === AST_NODE_TYPES.Identifier && b.property.type === AST_NODE_TYPES.Identifier) return a.property.name === b.property.name;
|
|
242
|
+
return false;
|
|
243
|
+
}
|
|
244
|
+
if (a.type === AST_NODE_TYPES.CallExpression && b.type === AST_NODE_TYPES.CallExpression) return calleesEqual(a.callee, b.callee);
|
|
245
|
+
return false;
|
|
246
|
+
}
|
|
247
|
+
function isGuardIf(stmt) {
|
|
248
|
+
return stmt.type === AST_NODE_TYPES.IfStatement && blockAlwaysTerminates(stmt.consequent);
|
|
249
|
+
}
|
|
250
|
+
function blockAlwaysTerminates(node) {
|
|
251
|
+
if (!node) return false;
|
|
252
|
+
if (TERMINATING_STATEMENT_TYPES.has(node.type)) return true;
|
|
253
|
+
if (node.type === AST_NODE_TYPES.BlockStatement) {
|
|
254
|
+
const last = node.body[node.body.length - 1];
|
|
255
|
+
return last ? blockAlwaysTerminates(last) : false;
|
|
256
|
+
}
|
|
257
|
+
if (node.type === AST_NODE_TYPES.IfStatement) return Boolean(node.alternate) && blockAlwaysTerminates(node.consequent) && blockAlwaysTerminates(node.alternate);
|
|
258
|
+
if (node.type === AST_NODE_TYPES.TryStatement) {
|
|
259
|
+
if (node.finalizer && blockAlwaysTerminates(node.finalizer)) return true;
|
|
260
|
+
if (!blockAlwaysTerminates(node.block)) return false;
|
|
261
|
+
if (!node.handler) return true;
|
|
262
|
+
return blockAlwaysTerminates(node.handler.body);
|
|
263
|
+
}
|
|
264
|
+
return false;
|
|
265
|
+
}
|
|
266
|
+
function collectIntroducedOrAssignedNames(stmt) {
|
|
267
|
+
const set = /* @__PURE__ */ new Set();
|
|
268
|
+
const s = unwrapExport(stmt);
|
|
269
|
+
if (s.type === AST_NODE_TYPES.VariableDeclaration) for (const decl of s.declarations) collectBindingNames(decl.id, set);
|
|
270
|
+
else if (s.type === AST_NODE_TYPES.FunctionDeclaration && s.id) set.add(s.id.name);
|
|
271
|
+
else if (s.type === AST_NODE_TYPES.ClassDeclaration && s.id) set.add(s.id.name);
|
|
272
|
+
else if (s.type === AST_NODE_TYPES.TSInterfaceDeclaration) set.add(s.id.name);
|
|
273
|
+
else if (s.type === AST_NODE_TYPES.ExpressionStatement) {
|
|
274
|
+
const expr = s.expression;
|
|
275
|
+
if (expr.type === AST_NODE_TYPES.AssignmentExpression) collectAssignmentRoots(expr.left, set);
|
|
276
|
+
else if (expr.type === AST_NODE_TYPES.UpdateExpression && expr.argument.type === AST_NODE_TYPES.Identifier) set.add(expr.argument.name);
|
|
277
|
+
}
|
|
278
|
+
return set;
|
|
279
|
+
}
|
|
280
|
+
function collectBindingNames(node, set) {
|
|
281
|
+
if (!node) return;
|
|
282
|
+
switch (node.type) {
|
|
283
|
+
case AST_NODE_TYPES.Identifier:
|
|
284
|
+
set.add(node.name);
|
|
285
|
+
break;
|
|
286
|
+
case AST_NODE_TYPES.ObjectPattern:
|
|
287
|
+
for (const prop of node.properties) if (prop.type === AST_NODE_TYPES.Property) collectBindingNames(prop.value, set);
|
|
288
|
+
else collectBindingNames(prop.argument, set);
|
|
289
|
+
break;
|
|
290
|
+
case AST_NODE_TYPES.ArrayPattern:
|
|
291
|
+
for (const el of node.elements) if (el) collectBindingNames(el, set);
|
|
292
|
+
break;
|
|
293
|
+
case AST_NODE_TYPES.RestElement:
|
|
294
|
+
collectBindingNames(node.argument, set);
|
|
295
|
+
break;
|
|
296
|
+
case AST_NODE_TYPES.AssignmentPattern:
|
|
297
|
+
collectBindingNames(node.left, set);
|
|
298
|
+
break;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
function collectAssignmentRoots(target, set) {
|
|
302
|
+
if (target.type === AST_NODE_TYPES.Identifier) set.add(target.name);
|
|
303
|
+
else if (target.type === AST_NODE_TYPES.MemberExpression) {
|
|
304
|
+
let root = target;
|
|
305
|
+
while (root.type === AST_NODE_TYPES.MemberExpression) root = root.object;
|
|
306
|
+
if (root.type === AST_NODE_TYPES.Identifier) set.add(root.name);
|
|
307
|
+
else if (root.type === AST_NODE_TYPES.ThisExpression) set.add("this");
|
|
308
|
+
} else if (target.type === AST_NODE_TYPES.ObjectPattern || target.type === AST_NODE_TYPES.ArrayPattern) collectBindingNames(target, set);
|
|
309
|
+
}
|
|
310
|
+
function collectReferencedNames(root, sourceCode) {
|
|
311
|
+
const set = /* @__PURE__ */ new Set();
|
|
312
|
+
walk(root, (node) => {
|
|
313
|
+
if (node.type === AST_NODE_TYPES.Identifier && !isDeclarationOrPropertyKey(node)) {
|
|
314
|
+
if (resolvesOutsideRoot(node, root, sourceCode)) set.add(node.name);
|
|
315
|
+
} else if (node.type === AST_NODE_TYPES.ThisExpression) {
|
|
316
|
+
if (thisResolvesOutsideRoot(node, root)) set.add("this");
|
|
317
|
+
} else if (node.type === AST_NODE_TYPES.JSXIdentifier && isJsxComponentIdentifier(node)) {
|
|
318
|
+
if (resolvesOutsideRoot(node, root, sourceCode)) set.add(node.name);
|
|
319
|
+
}
|
|
320
|
+
});
|
|
321
|
+
return set;
|
|
322
|
+
}
|
|
323
|
+
function resolvesOutsideRoot(idNode, root, sourceCode) {
|
|
324
|
+
let scope = sourceCode.getScope(idNode);
|
|
325
|
+
while (scope) {
|
|
326
|
+
const variable = scope.variables.find((v) => v.name === idNode.name);
|
|
327
|
+
if (variable) {
|
|
328
|
+
for (const def of variable.defs) if (isInSubtree(def.node, root)) return false;
|
|
329
|
+
return true;
|
|
330
|
+
}
|
|
331
|
+
scope = scope.upper;
|
|
332
|
+
}
|
|
333
|
+
return true;
|
|
334
|
+
}
|
|
335
|
+
function thisResolvesOutsideRoot(thisNode, root) {
|
|
336
|
+
let p = parentOf(thisNode);
|
|
337
|
+
while (p) {
|
|
338
|
+
if (p.type === AST_NODE_TYPES.FunctionDeclaration || p.type === AST_NODE_TYPES.FunctionExpression) return !isInSubtree(p, root);
|
|
339
|
+
p = parentOf(p);
|
|
340
|
+
}
|
|
341
|
+
return true;
|
|
342
|
+
}
|
|
343
|
+
function isJsxComponentIdentifier(node) {
|
|
344
|
+
if (!/^[A-Z]/.test(node.name)) return false;
|
|
345
|
+
const parent = node.parent;
|
|
346
|
+
if (parent.type === AST_NODE_TYPES.JSXOpeningElement && parent.name === node) return true;
|
|
347
|
+
if (parent.type === AST_NODE_TYPES.JSXClosingElement && parent.name === node) return true;
|
|
348
|
+
if (parent.type === AST_NODE_TYPES.JSXMemberExpression && parent.object === node) return true;
|
|
349
|
+
return false;
|
|
350
|
+
}
|
|
351
|
+
function isInSubtree(node, root) {
|
|
352
|
+
let cur = node;
|
|
353
|
+
while (cur) {
|
|
354
|
+
if (cur === root) return true;
|
|
355
|
+
cur = parentOf(cur);
|
|
356
|
+
}
|
|
357
|
+
return false;
|
|
358
|
+
}
|
|
359
|
+
function isDeclarationOrPropertyKey(idNode) {
|
|
360
|
+
const parent = idNode.parent;
|
|
361
|
+
if (parent.type === AST_NODE_TYPES.VariableDeclarator && parent.id === idNode) return true;
|
|
362
|
+
if (FN_DECL_TYPES.has(parent.type) && "id" in parent && parent.id === idNode) return true;
|
|
363
|
+
if ((parent.type === AST_NODE_TYPES.ClassDeclaration || parent.type === AST_NODE_TYPES.ClassExpression) && parent.id === idNode) return true;
|
|
364
|
+
if (parent.type === AST_NODE_TYPES.MemberExpression && parent.property === idNode && !parent.computed) return true;
|
|
365
|
+
if (parent.type === AST_NODE_TYPES.MethodDefinition && parent.key === idNode && !parent.computed) return true;
|
|
366
|
+
if (parent.type === AST_NODE_TYPES.PropertyDefinition && parent.key === idNode && !parent.computed) return true;
|
|
367
|
+
if (IMPORT_SPECIFIER_TYPES.has(parent.type) && "local" in parent && parent.local === idNode) return true;
|
|
368
|
+
if (parent.type === AST_NODE_TYPES.Property && parent.key === idNode && !parent.computed && !parent.shorthand) return true;
|
|
369
|
+
if (isInBindingPosition(idNode)) return true;
|
|
370
|
+
return false;
|
|
371
|
+
}
|
|
372
|
+
function isInBindingPosition(idNode) {
|
|
373
|
+
let cur = idNode;
|
|
374
|
+
let p = parentOf(cur);
|
|
375
|
+
while (p) {
|
|
376
|
+
if (FN_DECL_TYPES.has(p.type) && "params" in p && Array.isArray(p.params) && p.params.includes(cur)) return true;
|
|
377
|
+
if (p.type === AST_NODE_TYPES.ObjectPattern || p.type === AST_NODE_TYPES.ArrayPattern) return true;
|
|
378
|
+
if (p.type === AST_NODE_TYPES.Property) {
|
|
379
|
+
if (p.computed && p.key === cur) return false;
|
|
380
|
+
if (!p.shorthand && p.value !== cur) return false;
|
|
381
|
+
cur = p;
|
|
382
|
+
p = parentOf(p);
|
|
383
|
+
continue;
|
|
384
|
+
}
|
|
385
|
+
if (p.type === AST_NODE_TYPES.AssignmentPattern) {
|
|
386
|
+
if (p.right === cur) return false;
|
|
387
|
+
cur = p;
|
|
388
|
+
p = parentOf(p);
|
|
389
|
+
continue;
|
|
390
|
+
}
|
|
391
|
+
if (p.type === AST_NODE_TYPES.RestElement) {
|
|
392
|
+
cur = p;
|
|
393
|
+
p = parentOf(p);
|
|
394
|
+
continue;
|
|
395
|
+
}
|
|
396
|
+
return false;
|
|
397
|
+
}
|
|
398
|
+
return false;
|
|
399
|
+
}
|
|
400
|
+
function sameJsxParagraph(prev, next, leading) {
|
|
401
|
+
const firstLeading = leading[0];
|
|
402
|
+
const lastLeading = leading[leading.length - 1];
|
|
403
|
+
if (firstLeading && lastLeading && lastLeading.loc.end.line > firstLeading.loc.start.line) return false;
|
|
404
|
+
if (siblingsIncludeLiteralText(next)) return true;
|
|
405
|
+
return !(isMultiLine(prev) || isMultiLine(next));
|
|
406
|
+
}
|
|
407
|
+
function isCommentOnlyContainer(child) {
|
|
408
|
+
return child.type === AST_NODE_TYPES.JSXExpressionContainer && child.expression.type === AST_NODE_TYPES.JSXEmptyExpression;
|
|
409
|
+
}
|
|
410
|
+
function siblingsIncludeLiteralText(node) {
|
|
411
|
+
const parent = parentOf(node);
|
|
412
|
+
if (!parent || parent.type !== AST_NODE_TYPES.JSXElement && parent.type !== AST_NODE_TYPES.JSXFragment) return false;
|
|
413
|
+
for (const sibling of parent.children) {
|
|
414
|
+
if (sibling.type === AST_NODE_TYPES.JSXText && sibling.value.trim() !== "") return true;
|
|
415
|
+
if (sibling.type === AST_NODE_TYPES.JSXExpressionContainer && expressionYieldsStringLiteral(sibling.expression)) return true;
|
|
416
|
+
}
|
|
417
|
+
return false;
|
|
418
|
+
}
|
|
419
|
+
function expressionYieldsStringLiteral(expr) {
|
|
420
|
+
if (expr.type === AST_NODE_TYPES.Literal) return typeof expr.value === "string";
|
|
421
|
+
if (expr.type === AST_NODE_TYPES.TemplateLiteral) return true;
|
|
422
|
+
if (expr.type === AST_NODE_TYPES.LogicalExpression) return expressionYieldsStringLiteral(expr.right);
|
|
423
|
+
if (expr.type === AST_NODE_TYPES.ConditionalExpression) return expressionYieldsStringLiteral(expr.consequent) || expressionYieldsStringLiteral(expr.alternate);
|
|
424
|
+
return false;
|
|
425
|
+
}
|
|
426
|
+
function walk(node, fn) {
|
|
427
|
+
if (!node || typeof node !== "object" || typeof node.type !== "string") return;
|
|
428
|
+
fn(node);
|
|
429
|
+
const obj = node;
|
|
430
|
+
for (const key of Object.keys(obj)) {
|
|
431
|
+
if (SKIP_KEYS.has(key)) continue;
|
|
432
|
+
if (key === "body" && OPAQUE_BODY_TYPES.has(node.type)) continue;
|
|
433
|
+
const child = obj[key];
|
|
434
|
+
if (Array.isArray(child)) for (const c of child) walk(c, fn);
|
|
435
|
+
else walk(child, fn);
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
function isMultiLine(node) {
|
|
439
|
+
return node.loc.start.line !== node.loc.end.line;
|
|
440
|
+
}
|
|
441
|
+
function hasMultiLineLeadingComment(node, sourceCode) {
|
|
442
|
+
const comments = sourceCode.getCommentsBefore(node);
|
|
443
|
+
if (comments.length === 0) return false;
|
|
444
|
+
const first = comments[0];
|
|
445
|
+
const last = comments[comments.length - 1];
|
|
446
|
+
return Boolean(first && last && last.loc.end.line > first.loc.start.line);
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
//#endregion
|
|
450
|
+
//#region src/index.ts
|
|
451
|
+
const plugin = {
|
|
452
|
+
meta: {
|
|
453
|
+
name: "@utilfirst/eslint-plugin",
|
|
454
|
+
version: "0.1.0"
|
|
455
|
+
},
|
|
456
|
+
rules: { "consistent-blank-lines": consistentBlankLines },
|
|
457
|
+
configs: {}
|
|
458
|
+
};
|
|
459
|
+
plugin.configs.recommended = {
|
|
460
|
+
plugins: { utilfirst: plugin },
|
|
461
|
+
rules: { "utilfirst/consistent-blank-lines": "error" }
|
|
462
|
+
};
|
|
463
|
+
var src_default = plugin;
|
|
464
|
+
|
|
465
|
+
//#endregion
|
|
466
|
+
export { src_default as default };
|
package/package.json
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@utilfirst/eslint-plugin",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Shared ESLint rules for utilfirst projects",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"eslint",
|
|
7
|
+
"eslintplugin",
|
|
8
|
+
"eslint-plugin"
|
|
9
|
+
],
|
|
10
|
+
"homepage": "https://github.com/utilfirst/utilfirst-eslint-plugin#readme",
|
|
11
|
+
"bugs": "https://github.com/utilfirst/utilfirst-eslint-plugin/issues",
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/utilfirst/utilfirst-eslint-plugin.git"
|
|
15
|
+
},
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"author": "Ayan Yenbekbay <ayan.yenb@gmail.com>",
|
|
18
|
+
"type": "module",
|
|
19
|
+
"exports": {
|
|
20
|
+
".": {
|
|
21
|
+
"types": "./dist/index.d.ts",
|
|
22
|
+
"default": "./dist/index.js"
|
|
23
|
+
},
|
|
24
|
+
"./package.json": "./package.json"
|
|
25
|
+
},
|
|
26
|
+
"main": "./dist/index.js",
|
|
27
|
+
"types": "./dist/index.d.ts",
|
|
28
|
+
"files": [
|
|
29
|
+
"dist"
|
|
30
|
+
],
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build": "tsdown",
|
|
33
|
+
"fix": "pnpm run --sequential '/^fix:[^:]+$/'",
|
|
34
|
+
"fix:eslint": "pnpm run lint:eslint --fix",
|
|
35
|
+
"fix:prettier": "pnpm run lint:prettier --write",
|
|
36
|
+
"lint": "pnpm run --sequential '/^lint:[^:]+$/'",
|
|
37
|
+
"lint:eslint": "eslint .",
|
|
38
|
+
"lint:prettier": "prettier --check .",
|
|
39
|
+
"lint:publint": "publint",
|
|
40
|
+
"lint:typecheck": "tsc --pretty --noEmit",
|
|
41
|
+
"prepack": "pnpm run build",
|
|
42
|
+
"setup-hooks": "simple-git-hooks",
|
|
43
|
+
"test": "vitest run"
|
|
44
|
+
},
|
|
45
|
+
"simple-git-hooks": {
|
|
46
|
+
"pre-commit": "pnpm exec lint-staged && pnpm run lint:typecheck"
|
|
47
|
+
},
|
|
48
|
+
"lint-staged": {
|
|
49
|
+
"*": "prettier --ignore-unknown --write",
|
|
50
|
+
"*.{js,cjs,mjs,ts,tsx}": "eslint --fix"
|
|
51
|
+
},
|
|
52
|
+
"dependencies": {
|
|
53
|
+
"@typescript-eslint/utils": "^8.59.3"
|
|
54
|
+
},
|
|
55
|
+
"devDependencies": {
|
|
56
|
+
"@eslint/js": "^10.0.1",
|
|
57
|
+
"@types/node": "^25.9.1",
|
|
58
|
+
"@typescript-eslint/rule-tester": "^8.59.3",
|
|
59
|
+
"eslint": "^10.3.0",
|
|
60
|
+
"lint-staged": "^17.0.4",
|
|
61
|
+
"prettier": "^3.8.3",
|
|
62
|
+
"prettier-plugin-organize-imports": "^4.3.0",
|
|
63
|
+
"prettier-plugin-packagejson": "^3.0.2",
|
|
64
|
+
"prettier-plugin-sh": "^0.18.1",
|
|
65
|
+
"prettier-plugin-sort-json": "^4.2.0",
|
|
66
|
+
"publint": "^0.3.16",
|
|
67
|
+
"simple-git-hooks": "^2.13.1",
|
|
68
|
+
"tsdown": "^0.18.4",
|
|
69
|
+
"typescript": "^6.0.3",
|
|
70
|
+
"typescript-eslint": "^8.59.3",
|
|
71
|
+
"vitest": "^4.0.7"
|
|
72
|
+
},
|
|
73
|
+
"peerDependencies": {
|
|
74
|
+
"eslint": "^9.0.0 || ^10.0.0"
|
|
75
|
+
},
|
|
76
|
+
"packageManager": "pnpm@11.0.9",
|
|
77
|
+
"engines": {
|
|
78
|
+
"node": ">=20.19.0"
|
|
79
|
+
},
|
|
80
|
+
"publishConfig": {
|
|
81
|
+
"access": "public",
|
|
82
|
+
"provenance": true
|
|
83
|
+
}
|
|
84
|
+
}
|