@webpieces/dev-config 0.2.45 → 0.2.47

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.
@@ -0,0 +1,569 @@
1
+ /**
2
+ * Validate Modified Methods Executor
3
+ *
4
+ * Validates that modified methods don't exceed a maximum line count (default 80).
5
+ * This encourages gradual cleanup of legacy long methods - when you touch a method,
6
+ * you must bring it under the limit.
7
+ *
8
+ * Combined with validate-new-methods (30 line limit), this creates a gradual
9
+ * transition to cleaner code:
10
+ * - New methods: strict 30 line limit
11
+ * - Modified methods: lenient 80 line limit (cleanup when touched)
12
+ * - Untouched methods: no limit (legacy allowed)
13
+ *
14
+ * Usage:
15
+ * nx affected --target=validate-modified-methods --base=origin/main
16
+ *
17
+ * Escape hatch: Add webpieces-disable max-lines-new-and-modified comment with justification
18
+ */
19
+
20
+ import type { ExecutorContext } from '@nx/devkit';
21
+ import { execSync } from 'child_process';
22
+ import * as fs from 'fs';
23
+ import * as path from 'path';
24
+ import * as ts from 'typescript';
25
+
26
+ export interface ValidateModifiedMethodsOptions {
27
+ max?: number;
28
+ }
29
+
30
+ export interface ExecutorResult {
31
+ success: boolean;
32
+ }
33
+
34
+ interface MethodViolation {
35
+ file: string;
36
+ methodName: string;
37
+ line: number;
38
+ lines: number;
39
+ }
40
+
41
+ const TMP_DIR = 'tmp/webpieces';
42
+ const TMP_MD_FILE = 'webpieces.methodsize.md';
43
+
44
+ const METHODSIZE_DOC_CONTENT = `# Instructions: Method Too Long
45
+
46
+ ## Requirement
47
+
48
+ **~50% of the time**, you can stay under the \`newMethodsMaxLines\` limit from nx.json
49
+ by extracting logical units into well-named methods.
50
+
51
+ **~99% of the time**, you can stay under the \`modifiedAndNewMethodsMaxLines\` limit from nx.json.
52
+ Nearly all software can be written with methods under this size.
53
+ Take the extra time to refactor - it's worth it for long-term maintainability.
54
+
55
+ ## The "Table of Contents" Principle
56
+
57
+ Good code reads like a book's table of contents:
58
+ - Chapter titles (method names) tell you WHAT happens
59
+ - Reading chapter titles gives you the full story
60
+ - You can dive into chapters (implementations) for details
61
+
62
+ ## Why Limit Method Sizes?
63
+
64
+ Methods under reasonable limits are:
65
+ - Easy to review in a single screen
66
+ - Simple to understand without scrolling
67
+ - Quick for AI to analyze and suggest improvements
68
+ - More testable in isolation
69
+ - Self-documenting through well-named extracted methods
70
+
71
+ ## Gradual Cleanup Strategy
72
+
73
+ This codebase uses a gradual cleanup approach:
74
+ - **New methods**: Must be under \`newMethodsMaxLines\` from nx.json
75
+ - **Modified methods**: Must be under \`modifiedAndNewMethodsMaxLines\` from nx.json
76
+ - **Untouched methods**: No limit (legacy code is allowed until touched)
77
+
78
+ ## How to Refactor
79
+
80
+ Instead of:
81
+ \`\`\`typescript
82
+ async processOrder(order: Order): Promise<Result> {
83
+ // 100 lines of validation, transformation, saving, notifications...
84
+ }
85
+ \`\`\`
86
+
87
+ Write:
88
+ \`\`\`typescript
89
+ async processOrder(order: Order): Promise<Result> {
90
+ const validated = this.validateOrder(order);
91
+ const transformed = this.applyBusinessRules(validated);
92
+ const saved = await this.saveToDatabase(transformed);
93
+ await this.notifyStakeholders(saved);
94
+ return this.buildResult(saved);
95
+ }
96
+ \`\`\`
97
+
98
+ Now the main method is a "table of contents" - each line tells part of the story!
99
+
100
+ ## Patterns for Extraction
101
+
102
+ ### Pattern 1: Extract Loop Bodies
103
+ \`\`\`typescript
104
+ // BEFORE
105
+ for (const item of items) {
106
+ // 20 lines of processing
107
+ }
108
+
109
+ // AFTER
110
+ for (const item of items) {
111
+ this.processItem(item);
112
+ }
113
+ \`\`\`
114
+
115
+ ### Pattern 2: Extract Conditional Blocks
116
+ \`\`\`typescript
117
+ // BEFORE
118
+ if (isAdmin(user)) {
119
+ // 15 lines of admin logic
120
+ }
121
+
122
+ // AFTER
123
+ if (isAdmin(user)) {
124
+ this.handleAdminUser(user);
125
+ }
126
+ \`\`\`
127
+
128
+ ### Pattern 3: Extract Data Transformations
129
+ \`\`\`typescript
130
+ // BEFORE
131
+ const result = {
132
+ // 10+ lines of object construction
133
+ };
134
+
135
+ // AFTER
136
+ const result = this.buildResultObject(data);
137
+ \`\`\`
138
+
139
+ ## If Refactoring Is Not Feasible
140
+
141
+ Sometimes methods genuinely need to be longer (complex algorithms, state machines, etc.).
142
+
143
+ **Escape hatch**: Add a webpieces-disable comment with justification:
144
+
145
+ \`\`\`typescript
146
+ // webpieces-disable max-lines-new-and-modified -- Complex state machine, splitting reduces clarity
147
+ async complexStateMachine(): Promise<void> {
148
+ // ... longer method with justification
149
+ }
150
+ \`\`\`
151
+
152
+ ## AI Agent Action Steps
153
+
154
+ 1. **READ** the method to understand its logical sections
155
+ 2. **IDENTIFY** logical units that can be extracted
156
+ 3. **EXTRACT** into well-named private methods
157
+ 4. **VERIFY** the main method now reads like a table of contents
158
+ 5. **IF NOT FEASIBLE**: Add webpieces-disable max-lines-new-and-modified comment with clear justification
159
+
160
+ ## Remember
161
+
162
+ - Every method you write today will be read many times tomorrow
163
+ - The best code explains itself through structure
164
+ - When in doubt, extract and name it
165
+ `;
166
+
167
+ /**
168
+ * Write the instructions documentation to tmp directory
169
+ */
170
+ function writeTmpInstructions(workspaceRoot: string): string {
171
+ const tmpDir = path.join(workspaceRoot, TMP_DIR);
172
+ const mdPath = path.join(tmpDir, TMP_MD_FILE);
173
+
174
+ fs.mkdirSync(tmpDir, { recursive: true });
175
+ fs.writeFileSync(mdPath, METHODSIZE_DOC_CONTENT);
176
+
177
+ return mdPath;
178
+ }
179
+
180
+ /**
181
+ * Get changed TypeScript files between base and working tree.
182
+ * Uses `git diff base` (no three-dots) to match what `nx affected` does -
183
+ * this includes both committed and uncommitted changes in one diff.
184
+ */
185
+ function getChangedTypeScriptFiles(workspaceRoot: string, base: string): string[] {
186
+ try {
187
+ // Use two-dot diff (base to working tree) - same as nx affected
188
+ const output = execSync(`git diff --name-only ${base} -- '*.ts' '*.tsx'`, {
189
+ cwd: workspaceRoot,
190
+ encoding: 'utf-8',
191
+ });
192
+ return output
193
+ .trim()
194
+ .split('\n')
195
+ .filter((f) => f && !f.includes('.spec.ts') && !f.includes('.test.ts'));
196
+ } catch {
197
+ return [];
198
+ }
199
+ }
200
+
201
+ /**
202
+ * Get the diff content for a specific file between base and working tree.
203
+ * Uses `git diff base` (no three-dots) to match what `nx affected` does -
204
+ * this includes both committed and uncommitted changes in one diff.
205
+ */
206
+ function getFileDiff(workspaceRoot: string, file: string, base: string): string {
207
+ try {
208
+ // Use two-dot diff (base to working tree) - same as nx affected
209
+ return execSync(`git diff ${base} -- "${file}"`, {
210
+ cwd: workspaceRoot,
211
+ encoding: 'utf-8',
212
+ });
213
+ } catch {
214
+ return '';
215
+ }
216
+ }
217
+
218
+ /**
219
+ * Parse diff to find NEW method signatures.
220
+ * Must handle: export function, async function, const/let arrow functions, class methods
221
+ */
222
+ // webpieces-disable max-lines-new-methods -- Regex patterns require inline documentation
223
+ function findNewMethodSignaturesInDiff(diffContent: string): Set<string> {
224
+ const newMethods = new Set<string>();
225
+ const lines = diffContent.split('\n');
226
+
227
+ // Patterns to match method definitions (same as validate-new-methods)
228
+ const patterns = [
229
+ // [export] [async] function methodName( - most explicit, check first
230
+ /^\+\s*(?:export\s+)?(?:async\s+)?function\s+(\w+)\s*\(/,
231
+ // [export] const/let methodName = [async] (
232
+ /^\+\s*(?:export\s+)?(?:const|let)\s+(\w+)\s*=\s*(?:async\s*)?\(/,
233
+ // [export] const/let methodName = [async] function
234
+ /^\+\s*(?:export\s+)?(?:const|let)\s+(\w+)\s*=\s*(?:async\s+)?function/,
235
+ // class method: [async] methodName( - but NOT constructor, if, for, while, etc.
236
+ /^\+\s*(?:async\s+)?(\w+)\s*\(/,
237
+ ];
238
+
239
+ for (const line of lines) {
240
+ if (line.startsWith('+') && !line.startsWith('+++')) {
241
+ for (const pattern of patterns) {
242
+ const match = line.match(pattern);
243
+ if (match) {
244
+ // Extract method name - now always in capture group 1
245
+ const methodName = match[1];
246
+ if (methodName && !['if', 'for', 'while', 'switch', 'catch', 'constructor'].includes(methodName)) {
247
+ newMethods.add(methodName);
248
+ }
249
+ break;
250
+ }
251
+ }
252
+ }
253
+ }
254
+
255
+ return newMethods;
256
+ }
257
+
258
+ /**
259
+ * Parse diff to find line numbers that have changes in the new file
260
+ */
261
+ function getChangedLineNumbers(diffContent: string): Set<number> {
262
+ const changedLines = new Set<number>();
263
+ const lines = diffContent.split('\n');
264
+
265
+ let currentNewLine = 0;
266
+
267
+ for (const line of lines) {
268
+ // Parse hunk header: @@ -oldStart,oldCount +newStart,newCount @@
269
+ const hunkMatch = line.match(/^@@ -\d+(?:,\d+)? \+(\d+)(?:,\d+)? @@/);
270
+ if (hunkMatch) {
271
+ currentNewLine = parseInt(hunkMatch[1], 10);
272
+ continue;
273
+ }
274
+
275
+ if (currentNewLine === 0) continue;
276
+
277
+ if (line.startsWith('+') && !line.startsWith('+++')) {
278
+ // Added line
279
+ changedLines.add(currentNewLine);
280
+ currentNewLine++;
281
+ } else if (line.startsWith('-') && !line.startsWith('---')) {
282
+ // Removed line - doesn't increment new line counter
283
+ } else if (!line.startsWith('\\')) {
284
+ // Context line (unchanged)
285
+ currentNewLine++;
286
+ }
287
+ }
288
+
289
+ return changedLines;
290
+ }
291
+
292
+ /**
293
+ * Check what kind of webpieces-disable comment is present for a method.
294
+ * Returns: 'full' | 'new-only' | 'none'
295
+ * - 'full': max-lines-new-and-modified (ultimate escape, skips both validators)
296
+ * - 'new-only': max-lines-new-methods (escaped 30-line check, still needs 80-line check)
297
+ * - 'none': no escape hatch
298
+ */
299
+ function getDisableType(lines: string[], lineNumber: number): 'full' | 'new-only' | 'none' {
300
+ const startCheck = Math.max(0, lineNumber - 5);
301
+ for (let i = lineNumber - 2; i >= startCheck; i--) {
302
+ const line = lines[i]?.trim() ?? '';
303
+ if (line.startsWith('function ') || line.startsWith('class ') || line.endsWith('}')) {
304
+ break;
305
+ }
306
+ if (line.includes('webpieces-disable')) {
307
+ if (line.includes('max-lines-new-and-modified')) {
308
+ return 'full';
309
+ }
310
+ if (line.includes('max-lines-new-methods')) {
311
+ return 'new-only';
312
+ }
313
+ }
314
+ }
315
+ return 'none';
316
+ }
317
+
318
+ /**
319
+ * Parse a TypeScript file and find methods with their line counts
320
+ */
321
+ // webpieces-disable max-lines-new-methods -- AST traversal requires inline visitor function
322
+ function findMethodsInFile(
323
+ filePath: string,
324
+ workspaceRoot: string
325
+ ): Array<{ name: string; line: number; endLine: number; lines: number; disableType: 'full' | 'new-only' | 'none' }> {
326
+ const fullPath = path.join(workspaceRoot, filePath);
327
+ if (!fs.existsSync(fullPath)) return [];
328
+
329
+ const content = fs.readFileSync(fullPath, 'utf-8');
330
+ const fileLines = content.split('\n');
331
+ const sourceFile = ts.createSourceFile(filePath, content, ts.ScriptTarget.Latest, true);
332
+
333
+ const methods: Array<{ name: string; line: number; endLine: number; lines: number; disableType: 'full' | 'new-only' | 'none' }> =
334
+ [];
335
+
336
+ // webpieces-disable max-lines-new-methods -- AST visitor pattern requires handling multiple node types
337
+ function visit(node: ts.Node): void {
338
+ let methodName: string | undefined;
339
+ let startLine: number | undefined;
340
+ let endLine: number | undefined;
341
+
342
+ if (ts.isMethodDeclaration(node) && node.name) {
343
+ methodName = node.name.getText(sourceFile);
344
+ const start = sourceFile.getLineAndCharacterOfPosition(node.getStart());
345
+ const end = sourceFile.getLineAndCharacterOfPosition(node.getEnd());
346
+ startLine = start.line + 1;
347
+ endLine = end.line + 1;
348
+ } else if (ts.isFunctionDeclaration(node) && node.name) {
349
+ methodName = node.name.getText(sourceFile);
350
+ const start = sourceFile.getLineAndCharacterOfPosition(node.getStart());
351
+ const end = sourceFile.getLineAndCharacterOfPosition(node.getEnd());
352
+ startLine = start.line + 1;
353
+ endLine = end.line + 1;
354
+ } else if (ts.isArrowFunction(node)) {
355
+ if (ts.isVariableDeclaration(node.parent) && ts.isIdentifier(node.parent.name)) {
356
+ methodName = node.parent.name.getText(sourceFile);
357
+ const start = sourceFile.getLineAndCharacterOfPosition(node.getStart());
358
+ const end = sourceFile.getLineAndCharacterOfPosition(node.getEnd());
359
+ startLine = start.line + 1;
360
+ endLine = end.line + 1;
361
+ }
362
+ }
363
+
364
+ if (methodName && startLine !== undefined && endLine !== undefined) {
365
+ methods.push({
366
+ name: methodName,
367
+ line: startLine,
368
+ endLine: endLine,
369
+ lines: endLine - startLine + 1,
370
+ disableType: getDisableType(fileLines, startLine),
371
+ });
372
+ }
373
+
374
+ ts.forEachChild(node, visit);
375
+ }
376
+
377
+ visit(sourceFile);
378
+ return methods;
379
+ }
380
+
381
+ /**
382
+ * Find methods that exceed the 80-line limit.
383
+ *
384
+ * This validator checks:
385
+ * 1. NEW methods that have `max-lines-new-methods` escape (they passed 30-line check, now need 80-line check)
386
+ * 2. MODIFIED methods (existing methods with changes)
387
+ *
388
+ * Skips:
389
+ * - NEW methods without any escape (let validate-new-methods handle them first)
390
+ * - Methods with `max-lines-new-and-modified` escape (ultimate escape hatch)
391
+ */
392
+ // webpieces-disable max-lines-new-methods -- Core validation logic with multiple file operations
393
+ function findViolations(
394
+ workspaceRoot: string,
395
+ changedFiles: string[],
396
+ base: string,
397
+ maxLines: number
398
+ ): MethodViolation[] {
399
+ const violations: MethodViolation[] = [];
400
+
401
+ for (const file of changedFiles) {
402
+ const diff = getFileDiff(workspaceRoot, file, base);
403
+ if (!diff) continue;
404
+
405
+ // Find NEW methods from the diff
406
+ const newMethodNames = findNewMethodSignaturesInDiff(diff);
407
+
408
+ // Find which lines have changes
409
+ const changedLineNumbers = getChangedLineNumbers(diff);
410
+ if (changedLineNumbers.size === 0) continue;
411
+
412
+ // Parse the current file to get all methods
413
+ const methods = findMethodsInFile(file, workspaceRoot);
414
+
415
+ for (const method of methods) {
416
+ const isNewMethod = newMethodNames.has(method.name);
417
+
418
+ // Skip methods with full escape (max-lines-new-and-modified)
419
+ if (method.disableType === 'full') continue;
420
+
421
+ // Skip methods under the limit
422
+ if (method.lines <= maxLines) continue;
423
+
424
+ if (isNewMethod) {
425
+ // For NEW methods:
426
+ // - If has 'new-only' escape → check (they escaped 30-line, now need 80-line check)
427
+ // - If has 'none' → skip (let validate-new-methods handle first)
428
+ if (method.disableType !== 'new-only') continue;
429
+
430
+ // New method with max-lines-new-methods escape - check against 80-line limit
431
+ violations.push({
432
+ file,
433
+ methodName: method.name,
434
+ line: method.line,
435
+ lines: method.lines,
436
+ });
437
+ } else {
438
+ // For MODIFIED methods: check if any changed line falls within method's range
439
+ let hasChanges = false;
440
+ for (let line = method.line; line <= method.endLine; line++) {
441
+ if (changedLineNumbers.has(line)) {
442
+ hasChanges = true;
443
+ break;
444
+ }
445
+ }
446
+
447
+ if (hasChanges) {
448
+ violations.push({
449
+ file,
450
+ methodName: method.name,
451
+ line: method.line,
452
+ lines: method.lines,
453
+ });
454
+ }
455
+ }
456
+ }
457
+ }
458
+
459
+ return violations;
460
+ }
461
+
462
+ /**
463
+ * Auto-detect the base branch by finding the merge-base with origin/main.
464
+ */
465
+ function detectBase(workspaceRoot: string): string | null {
466
+ try {
467
+ const mergeBase = execSync('git merge-base HEAD origin/main', {
468
+ cwd: workspaceRoot,
469
+ encoding: 'utf-8',
470
+ stdio: ['pipe', 'pipe', 'pipe'],
471
+ }).trim();
472
+
473
+ if (mergeBase) {
474
+ return mergeBase;
475
+ }
476
+ } catch {
477
+ try {
478
+ const mergeBase = execSync('git merge-base HEAD main', {
479
+ cwd: workspaceRoot,
480
+ encoding: 'utf-8',
481
+ stdio: ['pipe', 'pipe', 'pipe'],
482
+ }).trim();
483
+
484
+ if (mergeBase) {
485
+ return mergeBase;
486
+ }
487
+ } catch {
488
+ // Ignore
489
+ }
490
+ }
491
+ return null;
492
+ }
493
+
494
+ export default async function runExecutor(
495
+ options: ValidateModifiedMethodsOptions,
496
+ context: ExecutorContext
497
+ ): Promise<ExecutorResult> {
498
+ const workspaceRoot = context.root;
499
+ const maxLines = options.max ?? 80;
500
+
501
+ let base = process.env['NX_BASE'];
502
+
503
+ if (!base) {
504
+ base = detectBase(workspaceRoot) ?? undefined;
505
+
506
+ if (!base) {
507
+ console.log('\n⏭️ Skipping modified method validation (could not detect base branch)');
508
+ console.log(' To run explicitly: nx affected --target=validate-modified-methods --base=origin/main');
509
+ console.log('');
510
+ return { success: true };
511
+ }
512
+
513
+ console.log('\n📏 Validating Modified Method Sizes (auto-detected base)\n');
514
+ } else {
515
+ console.log('\n📏 Validating Modified Method Sizes\n');
516
+ }
517
+
518
+ console.log(` Base: ${base}`);
519
+ console.log(' Comparing to: working tree (includes uncommitted changes)');
520
+ console.log(` Max lines for modified methods: ${maxLines}`);
521
+ console.log('');
522
+
523
+ try {
524
+ const changedFiles = getChangedTypeScriptFiles(workspaceRoot, base);
525
+
526
+ if (changedFiles.length === 0) {
527
+ console.log('✅ No TypeScript files changed');
528
+ return { success: true };
529
+ }
530
+
531
+ console.log(`📂 Checking ${changedFiles.length} changed file(s)...`);
532
+
533
+ const violations = findViolations(workspaceRoot, changedFiles, base, maxLines);
534
+
535
+ if (violations.length === 0) {
536
+ console.log('✅ All modified methods are under ' + maxLines + ' lines');
537
+ return { success: true };
538
+ }
539
+
540
+ // Write instructions file
541
+ writeTmpInstructions(workspaceRoot);
542
+
543
+ // Report violations
544
+ console.error('');
545
+ console.error('❌ Modified methods exceed ' + maxLines + ' lines!');
546
+ console.error('');
547
+ console.error('📚 When you modify a method, you must bring it under ' + maxLines + ' lines.');
548
+ console.error(' This encourages gradual cleanup of legacy code.');
549
+ console.error(' You can refactor to stay under the limit 50% of the time.');
550
+ console.error(' If not feasible, use the escape hatch.');
551
+ console.error('');
552
+ console.error(
553
+ '⚠️ *** READ tmp/webpieces/webpieces.methodsize.md for detailed guidance on how to fix this easily *** ⚠️'
554
+ );
555
+ console.error('');
556
+
557
+ for (const v of violations) {
558
+ console.error(` ❌ ${v.file}:${v.line}`);
559
+ console.error(` Method: ${v.methodName} (${v.lines} lines, max: ${maxLines})`);
560
+ }
561
+ console.error('');
562
+
563
+ return { success: false };
564
+ } catch (err: unknown) {
565
+ const error = err instanceof Error ? err : new Error(String(err));
566
+ console.error('❌ Modified method validation failed:', error.message);
567
+ return { success: false };
568
+ }
569
+ }
@@ -0,0 +1,14 @@
1
+ {
2
+ "$schema": "http://json-schema.org/schema",
3
+ "title": "Validate Modified Methods Executor",
4
+ "description": "Validates that modified methods don't exceed a maximum line count. Encourages gradual cleanup of legacy long methods.",
5
+ "type": "object",
6
+ "properties": {
7
+ "max": {
8
+ "type": "number",
9
+ "description": "Maximum number of lines allowed for modified methods",
10
+ "default": 80
11
+ }
12
+ },
13
+ "required": []
14
+ }
@@ -2,15 +2,18 @@
2
2
  * Validate New Methods Executor
3
3
  *
4
4
  * Validates that newly added methods don't exceed a maximum line count.
5
- * Only runs when NX_BASE environment variable is set (affected mode).
5
+ * Runs in affected mode when:
6
+ * 1. NX_BASE environment variable is set (via nx affected), OR
7
+ * 2. Auto-detects base by finding merge-base with origin/main
6
8
  *
7
9
  * This validator encourages writing methods that read like a "table of contents"
8
10
  * where each method call describes a larger piece of work.
9
11
  *
10
12
  * Usage:
11
13
  * nx affected --target=validate-new-methods --base=origin/main
14
+ * OR: runs automatically via build's architecture:validate-complete dependency
12
15
  *
13
- * Escape hatch: Add eslint-disable comment with justification
16
+ * Escape hatch: Add webpieces-disable max-lines-new-methods comment with justification
14
17
  */
15
18
  import type { ExecutorContext } from '@nx/devkit';
16
19
  export interface ValidateNewMethodsOptions {