@webpieces/dev-config 0.2.73 ā 0.2.75
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/validate-code/executor.d.ts +4 -0
- package/architecture/executors/validate-code/executor.js +11 -1
- package/architecture/executors/validate-code/executor.js.map +1 -1
- package/architecture/executors/validate-code/executor.ts +15 -1
- package/architecture/executors/validate-code/schema.json +12 -0
- package/architecture/executors/validate-no-any-unknown/executor.d.ts +41 -0
- package/architecture/executors/validate-no-any-unknown/executor.js +442 -0
- package/architecture/executors/validate-no-any-unknown/executor.js.map +1 -0
- package/architecture/executors/validate-no-any-unknown/executor.ts +513 -0
- package/architecture/executors/validate-no-any-unknown/schema.json +15 -0
- package/architecture/executors/validate-no-inline-types/executor.d.ts +90 -0
- package/architecture/executors/validate-no-inline-types/executor.js +684 -0
- package/architecture/executors/validate-no-inline-types/executor.js.map +1 -0
- package/architecture/executors/validate-no-inline-types/executor.ts +785 -0
- package/architecture/executors/validate-no-inline-types/schema.json +15 -0
- package/executors.json +10 -0
- package/package.json +1 -1
|
@@ -0,0 +1,684 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Validate No Inline Types Executor
|
|
4
|
+
*
|
|
5
|
+
* Validates that inline type literals AND tuple types are not used in type positions.
|
|
6
|
+
* Prefer named types/interfaces/classes for clarity and reusability.
|
|
7
|
+
*
|
|
8
|
+
* ============================================================================
|
|
9
|
+
* VIOLATIONS (BAD) - These patterns are flagged:
|
|
10
|
+
* ============================================================================
|
|
11
|
+
*
|
|
12
|
+
* 1. INLINE TYPE LITERALS { }
|
|
13
|
+
* -------------------------
|
|
14
|
+
* - Inline parameter type: function foo(arg: { x: number }) { }
|
|
15
|
+
* - Inline return type: function foo(): { x: number } { }
|
|
16
|
+
* - Inline variable type: const config: { timeout: number } = { timeout: 5 };
|
|
17
|
+
* - Inline property type: class C { data: { id: number }; }
|
|
18
|
+
* - Inline in union: type T = { x: number } | null;
|
|
19
|
+
* - Inline in intersection: type T = { x: number } & { y: number };
|
|
20
|
+
* - Inline in generic: Promise<{ data: string }>
|
|
21
|
+
* - Inline in array: function foo(): { id: string }[] { }
|
|
22
|
+
* - Nested inline in alias: type T = { data: { nested: number } }; // inner { } flagged
|
|
23
|
+
* - Inline in tuple: type T = [{ x: number }, string];
|
|
24
|
+
*
|
|
25
|
+
* 2. TUPLE TYPES [ ]
|
|
26
|
+
* ----------------
|
|
27
|
+
* - Tuple return type: function foo(): [Items[], number] { }
|
|
28
|
+
* - Tuple parameter type: function foo(arg: [string, number]) { }
|
|
29
|
+
* - Tuple variable type: const result: [Data[], number] = getData();
|
|
30
|
+
* - Tuple in generic: Promise<[Items[], number]>
|
|
31
|
+
* - Tuple in union: type T = [A, B] | null;
|
|
32
|
+
* - Nested tuple: type T = { data: [A, B] };
|
|
33
|
+
*
|
|
34
|
+
* ============================================================================
|
|
35
|
+
* ALLOWED (GOOD) - These patterns pass validation:
|
|
36
|
+
* ============================================================================
|
|
37
|
+
*
|
|
38
|
+
* 1. TYPE ALIAS DEFINITIONS (direct body only)
|
|
39
|
+
* -----------------------------------------
|
|
40
|
+
* - Type alias with literal: type MyConfig = { timeout: number };
|
|
41
|
+
* - Type alias with tuple: type MyResult = [Items[], number];
|
|
42
|
+
* - Interface definition: interface MyData { id: number }
|
|
43
|
+
* - Class definition: class UserData { id: number; name: string; }
|
|
44
|
+
*
|
|
45
|
+
* 2. USING NAMED TYPES
|
|
46
|
+
* ------------------
|
|
47
|
+
* - Named param type: function foo(arg: MyConfig) { }
|
|
48
|
+
* - Named return type: function foo(): MyConfig { }
|
|
49
|
+
* - Named with null: function foo(): MyConfig | null { }
|
|
50
|
+
* - Named with undefined: function foo(): MyConfig | undefined { }
|
|
51
|
+
* - Union of named types: type Either = TypeA | TypeB;
|
|
52
|
+
* - Named in generic: Promise<MyResult>
|
|
53
|
+
* - Named tuple alias: function foo(): MyTupleResult { }
|
|
54
|
+
*
|
|
55
|
+
* 3. PRIMITIVES AND BUILT-INS
|
|
56
|
+
* -------------------------
|
|
57
|
+
* - Primitive types: function foo(): string { }
|
|
58
|
+
* - Primitive arrays: function foo(): string[] { }
|
|
59
|
+
* - Built-in generics: function foo(): Promise<string> { }
|
|
60
|
+
* - Void return: function foo(): void { }
|
|
61
|
+
*
|
|
62
|
+
* ============================================================================
|
|
63
|
+
* MODES
|
|
64
|
+
* ============================================================================
|
|
65
|
+
* - OFF: Skip validation entirely
|
|
66
|
+
* - NEW_METHODS: Only validate in new methods (detected via git diff)
|
|
67
|
+
* - MODIFIED_AND_NEW_METHODS: Validate in new methods + methods with changes
|
|
68
|
+
* - MODIFIED_FILES: Validate all violations in modified files
|
|
69
|
+
* - ALL: Validate all violations in all TypeScript files
|
|
70
|
+
*
|
|
71
|
+
* ============================================================================
|
|
72
|
+
* ESCAPE HATCH
|
|
73
|
+
* ============================================================================
|
|
74
|
+
* Add comment above the violation:
|
|
75
|
+
* // webpieces-disable no-inline-types -- [your justification]
|
|
76
|
+
* function foo(arg: { x: number }) { }
|
|
77
|
+
*
|
|
78
|
+
* Use sparingly! Common valid reasons:
|
|
79
|
+
* - Prisma payload types that require inline generics
|
|
80
|
+
* - Third-party library APIs that expect inline types
|
|
81
|
+
* - Legacy code being incrementally migrated
|
|
82
|
+
*/
|
|
83
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
84
|
+
exports.default = runExecutor;
|
|
85
|
+
const tslib_1 = require("tslib");
|
|
86
|
+
const child_process_1 = require("child_process");
|
|
87
|
+
const fs = tslib_1.__importStar(require("fs"));
|
|
88
|
+
const path = tslib_1.__importStar(require("path"));
|
|
89
|
+
const ts = tslib_1.__importStar(require("typescript"));
|
|
90
|
+
/**
|
|
91
|
+
* Get changed TypeScript files between base and head (or working tree if head not specified).
|
|
92
|
+
*/
|
|
93
|
+
// webpieces-disable max-lines-new-methods -- Git command handling with untracked files requires multiple code paths
|
|
94
|
+
function getChangedTypeScriptFiles(workspaceRoot, base, head) {
|
|
95
|
+
try {
|
|
96
|
+
const diffTarget = head ? `${base} ${head}` : base;
|
|
97
|
+
const output = (0, child_process_1.execSync)(`git diff --name-only ${diffTarget} -- '*.ts' '*.tsx'`, {
|
|
98
|
+
cwd: workspaceRoot,
|
|
99
|
+
encoding: 'utf-8',
|
|
100
|
+
});
|
|
101
|
+
const changedFiles = output
|
|
102
|
+
.trim()
|
|
103
|
+
.split('\n')
|
|
104
|
+
.filter((f) => f && !f.includes('.spec.ts') && !f.includes('.test.ts'));
|
|
105
|
+
if (!head) {
|
|
106
|
+
try {
|
|
107
|
+
const untrackedOutput = (0, child_process_1.execSync)(`git ls-files --others --exclude-standard '*.ts' '*.tsx'`, {
|
|
108
|
+
cwd: workspaceRoot,
|
|
109
|
+
encoding: 'utf-8',
|
|
110
|
+
});
|
|
111
|
+
const untrackedFiles = untrackedOutput
|
|
112
|
+
.trim()
|
|
113
|
+
.split('\n')
|
|
114
|
+
.filter((f) => f && !f.includes('.spec.ts') && !f.includes('.test.ts'));
|
|
115
|
+
const allFiles = new Set([...changedFiles, ...untrackedFiles]);
|
|
116
|
+
return Array.from(allFiles);
|
|
117
|
+
}
|
|
118
|
+
catch {
|
|
119
|
+
return changedFiles;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return changedFiles;
|
|
123
|
+
}
|
|
124
|
+
catch {
|
|
125
|
+
return [];
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Get all TypeScript files in the workspace using git ls-files (excluding tests).
|
|
130
|
+
*/
|
|
131
|
+
function getAllTypeScriptFiles(workspaceRoot) {
|
|
132
|
+
try {
|
|
133
|
+
const output = (0, child_process_1.execSync)(`git ls-files '*.ts' '*.tsx'`, {
|
|
134
|
+
cwd: workspaceRoot,
|
|
135
|
+
encoding: 'utf-8',
|
|
136
|
+
});
|
|
137
|
+
return output
|
|
138
|
+
.trim()
|
|
139
|
+
.split('\n')
|
|
140
|
+
.filter((f) => f && !f.includes('.spec.ts') && !f.includes('.test.ts'));
|
|
141
|
+
}
|
|
142
|
+
catch {
|
|
143
|
+
return [];
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Get the diff content for a specific file.
|
|
148
|
+
*/
|
|
149
|
+
function getFileDiff(workspaceRoot, file, base, head) {
|
|
150
|
+
try {
|
|
151
|
+
const diffTarget = head ? `${base} ${head}` : base;
|
|
152
|
+
const diff = (0, child_process_1.execSync)(`git diff ${diffTarget} -- "${file}"`, {
|
|
153
|
+
cwd: workspaceRoot,
|
|
154
|
+
encoding: 'utf-8',
|
|
155
|
+
});
|
|
156
|
+
if (!diff && !head) {
|
|
157
|
+
const fullPath = path.join(workspaceRoot, file);
|
|
158
|
+
if (fs.existsSync(fullPath)) {
|
|
159
|
+
const isUntracked = (0, child_process_1.execSync)(`git ls-files --others --exclude-standard "${file}"`, {
|
|
160
|
+
cwd: workspaceRoot,
|
|
161
|
+
encoding: 'utf-8',
|
|
162
|
+
}).trim();
|
|
163
|
+
if (isUntracked) {
|
|
164
|
+
const content = fs.readFileSync(fullPath, 'utf-8');
|
|
165
|
+
const lines = content.split('\n');
|
|
166
|
+
return lines.map((line) => `+${line}`).join('\n');
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return diff;
|
|
171
|
+
}
|
|
172
|
+
catch {
|
|
173
|
+
return '';
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Parse diff to extract changed line numbers (both additions and modifications).
|
|
178
|
+
*/
|
|
179
|
+
function getChangedLineNumbers(diffContent) {
|
|
180
|
+
const changedLines = new Set();
|
|
181
|
+
const lines = diffContent.split('\n');
|
|
182
|
+
let currentLine = 0;
|
|
183
|
+
for (const line of lines) {
|
|
184
|
+
const hunkMatch = line.match(/^@@ -\d+(?:,\d+)? \+(\d+)(?:,\d+)? @@/);
|
|
185
|
+
if (hunkMatch) {
|
|
186
|
+
currentLine = parseInt(hunkMatch[1], 10);
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
if (line.startsWith('+') && !line.startsWith('+++')) {
|
|
190
|
+
changedLines.add(currentLine);
|
|
191
|
+
currentLine++;
|
|
192
|
+
}
|
|
193
|
+
else if (line.startsWith('-') && !line.startsWith('---')) {
|
|
194
|
+
// Deletions don't increment line number
|
|
195
|
+
}
|
|
196
|
+
else {
|
|
197
|
+
currentLine++;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
return changedLines;
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Check if a line contains a webpieces-disable comment for no-inline-types.
|
|
204
|
+
*/
|
|
205
|
+
function hasDisableComment(lines, lineNumber) {
|
|
206
|
+
const startCheck = Math.max(0, lineNumber - 5);
|
|
207
|
+
for (let i = lineNumber - 2; i >= startCheck; i--) {
|
|
208
|
+
const line = lines[i]?.trim() ?? '';
|
|
209
|
+
if (line.startsWith('function ') || line.startsWith('class ') || line.endsWith('}')) {
|
|
210
|
+
break;
|
|
211
|
+
}
|
|
212
|
+
if (line.includes('webpieces-disable') && line.includes('no-inline-types')) {
|
|
213
|
+
return true;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
return false;
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Check if a TypeLiteral or TupleType node is in an allowed context.
|
|
220
|
+
* Only allowed if the DIRECT parent is a TypeAliasDeclaration.
|
|
221
|
+
*
|
|
222
|
+
* ALLOWED:
|
|
223
|
+
* type MyConfig = { x: number }; // TypeLiteral direct child of TypeAliasDeclaration
|
|
224
|
+
* type MyTuple = [A, B]; // TupleType direct child of TypeAliasDeclaration
|
|
225
|
+
*
|
|
226
|
+
* NOT ALLOWED (flagged):
|
|
227
|
+
* type T = { x: number } | null; // Parent is UnionType, not TypeAliasDeclaration
|
|
228
|
+
* type T = { data: { nested: number } }; // Inner TypeLiteral's parent is PropertySignature
|
|
229
|
+
* function foo(): [A, B] { } // TupleType's parent is FunctionDeclaration
|
|
230
|
+
* type T = Prisma.GetPayload<{ include: {...} }>; // TypeLiteral in generic argument
|
|
231
|
+
*
|
|
232
|
+
* NOTE: Prisma types require inline type literals in generic arguments. Use the escape hatch:
|
|
233
|
+
* // webpieces-disable no-inline-types -- Prisma API requires inline type argument
|
|
234
|
+
* type T = Prisma.GetPayload<{ include: {...} }>;
|
|
235
|
+
*/
|
|
236
|
+
function isInAllowedContext(node) {
|
|
237
|
+
const parent = node.parent;
|
|
238
|
+
if (!parent)
|
|
239
|
+
return false;
|
|
240
|
+
// Only allowed if it's the DIRECT body of a type alias
|
|
241
|
+
if (ts.isTypeAliasDeclaration(parent)) {
|
|
242
|
+
return true;
|
|
243
|
+
}
|
|
244
|
+
return false;
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Get a description of the context where the inline type or tuple appears.
|
|
248
|
+
*
|
|
249
|
+
* Returns human-readable context like:
|
|
250
|
+
* - "inline parameter type"
|
|
251
|
+
* - "tuple return type"
|
|
252
|
+
* - "inline type in generic argument"
|
|
253
|
+
*/
|
|
254
|
+
// webpieces-disable max-lines-new-methods -- Context detection requires checking many AST node types
|
|
255
|
+
function getViolationContext(node, sourceFile) {
|
|
256
|
+
try {
|
|
257
|
+
const isTuple = ts.isTupleTypeNode(node);
|
|
258
|
+
const prefix = isTuple ? 'tuple' : 'inline';
|
|
259
|
+
let current = node;
|
|
260
|
+
while (current.parent) {
|
|
261
|
+
const parent = current.parent;
|
|
262
|
+
if (ts.isParameter(parent)) {
|
|
263
|
+
return `${prefix} parameter type`;
|
|
264
|
+
}
|
|
265
|
+
if (ts.isFunctionDeclaration(parent) || ts.isMethodDeclaration(parent) || ts.isArrowFunction(parent)) {
|
|
266
|
+
if (parent.type === current) {
|
|
267
|
+
return `${prefix} return type`;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
if (ts.isVariableDeclaration(parent)) {
|
|
271
|
+
return `${prefix} variable type`;
|
|
272
|
+
}
|
|
273
|
+
if (ts.isPropertyDeclaration(parent) || ts.isPropertySignature(parent)) {
|
|
274
|
+
if (parent.type === current) {
|
|
275
|
+
return `${prefix} property type`;
|
|
276
|
+
}
|
|
277
|
+
// Check if it's nested inside another type literal
|
|
278
|
+
let ancestor = parent.parent;
|
|
279
|
+
while (ancestor) {
|
|
280
|
+
if (ts.isTypeLiteralNode(ancestor)) {
|
|
281
|
+
return `nested ${prefix} type`;
|
|
282
|
+
}
|
|
283
|
+
if (ts.isTypeAliasDeclaration(ancestor)) {
|
|
284
|
+
return `nested ${prefix} type in type alias`;
|
|
285
|
+
}
|
|
286
|
+
ancestor = ancestor.parent;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
if (ts.isUnionTypeNode(parent) || ts.isIntersectionTypeNode(parent)) {
|
|
290
|
+
return `${prefix} type in union/intersection`;
|
|
291
|
+
}
|
|
292
|
+
// Safely check parent.parent before accessing it
|
|
293
|
+
if (parent.parent && ts.isTypeReferenceNode(parent.parent) && ts.isTypeNode(parent)) {
|
|
294
|
+
return `${prefix} type in generic argument`;
|
|
295
|
+
}
|
|
296
|
+
// Direct parent is TypeReferenceNode (e.g., Prisma.GetPayload<{...}>)
|
|
297
|
+
if (ts.isTypeReferenceNode(parent)) {
|
|
298
|
+
return `${prefix} type in generic argument`;
|
|
299
|
+
}
|
|
300
|
+
if (ts.isArrayTypeNode(parent)) {
|
|
301
|
+
return `${prefix} type in array`;
|
|
302
|
+
}
|
|
303
|
+
if (ts.isTupleTypeNode(parent) && !isTuple) {
|
|
304
|
+
return `inline type in tuple`;
|
|
305
|
+
}
|
|
306
|
+
current = parent;
|
|
307
|
+
}
|
|
308
|
+
return isTuple ? 'tuple type' : 'inline type literal';
|
|
309
|
+
}
|
|
310
|
+
catch (error) {
|
|
311
|
+
// Defensive: return generic context if AST traversal fails
|
|
312
|
+
return ts.isTupleTypeNode(node) ? 'tuple type' : 'inline type literal';
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* Find all methods/functions in a file with their line ranges.
|
|
317
|
+
*/
|
|
318
|
+
// webpieces-disable max-lines-new-methods -- AST traversal requires inline visitor function
|
|
319
|
+
function findMethodsInFile(filePath, workspaceRoot) {
|
|
320
|
+
const fullPath = path.join(workspaceRoot, filePath);
|
|
321
|
+
if (!fs.existsSync(fullPath))
|
|
322
|
+
return [];
|
|
323
|
+
const content = fs.readFileSync(fullPath, 'utf-8');
|
|
324
|
+
const sourceFile = ts.createSourceFile(filePath, content, ts.ScriptTarget.Latest, true);
|
|
325
|
+
const methods = [];
|
|
326
|
+
// webpieces-disable max-lines-new-methods -- AST visitor pattern requires handling multiple node types
|
|
327
|
+
function visit(node) {
|
|
328
|
+
let methodName;
|
|
329
|
+
let startLine;
|
|
330
|
+
let endLine;
|
|
331
|
+
if (ts.isMethodDeclaration(node) && node.name) {
|
|
332
|
+
methodName = node.name.getText(sourceFile);
|
|
333
|
+
const start = sourceFile.getLineAndCharacterOfPosition(node.getStart());
|
|
334
|
+
const end = sourceFile.getLineAndCharacterOfPosition(node.getEnd());
|
|
335
|
+
startLine = start.line + 1;
|
|
336
|
+
endLine = end.line + 1;
|
|
337
|
+
}
|
|
338
|
+
else if (ts.isFunctionDeclaration(node) && node.name) {
|
|
339
|
+
methodName = node.name.getText(sourceFile);
|
|
340
|
+
const start = sourceFile.getLineAndCharacterOfPosition(node.getStart());
|
|
341
|
+
const end = sourceFile.getLineAndCharacterOfPosition(node.getEnd());
|
|
342
|
+
startLine = start.line + 1;
|
|
343
|
+
endLine = end.line + 1;
|
|
344
|
+
}
|
|
345
|
+
else if (ts.isArrowFunction(node)) {
|
|
346
|
+
if (ts.isVariableDeclaration(node.parent) && ts.isIdentifier(node.parent.name)) {
|
|
347
|
+
methodName = node.parent.name.getText(sourceFile);
|
|
348
|
+
const start = sourceFile.getLineAndCharacterOfPosition(node.getStart());
|
|
349
|
+
const end = sourceFile.getLineAndCharacterOfPosition(node.getEnd());
|
|
350
|
+
startLine = start.line + 1;
|
|
351
|
+
endLine = end.line + 1;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
if (methodName && startLine !== undefined && endLine !== undefined) {
|
|
355
|
+
methods.push({ name: methodName, startLine, endLine });
|
|
356
|
+
}
|
|
357
|
+
ts.forEachChild(node, visit);
|
|
358
|
+
}
|
|
359
|
+
visit(sourceFile);
|
|
360
|
+
return methods;
|
|
361
|
+
}
|
|
362
|
+
/**
|
|
363
|
+
* Check if a line is within any method's range and if that method has changes.
|
|
364
|
+
*/
|
|
365
|
+
function isLineInChangedMethod(line, methods, changedLines, newMethodNames) {
|
|
366
|
+
for (const method of methods) {
|
|
367
|
+
if (line >= method.startLine && line <= method.endLine) {
|
|
368
|
+
// Check if this method is new or has changes
|
|
369
|
+
if (newMethodNames.has(method.name)) {
|
|
370
|
+
return true;
|
|
371
|
+
}
|
|
372
|
+
// Check if any line in the method range has changes
|
|
373
|
+
for (let l = method.startLine; l <= method.endLine; l++) {
|
|
374
|
+
if (changedLines.has(l)) {
|
|
375
|
+
return true;
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
return false;
|
|
381
|
+
}
|
|
382
|
+
/**
|
|
383
|
+
* Check if a line is within a new method.
|
|
384
|
+
*/
|
|
385
|
+
function isLineInNewMethod(line, methods, newMethodNames) {
|
|
386
|
+
for (const method of methods) {
|
|
387
|
+
if (line >= method.startLine && line <= method.endLine && newMethodNames.has(method.name)) {
|
|
388
|
+
return true;
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
return false;
|
|
392
|
+
}
|
|
393
|
+
/**
|
|
394
|
+
* Parse diff to find newly added method signatures.
|
|
395
|
+
*/
|
|
396
|
+
function findNewMethodSignaturesInDiff(diffContent) {
|
|
397
|
+
const newMethods = new Set();
|
|
398
|
+
const lines = diffContent.split('\n');
|
|
399
|
+
const patterns = [
|
|
400
|
+
/^\+\s*(?:export\s+)?(?:async\s+)?function\s+(\w+)\s*\(/,
|
|
401
|
+
/^\+\s*(?:export\s+)?(?:const|let)\s+(\w+)\s*=\s*(?:async\s*)?\(/,
|
|
402
|
+
/^\+\s*(?:export\s+)?(?:const|let)\s+(\w+)\s*=\s*(?:async\s+)?function/,
|
|
403
|
+
/^\+\s*(?:(?:public|private|protected)\s+)?(?:static\s+)?(?:async\s+)?(\w+)\s*\(/,
|
|
404
|
+
];
|
|
405
|
+
for (const line of lines) {
|
|
406
|
+
if (line.startsWith('+') && !line.startsWith('+++')) {
|
|
407
|
+
for (const pattern of patterns) {
|
|
408
|
+
const match = line.match(pattern);
|
|
409
|
+
if (match) {
|
|
410
|
+
const methodName = match[1];
|
|
411
|
+
if (methodName && !['if', 'for', 'while', 'switch', 'catch', 'constructor'].includes(methodName)) {
|
|
412
|
+
newMethods.add(methodName);
|
|
413
|
+
}
|
|
414
|
+
break;
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
return newMethods;
|
|
420
|
+
}
|
|
421
|
+
/**
|
|
422
|
+
* Find all inline type literals AND tuple types in a file.
|
|
423
|
+
*
|
|
424
|
+
* Detects:
|
|
425
|
+
* - TypeLiteral nodes: { x: number }
|
|
426
|
+
* - TupleType nodes: [A, B]
|
|
427
|
+
*
|
|
428
|
+
* Both are flagged unless they are the DIRECT body of a type alias.
|
|
429
|
+
*/
|
|
430
|
+
// webpieces-disable max-lines-new-methods -- AST traversal with visitor pattern
|
|
431
|
+
function findInlineTypesInFile(filePath, workspaceRoot) {
|
|
432
|
+
const fullPath = path.join(workspaceRoot, filePath);
|
|
433
|
+
if (!fs.existsSync(fullPath))
|
|
434
|
+
return [];
|
|
435
|
+
const content = fs.readFileSync(fullPath, 'utf-8');
|
|
436
|
+
const fileLines = content.split('\n');
|
|
437
|
+
const sourceFile = ts.createSourceFile(filePath, content, ts.ScriptTarget.Latest, true);
|
|
438
|
+
const inlineTypes = [];
|
|
439
|
+
function visit(node) {
|
|
440
|
+
try {
|
|
441
|
+
// Check for inline type literals: { x: number }
|
|
442
|
+
if (ts.isTypeLiteralNode(node)) {
|
|
443
|
+
if (!isInAllowedContext(node)) {
|
|
444
|
+
const startPos = node.getStart(sourceFile);
|
|
445
|
+
if (startPos >= 0) {
|
|
446
|
+
const pos = sourceFile.getLineAndCharacterOfPosition(startPos);
|
|
447
|
+
const line = pos.line + 1;
|
|
448
|
+
const column = pos.character + 1;
|
|
449
|
+
const context = getViolationContext(node, sourceFile);
|
|
450
|
+
const disabled = hasDisableComment(fileLines, line);
|
|
451
|
+
inlineTypes.push({
|
|
452
|
+
line,
|
|
453
|
+
column,
|
|
454
|
+
context,
|
|
455
|
+
hasDisableComment: disabled,
|
|
456
|
+
});
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
// Check for tuple types: [A, B]
|
|
461
|
+
if (ts.isTupleTypeNode(node)) {
|
|
462
|
+
if (!isInAllowedContext(node)) {
|
|
463
|
+
const startPos = node.getStart(sourceFile);
|
|
464
|
+
if (startPos >= 0) {
|
|
465
|
+
const pos = sourceFile.getLineAndCharacterOfPosition(startPos);
|
|
466
|
+
const line = pos.line + 1;
|
|
467
|
+
const column = pos.character + 1;
|
|
468
|
+
const context = getViolationContext(node, sourceFile);
|
|
469
|
+
const disabled = hasDisableComment(fileLines, line);
|
|
470
|
+
inlineTypes.push({
|
|
471
|
+
line,
|
|
472
|
+
column,
|
|
473
|
+
context,
|
|
474
|
+
hasDisableComment: disabled,
|
|
475
|
+
});
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
catch (error) {
|
|
481
|
+
// Skip nodes that cause errors during analysis
|
|
482
|
+
}
|
|
483
|
+
ts.forEachChild(node, visit);
|
|
484
|
+
}
|
|
485
|
+
visit(sourceFile);
|
|
486
|
+
return inlineTypes;
|
|
487
|
+
}
|
|
488
|
+
/**
|
|
489
|
+
* Find violations in new methods only (NEW_METHODS mode).
|
|
490
|
+
*/
|
|
491
|
+
// webpieces-disable max-lines-new-methods -- File iteration with diff parsing and method matching
|
|
492
|
+
function findViolationsForNewMethods(workspaceRoot, changedFiles, base, head) {
|
|
493
|
+
const violations = [];
|
|
494
|
+
for (const file of changedFiles) {
|
|
495
|
+
const diff = getFileDiff(workspaceRoot, file, base, head);
|
|
496
|
+
const newMethodNames = findNewMethodSignaturesInDiff(diff);
|
|
497
|
+
if (newMethodNames.size === 0)
|
|
498
|
+
continue;
|
|
499
|
+
const methods = findMethodsInFile(file, workspaceRoot);
|
|
500
|
+
const inlineTypes = findInlineTypesInFile(file, workspaceRoot);
|
|
501
|
+
for (const inlineType of inlineTypes) {
|
|
502
|
+
if (inlineType.hasDisableComment)
|
|
503
|
+
continue;
|
|
504
|
+
if (!isLineInNewMethod(inlineType.line, methods, newMethodNames))
|
|
505
|
+
continue;
|
|
506
|
+
violations.push({
|
|
507
|
+
file,
|
|
508
|
+
line: inlineType.line,
|
|
509
|
+
column: inlineType.column,
|
|
510
|
+
context: inlineType.context,
|
|
511
|
+
});
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
return violations;
|
|
515
|
+
}
|
|
516
|
+
/**
|
|
517
|
+
* Find violations in new and modified methods (MODIFIED_AND_NEW_METHODS mode).
|
|
518
|
+
*/
|
|
519
|
+
// webpieces-disable max-lines-new-methods -- Combines new method detection with change detection
|
|
520
|
+
function findViolationsForModifiedAndNewMethods(workspaceRoot, changedFiles, base, head) {
|
|
521
|
+
const violations = [];
|
|
522
|
+
for (const file of changedFiles) {
|
|
523
|
+
const diff = getFileDiff(workspaceRoot, file, base, head);
|
|
524
|
+
const newMethodNames = findNewMethodSignaturesInDiff(diff);
|
|
525
|
+
const changedLines = getChangedLineNumbers(diff);
|
|
526
|
+
const methods = findMethodsInFile(file, workspaceRoot);
|
|
527
|
+
const inlineTypes = findInlineTypesInFile(file, workspaceRoot);
|
|
528
|
+
for (const inlineType of inlineTypes) {
|
|
529
|
+
if (inlineType.hasDisableComment)
|
|
530
|
+
continue;
|
|
531
|
+
if (!isLineInChangedMethod(inlineType.line, methods, changedLines, newMethodNames))
|
|
532
|
+
continue;
|
|
533
|
+
violations.push({
|
|
534
|
+
file,
|
|
535
|
+
line: inlineType.line,
|
|
536
|
+
column: inlineType.column,
|
|
537
|
+
context: inlineType.context,
|
|
538
|
+
});
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
return violations;
|
|
542
|
+
}
|
|
543
|
+
/**
|
|
544
|
+
* Find all violations in modified files (MODIFIED_FILES mode).
|
|
545
|
+
*/
|
|
546
|
+
function findViolationsForModifiedFiles(workspaceRoot, changedFiles) {
|
|
547
|
+
const violations = [];
|
|
548
|
+
for (const file of changedFiles) {
|
|
549
|
+
const inlineTypes = findInlineTypesInFile(file, workspaceRoot);
|
|
550
|
+
for (const inlineType of inlineTypes) {
|
|
551
|
+
if (inlineType.hasDisableComment)
|
|
552
|
+
continue;
|
|
553
|
+
violations.push({
|
|
554
|
+
file,
|
|
555
|
+
line: inlineType.line,
|
|
556
|
+
column: inlineType.column,
|
|
557
|
+
context: inlineType.context,
|
|
558
|
+
});
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
return violations;
|
|
562
|
+
}
|
|
563
|
+
/**
|
|
564
|
+
* Find all violations in all files (ALL mode).
|
|
565
|
+
*/
|
|
566
|
+
function findViolationsForAll(workspaceRoot) {
|
|
567
|
+
const allFiles = getAllTypeScriptFiles(workspaceRoot);
|
|
568
|
+
return findViolationsForModifiedFiles(workspaceRoot, allFiles);
|
|
569
|
+
}
|
|
570
|
+
/**
|
|
571
|
+
* Auto-detect the base branch by finding the merge-base with origin/main.
|
|
572
|
+
*/
|
|
573
|
+
function detectBase(workspaceRoot) {
|
|
574
|
+
try {
|
|
575
|
+
const mergeBase = (0, child_process_1.execSync)('git merge-base HEAD origin/main', {
|
|
576
|
+
cwd: workspaceRoot,
|
|
577
|
+
encoding: 'utf-8',
|
|
578
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
579
|
+
}).trim();
|
|
580
|
+
if (mergeBase) {
|
|
581
|
+
return mergeBase;
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
catch {
|
|
585
|
+
try {
|
|
586
|
+
const mergeBase = (0, child_process_1.execSync)('git merge-base HEAD main', {
|
|
587
|
+
cwd: workspaceRoot,
|
|
588
|
+
encoding: 'utf-8',
|
|
589
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
590
|
+
}).trim();
|
|
591
|
+
if (mergeBase) {
|
|
592
|
+
return mergeBase;
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
catch {
|
|
596
|
+
// Ignore
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
return null;
|
|
600
|
+
}
|
|
601
|
+
/**
|
|
602
|
+
* Report violations to console.
|
|
603
|
+
*/
|
|
604
|
+
function reportViolations(violations, mode) {
|
|
605
|
+
console.error('');
|
|
606
|
+
console.error('ā Inline type literals found! Use named types instead.');
|
|
607
|
+
console.error('');
|
|
608
|
+
console.error('š Named types improve code clarity and reusability:');
|
|
609
|
+
console.error('');
|
|
610
|
+
console.error(' BAD: function foo(arg: { x: number }) { }');
|
|
611
|
+
console.error(' GOOD: type MyConfig = { x: number };');
|
|
612
|
+
console.error(' function foo(arg: MyConfig) { }');
|
|
613
|
+
console.error('');
|
|
614
|
+
console.error(' BAD: type Nullable = { x: number } | null;');
|
|
615
|
+
console.error(' GOOD: type MyData = { x: number };');
|
|
616
|
+
console.error(' type Nullable = MyData | null;');
|
|
617
|
+
console.error('');
|
|
618
|
+
for (const v of violations) {
|
|
619
|
+
console.error(` ā ${v.file}:${v.line}:${v.column}`);
|
|
620
|
+
console.error(` ${v.context}`);
|
|
621
|
+
}
|
|
622
|
+
console.error('');
|
|
623
|
+
console.error(' To fix: Extract inline types to named type aliases or interfaces');
|
|
624
|
+
console.error('');
|
|
625
|
+
console.error(' Escape hatch (use sparingly):');
|
|
626
|
+
console.error(' // webpieces-disable no-inline-types -- [your reason]');
|
|
627
|
+
console.error('');
|
|
628
|
+
console.error(` Current mode: ${mode}`);
|
|
629
|
+
console.error('');
|
|
630
|
+
}
|
|
631
|
+
async function runExecutor(options, context) {
|
|
632
|
+
const workspaceRoot = context.root;
|
|
633
|
+
const mode = options.mode ?? 'OFF';
|
|
634
|
+
if (mode === 'OFF') {
|
|
635
|
+
console.log('\nāļø Skipping no-inline-types validation (mode: OFF)');
|
|
636
|
+
console.log('');
|
|
637
|
+
return { success: true };
|
|
638
|
+
}
|
|
639
|
+
console.log('\nš Validating No Inline Types\n');
|
|
640
|
+
console.log(` Mode: ${mode}`);
|
|
641
|
+
let violations = [];
|
|
642
|
+
if (mode === 'ALL') {
|
|
643
|
+
console.log(' Scope: All tracked TypeScript files');
|
|
644
|
+
console.log('');
|
|
645
|
+
violations = findViolationsForAll(workspaceRoot);
|
|
646
|
+
}
|
|
647
|
+
else {
|
|
648
|
+
let base = process.env['NX_BASE'];
|
|
649
|
+
const head = process.env['NX_HEAD'];
|
|
650
|
+
if (!base) {
|
|
651
|
+
base = detectBase(workspaceRoot) ?? undefined;
|
|
652
|
+
if (!base) {
|
|
653
|
+
console.log('\nāļø Skipping no-inline-types validation (could not detect base branch)');
|
|
654
|
+
console.log('');
|
|
655
|
+
return { success: true };
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
console.log(` Base: ${base}`);
|
|
659
|
+
console.log(` Head: ${head ?? 'working tree (includes uncommitted changes)'}`);
|
|
660
|
+
console.log('');
|
|
661
|
+
const changedFiles = getChangedTypeScriptFiles(workspaceRoot, base, head);
|
|
662
|
+
if (changedFiles.length === 0) {
|
|
663
|
+
console.log('ā
No TypeScript files changed');
|
|
664
|
+
return { success: true };
|
|
665
|
+
}
|
|
666
|
+
console.log(`š Checking ${changedFiles.length} changed file(s)...`);
|
|
667
|
+
if (mode === 'NEW_METHODS') {
|
|
668
|
+
violations = findViolationsForNewMethods(workspaceRoot, changedFiles, base, head);
|
|
669
|
+
}
|
|
670
|
+
else if (mode === 'MODIFIED_AND_NEW_METHODS') {
|
|
671
|
+
violations = findViolationsForModifiedAndNewMethods(workspaceRoot, changedFiles, base, head);
|
|
672
|
+
}
|
|
673
|
+
else if (mode === 'MODIFIED_FILES') {
|
|
674
|
+
violations = findViolationsForModifiedFiles(workspaceRoot, changedFiles);
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
if (violations.length === 0) {
|
|
678
|
+
console.log('ā
No inline type literals found');
|
|
679
|
+
return { success: true };
|
|
680
|
+
}
|
|
681
|
+
reportViolations(violations, mode);
|
|
682
|
+
return { success: false };
|
|
683
|
+
}
|
|
684
|
+
//# sourceMappingURL=executor.js.map
|