@webpieces/dev-config 0.2.84 → 0.2.86
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/architecture/executors/diff-utils.d.ts +24 -0
- package/architecture/executors/diff-utils.js +119 -0
- package/architecture/executors/diff-utils.js.map +1 -0
- package/architecture/executors/diff-utils.ts +127 -0
- package/architecture/executors/validate-code/executor.d.ts +7 -0
- package/architecture/executors/validate-code/executor.js +57 -2
- package/architecture/executors/validate-code/executor.js.map +1 -1
- package/architecture/executors/validate-code/executor.ts +72 -2
- package/architecture/executors/validate-code/schema.json +21 -8
- package/architecture/executors/validate-no-destructure/executor.d.ts +52 -0
- package/architecture/executors/validate-no-destructure/executor.js +493 -0
- package/architecture/executors/validate-no-destructure/executor.js.map +1 -0
- package/architecture/executors/validate-no-destructure/executor.ts +580 -0
- package/architecture/executors/validate-no-destructure/schema.json +24 -0
- package/architecture/executors/validate-no-inline-types/executor.d.ts +2 -2
- package/architecture/executors/validate-no-inline-types/executor.js +3 -3
- package/architecture/executors/validate-no-inline-types/executor.js.map +1 -1
- package/architecture/executors/validate-no-inline-types/executor.ts +4 -4
- package/architecture/executors/validate-no-inline-types/schema.json +2 -2
- package/architecture/executors/validate-prisma-converters/executor.d.ts +4 -3
- package/architecture/executors/validate-prisma-converters/executor.js +122 -3
- package/architecture/executors/validate-prisma-converters/executor.js.map +1 -1
- package/architecture/executors/validate-prisma-converters/executor.ts +170 -4
- package/architecture/executors/validate-prisma-converters/schema.json +2 -2
- package/architecture/executors/validate-return-types/executor.d.ts +2 -2
- package/architecture/executors/validate-return-types/executor.js +3 -3
- package/architecture/executors/validate-return-types/executor.js.map +1 -1
- package/architecture/executors/validate-return-types/executor.ts +4 -4
- package/architecture/executors/validate-return-types/schema.json +2 -2
- package/executors.json +5 -0
- package/package.json +1 -1
|
@@ -0,0 +1,580 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validate No Destructure Executor
|
|
3
|
+
*
|
|
4
|
+
* Validates that destructuring patterns are not used in TypeScript code.
|
|
5
|
+
* Uses LINE-BASED detection (not method-based) for git diff filtering.
|
|
6
|
+
*
|
|
7
|
+
* ============================================================================
|
|
8
|
+
* VIOLATIONS (BAD) - These patterns are flagged:
|
|
9
|
+
* ============================================================================
|
|
10
|
+
*
|
|
11
|
+
* - const { x, y } = obj — object destructuring in variable declarations
|
|
12
|
+
* - const [a, b] = fn() — array destructuring (except Promise.all)
|
|
13
|
+
* - for (const { email } of items) — object destructuring in for-of loops
|
|
14
|
+
* - for (const [a, b] of items) — array destructuring in for-of (except Object.entries)
|
|
15
|
+
* - const { page = 0 } = opts — destructuring with defaults
|
|
16
|
+
* - const { done: streamDone } = obj — destructuring with renaming
|
|
17
|
+
* - function foo({ x, y }: Type) — function parameter destructuring
|
|
18
|
+
*
|
|
19
|
+
* ============================================================================
|
|
20
|
+
* ALLOWED (skip — NOT violations)
|
|
21
|
+
* ============================================================================
|
|
22
|
+
*
|
|
23
|
+
* - const [a, b] = await Promise.all([...]) — Promise.all array destructuring
|
|
24
|
+
* - for (const [key, value] of Object.entries(obj)) — Object.entries in for-of
|
|
25
|
+
* - const { extracted, ...rest } = obj — rest operator separation
|
|
26
|
+
* - Lines with // webpieces-disable no-destructure -- [reason] (only when disableAllowed: true)
|
|
27
|
+
*
|
|
28
|
+
* ============================================================================
|
|
29
|
+
* MODES (LINE-BASED)
|
|
30
|
+
* ============================================================================
|
|
31
|
+
* - OFF: Skip validation entirely
|
|
32
|
+
* - MODIFIED_CODE: Flag destructuring on changed lines (lines in diff hunks)
|
|
33
|
+
* - MODIFIED_FILES: Flag ALL destructuring in files that were modified
|
|
34
|
+
*
|
|
35
|
+
* ============================================================================
|
|
36
|
+
* ESCAPE HATCH
|
|
37
|
+
* ============================================================================
|
|
38
|
+
* Add comment above the violation:
|
|
39
|
+
* // webpieces-disable no-destructure -- [your justification]
|
|
40
|
+
* const { x, y } = obj;
|
|
41
|
+
*/
|
|
42
|
+
|
|
43
|
+
import type { ExecutorContext } from '@nx/devkit';
|
|
44
|
+
import { execSync } from 'child_process';
|
|
45
|
+
import * as fs from 'fs';
|
|
46
|
+
import * as path from 'path';
|
|
47
|
+
import * as ts from 'typescript';
|
|
48
|
+
|
|
49
|
+
export type NoDestructureMode = 'OFF' | 'MODIFIED_CODE' | 'MODIFIED_FILES';
|
|
50
|
+
|
|
51
|
+
export interface ValidateNoDestructureOptions {
|
|
52
|
+
mode?: NoDestructureMode;
|
|
53
|
+
disableAllowed?: boolean;
|
|
54
|
+
ignoreModifiedUntilEpoch?: number;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export interface ExecutorResult {
|
|
58
|
+
success: boolean;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
interface DestructureViolation {
|
|
62
|
+
file: string;
|
|
63
|
+
line: number;
|
|
64
|
+
column: number;
|
|
65
|
+
context: string;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Get changed TypeScript files between base and head (or working tree if head not specified).
|
|
70
|
+
*/
|
|
71
|
+
// webpieces-disable max-lines-new-methods -- Git command handling with untracked files requires multiple code paths
|
|
72
|
+
function getChangedTypeScriptFiles(workspaceRoot: string, base: string, head?: string): string[] {
|
|
73
|
+
try {
|
|
74
|
+
const diffTarget = head ? `${base} ${head}` : base;
|
|
75
|
+
const output = execSync(`git diff --name-only ${diffTarget} -- '*.ts' '*.tsx'`, {
|
|
76
|
+
cwd: workspaceRoot,
|
|
77
|
+
encoding: 'utf-8',
|
|
78
|
+
});
|
|
79
|
+
const changedFiles = output
|
|
80
|
+
.trim()
|
|
81
|
+
.split('\n')
|
|
82
|
+
.filter((f) => f && !f.includes('.spec.ts') && !f.includes('.test.ts'));
|
|
83
|
+
|
|
84
|
+
if (!head) {
|
|
85
|
+
try {
|
|
86
|
+
const untrackedOutput = execSync(`git ls-files --others --exclude-standard '*.ts' '*.tsx'`, {
|
|
87
|
+
cwd: workspaceRoot,
|
|
88
|
+
encoding: 'utf-8',
|
|
89
|
+
});
|
|
90
|
+
const untrackedFiles = untrackedOutput
|
|
91
|
+
.trim()
|
|
92
|
+
.split('\n')
|
|
93
|
+
.filter((f) => f && !f.includes('.spec.ts') && !f.includes('.test.ts'));
|
|
94
|
+
const allFiles = new Set([...changedFiles, ...untrackedFiles]);
|
|
95
|
+
return Array.from(allFiles);
|
|
96
|
+
} catch {
|
|
97
|
+
return changedFiles;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return changedFiles;
|
|
102
|
+
} catch {
|
|
103
|
+
return [];
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Get the diff content for a specific file.
|
|
109
|
+
*/
|
|
110
|
+
function getFileDiff(workspaceRoot: string, file: string, base: string, head?: string): string {
|
|
111
|
+
try {
|
|
112
|
+
const diffTarget = head ? `${base} ${head}` : base;
|
|
113
|
+
const diff = execSync(`git diff ${diffTarget} -- "${file}"`, {
|
|
114
|
+
cwd: workspaceRoot,
|
|
115
|
+
encoding: 'utf-8',
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
if (!diff && !head) {
|
|
119
|
+
const fullPath = path.join(workspaceRoot, file);
|
|
120
|
+
if (fs.existsSync(fullPath)) {
|
|
121
|
+
const isUntracked = execSync(`git ls-files --others --exclude-standard "${file}"`, {
|
|
122
|
+
cwd: workspaceRoot,
|
|
123
|
+
encoding: 'utf-8',
|
|
124
|
+
}).trim();
|
|
125
|
+
|
|
126
|
+
if (isUntracked) {
|
|
127
|
+
const content = fs.readFileSync(fullPath, 'utf-8');
|
|
128
|
+
const lines = content.split('\n');
|
|
129
|
+
return lines.map((line) => `+${line}`).join('\n');
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return diff;
|
|
135
|
+
} catch {
|
|
136
|
+
return '';
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Parse diff to extract changed line numbers (additions only - lines starting with +).
|
|
142
|
+
*/
|
|
143
|
+
function getChangedLineNumbers(diffContent: string): Set<number> {
|
|
144
|
+
const changedLines = new Set<number>();
|
|
145
|
+
const lines = diffContent.split('\n');
|
|
146
|
+
let currentLine = 0;
|
|
147
|
+
|
|
148
|
+
for (const line of lines) {
|
|
149
|
+
const hunkMatch = line.match(/^@@ -\d+(?:,\d+)? \+(\d+)(?:,\d+)? @@/);
|
|
150
|
+
if (hunkMatch) {
|
|
151
|
+
currentLine = parseInt(hunkMatch[1], 10);
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
if (line.startsWith('+') && !line.startsWith('+++')) {
|
|
156
|
+
changedLines.add(currentLine);
|
|
157
|
+
currentLine++;
|
|
158
|
+
} else if (line.startsWith('-') && !line.startsWith('---')) {
|
|
159
|
+
// Deletions don't increment line number
|
|
160
|
+
} else {
|
|
161
|
+
currentLine++;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
return changedLines;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Check if a line contains a webpieces-disable comment for no-destructure.
|
|
170
|
+
*/
|
|
171
|
+
function hasDisableComment(lines: string[], lineNumber: number): boolean {
|
|
172
|
+
const startCheck = Math.max(0, lineNumber - 5);
|
|
173
|
+
for (let i = lineNumber - 2; i >= startCheck; i--) {
|
|
174
|
+
const line = lines[i]?.trim() ?? '';
|
|
175
|
+
if (line.startsWith('function ') || line.startsWith('class ') || line.endsWith('}')) {
|
|
176
|
+
break;
|
|
177
|
+
}
|
|
178
|
+
if (line.includes('webpieces-disable') && line.includes('no-destructure')) {
|
|
179
|
+
return true;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
return false;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Check if an ArrayBindingPattern's initializer is `await Promise.all(...)`.
|
|
187
|
+
*/
|
|
188
|
+
function isPromiseAllDestructure(node: ts.ArrayBindingPattern): boolean {
|
|
189
|
+
const parent = node.parent;
|
|
190
|
+
if (!ts.isVariableDeclaration(parent)) return false;
|
|
191
|
+
const initializer = parent.initializer;
|
|
192
|
+
if (!initializer) return false;
|
|
193
|
+
|
|
194
|
+
// Handle: const [a, b] = await Promise.all([...])
|
|
195
|
+
if (ts.isAwaitExpression(initializer)) {
|
|
196
|
+
const awaitedExpr = initializer.expression;
|
|
197
|
+
if (ts.isCallExpression(awaitedExpr)) {
|
|
198
|
+
const callExpr = awaitedExpr.expression;
|
|
199
|
+
// Promise.all(...)
|
|
200
|
+
if (ts.isPropertyAccessExpression(callExpr) && callExpr.name.text === 'all') {
|
|
201
|
+
const obj = callExpr.expression;
|
|
202
|
+
if (ts.isIdentifier(obj) && obj.text === 'Promise') {
|
|
203
|
+
return true;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
return false;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Check if an ArrayBindingPattern in a for-of loop iterates over Object.entries(...).
|
|
214
|
+
*/
|
|
215
|
+
function isObjectEntriesForOf(node: ts.ArrayBindingPattern): boolean {
|
|
216
|
+
// Walk up: ArrayBindingPattern -> VariableDeclaration -> VariableDeclarationList -> ForOfStatement
|
|
217
|
+
const varDecl = node.parent;
|
|
218
|
+
if (!ts.isVariableDeclaration(varDecl)) return false;
|
|
219
|
+
|
|
220
|
+
const varDeclList = varDecl.parent;
|
|
221
|
+
if (!ts.isVariableDeclarationList(varDeclList)) return false;
|
|
222
|
+
|
|
223
|
+
const forOfStmt = varDeclList.parent;
|
|
224
|
+
if (!ts.isForOfStatement(forOfStmt)) return false;
|
|
225
|
+
|
|
226
|
+
// Check iterable expression ends with .entries()
|
|
227
|
+
const iterable = forOfStmt.expression;
|
|
228
|
+
if (ts.isCallExpression(iterable)) {
|
|
229
|
+
const callExpr = iterable.expression;
|
|
230
|
+
if (ts.isPropertyAccessExpression(callExpr) && callExpr.name.text === 'entries') {
|
|
231
|
+
return true;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
return false;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Check if an ObjectBindingPattern contains a rest element (...rest).
|
|
240
|
+
*/
|
|
241
|
+
function hasRestElement(node: ts.ObjectBindingPattern): boolean {
|
|
242
|
+
for (const element of node.elements) {
|
|
243
|
+
if (element.dotDotDotToken) {
|
|
244
|
+
return true;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
return false;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
interface DestructureInfo {
|
|
251
|
+
line: number;
|
|
252
|
+
column: number;
|
|
253
|
+
context: string;
|
|
254
|
+
hasDisableComment: boolean;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* Find all destructuring patterns in a file using AST.
|
|
259
|
+
*/
|
|
260
|
+
// webpieces-disable max-lines-new-methods -- AST traversal with multiple destructuring pattern checks and exception detection
|
|
261
|
+
function findDestructuringInFile(filePath: string, workspaceRoot: string, disableAllowed: boolean): DestructureInfo[] {
|
|
262
|
+
const fullPath = path.join(workspaceRoot, filePath);
|
|
263
|
+
if (!fs.existsSync(fullPath)) return [];
|
|
264
|
+
|
|
265
|
+
const content = fs.readFileSync(fullPath, 'utf-8');
|
|
266
|
+
const fileLines = content.split('\n');
|
|
267
|
+
const sourceFile = ts.createSourceFile(filePath, content, ts.ScriptTarget.Latest, true);
|
|
268
|
+
|
|
269
|
+
const violations: DestructureInfo[] = [];
|
|
270
|
+
|
|
271
|
+
// webpieces-disable max-lines-new-methods -- AST visitor needs to handle object/array binding patterns in declarations, for-of, and parameters
|
|
272
|
+
function visit(node: ts.Node): void {
|
|
273
|
+
try {
|
|
274
|
+
// Check ObjectBindingPattern
|
|
275
|
+
if (ts.isObjectBindingPattern(node)) {
|
|
276
|
+
// Exception: rest operator separation
|
|
277
|
+
if (hasRestElement(node)) {
|
|
278
|
+
ts.forEachChild(node, visit);
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
const context = getDestructureContext(node);
|
|
283
|
+
recordViolation(node, context, fileLines, sourceFile, violations, disableAllowed);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
// Check ArrayBindingPattern
|
|
287
|
+
if (ts.isArrayBindingPattern(node)) {
|
|
288
|
+
// Exception: Promise.all destructure
|
|
289
|
+
if (isPromiseAllDestructure(node)) {
|
|
290
|
+
ts.forEachChild(node, visit);
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
// Exception: Object.entries in for-of
|
|
295
|
+
if (isObjectEntriesForOf(node)) {
|
|
296
|
+
ts.forEachChild(node, visit);
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
const context = getDestructureContext(node);
|
|
301
|
+
recordViolation(node, context, fileLines, sourceFile, violations, disableAllowed);
|
|
302
|
+
}
|
|
303
|
+
} catch {
|
|
304
|
+
// Skip nodes that cause errors during analysis
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
ts.forEachChild(node, visit);
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
visit(sourceFile);
|
|
311
|
+
return violations;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
function recordViolation(
|
|
315
|
+
node: ts.Node,
|
|
316
|
+
context: string,
|
|
317
|
+
fileLines: string[],
|
|
318
|
+
sourceFile: ts.SourceFile,
|
|
319
|
+
violations: DestructureInfo[],
|
|
320
|
+
disableAllowed: boolean,
|
|
321
|
+
): void {
|
|
322
|
+
const startPos = node.getStart(sourceFile);
|
|
323
|
+
if (startPos >= 0) {
|
|
324
|
+
const pos = sourceFile.getLineAndCharacterOfPosition(startPos);
|
|
325
|
+
const line = pos.line + 1;
|
|
326
|
+
const column = pos.character + 1;
|
|
327
|
+
const disabled = hasDisableComment(fileLines, line);
|
|
328
|
+
|
|
329
|
+
if (!disableAllowed && disabled) {
|
|
330
|
+
// When disableAllowed is false, ignore disable comments — still a violation
|
|
331
|
+
violations.push({ line, column, context, hasDisableComment: false });
|
|
332
|
+
} else {
|
|
333
|
+
violations.push({ line, column, context, hasDisableComment: disabled });
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* Get a description of where the destructuring pattern appears.
|
|
340
|
+
*/
|
|
341
|
+
function getDestructureContext(node: ts.Node): string {
|
|
342
|
+
const parent = node.parent;
|
|
343
|
+
if (ts.isParameter(parent)) {
|
|
344
|
+
return 'function parameter destructuring';
|
|
345
|
+
}
|
|
346
|
+
if (ts.isVariableDeclaration(parent)) {
|
|
347
|
+
const grandparent = parent.parent;
|
|
348
|
+
if (grandparent && ts.isVariableDeclarationList(grandparent)) {
|
|
349
|
+
const forOfParent = grandparent.parent;
|
|
350
|
+
if (forOfParent && ts.isForOfStatement(forOfParent)) {
|
|
351
|
+
return ts.isObjectBindingPattern(node)
|
|
352
|
+
? 'object destructuring in for-of loop'
|
|
353
|
+
: 'array destructuring in for-of loop';
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
return ts.isObjectBindingPattern(node)
|
|
357
|
+
? 'object destructuring in variable declaration'
|
|
358
|
+
: 'array destructuring in variable declaration';
|
|
359
|
+
}
|
|
360
|
+
return ts.isObjectBindingPattern(node)
|
|
361
|
+
? 'object destructuring'
|
|
362
|
+
: 'array destructuring';
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
/**
|
|
366
|
+
* MODIFIED_CODE mode: Flag violations on changed lines in diff hunks.
|
|
367
|
+
*/
|
|
368
|
+
// webpieces-disable max-lines-new-methods -- File iteration with diff parsing and line filtering
|
|
369
|
+
function findViolationsForModifiedCode(
|
|
370
|
+
workspaceRoot: string,
|
|
371
|
+
changedFiles: string[],
|
|
372
|
+
base: string,
|
|
373
|
+
head: string | undefined,
|
|
374
|
+
disableAllowed: boolean
|
|
375
|
+
): DestructureViolation[] {
|
|
376
|
+
const violations: DestructureViolation[] = [];
|
|
377
|
+
|
|
378
|
+
for (const file of changedFiles) {
|
|
379
|
+
const diff = getFileDiff(workspaceRoot, file, base, head);
|
|
380
|
+
const changedLines = getChangedLineNumbers(diff);
|
|
381
|
+
|
|
382
|
+
if (changedLines.size === 0) continue;
|
|
383
|
+
|
|
384
|
+
const allViolations = findDestructuringInFile(file, workspaceRoot, disableAllowed);
|
|
385
|
+
|
|
386
|
+
for (const v of allViolations) {
|
|
387
|
+
if (disableAllowed && v.hasDisableComment) continue;
|
|
388
|
+
// LINE-BASED: Only include if the violation is on a changed line
|
|
389
|
+
if (!changedLines.has(v.line)) continue;
|
|
390
|
+
|
|
391
|
+
violations.push({
|
|
392
|
+
file,
|
|
393
|
+
line: v.line,
|
|
394
|
+
column: v.column,
|
|
395
|
+
context: v.context,
|
|
396
|
+
});
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
return violations;
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
/**
|
|
404
|
+
* MODIFIED_FILES mode: Flag ALL violations in files that were modified.
|
|
405
|
+
*/
|
|
406
|
+
function findViolationsForModifiedFiles(workspaceRoot: string, changedFiles: string[], disableAllowed: boolean): DestructureViolation[] {
|
|
407
|
+
const violations: DestructureViolation[] = [];
|
|
408
|
+
|
|
409
|
+
for (const file of changedFiles) {
|
|
410
|
+
const allViolations = findDestructuringInFile(file, workspaceRoot, disableAllowed);
|
|
411
|
+
|
|
412
|
+
for (const v of allViolations) {
|
|
413
|
+
if (disableAllowed && v.hasDisableComment) continue;
|
|
414
|
+
|
|
415
|
+
violations.push({
|
|
416
|
+
file,
|
|
417
|
+
line: v.line,
|
|
418
|
+
column: v.column,
|
|
419
|
+
context: v.context,
|
|
420
|
+
});
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
return violations;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
/**
|
|
428
|
+
* Auto-detect the base branch by finding the merge-base with origin/main.
|
|
429
|
+
*/
|
|
430
|
+
function detectBase(workspaceRoot: string): string | null {
|
|
431
|
+
try {
|
|
432
|
+
const mergeBase = execSync('git merge-base HEAD origin/main', {
|
|
433
|
+
cwd: workspaceRoot,
|
|
434
|
+
encoding: 'utf-8',
|
|
435
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
436
|
+
}).trim();
|
|
437
|
+
|
|
438
|
+
if (mergeBase) {
|
|
439
|
+
return mergeBase;
|
|
440
|
+
}
|
|
441
|
+
} catch {
|
|
442
|
+
try {
|
|
443
|
+
const mergeBase = execSync('git merge-base HEAD main', {
|
|
444
|
+
cwd: workspaceRoot,
|
|
445
|
+
encoding: 'utf-8',
|
|
446
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
447
|
+
}).trim();
|
|
448
|
+
|
|
449
|
+
if (mergeBase) {
|
|
450
|
+
return mergeBase;
|
|
451
|
+
}
|
|
452
|
+
} catch {
|
|
453
|
+
// Ignore
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
return null;
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
/**
|
|
460
|
+
* Report violations to console.
|
|
461
|
+
*/
|
|
462
|
+
// webpieces-disable max-lines-new-methods -- Console output with examples and escape hatch information
|
|
463
|
+
function reportViolations(violations: DestructureViolation[], mode: NoDestructureMode, disableAllowed: boolean): void {
|
|
464
|
+
console.error('');
|
|
465
|
+
console.error('\u274c Destructuring patterns found! Use explicit property access instead.');
|
|
466
|
+
console.error('');
|
|
467
|
+
console.error('\ud83d\udcda Avoiding destructuring improves code traceability:');
|
|
468
|
+
console.error('');
|
|
469
|
+
console.error(' BAD: const { name, age } = user;');
|
|
470
|
+
console.error(' GOOD: const name = user.name;');
|
|
471
|
+
console.error(' const age = user.age;');
|
|
472
|
+
console.error('');
|
|
473
|
+
console.error(' BAD: function process({ x, y }: Point) { }');
|
|
474
|
+
console.error(' GOOD: function process(point: Point) { point.x; point.y; }');
|
|
475
|
+
console.error('');
|
|
476
|
+
|
|
477
|
+
for (const v of violations) {
|
|
478
|
+
console.error(` \u274c ${v.file}:${v.line}:${v.column}`);
|
|
479
|
+
console.error(` ${v.context}`);
|
|
480
|
+
}
|
|
481
|
+
console.error('');
|
|
482
|
+
|
|
483
|
+
console.error(' Allowed exceptions:');
|
|
484
|
+
console.error(' - const [a, b] = await Promise.all([...])');
|
|
485
|
+
console.error(' - for (const [key, value] of Object.entries(obj))');
|
|
486
|
+
console.error(' - const { extracted, ...rest } = obj (rest operator separation)');
|
|
487
|
+
console.error('');
|
|
488
|
+
|
|
489
|
+
if (disableAllowed) {
|
|
490
|
+
console.error(' Escape hatch (use sparingly):');
|
|
491
|
+
console.error(' // webpieces-disable no-destructure -- [your reason]');
|
|
492
|
+
} else {
|
|
493
|
+
console.error(' Escape hatch: DISABLED (disableAllowed: false)');
|
|
494
|
+
console.error(' Disable comments are ignored. Fix the destructuring directly.');
|
|
495
|
+
}
|
|
496
|
+
console.error('');
|
|
497
|
+
console.error(` Current mode: ${mode}`);
|
|
498
|
+
console.error('');
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
/**
|
|
502
|
+
* Resolve mode considering ignoreModifiedUntilEpoch override.
|
|
503
|
+
* When active, downgrades to OFF. When expired, logs a warning.
|
|
504
|
+
*/
|
|
505
|
+
function resolveNoDestructureMode(normalMode: NoDestructureMode, epoch: number | undefined): NoDestructureMode {
|
|
506
|
+
if (epoch === undefined || normalMode === 'OFF') {
|
|
507
|
+
return normalMode;
|
|
508
|
+
}
|
|
509
|
+
const nowSeconds = Date.now() / 1000;
|
|
510
|
+
if (nowSeconds < epoch) {
|
|
511
|
+
const expiresDate = new Date(epoch * 1000).toISOString().split('T')[0];
|
|
512
|
+
console.log(`\n\u23ed\ufe0f Skipping no-destructure validation (ignoreModifiedUntilEpoch active, expires: ${expiresDate})`);
|
|
513
|
+
console.log('');
|
|
514
|
+
return 'OFF';
|
|
515
|
+
}
|
|
516
|
+
const expiresDate = new Date(epoch * 1000).toISOString().split('T')[0];
|
|
517
|
+
console.log(`\n\u26a0\ufe0f noDestructure.ignoreModifiedUntilEpoch (${epoch}) has expired (${expiresDate}). Remove it from nx.json. Using normal mode: ${normalMode}\n`);
|
|
518
|
+
return normalMode;
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
export default async function runExecutor(
|
|
522
|
+
options: ValidateNoDestructureOptions,
|
|
523
|
+
context: ExecutorContext
|
|
524
|
+
): Promise<ExecutorResult> {
|
|
525
|
+
const workspaceRoot = context.root;
|
|
526
|
+
const mode: NoDestructureMode = resolveNoDestructureMode(options.mode ?? 'OFF', options.ignoreModifiedUntilEpoch);
|
|
527
|
+
const disableAllowed = options.disableAllowed ?? true;
|
|
528
|
+
|
|
529
|
+
if (mode === 'OFF') {
|
|
530
|
+
console.log('\n\u23ed\ufe0f Skipping no-destructure validation (mode: OFF)');
|
|
531
|
+
console.log('');
|
|
532
|
+
return { success: true };
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
console.log('\n\ud83d\udccf Validating No Destructuring\n');
|
|
536
|
+
console.log(` Mode: ${mode}`);
|
|
537
|
+
|
|
538
|
+
let base = process.env['NX_BASE'];
|
|
539
|
+
const head = process.env['NX_HEAD'];
|
|
540
|
+
|
|
541
|
+
if (!base) {
|
|
542
|
+
base = detectBase(workspaceRoot) ?? undefined;
|
|
543
|
+
|
|
544
|
+
if (!base) {
|
|
545
|
+
console.log('\n\u23ed\ufe0f Skipping no-destructure validation (could not detect base branch)');
|
|
546
|
+
console.log('');
|
|
547
|
+
return { success: true };
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
console.log(` Base: ${base}`);
|
|
552
|
+
console.log(` Head: ${head ?? 'working tree (includes uncommitted changes)'}`);
|
|
553
|
+
console.log('');
|
|
554
|
+
|
|
555
|
+
const changedFiles = getChangedTypeScriptFiles(workspaceRoot, base, head);
|
|
556
|
+
|
|
557
|
+
if (changedFiles.length === 0) {
|
|
558
|
+
console.log('\u2705 No TypeScript files changed');
|
|
559
|
+
return { success: true };
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
console.log(`\ud83d\udcc2 Checking ${changedFiles.length} changed file(s)...`);
|
|
563
|
+
|
|
564
|
+
let violations: DestructureViolation[] = [];
|
|
565
|
+
|
|
566
|
+
if (mode === 'MODIFIED_CODE') {
|
|
567
|
+
violations = findViolationsForModifiedCode(workspaceRoot, changedFiles, base, head, disableAllowed);
|
|
568
|
+
} else if (mode === 'MODIFIED_FILES') {
|
|
569
|
+
violations = findViolationsForModifiedFiles(workspaceRoot, changedFiles, disableAllowed);
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
if (violations.length === 0) {
|
|
573
|
+
console.log('\u2705 No destructuring patterns found');
|
|
574
|
+
return { success: true };
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
reportViolations(violations, mode, disableAllowed);
|
|
578
|
+
|
|
579
|
+
return { success: false };
|
|
580
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "http://json-schema.org/schema",
|
|
3
|
+
"title": "Validate No Destructure Executor",
|
|
4
|
+
"description": "Validates that destructuring patterns are not used. Uses LINE-BASED detection for git diff filtering.",
|
|
5
|
+
"type": "object",
|
|
6
|
+
"properties": {
|
|
7
|
+
"mode": {
|
|
8
|
+
"type": "string",
|
|
9
|
+
"enum": ["OFF", "MODIFIED_CODE", "MODIFIED_FILES"],
|
|
10
|
+
"description": "OFF: skip validation. MODIFIED_CODE: only changed lines in diff. MODIFIED_FILES: all in modified files. Disallows destructuring patterns.",
|
|
11
|
+
"default": "OFF"
|
|
12
|
+
},
|
|
13
|
+
"disableAllowed": {
|
|
14
|
+
"type": "boolean",
|
|
15
|
+
"description": "Whether disable comments work. When false, no escape hatch.",
|
|
16
|
+
"default": true
|
|
17
|
+
},
|
|
18
|
+
"ignoreModifiedUntilEpoch": {
|
|
19
|
+
"type": "number",
|
|
20
|
+
"description": "Epoch seconds. Until this time, skip validation entirely. When expired, normal mode resumes. Omit when not needed."
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"required": []
|
|
24
|
+
}
|
|
@@ -63,7 +63,7 @@
|
|
|
63
63
|
* ============================================================================
|
|
64
64
|
* - OFF: Skip validation entirely
|
|
65
65
|
* - NEW_METHODS: Only validate in new methods (detected via git diff)
|
|
66
|
-
* -
|
|
66
|
+
* - NEW_AND_MODIFIED_METHODS: Validate in new methods + methods with changes
|
|
67
67
|
* - MODIFIED_FILES: Validate all violations in modified files
|
|
68
68
|
*
|
|
69
69
|
* ============================================================================
|
|
@@ -79,7 +79,7 @@
|
|
|
79
79
|
* - Legacy code being incrementally migrated
|
|
80
80
|
*/
|
|
81
81
|
import type { ExecutorContext } from '@nx/devkit';
|
|
82
|
-
export type NoInlineTypesMode = 'OFF' | 'NEW_METHODS' | '
|
|
82
|
+
export type NoInlineTypesMode = 'OFF' | 'NEW_METHODS' | 'NEW_AND_MODIFIED_METHODS' | 'MODIFIED_FILES';
|
|
83
83
|
export interface ValidateNoInlineTypesOptions {
|
|
84
84
|
mode?: NoInlineTypesMode;
|
|
85
85
|
disableAllowed?: boolean;
|
|
@@ -64,7 +64,7 @@
|
|
|
64
64
|
* ============================================================================
|
|
65
65
|
* - OFF: Skip validation entirely
|
|
66
66
|
* - NEW_METHODS: Only validate in new methods (detected via git diff)
|
|
67
|
-
* -
|
|
67
|
+
* - NEW_AND_MODIFIED_METHODS: Validate in new methods + methods with changes
|
|
68
68
|
* - MODIFIED_FILES: Validate all violations in modified files
|
|
69
69
|
*
|
|
70
70
|
* ============================================================================
|
|
@@ -495,7 +495,7 @@ function findViolationsForNewMethods(workspaceRoot, changedFiles, base, head, di
|
|
|
495
495
|
return violations;
|
|
496
496
|
}
|
|
497
497
|
/**
|
|
498
|
-
* Find violations in new and modified methods (
|
|
498
|
+
* Find violations in new and modified methods (NEW_AND_MODIFIED_METHODS mode).
|
|
499
499
|
*/
|
|
500
500
|
// webpieces-disable max-lines-new-methods -- Combines new method detection with change detection
|
|
501
501
|
function findViolationsForModifiedAndNewMethods(workspaceRoot, changedFiles, base, head, disableAllowed) {
|
|
@@ -655,7 +655,7 @@ async function runExecutor(options, context) {
|
|
|
655
655
|
if (mode === 'NEW_METHODS') {
|
|
656
656
|
violations = findViolationsForNewMethods(workspaceRoot, changedFiles, base, head, disableAllowed);
|
|
657
657
|
}
|
|
658
|
-
else if (mode === '
|
|
658
|
+
else if (mode === 'NEW_AND_MODIFIED_METHODS') {
|
|
659
659
|
violations = findViolationsForModifiedAndNewMethods(workspaceRoot, changedFiles, base, head, disableAllowed);
|
|
660
660
|
}
|
|
661
661
|
else if (mode === 'MODIFIED_FILES') {
|