@dloizides/frontend-devtools 1.0.1
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/.eslintrc.js +34 -0
- package/CHANGELOG.md +26 -0
- package/LICENSE +21 -0
- package/README.md +131 -0
- package/jest.config.js +24 -0
- package/package.json +104 -0
- package/publish.ps1 +107 -0
- package/src/eslint/index.ts +16 -0
- package/src/eslint/legacy.ts +44 -0
- package/src/eslint/plugin.test.ts +60 -0
- package/src/eslint/plugin.ts +87 -0
- package/src/eslint/rules.behaviour.test.ts +86 -0
- package/src/eslint/rules.d.ts +16 -0
- package/src/eslint/types.ts +27 -0
- package/src/i18n/initI18n.test.ts +101 -0
- package/src/i18n/initI18n.ts +109 -0
- package/src/index.ts +31 -0
- package/src/rules/enforce-function-style.js +278 -0
- package/src/rules/enforce-module-structure.js +323 -0
- package/src/rules/enforce-route-preload.js +288 -0
- package/src/rules/enforce-test-colocation.js +82 -0
- package/src/rules/enum-file-isolation.js +149 -0
- package/src/rules/i18n-interpolation.js +185 -0
- package/src/rules/i18n-param-names.js +119 -0
- package/src/rules/no-barrel-companion-file.js +142 -0
- package/src/rules/no-duplicate-nav-prefix.js +107 -0
- package/src/rules/no-duplicate-shared-patterns.js +166 -0
- package/src/rules/no-generated-models-barrel-value-import.js +70 -0
- package/src/rules/no-null-check.js +171 -0
- package/src/rules/no-optional-undefined.js +99 -0
- package/src/rules/no-product-imports-in-shared.js +164 -0
- package/src/rules/prefer-const-enum.js +127 -0
- package/src/rules/require-stable-hook-args.js +372 -0
- package/src/rules/smart-max-lines.js +344 -0
- package/tsconfig.json +25 -0
- package/tsconfig.test.json +11 -0
- package/tsup.config.ts +14 -0
|
@@ -0,0 +1,344 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Custom ESLint Plugin: Smart Max Lines Per Function
|
|
3
|
+
*
|
|
4
|
+
* Applies different line limits based on function type:
|
|
5
|
+
* - React components (functions returning JSX): higher limit (default 200)
|
|
6
|
+
* - Regular functions: stricter limit (default 50, warning at 30)
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
const COMPONENT_MAX_DEFAULT = 200;
|
|
10
|
+
const FUNCTION_MAX_DEFAULT = 50;
|
|
11
|
+
const FUNCTION_WARN_DEFAULT = 30;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Check if an AST node is a JSX element or fragment
|
|
15
|
+
*/
|
|
16
|
+
function isJSX(node) {
|
|
17
|
+
if (!node) {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
return (
|
|
21
|
+
node.type === 'JSXElement' ||
|
|
22
|
+
node.type === 'JSXFragment'
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Check if an expression could resolve to JSX
|
|
28
|
+
*/
|
|
29
|
+
function expressionReturnsJSX(node) {
|
|
30
|
+
if (!node) {
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// Direct JSX
|
|
35
|
+
if (isJSX(node)) {
|
|
36
|
+
return true;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Conditional expression: condition ? <A/> : <B/>
|
|
40
|
+
if (node.type === 'ConditionalExpression') {
|
|
41
|
+
return expressionReturnsJSX(node.consequent) || expressionReturnsJSX(node.alternate);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Logical expressions: condition && <A/> or condition || <A/>
|
|
45
|
+
if (node.type === 'LogicalExpression') {
|
|
46
|
+
return expressionReturnsJSX(node.left) || expressionReturnsJSX(node.right);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Parenthesized expression
|
|
50
|
+
if (node.type === 'ParenthesizedExpression') {
|
|
51
|
+
return expressionReturnsJSX(node.expression);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// Sequence expression (rare but possible)
|
|
55
|
+
if (node.type === 'SequenceExpression') {
|
|
56
|
+
const lastExpr = node.expressions[node.expressions.length - 1];
|
|
57
|
+
return expressionReturnsJSX(lastExpr);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Recursively check if a block contains a return statement with JSX
|
|
65
|
+
*/
|
|
66
|
+
function blockReturnsJSX(blockBody) {
|
|
67
|
+
if (!blockBody || !Array.isArray(blockBody)) {
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
for (const statement of blockBody) {
|
|
72
|
+
if (statementReturnsJSX(statement)) {
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Check if a statement returns JSX
|
|
82
|
+
*/
|
|
83
|
+
function statementReturnsJSX(statement) {
|
|
84
|
+
if (!statement) {
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
switch (statement.type) {
|
|
89
|
+
case 'ReturnStatement':
|
|
90
|
+
return expressionReturnsJSX(statement.argument);
|
|
91
|
+
|
|
92
|
+
case 'IfStatement':
|
|
93
|
+
// Check consequent
|
|
94
|
+
if (statement.consequent) {
|
|
95
|
+
if (statement.consequent.type === 'BlockStatement') {
|
|
96
|
+
if (blockReturnsJSX(statement.consequent.body)) {
|
|
97
|
+
return true;
|
|
98
|
+
}
|
|
99
|
+
} else if (statementReturnsJSX(statement.consequent)) {
|
|
100
|
+
return true;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
// Check alternate (else branch)
|
|
104
|
+
if (statement.alternate) {
|
|
105
|
+
if (statement.alternate.type === 'BlockStatement') {
|
|
106
|
+
if (blockReturnsJSX(statement.alternate.body)) {
|
|
107
|
+
return true;
|
|
108
|
+
}
|
|
109
|
+
} else if (statementReturnsJSX(statement.alternate)) {
|
|
110
|
+
return true;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return false;
|
|
114
|
+
|
|
115
|
+
case 'SwitchStatement':
|
|
116
|
+
for (const switchCase of statement.cases) {
|
|
117
|
+
if (blockReturnsJSX(switchCase.consequent)) {
|
|
118
|
+
return true;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return false;
|
|
122
|
+
|
|
123
|
+
case 'TryStatement':
|
|
124
|
+
if (statement.block && blockReturnsJSX(statement.block.body)) {
|
|
125
|
+
return true;
|
|
126
|
+
}
|
|
127
|
+
if (statement.handler && statement.handler.body && blockReturnsJSX(statement.handler.body.body)) {
|
|
128
|
+
return true;
|
|
129
|
+
}
|
|
130
|
+
if (statement.finalizer && blockReturnsJSX(statement.finalizer.body)) {
|
|
131
|
+
return true;
|
|
132
|
+
}
|
|
133
|
+
return false;
|
|
134
|
+
|
|
135
|
+
case 'BlockStatement':
|
|
136
|
+
return blockReturnsJSX(statement.body);
|
|
137
|
+
|
|
138
|
+
default:
|
|
139
|
+
return false;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Check if a function node is a React component (returns JSX)
|
|
145
|
+
*/
|
|
146
|
+
function isReactComponent(node) {
|
|
147
|
+
// Arrow function with implicit JSX return: () => <div/>
|
|
148
|
+
if (node.type === 'ArrowFunctionExpression') {
|
|
149
|
+
if (node.body.type !== 'BlockStatement') {
|
|
150
|
+
return expressionReturnsJSX(node.body);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// Function with block body - check for JSX returns
|
|
155
|
+
const body = node.body;
|
|
156
|
+
if (body && body.type === 'BlockStatement') {
|
|
157
|
+
return blockReturnsJSX(body.body);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
return false;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Count effective lines in a function (optionally skipping blanks/comments)
|
|
165
|
+
*/
|
|
166
|
+
function countLines(node, sourceCode, skipBlankLines, skipComments) {
|
|
167
|
+
const startLine = node.loc.start.line;
|
|
168
|
+
const endLine = node.loc.end.line;
|
|
169
|
+
|
|
170
|
+
if (!skipBlankLines && !skipComments) {
|
|
171
|
+
return endLine - startLine + 1;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const lines = sourceCode.lines.slice(startLine - 1, endLine);
|
|
175
|
+
let count = 0;
|
|
176
|
+
|
|
177
|
+
// Get all comments in the function
|
|
178
|
+
const allComments = sourceCode.getAllComments();
|
|
179
|
+
const functionComments = allComments.filter(
|
|
180
|
+
(comment) => comment.loc.start.line >= startLine && comment.loc.end.line <= endLine
|
|
181
|
+
);
|
|
182
|
+
|
|
183
|
+
// Build a set of line numbers that are purely comment lines
|
|
184
|
+
const commentOnlyLines = new Set();
|
|
185
|
+
if (skipComments) {
|
|
186
|
+
for (const comment of functionComments) {
|
|
187
|
+
for (let line = comment.loc.start.line; line <= comment.loc.end.line; line++) {
|
|
188
|
+
const lineContent = sourceCode.lines[line - 1];
|
|
189
|
+
if (lineContent) {
|
|
190
|
+
const trimmed = lineContent.trim();
|
|
191
|
+
// Check if line is only a comment (starts with // or /* or * for multiline)
|
|
192
|
+
const isOnlyComment =
|
|
193
|
+
trimmed.startsWith('//') ||
|
|
194
|
+
trimmed.startsWith('/*') ||
|
|
195
|
+
trimmed.startsWith('*') ||
|
|
196
|
+
trimmed.endsWith('*/') ||
|
|
197
|
+
trimmed === '';
|
|
198
|
+
if (isOnlyComment) {
|
|
199
|
+
commentOnlyLines.add(line);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
for (let i = 0; i < lines.length; i++) {
|
|
207
|
+
const lineNumber = startLine + i;
|
|
208
|
+
const lineContent = lines[i];
|
|
209
|
+
|
|
210
|
+
// Skip blank lines
|
|
211
|
+
if (skipBlankLines && lineContent.trim() === '') {
|
|
212
|
+
continue;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// Skip comment-only lines
|
|
216
|
+
if (skipComments && commentOnlyLines.has(lineNumber)) {
|
|
217
|
+
continue;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
count++;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
return count;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* The ESLint rule definition
|
|
228
|
+
*/
|
|
229
|
+
const smartMaxLinesRule = {
|
|
230
|
+
meta: {
|
|
231
|
+
type: 'suggestion',
|
|
232
|
+
docs: {
|
|
233
|
+
description:
|
|
234
|
+
'Enforce different line limits for React components vs regular functions',
|
|
235
|
+
category: 'Best Practices',
|
|
236
|
+
recommended: true,
|
|
237
|
+
},
|
|
238
|
+
schema: [
|
|
239
|
+
{
|
|
240
|
+
type: 'object',
|
|
241
|
+
properties: {
|
|
242
|
+
componentMax: {
|
|
243
|
+
type: 'integer',
|
|
244
|
+
minimum: 1,
|
|
245
|
+
description: 'Maximum lines for React components',
|
|
246
|
+
},
|
|
247
|
+
functionMax: {
|
|
248
|
+
type: 'integer',
|
|
249
|
+
minimum: 1,
|
|
250
|
+
description: 'Maximum lines for regular functions (error threshold)',
|
|
251
|
+
},
|
|
252
|
+
functionWarn: {
|
|
253
|
+
type: 'integer',
|
|
254
|
+
minimum: 1,
|
|
255
|
+
description: 'Warning threshold for regular functions',
|
|
256
|
+
},
|
|
257
|
+
skipBlankLines: {
|
|
258
|
+
type: 'boolean',
|
|
259
|
+
description: 'Skip blank lines when counting',
|
|
260
|
+
},
|
|
261
|
+
skipComments: {
|
|
262
|
+
type: 'boolean',
|
|
263
|
+
description: 'Skip comment lines when counting',
|
|
264
|
+
},
|
|
265
|
+
},
|
|
266
|
+
additionalProperties: false,
|
|
267
|
+
},
|
|
268
|
+
],
|
|
269
|
+
messages: {
|
|
270
|
+
componentTooLong:
|
|
271
|
+
'React component has too many lines ({{count}}). Maximum allowed is {{max}}.',
|
|
272
|
+
functionTooLong:
|
|
273
|
+
'Function has too many lines ({{count}}). Maximum allowed is {{max}}.',
|
|
274
|
+
functionWarning:
|
|
275
|
+
'Function has {{count}} lines which exceeds recommended limit of {{warn}}. Consider refactoring.',
|
|
276
|
+
},
|
|
277
|
+
},
|
|
278
|
+
|
|
279
|
+
create(context) {
|
|
280
|
+
const options = context.options[0] || {};
|
|
281
|
+
const componentMax = options.componentMax ?? COMPONENT_MAX_DEFAULT;
|
|
282
|
+
const functionMax = options.functionMax ?? FUNCTION_MAX_DEFAULT;
|
|
283
|
+
const functionWarn = options.functionWarn ?? FUNCTION_WARN_DEFAULT;
|
|
284
|
+
const skipBlankLines = options.skipBlankLines ?? true;
|
|
285
|
+
const skipComments = options.skipComments ?? true;
|
|
286
|
+
|
|
287
|
+
const sourceCode = context.sourceCode || context.getSourceCode();
|
|
288
|
+
|
|
289
|
+
function checkFunction(node) {
|
|
290
|
+
const lineCount = countLines(node, sourceCode, skipBlankLines, skipComments);
|
|
291
|
+
const isComponent = isReactComponent(node);
|
|
292
|
+
|
|
293
|
+
if (isComponent) {
|
|
294
|
+
// React component - apply componentMax limit
|
|
295
|
+
if (lineCount > componentMax) {
|
|
296
|
+
context.report({
|
|
297
|
+
node,
|
|
298
|
+
messageId: 'componentTooLong',
|
|
299
|
+
data: {
|
|
300
|
+
count: String(lineCount),
|
|
301
|
+
max: String(componentMax),
|
|
302
|
+
},
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
} else {
|
|
306
|
+
// Regular function - apply functionMax (error) and functionWarn (warning)
|
|
307
|
+
if (lineCount > functionMax) {
|
|
308
|
+
context.report({
|
|
309
|
+
node,
|
|
310
|
+
messageId: 'functionTooLong',
|
|
311
|
+
data: {
|
|
312
|
+
count: String(lineCount),
|
|
313
|
+
max: String(functionMax),
|
|
314
|
+
},
|
|
315
|
+
});
|
|
316
|
+
} else if (lineCount > functionWarn) {
|
|
317
|
+
context.report({
|
|
318
|
+
node,
|
|
319
|
+
messageId: 'functionWarning',
|
|
320
|
+
data: {
|
|
321
|
+
count: String(lineCount),
|
|
322
|
+
warn: String(functionWarn),
|
|
323
|
+
},
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
return {
|
|
330
|
+
FunctionDeclaration: checkFunction,
|
|
331
|
+
FunctionExpression: checkFunction,
|
|
332
|
+
ArrowFunctionExpression: checkFunction,
|
|
333
|
+
};
|
|
334
|
+
},
|
|
335
|
+
};
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* Export the plugin
|
|
339
|
+
*/
|
|
340
|
+
export default {
|
|
341
|
+
rules: {
|
|
342
|
+
'smart-max-lines': smartMaxLinesRule,
|
|
343
|
+
},
|
|
344
|
+
};
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2020",
|
|
4
|
+
"module": "ESNext",
|
|
5
|
+
"moduleResolution": "bundler",
|
|
6
|
+
"lib": ["ES2020", "DOM"],
|
|
7
|
+
"declaration": true,
|
|
8
|
+
"declarationMap": true,
|
|
9
|
+
"strict": true,
|
|
10
|
+
"noUnusedLocals": true,
|
|
11
|
+
"noUnusedParameters": true,
|
|
12
|
+
"noImplicitReturns": true,
|
|
13
|
+
"noFallthroughCasesInSwitch": true,
|
|
14
|
+
"noUncheckedIndexedAccess": true,
|
|
15
|
+
"esModuleInterop": true,
|
|
16
|
+
"skipLibCheck": true,
|
|
17
|
+
"forceConsistentCasingInFileNames": true,
|
|
18
|
+
"allowJs": false,
|
|
19
|
+
"resolveJsonModule": true,
|
|
20
|
+
"outDir": "./dist",
|
|
21
|
+
"rootDir": "./src"
|
|
22
|
+
},
|
|
23
|
+
"include": ["src/**/*"],
|
|
24
|
+
"exclude": ["node_modules", "dist", "**/*.test.ts"]
|
|
25
|
+
}
|
package/tsup.config.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { defineConfig } from 'tsup';
|
|
2
|
+
|
|
3
|
+
export default defineConfig({
|
|
4
|
+
entry: ['src/index.ts', 'src/eslint/index.ts'],
|
|
5
|
+
format: ['cjs', 'esm'],
|
|
6
|
+
dts: true,
|
|
7
|
+
splitting: false,
|
|
8
|
+
sourcemap: true,
|
|
9
|
+
clean: false,
|
|
10
|
+
treeshake: true,
|
|
11
|
+
minify: false,
|
|
12
|
+
target: 'es2020',
|
|
13
|
+
outDir: 'dist',
|
|
14
|
+
});
|