@webpieces/code-rules 0.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/LICENSE +373 -0
- package/jest.config.ts +20 -0
- package/package.json +23 -0
- package/project.json +22 -0
- package/src/cli.ts +17 -0
- package/src/diff-utils.ts +129 -0
- package/src/from-shared-config.ts +118 -0
- package/src/index.ts +14 -0
- package/src/validate-catch-error-pattern.ts +639 -0
- package/src/validate-code.ts +491 -0
- package/src/validate-dtos.ts +697 -0
- package/src/validate-modified-files.ts +579 -0
- package/src/validate-modified-methods.ts +812 -0
- package/src/validate-new-methods.ts +594 -0
- package/src/validate-no-any-unknown.ts +552 -0
- package/src/validate-no-destructure.ts +588 -0
- package/src/validate-no-direct-api-resolver.ts +676 -0
- package/src/validate-no-implicit-any.ts +378 -0
- package/src/validate-no-inline-types.ts +787 -0
- package/src/validate-no-unmanaged-exceptions.ts +431 -0
- package/src/validate-prisma-converters.ts +830 -0
- package/src/validate-return-types.ts +532 -0
- package/tsconfig.json +22 -0
- package/tsconfig.lib.json +10 -0
- package/tsconfig.spec.json +14 -0
|
@@ -0,0 +1,552 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validate No Any Unknown Executor
|
|
3
|
+
*
|
|
4
|
+
* Validates that `any` and `unknown` TypeScript keywords are not used.
|
|
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: any = ...
|
|
12
|
+
* - function foo(arg: any): any { }
|
|
13
|
+
* - const data = response as any;
|
|
14
|
+
* - type T = any;
|
|
15
|
+
* - const x: unknown = ...
|
|
16
|
+
* - function foo(arg: unknown): unknown { }
|
|
17
|
+
*
|
|
18
|
+
* ============================================================================
|
|
19
|
+
* MODES (LINE-BASED)
|
|
20
|
+
* ============================================================================
|
|
21
|
+
* - OFF: Skip validation entirely
|
|
22
|
+
* - MODIFIED_CODE: Flag any/unknown on changed lines (lines in diff hunks)
|
|
23
|
+
* - MODIFIED_FILES: Flag ALL any/unknown in files that were modified
|
|
24
|
+
*
|
|
25
|
+
* ============================================================================
|
|
26
|
+
* ESCAPE HATCH
|
|
27
|
+
* ============================================================================
|
|
28
|
+
* Add comment above the violation:
|
|
29
|
+
* // webpieces-disable no-any-unknown -- [your justification]
|
|
30
|
+
* const x: any = ...;
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
import { execSync } from 'child_process';
|
|
34
|
+
import * as fs from 'fs';
|
|
35
|
+
import * as path from 'path';
|
|
36
|
+
import * as ts from 'typescript';
|
|
37
|
+
|
|
38
|
+
export type NoAnyUnknownMode = 'OFF' | 'MODIFIED_CODE' | 'MODIFIED_FILES';
|
|
39
|
+
|
|
40
|
+
export interface ValidateNoAnyUnknownOptions {
|
|
41
|
+
mode?: NoAnyUnknownMode;
|
|
42
|
+
disableAllowed?: boolean;
|
|
43
|
+
ignoreModifiedUntilEpoch?: number;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface ExecutorResult {
|
|
47
|
+
success: boolean;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
interface AnyUnknownViolation {
|
|
51
|
+
file: string;
|
|
52
|
+
line: number;
|
|
53
|
+
column: number;
|
|
54
|
+
keyword: 'any' | 'unknown';
|
|
55
|
+
context: string;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Get changed TypeScript files between base and head (or working tree if head not specified).
|
|
60
|
+
*/
|
|
61
|
+
// webpieces-disable max-lines-new-methods -- Git command handling with untracked files requires multiple code paths
|
|
62
|
+
function getChangedTypeScriptFiles(workspaceRoot: string, base: string, head?: string): string[] {
|
|
63
|
+
// eslint-disable-next-line @webpieces/no-unmanaged-exceptions
|
|
64
|
+
try {
|
|
65
|
+
const diffTarget = head ? `${base} ${head}` : base;
|
|
66
|
+
const output = execSync(`git diff --name-only ${diffTarget} -- '*.ts' '*.tsx'`, {
|
|
67
|
+
cwd: workspaceRoot,
|
|
68
|
+
encoding: 'utf-8',
|
|
69
|
+
});
|
|
70
|
+
const changedFiles = output
|
|
71
|
+
.trim()
|
|
72
|
+
.split('\n')
|
|
73
|
+
.filter((f) => f && !f.includes('.spec.ts') && !f.includes('.test.ts'));
|
|
74
|
+
|
|
75
|
+
if (!head) {
|
|
76
|
+
// eslint-disable-next-line @webpieces/no-unmanaged-exceptions
|
|
77
|
+
try {
|
|
78
|
+
const untrackedOutput = execSync(`git ls-files --others --exclude-standard '*.ts' '*.tsx'`, {
|
|
79
|
+
cwd: workspaceRoot,
|
|
80
|
+
encoding: 'utf-8',
|
|
81
|
+
});
|
|
82
|
+
const untrackedFiles = untrackedOutput
|
|
83
|
+
.trim()
|
|
84
|
+
.split('\n')
|
|
85
|
+
.filter((f) => f && !f.includes('.spec.ts') && !f.includes('.test.ts'));
|
|
86
|
+
const allFiles = new Set([...changedFiles, ...untrackedFiles]);
|
|
87
|
+
return Array.from(allFiles);
|
|
88
|
+
} catch (err: unknown) {
|
|
89
|
+
//const error = toError(err);
|
|
90
|
+
return changedFiles;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return changedFiles;
|
|
95
|
+
} catch (err: unknown) {
|
|
96
|
+
//const error = toError(err);
|
|
97
|
+
return [];
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Get the diff content for a specific file.
|
|
103
|
+
*/
|
|
104
|
+
function getFileDiff(workspaceRoot: string, file: string, base: string, head?: string): string {
|
|
105
|
+
// eslint-disable-next-line @webpieces/no-unmanaged-exceptions
|
|
106
|
+
try {
|
|
107
|
+
const diffTarget = head ? `${base} ${head}` : base;
|
|
108
|
+
const diff = execSync(`git diff ${diffTarget} -- "${file}"`, {
|
|
109
|
+
cwd: workspaceRoot,
|
|
110
|
+
encoding: 'utf-8',
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
if (!diff && !head) {
|
|
114
|
+
const fullPath = path.join(workspaceRoot, file);
|
|
115
|
+
if (fs.existsSync(fullPath)) {
|
|
116
|
+
const isUntracked = execSync(`git ls-files --others --exclude-standard "${file}"`, {
|
|
117
|
+
cwd: workspaceRoot,
|
|
118
|
+
encoding: 'utf-8',
|
|
119
|
+
}).trim();
|
|
120
|
+
|
|
121
|
+
if (isUntracked) {
|
|
122
|
+
const content = fs.readFileSync(fullPath, 'utf-8');
|
|
123
|
+
const lines = content.split('\n');
|
|
124
|
+
return lines.map((line) => `+${line}`).join('\n');
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return diff;
|
|
130
|
+
} catch (err: unknown) {
|
|
131
|
+
//const error = toError(err);
|
|
132
|
+
return '';
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Parse diff to extract changed line numbers (additions only - lines starting with +).
|
|
138
|
+
*/
|
|
139
|
+
function getChangedLineNumbers(diffContent: string): Set<number> {
|
|
140
|
+
const changedLines = new Set<number>();
|
|
141
|
+
const lines = diffContent.split('\n');
|
|
142
|
+
let currentLine = 0;
|
|
143
|
+
|
|
144
|
+
for (const line of lines) {
|
|
145
|
+
const hunkMatch = line.match(/^@@ -\d+(?:,\d+)? \+(\d+)(?:,\d+)? @@/);
|
|
146
|
+
if (hunkMatch) {
|
|
147
|
+
currentLine = parseInt(hunkMatch[1], 10);
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if (line.startsWith('+') && !line.startsWith('+++')) {
|
|
152
|
+
changedLines.add(currentLine);
|
|
153
|
+
currentLine++;
|
|
154
|
+
} else if (line.startsWith('-') && !line.startsWith('---')) {
|
|
155
|
+
// Deletions don't increment line number
|
|
156
|
+
} else {
|
|
157
|
+
currentLine++;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
return changedLines;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Check if a line contains a webpieces-disable comment for no-any-unknown.
|
|
166
|
+
*/
|
|
167
|
+
function hasDisableComment(lines: string[], lineNumber: number): boolean {
|
|
168
|
+
const startCheck = Math.max(0, lineNumber - 5);
|
|
169
|
+
for (let i = lineNumber - 2; i >= startCheck; i--) {
|
|
170
|
+
const line = lines[i]?.trim() ?? '';
|
|
171
|
+
if (line.startsWith('function ') || line.startsWith('class ') || line.endsWith('}')) {
|
|
172
|
+
break;
|
|
173
|
+
}
|
|
174
|
+
if (line.includes('webpieces-disable') && line.includes('no-any-unknown')) {
|
|
175
|
+
return true;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
return false;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Get a description of the context where the any/unknown keyword appears.
|
|
183
|
+
*/
|
|
184
|
+
// webpieces-disable max-lines-new-methods -- Context detection requires checking many AST node types
|
|
185
|
+
function getViolationContext(node: ts.Node, sourceFile: ts.SourceFile): string {
|
|
186
|
+
// eslint-disable-next-line @webpieces/no-unmanaged-exceptions
|
|
187
|
+
try {
|
|
188
|
+
let current: ts.Node = node;
|
|
189
|
+
while (current.parent) {
|
|
190
|
+
const parent = current.parent;
|
|
191
|
+
if (ts.isParameter(parent)) {
|
|
192
|
+
return 'parameter type';
|
|
193
|
+
}
|
|
194
|
+
if (ts.isFunctionDeclaration(parent) || ts.isMethodDeclaration(parent) || ts.isArrowFunction(parent)) {
|
|
195
|
+
if (parent.type === current) {
|
|
196
|
+
return 'return type';
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
if (ts.isVariableDeclaration(parent)) {
|
|
200
|
+
return 'variable type';
|
|
201
|
+
}
|
|
202
|
+
if (ts.isPropertyDeclaration(parent) || ts.isPropertySignature(parent)) {
|
|
203
|
+
return 'property type';
|
|
204
|
+
}
|
|
205
|
+
if (ts.isAsExpression(parent)) {
|
|
206
|
+
return 'type assertion';
|
|
207
|
+
}
|
|
208
|
+
if (ts.isTypeAliasDeclaration(parent)) {
|
|
209
|
+
return 'type alias';
|
|
210
|
+
}
|
|
211
|
+
if (ts.isTypeReferenceNode(parent)) {
|
|
212
|
+
return 'generic argument';
|
|
213
|
+
}
|
|
214
|
+
if (ts.isArrayTypeNode(parent)) {
|
|
215
|
+
return 'array element type';
|
|
216
|
+
}
|
|
217
|
+
if (ts.isUnionTypeNode(parent) || ts.isIntersectionTypeNode(parent)) {
|
|
218
|
+
return 'union/intersection type';
|
|
219
|
+
}
|
|
220
|
+
current = parent;
|
|
221
|
+
}
|
|
222
|
+
return 'type position';
|
|
223
|
+
} catch (err: unknown) {
|
|
224
|
+
//const error = toError(err);
|
|
225
|
+
return 'type position';
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
interface AnyUnknownInfo {
|
|
230
|
+
line: number;
|
|
231
|
+
column: number;
|
|
232
|
+
keyword: 'any' | 'unknown';
|
|
233
|
+
context: string;
|
|
234
|
+
hasDisableComment: boolean;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Check if a node is in a catch clause variable declaration.
|
|
239
|
+
* This allows `catch (err: unknown)` and `catch (err: unknown)` patterns.
|
|
240
|
+
*/
|
|
241
|
+
function isInCatchClause(node: ts.Node): boolean {
|
|
242
|
+
let current: ts.Node | undefined = node.parent;
|
|
243
|
+
while (current) {
|
|
244
|
+
if (ts.isCatchClause(current)) {
|
|
245
|
+
// We're somewhere in a catch clause - check if we're in the variable declaration
|
|
246
|
+
const catchClause = current as ts.CatchClause;
|
|
247
|
+
if (catchClause.variableDeclaration) {
|
|
248
|
+
// Walk back up from the original node to see if we're part of the variable declaration
|
|
249
|
+
let checkNode: ts.Node | undefined = node.parent;
|
|
250
|
+
while (checkNode && checkNode !== current) {
|
|
251
|
+
if (checkNode === catchClause.variableDeclaration) {
|
|
252
|
+
return true;
|
|
253
|
+
}
|
|
254
|
+
checkNode = checkNode.parent;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
current = current.parent;
|
|
259
|
+
}
|
|
260
|
+
return false;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Find all `any` and `unknown` keywords in a file using AST.
|
|
265
|
+
*/
|
|
266
|
+
// webpieces-disable max-lines-new-methods -- AST traversal with nested visitor function for keyword detection
|
|
267
|
+
function findAnyUnknownInFile(filePath: string, workspaceRoot: string): AnyUnknownInfo[] {
|
|
268
|
+
const fullPath = path.join(workspaceRoot, filePath);
|
|
269
|
+
if (!fs.existsSync(fullPath)) return [];
|
|
270
|
+
|
|
271
|
+
const content = fs.readFileSync(fullPath, 'utf-8');
|
|
272
|
+
const fileLines = content.split('\n');
|
|
273
|
+
const sourceFile = ts.createSourceFile(filePath, content, ts.ScriptTarget.Latest, true);
|
|
274
|
+
|
|
275
|
+
const violations: AnyUnknownInfo[] = [];
|
|
276
|
+
|
|
277
|
+
// webpieces-disable max-lines-new-methods -- AST visitor needs to handle both any and unknown keywords with full context detection
|
|
278
|
+
function visit(node: ts.Node): void {
|
|
279
|
+
// eslint-disable-next-line @webpieces/no-unmanaged-exceptions
|
|
280
|
+
try {
|
|
281
|
+
// Detect `any` keyword
|
|
282
|
+
if (node.kind === ts.SyntaxKind.AnyKeyword) {
|
|
283
|
+
// Skip catch clause variable types: catch (err: unknown) is allowed
|
|
284
|
+
if (isInCatchClause(node)) {
|
|
285
|
+
ts.forEachChild(node, visit);
|
|
286
|
+
return;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
const startPos = node.getStart(sourceFile);
|
|
290
|
+
if (startPos >= 0) {
|
|
291
|
+
const pos = sourceFile.getLineAndCharacterOfPosition(startPos);
|
|
292
|
+
const line = pos.line + 1;
|
|
293
|
+
const column = pos.character + 1;
|
|
294
|
+
const context = getViolationContext(node, sourceFile);
|
|
295
|
+
const disabled = hasDisableComment(fileLines, line);
|
|
296
|
+
|
|
297
|
+
violations.push({
|
|
298
|
+
line,
|
|
299
|
+
column,
|
|
300
|
+
keyword: 'any',
|
|
301
|
+
context,
|
|
302
|
+
hasDisableComment: disabled,
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
// Detect `unknown` keyword
|
|
308
|
+
if (node.kind === ts.SyntaxKind.UnknownKeyword) {
|
|
309
|
+
// Skip catch clause variable types: catch (err: unknown) is allowed
|
|
310
|
+
if (isInCatchClause(node)) {
|
|
311
|
+
ts.forEachChild(node, visit);
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
const startPos = node.getStart(sourceFile);
|
|
316
|
+
if (startPos >= 0) {
|
|
317
|
+
const pos = sourceFile.getLineAndCharacterOfPosition(startPos);
|
|
318
|
+
const line = pos.line + 1;
|
|
319
|
+
const column = pos.character + 1;
|
|
320
|
+
const context = getViolationContext(node, sourceFile);
|
|
321
|
+
const disabled = hasDisableComment(fileLines, line);
|
|
322
|
+
|
|
323
|
+
violations.push({
|
|
324
|
+
line,
|
|
325
|
+
column,
|
|
326
|
+
keyword: 'unknown',
|
|
327
|
+
context,
|
|
328
|
+
hasDisableComment: disabled,
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
} catch (err: unknown) {
|
|
333
|
+
//const error = toError(err);
|
|
334
|
+
// Skip nodes that cause errors during analysis
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
ts.forEachChild(node, visit);
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
visit(sourceFile);
|
|
341
|
+
return violations;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* MODIFIED_CODE mode: Flag violations on changed lines in diff hunks.
|
|
346
|
+
* This is LINE-BASED detection.
|
|
347
|
+
*/
|
|
348
|
+
// webpieces-disable max-lines-new-methods -- File iteration with diff parsing and line filtering
|
|
349
|
+
function findViolationsForModifiedCode(
|
|
350
|
+
workspaceRoot: string,
|
|
351
|
+
changedFiles: string[],
|
|
352
|
+
base: string,
|
|
353
|
+
head: string | undefined,
|
|
354
|
+
disableAllowed: boolean
|
|
355
|
+
): AnyUnknownViolation[] {
|
|
356
|
+
const violations: AnyUnknownViolation[] = [];
|
|
357
|
+
|
|
358
|
+
for (const file of changedFiles) {
|
|
359
|
+
const diff = getFileDiff(workspaceRoot, file, base, head);
|
|
360
|
+
const changedLines = getChangedLineNumbers(diff);
|
|
361
|
+
|
|
362
|
+
if (changedLines.size === 0) continue;
|
|
363
|
+
|
|
364
|
+
const allViolations = findAnyUnknownInFile(file, workspaceRoot);
|
|
365
|
+
|
|
366
|
+
for (const v of allViolations) {
|
|
367
|
+
if (disableAllowed && v.hasDisableComment) continue;
|
|
368
|
+
// LINE-BASED: Only include if the violation is on a changed line
|
|
369
|
+
if (!changedLines.has(v.line)) continue;
|
|
370
|
+
|
|
371
|
+
violations.push({
|
|
372
|
+
file,
|
|
373
|
+
line: v.line,
|
|
374
|
+
column: v.column,
|
|
375
|
+
keyword: v.keyword,
|
|
376
|
+
context: v.context,
|
|
377
|
+
});
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
return violations;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/**
|
|
385
|
+
* MODIFIED_FILES mode: Flag ALL violations in files that were modified.
|
|
386
|
+
*/
|
|
387
|
+
function findViolationsForModifiedFiles(workspaceRoot: string, changedFiles: string[], disableAllowed: boolean): AnyUnknownViolation[] {
|
|
388
|
+
const violations: AnyUnknownViolation[] = [];
|
|
389
|
+
|
|
390
|
+
for (const file of changedFiles) {
|
|
391
|
+
const allViolations = findAnyUnknownInFile(file, workspaceRoot);
|
|
392
|
+
|
|
393
|
+
for (const v of allViolations) {
|
|
394
|
+
if (disableAllowed && v.hasDisableComment) continue;
|
|
395
|
+
|
|
396
|
+
violations.push({
|
|
397
|
+
file,
|
|
398
|
+
line: v.line,
|
|
399
|
+
column: v.column,
|
|
400
|
+
keyword: v.keyword,
|
|
401
|
+
context: v.context,
|
|
402
|
+
});
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
return violations;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
/**
|
|
410
|
+
* Auto-detect the base branch by finding the merge-base with origin/main.
|
|
411
|
+
*/
|
|
412
|
+
function detectBase(workspaceRoot: string): string | null {
|
|
413
|
+
// eslint-disable-next-line @webpieces/no-unmanaged-exceptions
|
|
414
|
+
try {
|
|
415
|
+
const mergeBase = execSync('git merge-base HEAD origin/main', {
|
|
416
|
+
cwd: workspaceRoot,
|
|
417
|
+
encoding: 'utf-8',
|
|
418
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
419
|
+
}).trim();
|
|
420
|
+
|
|
421
|
+
if (mergeBase) {
|
|
422
|
+
return mergeBase;
|
|
423
|
+
}
|
|
424
|
+
} catch (err: unknown) {
|
|
425
|
+
//const error = toError(err);
|
|
426
|
+
// eslint-disable-next-line @webpieces/no-unmanaged-exceptions
|
|
427
|
+
try {
|
|
428
|
+
const mergeBase = execSync('git merge-base HEAD main', {
|
|
429
|
+
cwd: workspaceRoot,
|
|
430
|
+
encoding: 'utf-8',
|
|
431
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
432
|
+
}).trim();
|
|
433
|
+
|
|
434
|
+
if (mergeBase) {
|
|
435
|
+
return mergeBase;
|
|
436
|
+
}
|
|
437
|
+
} catch (err2: unknown) {
|
|
438
|
+
//const error2 = toError(err2);
|
|
439
|
+
// Ignore
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
return null;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
/**
|
|
446
|
+
* Report violations to console.
|
|
447
|
+
*/
|
|
448
|
+
function reportViolations(violations: AnyUnknownViolation[], mode: NoAnyUnknownMode): void {
|
|
449
|
+
console.error('');
|
|
450
|
+
console.error('ā `any` and `unknown` keywords found! Use specific types instead.');
|
|
451
|
+
console.error('');
|
|
452
|
+
console.error('š Avoiding any/unknown improves type safety:');
|
|
453
|
+
console.error('');
|
|
454
|
+
console.error(' BAD: const data: any = fetchData();');
|
|
455
|
+
console.error(' GOOD: const data: UserData = fetchData();');
|
|
456
|
+
console.error('');
|
|
457
|
+
console.error(' BAD: function process(input: unknown): unknown { }');
|
|
458
|
+
console.error(' GOOD: function process(input: ValidInput): ValidOutput { }');
|
|
459
|
+
console.error('');
|
|
460
|
+
|
|
461
|
+
for (const v of violations) {
|
|
462
|
+
console.error(` ā ${v.file}:${v.line}:${v.column}`);
|
|
463
|
+
console.error(` \`${v.keyword}\` keyword in ${v.context}`);
|
|
464
|
+
}
|
|
465
|
+
console.error('');
|
|
466
|
+
|
|
467
|
+
console.error(' To fix: Replace with specific types or interfaces');
|
|
468
|
+
console.error('');
|
|
469
|
+
console.error(' Escape hatch (use sparingly):');
|
|
470
|
+
console.error(' // webpieces-disable no-any-unknown -- [your reason]');
|
|
471
|
+
console.error('');
|
|
472
|
+
console.error(` Current mode: ${mode}`);
|
|
473
|
+
console.error('');
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
/**
|
|
477
|
+
* Resolve mode considering ignoreModifiedUntilEpoch override.
|
|
478
|
+
* When active, downgrades to OFF. When expired, logs a warning.
|
|
479
|
+
*/
|
|
480
|
+
function resolveMode(normalMode: NoAnyUnknownMode, epoch: number | undefined): NoAnyUnknownMode {
|
|
481
|
+
if (epoch === undefined || normalMode === 'OFF') {
|
|
482
|
+
return normalMode;
|
|
483
|
+
}
|
|
484
|
+
const nowSeconds = Date.now() / 1000;
|
|
485
|
+
if (nowSeconds < epoch) {
|
|
486
|
+
const expiresDate = new Date(epoch * 1000).toISOString().split('T')[0];
|
|
487
|
+
console.log(`\nāļø Skipping no-any-unknown validation (ignoreModifiedUntilEpoch active, expires: ${expiresDate})`);
|
|
488
|
+
console.log('');
|
|
489
|
+
return 'OFF';
|
|
490
|
+
}
|
|
491
|
+
return normalMode;
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
export default async function runValidator(
|
|
495
|
+
options: ValidateNoAnyUnknownOptions,
|
|
496
|
+
workspaceRoot: string
|
|
497
|
+
): Promise<ExecutorResult> {
|
|
498
|
+
const mode: NoAnyUnknownMode = resolveMode(options.mode ?? 'OFF', options.ignoreModifiedUntilEpoch);
|
|
499
|
+
const disableAllowed = options.disableAllowed ?? true;
|
|
500
|
+
|
|
501
|
+
if (mode === 'OFF') {
|
|
502
|
+
console.log('\nāļø Skipping no-any-unknown validation (mode: OFF)');
|
|
503
|
+
console.log('');
|
|
504
|
+
return { success: true };
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
console.log('\nš Validating No Any/Unknown\n');
|
|
508
|
+
console.log(` Mode: ${mode}`);
|
|
509
|
+
|
|
510
|
+
let base = process.env['NX_BASE'];
|
|
511
|
+
const head = process.env['NX_HEAD'];
|
|
512
|
+
|
|
513
|
+
if (!base) {
|
|
514
|
+
base = detectBase(workspaceRoot) ?? undefined;
|
|
515
|
+
|
|
516
|
+
if (!base) {
|
|
517
|
+
console.log('\nāļø Skipping no-any-unknown validation (could not detect base branch)');
|
|
518
|
+
console.log('');
|
|
519
|
+
return { success: true };
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
console.log(` Base: ${base}`);
|
|
524
|
+
console.log(` Head: ${head ?? 'working tree (includes uncommitted changes)'}`);
|
|
525
|
+
console.log('');
|
|
526
|
+
|
|
527
|
+
const changedFiles = getChangedTypeScriptFiles(workspaceRoot, base, head);
|
|
528
|
+
|
|
529
|
+
if (changedFiles.length === 0) {
|
|
530
|
+
console.log('ā
No TypeScript files changed');
|
|
531
|
+
return { success: true };
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
console.log(`š Checking ${changedFiles.length} changed file(s)...`);
|
|
535
|
+
|
|
536
|
+
let violations: AnyUnknownViolation[] = [];
|
|
537
|
+
|
|
538
|
+
if (mode === 'MODIFIED_CODE') {
|
|
539
|
+
violations = findViolationsForModifiedCode(workspaceRoot, changedFiles, base, head, disableAllowed);
|
|
540
|
+
} else if (mode === 'MODIFIED_FILES') {
|
|
541
|
+
violations = findViolationsForModifiedFiles(workspaceRoot, changedFiles, disableAllowed);
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
if (violations.length === 0) {
|
|
545
|
+
console.log('ā
No any/unknown keywords found');
|
|
546
|
+
return { success: true };
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
reportViolations(violations, mode);
|
|
550
|
+
|
|
551
|
+
return { success: false };
|
|
552
|
+
}
|