@interlace/eslint-devkit 1.4.4 → 1.6.0
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/package.json +22 -12
- package/src/aria-definitions.js +0 -14
- package/src/ast/ast-utils.js +0 -72
- package/src/ast/index.js +0 -6
- package/src/ast-node-types.d.ts +29 -0
- package/src/ast-node-types.js +174 -0
- package/src/index.d.ts +11 -2
- package/src/index.js +5 -38
- package/src/messaging/config.js +0 -88
- package/src/messaging/constants.js +20 -142
- package/src/messaging/formatters-ai.js +0 -89
- package/src/messaging/formatters-v2.js +0 -151
- package/src/messaging/formatters.js +2 -145
- package/src/messaging/index.js +0 -6
- package/src/messaging/types-v2.js +0 -5
- package/src/messaging/types.js +0 -5
- package/src/node/fs.js +0 -61
- package/src/node/index.js +0 -6
- package/src/node/os.js +0 -33
- package/src/node/path.js +0 -126
- package/src/resolver/dependency-analysis.js +64 -562
- package/src/resolver/index.js +0 -6
- package/src/resolver/resolver-adapter.js +0 -39
- package/src/resolver/resolver-detection.js +0 -48
- package/src/resolver/resolver.d.ts +11 -0
- package/src/resolver/resolver.js +49 -112
- package/src/resolver.js +0 -5
- package/src/rule-creation/index.d.ts +1 -0
- package/src/rule-creation/index.js +1 -6
- package/src/rule-creation/mock-context.d.ts +5 -0
- package/src/rule-creation/mock-context.js +1 -9
- package/src/rule-creation/rule-creator.d.ts +59 -10
- package/src/rule-creation/rule-creator.js +64 -40
- package/src/rule-creation/sql-injection-rule.d.ts +68 -0
- package/src/rule-creation/sql-injection-rule.js +116 -0
- package/src/security/index.js +0 -6
- package/src/security/security-utils.js +9 -287
- package/src/types/index.js +0 -9
- package/src/types/meta-augmentation.js +0 -5
- package/src/types/type-utils.d.ts +1 -1
- package/src/types/type-utils.js +34 -145
- package/AGENTS.md +0 -151
- package/CHANGELOG.md +0 -153
|
@@ -1,9 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* Copyright (c) 2025 Ofri Peretz
|
|
4
|
-
* Licensed under the MIT License. Use of this source code is governed by the
|
|
5
|
-
* MIT license that can be found in the LICENSE file.
|
|
6
|
-
*/
|
|
7
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
3
|
exports.formatLLMMessage = formatLLMMessage;
|
|
9
4
|
exports.formatLLMMessageTemplate = formatLLMMessageTemplate;
|
|
@@ -15,12 +10,6 @@ exports.clearMessageTemplates = clearMessageTemplates;
|
|
|
15
10
|
exports.formatWithTemplate = formatWithTemplate;
|
|
16
11
|
exports.toSARIF = toSARIF;
|
|
17
12
|
const constants_1 = require("./constants");
|
|
18
|
-
// ============================================================================
|
|
19
|
-
// CORE FORMATTING FUNCTIONS
|
|
20
|
-
// ============================================================================
|
|
21
|
-
/**
|
|
22
|
-
* Auto-enriches options with security benchmark data based on CWE
|
|
23
|
-
*/
|
|
24
13
|
function enrichFromCWE(options) {
|
|
25
14
|
if (!options.cwe)
|
|
26
15
|
return options;
|
|
@@ -34,78 +23,34 @@ function enrichFromCWE(options) {
|
|
|
34
23
|
compliance: options.compliance ?? constants_1.CWE_COMPLIANCE_MAPPING[options.cwe],
|
|
35
24
|
};
|
|
36
25
|
}
|
|
37
|
-
/**
|
|
38
|
-
* Creates an LLM-optimized error message with comprehensive security benchmarks.
|
|
39
|
-
*
|
|
40
|
-
* Format (compact, ~2 lines):
|
|
41
|
-
* Line 1: [Icon] [CWE] [OWASP] [CVSS] | [Description] | [SEVERITY] [Compliance]
|
|
42
|
-
* Line 2: Fix: [instruction] | [doc-link]
|
|
43
|
-
*
|
|
44
|
-
* @param options - Message configuration options
|
|
45
|
-
* @returns Formatted error message string
|
|
46
|
-
*
|
|
47
|
-
* @example
|
|
48
|
-
* ```typescript
|
|
49
|
-
* const message = formatLLMMessage({
|
|
50
|
-
* icon: '🔒',
|
|
51
|
-
* issueName: 'SQL Injection',
|
|
52
|
-
* cwe: 'CWE-89',
|
|
53
|
-
* description: 'SQL Injection detected',
|
|
54
|
-
* severity: 'CRITICAL',
|
|
55
|
-
* fix: 'Use parameterized query: db.query("SELECT * FROM users WHERE id = ?", [userId])',
|
|
56
|
-
* documentationLink: 'https://owasp.org/www-community/attacks/SQL_Injection'
|
|
57
|
-
* });
|
|
58
|
-
* // Returns:
|
|
59
|
-
* // 🔒 CWE-89 OWASP:A05-Injection CVSS:9.8 | SQL Injection | CRITICAL [SOC2,PCI-DSS,HIPAA]
|
|
60
|
-
* // Fix: Use parameterized query: db.query("SELECT * FROM users WHERE id = ?", [userId]) | https://owasp.org/www-community/attacks/SQL_Injection
|
|
61
|
-
* ```
|
|
62
|
-
*/
|
|
63
26
|
function formatLLMMessage(options) {
|
|
64
|
-
// Enrich with security benchmark data from CWE
|
|
65
27
|
const enriched = enrichFromCWE(options);
|
|
66
28
|
const { icon, cwe, description, severity, fix, documentationLink } = enriched;
|
|
67
29
|
const owasp = enriched.owasp;
|
|
68
30
|
const cvss = enriched.cvss;
|
|
69
31
|
const compliance = enriched.compliance;
|
|
70
|
-
// Build standards reference string (labeled for LLM + human clarity)
|
|
71
32
|
const standards = [];
|
|
72
33
|
if (cwe)
|
|
73
34
|
standards.push(cwe);
|
|
74
35
|
if (owasp) {
|
|
75
|
-
|
|
76
|
-
const owaspCode = owasp.split(':')[0]; // "A05" from "A05:2025"
|
|
36
|
+
const owaspCode = owasp.split(':')[0];
|
|
77
37
|
const owaspDetails = constants_1.OWASP_DETAILS[owasp];
|
|
78
|
-
const owaspName = owaspDetails?.name?.split(' ')[0] || '';
|
|
38
|
+
const owaspName = owaspDetails?.name?.split(' ')[0] || '';
|
|
79
39
|
standards.push(`OWASP:${owaspCode}-${owaspName}`);
|
|
80
40
|
}
|
|
81
41
|
if (cvss !== undefined)
|
|
82
42
|
standards.push(`CVSS:${cvss}`);
|
|
83
43
|
const standardsPart = standards.length > 0 ? `${standards.join(' ')} | ` : '';
|
|
84
|
-
// Build compliance tags (compact)
|
|
85
44
|
const compliancePart = compliance && compliance.length > 0
|
|
86
45
|
? ` [${compliance.slice(0, 4).join(',')}]`
|
|
87
46
|
: '';
|
|
88
|
-
// Line 1: Icon + Standards + Description + Severity + Compliance
|
|
89
47
|
const firstLine = `${icon} ${standardsPart}${description} | ${severity}${compliancePart}`;
|
|
90
|
-
// Line 2: Fix instruction + documentation link
|
|
91
48
|
const secondLine = ` Fix: ${fix} | ${documentationLink}`;
|
|
92
49
|
return `${firstLine}\n${secondLine}`;
|
|
93
50
|
}
|
|
94
|
-
/**
|
|
95
|
-
* Helper function for creating LLM-optimized messages with template variables.
|
|
96
|
-
* @param options - Message configuration options (supports template variables)
|
|
97
|
-
* @returns Formatted error message string with template variables
|
|
98
|
-
*/
|
|
99
51
|
function formatLLMMessageTemplate(options) {
|
|
100
52
|
return formatLLMMessage(options);
|
|
101
53
|
}
|
|
102
|
-
/**
|
|
103
|
-
* Get enriched security benchmark data for a CWE
|
|
104
|
-
* Useful for programmatic access to OWASP/CVSS data
|
|
105
|
-
*
|
|
106
|
-
* @param cwe - CWE identifier (e.g., 'CWE-89')
|
|
107
|
-
* @returns Security benchmark data or undefined
|
|
108
|
-
*/
|
|
109
54
|
function getSecurityBenchmarks(cwe) {
|
|
110
55
|
const cweData = constants_1.CWE_MAPPING[cwe];
|
|
111
56
|
if (!cweData)
|
|
@@ -121,107 +66,37 @@ function getSecurityBenchmarks(cwe) {
|
|
|
121
66
|
compliance: constants_1.CWE_COMPLIANCE_MAPPING[cwe] ?? [],
|
|
122
67
|
};
|
|
123
68
|
}
|
|
124
|
-
// ============================================================================
|
|
125
|
-
// CUSTOM MESSAGE TEMPLATES FOR ORGANIZATIONS
|
|
126
|
-
// ============================================================================
|
|
127
|
-
/**
|
|
128
|
-
* Global message template registry
|
|
129
|
-
* Organizations can register their custom templates here
|
|
130
|
-
*/
|
|
131
69
|
const templateRegistry = new Map();
|
|
132
|
-
/**
|
|
133
|
-
* Register a custom message template for your organization
|
|
134
|
-
*
|
|
135
|
-
* @example
|
|
136
|
-
* ```typescript
|
|
137
|
-
* // In your ESLint plugin setup
|
|
138
|
-
* import { registerMessageTemplate } from '@interlace/eslint-devkit';
|
|
139
|
-
*
|
|
140
|
-
* registerMessageTemplate('my-company', {
|
|
141
|
-
* organizationName: 'Acme Corp',
|
|
142
|
-
* line1Format: '{{icon}} [{{severity}}] {{description}} ({{cwe}})',
|
|
143
|
-
* line2Format: ' Fix: {{fix}} | Docs: {{confluence}} | Jira: {{jira}}',
|
|
144
|
-
* jiraTemplate: 'https://jira.acme.com/create?summary={{summary}}&project=SEC',
|
|
145
|
-
* confluenceTemplate: 'https://confluence.acme.com/security/{{cwe}}',
|
|
146
|
-
* customCompliance: ['ACME-SEC-001', 'ACME-DATA-002'],
|
|
147
|
-
* });
|
|
148
|
-
* ```
|
|
149
|
-
*/
|
|
150
70
|
function registerMessageTemplate(name, config) {
|
|
151
71
|
templateRegistry.set(name, config);
|
|
152
72
|
}
|
|
153
|
-
/**
|
|
154
|
-
* Get a registered message template
|
|
155
|
-
*/
|
|
156
73
|
function getMessageTemplate(name) {
|
|
157
74
|
return templateRegistry.get(name);
|
|
158
75
|
}
|
|
159
|
-
/**
|
|
160
|
-
* List all registered message templates
|
|
161
|
-
*/
|
|
162
76
|
function listMessageTemplates() {
|
|
163
77
|
return Array.from(templateRegistry.keys());
|
|
164
78
|
}
|
|
165
|
-
/**
|
|
166
|
-
* Clear all registered templates (useful for testing)
|
|
167
|
-
*/
|
|
168
79
|
function clearMessageTemplates() {
|
|
169
80
|
templateRegistry.clear();
|
|
170
81
|
}
|
|
171
|
-
/**
|
|
172
|
-
* Replace template placeholders with actual values
|
|
173
|
-
*/
|
|
174
82
|
function replacePlaceholders(template, values) {
|
|
175
83
|
return template.replace(/\{\{(\w+)\}\}/g, (match, key) => {
|
|
176
84
|
return values[key] ?? match;
|
|
177
85
|
});
|
|
178
86
|
}
|
|
179
|
-
/**
|
|
180
|
-
* Format an LLM message using a custom organization template
|
|
181
|
-
*
|
|
182
|
-
* @example
|
|
183
|
-
* ```typescript
|
|
184
|
-
* import { formatWithTemplate, registerMessageTemplate } from '@interlace/eslint-devkit';
|
|
185
|
-
*
|
|
186
|
-
* // Register your template once
|
|
187
|
-
* registerMessageTemplate('my-company', {
|
|
188
|
-
* jiraTemplate: 'https://jira.company.com/create?summary={{summary}}',
|
|
189
|
-
* line2Format: ' Fix: {{fix}} | Create ticket: {{jira}}',
|
|
190
|
-
* });
|
|
191
|
-
*
|
|
192
|
-
* // Use in rules
|
|
193
|
-
* const message = formatWithTemplate({
|
|
194
|
-
* templateName: 'my-company',
|
|
195
|
-
* icon: MessageIcons.SECURITY,
|
|
196
|
-
* cwe: 'CWE-89',
|
|
197
|
-
* description: 'SQL Injection detected',
|
|
198
|
-
* severity: 'CRITICAL',
|
|
199
|
-
* fix: 'Use parameterized queries',
|
|
200
|
-
* documentationLink: 'https://owasp.org/...',
|
|
201
|
-
* jiraData: {
|
|
202
|
-
* summary: 'SQL Injection in user query',
|
|
203
|
-
* priority: 'Critical',
|
|
204
|
-
* },
|
|
205
|
-
* });
|
|
206
|
-
* ```
|
|
207
|
-
*/
|
|
208
87
|
function formatWithTemplate(options) {
|
|
209
|
-
// Get template configuration
|
|
210
88
|
const templateConfig = options.templateConfig ??
|
|
211
89
|
(options.templateName
|
|
212
90
|
? getMessageTemplate(options.templateName)
|
|
213
91
|
: undefined);
|
|
214
|
-
// If no template, use default formatting
|
|
215
92
|
if (!templateConfig) {
|
|
216
93
|
return formatLLMMessage(options);
|
|
217
94
|
}
|
|
218
|
-
// Enrich with security benchmark data
|
|
219
95
|
const enriched = enrichFromCWE(options);
|
|
220
96
|
const owasp = enriched.owasp;
|
|
221
97
|
const cvss = enriched.cvss ??
|
|
222
98
|
(0, constants_1.severityToCVSS)(enriched.severity);
|
|
223
99
|
const compliance = enriched.compliance ?? [];
|
|
224
|
-
// Build Jira URL if template provided
|
|
225
100
|
let jiraUrl = '';
|
|
226
101
|
if (templateConfig.jiraTemplate && options.jiraData) {
|
|
227
102
|
jiraUrl = replacePlaceholders(templateConfig.jiraTemplate, {
|
|
@@ -232,7 +107,6 @@ function formatWithTemplate(options) {
|
|
|
232
107
|
description: encodeURIComponent(options.description),
|
|
233
108
|
});
|
|
234
109
|
}
|
|
235
|
-
// Build Confluence URL if template provided
|
|
236
110
|
let confluenceUrl = '';
|
|
237
111
|
if (templateConfig.confluenceTemplate) {
|
|
238
112
|
confluenceUrl = replacePlaceholders(templateConfig.confluenceTemplate, {
|
|
@@ -240,15 +114,12 @@ function formatWithTemplate(options) {
|
|
|
240
114
|
owasp: owasp ?? '',
|
|
241
115
|
});
|
|
242
116
|
}
|
|
243
|
-
// Build OWASP display string
|
|
244
117
|
const owaspDisplay = owasp ? `OWASP:${owasp.split(':')[0]}` : '';
|
|
245
|
-
// Build compliance display string (including custom compliance)
|
|
246
118
|
const allCompliance = [
|
|
247
119
|
...compliance,
|
|
248
120
|
...(templateConfig.customCompliance ?? []),
|
|
249
121
|
];
|
|
250
122
|
const complianceDisplay = allCompliance.length > 0 ? `[${allCompliance.slice(0, 4).join(',')}]` : '';
|
|
251
|
-
// Prepare all placeholder values
|
|
252
123
|
const placeholderValues = {
|
|
253
124
|
icon: enriched.icon,
|
|
254
125
|
cwe: enriched.cwe,
|
|
@@ -265,7 +136,6 @@ function formatWithTemplate(options) {
|
|
|
265
136
|
...templateConfig.customMetadata,
|
|
266
137
|
...options.customPlaceholders,
|
|
267
138
|
};
|
|
268
|
-
// Format lines using templates
|
|
269
139
|
const line1Template = templateConfig.line1Format ??
|
|
270
140
|
'{{icon}} {{cwe}} {{owasp}} {{cvss}} | {{description}} | {{severity}} {{compliance}}';
|
|
271
141
|
const line2Template = templateConfig.line2Format ?? ' Fix: {{fix}} | {{documentation}}';
|
|
@@ -281,16 +151,6 @@ function formatWithTemplate(options) {
|
|
|
281
151
|
.trim();
|
|
282
152
|
return `${line1}\n${line2}`;
|
|
283
153
|
}
|
|
284
|
-
// ============================================================================
|
|
285
|
-
// SARIF OUTPUT (Enterprise Security Tool Integration)
|
|
286
|
-
// ============================================================================
|
|
287
|
-
/**
|
|
288
|
-
* Converts message options to SARIF format for security tool integration
|
|
289
|
-
* Supports: GitHub Advanced Security, Snyk, SonarQube, Checkmarx
|
|
290
|
-
*
|
|
291
|
-
* @param options - Message options
|
|
292
|
-
* @returns SARIF-compatible result object
|
|
293
|
-
*/
|
|
294
154
|
function toSARIF(options) {
|
|
295
155
|
const enriched = enrichFromCWE(options);
|
|
296
156
|
return {
|
|
@@ -310,9 +170,6 @@ function toSARIF(options) {
|
|
|
310
170
|
},
|
|
311
171
|
};
|
|
312
172
|
}
|
|
313
|
-
/**
|
|
314
|
-
* Convert severity to SARIF level
|
|
315
|
-
*/
|
|
316
173
|
function severityToSARIFLevel(severity) {
|
|
317
174
|
switch (severity) {
|
|
318
175
|
case 'CRITICAL':
|
package/src/messaging/index.js
CHANGED
|
@@ -1,12 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* Copyright (c) 2025 Ofri Peretz
|
|
4
|
-
* Licensed under the MIT License. Use of this source code is governed by the
|
|
5
|
-
* MIT license that can be found in the LICENSE file.
|
|
6
|
-
*/
|
|
7
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
3
|
const tslib_1 = require("tslib");
|
|
9
|
-
// LLM message formatting utilities - types, constants, and runtime functions
|
|
10
4
|
tslib_1.__exportStar(require("./types"), exports);
|
|
11
5
|
tslib_1.__exportStar(require("./constants"), exports);
|
|
12
6
|
tslib_1.__exportStar(require("./formatters"), exports);
|
package/src/messaging/types.js
CHANGED
package/src/node/fs.js
CHANGED
|
@@ -1,9 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* Copyright (c) 2025 Ofri Peretz
|
|
4
|
-
* Licensed under the MIT License. Use of this source code is governed by the
|
|
5
|
-
* MIT license that can be found in the LICENSE file.
|
|
6
|
-
*/
|
|
7
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
3
|
exports.fileExistsSync = fileExistsSync;
|
|
9
4
|
exports.readFileSync = readFileSync;
|
|
@@ -13,32 +8,11 @@ exports.mkdirSync = mkdirSync;
|
|
|
13
8
|
exports.writeFileSync = writeFileSync;
|
|
14
9
|
exports.findFileUpward = findFileUpward;
|
|
15
10
|
const tslib_1 = require("tslib");
|
|
16
|
-
/**
|
|
17
|
-
* Node.js 'fs' module wrapper and utilities
|
|
18
|
-
*
|
|
19
|
-
* All file system operations should go through these wrappers for:
|
|
20
|
-
* - Testability (can mock these functions)
|
|
21
|
-
* - Code coverage (all operations tracked)
|
|
22
|
-
* - Consistency (error handling, return types)
|
|
23
|
-
*/
|
|
24
11
|
const fs = tslib_1.__importStar(require("node:fs"));
|
|
25
12
|
const path_1 = require("./path");
|
|
26
|
-
/**
|
|
27
|
-
* Check if a file exists (uncached)
|
|
28
|
-
*
|
|
29
|
-
* @param filePath - Path to the file
|
|
30
|
-
* @returns true if file exists
|
|
31
|
-
*/
|
|
32
13
|
function fileExistsSync(filePath) {
|
|
33
14
|
return fs.existsSync(filePath);
|
|
34
15
|
}
|
|
35
|
-
/**
|
|
36
|
-
* Read file content synchronously (uncached)
|
|
37
|
-
*
|
|
38
|
-
* @param filePath - Path to the file
|
|
39
|
-
* @param encoding - File encoding (default: 'utf-8')
|
|
40
|
-
* @returns File content or null if file doesn't exist or can't be read
|
|
41
|
-
*/
|
|
42
16
|
function readFileSync(filePath, encoding = 'utf-8') {
|
|
43
17
|
try {
|
|
44
18
|
return fs.readFileSync(filePath, encoding);
|
|
@@ -47,12 +21,6 @@ function readFileSync(filePath, encoding = 'utf-8') {
|
|
|
47
21
|
return null;
|
|
48
22
|
}
|
|
49
23
|
}
|
|
50
|
-
/**
|
|
51
|
-
* Read and parse JSON file synchronously
|
|
52
|
-
*
|
|
53
|
-
* @param filePath - Path to the JSON file
|
|
54
|
-
* @returns Parsed JSON object or null if file doesn't exist or is invalid JSON
|
|
55
|
-
*/
|
|
56
24
|
function readJsonFileSync(filePath) {
|
|
57
25
|
const content = readFileSync(filePath);
|
|
58
26
|
if (content === null) {
|
|
@@ -65,12 +33,6 @@ function readJsonFileSync(filePath) {
|
|
|
65
33
|
return null;
|
|
66
34
|
}
|
|
67
35
|
}
|
|
68
|
-
/**
|
|
69
|
-
* Get file stats synchronously
|
|
70
|
-
*
|
|
71
|
-
* @param filePath - Path to the file
|
|
72
|
-
* @returns File stats or null if file doesn't exist or can't be read
|
|
73
|
-
*/
|
|
74
36
|
function statSync(filePath) {
|
|
75
37
|
try {
|
|
76
38
|
return fs.statSync(filePath);
|
|
@@ -79,13 +41,6 @@ function statSync(filePath) {
|
|
|
79
41
|
return null;
|
|
80
42
|
}
|
|
81
43
|
}
|
|
82
|
-
/**
|
|
83
|
-
* Create directory synchronously (with parent directories if needed)
|
|
84
|
-
*
|
|
85
|
-
* @param dirPath - Path to the directory
|
|
86
|
-
* @param options - Options for directory creation
|
|
87
|
-
* @returns true if directory was created or already exists, false on error
|
|
88
|
-
*/
|
|
89
44
|
function mkdirSync(dirPath, options) {
|
|
90
45
|
try {
|
|
91
46
|
fs.mkdirSync(dirPath, { recursive: true, ...options });
|
|
@@ -95,14 +50,6 @@ function mkdirSync(dirPath, options) {
|
|
|
95
50
|
return false;
|
|
96
51
|
}
|
|
97
52
|
}
|
|
98
|
-
/**
|
|
99
|
-
* Write file content synchronously
|
|
100
|
-
*
|
|
101
|
-
* @param filePath - Path to the file
|
|
102
|
-
* @param content - Content to write
|
|
103
|
-
* @param encoding - File encoding (default: 'utf-8')
|
|
104
|
-
* @returns true if write succeeded, false on error
|
|
105
|
-
*/
|
|
106
53
|
function writeFileSync(filePath, content, encoding = 'utf-8') {
|
|
107
54
|
try {
|
|
108
55
|
fs.writeFileSync(filePath, content, encoding);
|
|
@@ -112,17 +59,9 @@ function writeFileSync(filePath, content, encoding = 'utf-8') {
|
|
|
112
59
|
return false;
|
|
113
60
|
}
|
|
114
61
|
}
|
|
115
|
-
/**
|
|
116
|
-
* Find a file by walking up directories from a starting point
|
|
117
|
-
*
|
|
118
|
-
* @param filename - Name of the file to find (e.g., 'package.json')
|
|
119
|
-
* @param startDir - Directory to start searching from
|
|
120
|
-
* @returns Absolute path to the file or null if not found
|
|
121
|
-
*/
|
|
122
62
|
function findFileUpward(filename, startDir) {
|
|
123
63
|
let currentDir = startDir;
|
|
124
64
|
while (currentDir !== (0, path_1.getDirname)(currentDir)) {
|
|
125
|
-
// Stop at root
|
|
126
65
|
const candidate = (0, path_1.joinPath)(currentDir, filename);
|
|
127
66
|
if (fileExistsSync(candidate)) {
|
|
128
67
|
return candidate;
|
package/src/node/index.js
CHANGED
|
@@ -1,12 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* Copyright (c) 2025 Ofri Peretz
|
|
4
|
-
* Licensed under the MIT License. Use of this source code is governed by the
|
|
5
|
-
* MIT license that can be found in the LICENSE file.
|
|
6
|
-
*/
|
|
7
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
3
|
const tslib_1 = require("tslib");
|
|
9
|
-
// Node.js utilities for cross-platform compatibility and testability
|
|
10
4
|
tslib_1.__exportStar(require("./fs"), exports);
|
|
11
5
|
tslib_1.__exportStar(require("./os"), exports);
|
|
12
6
|
tslib_1.__exportStar(require("./path"), exports);
|
package/src/node/os.js
CHANGED
|
@@ -1,53 +1,20 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* Copyright (c) 2025 Ofri Peretz
|
|
4
|
-
* Licensed under the MIT License. Use of this source code is governed by the
|
|
5
|
-
* MIT license that can be found in the LICENSE file.
|
|
6
|
-
*/
|
|
7
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
3
|
exports.getEOL = getEOL;
|
|
9
4
|
exports.getPlatform = getPlatform;
|
|
10
5
|
exports.getArch = getArch;
|
|
11
6
|
exports.getTmpDir = getTmpDir;
|
|
12
7
|
const tslib_1 = require("tslib");
|
|
13
|
-
/**
|
|
14
|
-
* Node.js 'os' module wrapper and utilities
|
|
15
|
-
*
|
|
16
|
-
* All OS operations should go through these wrappers for:
|
|
17
|
-
* - Testability (can mock these functions)
|
|
18
|
-
* - Code coverage (all operations tracked)
|
|
19
|
-
* - Consistency (error handling, return types)
|
|
20
|
-
*/
|
|
21
8
|
const os = tslib_1.__importStar(require("node:os"));
|
|
22
|
-
/**
|
|
23
|
-
* Get the platform-specific end-of-line marker
|
|
24
|
-
*
|
|
25
|
-
* @returns Platform EOL string ('\n' on Unix, '\r\n' on Windows)
|
|
26
|
-
*/
|
|
27
9
|
function getEOL() {
|
|
28
10
|
return os.EOL;
|
|
29
11
|
}
|
|
30
|
-
/**
|
|
31
|
-
* Get the operating system platform
|
|
32
|
-
*
|
|
33
|
-
* @returns Platform string ('darwin', 'linux', 'win32', etc.)
|
|
34
|
-
*/
|
|
35
12
|
function getPlatform() {
|
|
36
13
|
return os.platform();
|
|
37
14
|
}
|
|
38
|
-
/**
|
|
39
|
-
* Get the CPU architecture
|
|
40
|
-
*
|
|
41
|
-
* @returns Architecture string ('x64', 'arm64', etc.)
|
|
42
|
-
*/
|
|
43
15
|
function getArch() {
|
|
44
16
|
return os.arch();
|
|
45
17
|
}
|
|
46
|
-
/**
|
|
47
|
-
* Get the operating system's default directory for temporary files
|
|
48
|
-
*
|
|
49
|
-
* @returns Path to the temporary directory
|
|
50
|
-
*/
|
|
51
18
|
function getTmpDir() {
|
|
52
19
|
return os.tmpdir();
|
|
53
20
|
}
|
package/src/node/path.js
CHANGED
|
@@ -1,9 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* Copyright (c) 2025 Ofri Peretz
|
|
4
|
-
* Licensed under the MIT License. Use of this source code is governed by the
|
|
5
|
-
* MIT license that can be found in the LICENSE file.
|
|
6
|
-
*/
|
|
7
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
3
|
exports.createPatternCache = createPatternCache;
|
|
9
4
|
exports.patternToRegex = patternToRegex;
|
|
@@ -20,40 +15,15 @@ exports.joinPath = joinPath;
|
|
|
20
15
|
exports.resolvePath = resolvePath;
|
|
21
16
|
exports.getExtname = getExtname;
|
|
22
17
|
const tslib_1 = require("tslib");
|
|
23
|
-
/**
|
|
24
|
-
* Node.js 'path' module wrapper and utilities
|
|
25
|
-
*
|
|
26
|
-
* All path operations should go through these wrappers for:
|
|
27
|
-
* - Testability (can mock these functions)
|
|
28
|
-
* - Code coverage (all operations tracked)
|
|
29
|
-
* - Consistency (error handling, return types)
|
|
30
|
-
*/
|
|
31
18
|
const path = tslib_1.__importStar(require("node:path"));
|
|
32
|
-
/**
|
|
33
|
-
* Create a new empty pattern cache
|
|
34
|
-
*/
|
|
35
19
|
function createPatternCache() {
|
|
36
20
|
return new Map();
|
|
37
21
|
}
|
|
38
|
-
/**
|
|
39
|
-
* Convert glob pattern to regex
|
|
40
|
-
*
|
|
41
|
-
* @param pattern - Glob pattern (supports **, *, ?)
|
|
42
|
-
* @param cache - Cache for compiled patterns
|
|
43
|
-
* @returns Compiled RegExp
|
|
44
|
-
*/
|
|
45
22
|
function patternToRegex(pattern, cache) {
|
|
46
23
|
const cached = cache.get(pattern);
|
|
47
24
|
if (cached) {
|
|
48
25
|
return cached;
|
|
49
26
|
}
|
|
50
|
-
// Translate the glob in a single left-to-right pass: escape every regex
|
|
51
|
-
// metacharacter, then map the glob wildcards (**, *, ?) to their regex
|
|
52
|
-
// equivalents. Doing it in one pass means an escaped metacharacter can never
|
|
53
|
-
// be re-interpreted as a wildcard. The previous chained-replace only handled
|
|
54
|
-
// `. * ?`, leaving `\ + ( ) [ ] { } ^ $ |` to leak through as regex syntax
|
|
55
|
-
// (CWE-116, incomplete sanitization) — e.g. the glob `a+b` compiled to the
|
|
56
|
-
// quantifier `a+b` instead of matching the literal `a+b`.
|
|
57
27
|
let escaped = '';
|
|
58
28
|
for (let i = 0; i < pattern.length; i++) {
|
|
59
29
|
const ch = pattern[i];
|
|
@@ -80,14 +50,6 @@ function patternToRegex(pattern, cache) {
|
|
|
80
50
|
cache.set(pattern, regex);
|
|
81
51
|
return regex;
|
|
82
52
|
}
|
|
83
|
-
/**
|
|
84
|
-
* Check if file should be ignored based on patterns
|
|
85
|
-
*
|
|
86
|
-
* @param file - File path to check
|
|
87
|
-
* @param patterns - Array of glob patterns
|
|
88
|
-
* @param cache - Cache for compiled patterns
|
|
89
|
-
* @returns true if file matches any ignore pattern
|
|
90
|
-
*/
|
|
91
53
|
function shouldIgnoreFile(file, patterns, cache) {
|
|
92
54
|
const normalizedFile = path.normalize(file);
|
|
93
55
|
return patterns.some((pattern) => {
|
|
@@ -95,47 +57,20 @@ function shouldIgnoreFile(file, patterns, cache) {
|
|
|
95
57
|
return regex.test(normalizedFile);
|
|
96
58
|
});
|
|
97
59
|
}
|
|
98
|
-
/**
|
|
99
|
-
* Check if file is a barrel export (index file)
|
|
100
|
-
*
|
|
101
|
-
* @param file - File path to check
|
|
102
|
-
* @param barrelExports - List of barrel export filenames
|
|
103
|
-
* @returns true if file is a barrel export
|
|
104
|
-
*/
|
|
105
60
|
function isBarrelExport(file, barrelExports) {
|
|
106
61
|
const basename = path.basename(file);
|
|
107
62
|
return barrelExports.includes(basename);
|
|
108
63
|
}
|
|
109
|
-
/**
|
|
110
|
-
* Format a cycle of file paths for concise display
|
|
111
|
-
*
|
|
112
|
-
* Uses ⟷ for bidirectional (2 modules) or → chain for longer cycles.
|
|
113
|
-
*
|
|
114
|
-
* @param cycle - Array of absolute file paths in the cycle
|
|
115
|
-
* @param workspaceRoot - Workspace root for relative path calculation
|
|
116
|
-
* @returns Formatted string representation of the cycle
|
|
117
|
-
*/
|
|
118
64
|
function formatCycleDisplay(cycle, workspaceRoot) {
|
|
119
65
|
const formatted = cycle.map((file) => {
|
|
120
66
|
const relative = path.relative(workspaceRoot, file);
|
|
121
67
|
return relative.replace(/\\/g, '/');
|
|
122
68
|
});
|
|
123
|
-
// Use ⟷ for bidirectional (2 modules) or → chain for more
|
|
124
69
|
if (formatted.length === 2) {
|
|
125
70
|
return `${formatted[0]} ⟷ ${formatted[1]}`;
|
|
126
71
|
}
|
|
127
72
|
return formatted.join(' → ') + ` → ${formatted[0]}`;
|
|
128
73
|
}
|
|
129
|
-
/**
|
|
130
|
-
* Get module names from a cycle of file paths
|
|
131
|
-
*
|
|
132
|
-
* Extracts human-readable module names from file paths.
|
|
133
|
-
* For index files, uses the directory name instead.
|
|
134
|
-
*
|
|
135
|
-
* @param cycle - Array of absolute file paths in the cycle
|
|
136
|
-
* @param workspaceRoot - Workspace root for relative path calculation
|
|
137
|
-
* @returns Array of module names
|
|
138
|
-
*/
|
|
139
74
|
function getModuleNames(cycle, workspaceRoot) {
|
|
140
75
|
return cycle.map((file) => {
|
|
141
76
|
const relative = path.relative(workspaceRoot, file);
|
|
@@ -145,93 +80,32 @@ function getModuleNames(cycle, workspaceRoot) {
|
|
|
145
80
|
: basename;
|
|
146
81
|
});
|
|
147
82
|
}
|
|
148
|
-
/**
|
|
149
|
-
* Generate a relative import path from one file to another
|
|
150
|
-
*
|
|
151
|
-
* Calculates the relative path and ensures it starts with './' or '../'.
|
|
152
|
-
* Also normalizes path separators and removes file extensions.
|
|
153
|
-
*
|
|
154
|
-
* @param fromFile - Absolute path of the importing file
|
|
155
|
-
* @param toFile - Absolute path of the target file
|
|
156
|
-
* @returns Relative import path (e.g., './utils', '../helpers')
|
|
157
|
-
*/
|
|
158
83
|
function getRelativeImportPath(fromFile, toFile) {
|
|
159
84
|
const currentDir = path.dirname(fromFile);
|
|
160
85
|
let relativeImport = path.relative(currentDir, toFile);
|
|
161
|
-
// Ensure starts with ./ or ../
|
|
162
86
|
if (!relativeImport.startsWith('.')) {
|
|
163
87
|
relativeImport = './' + relativeImport;
|
|
164
88
|
}
|
|
165
|
-
// Normalize path separators and remove extension
|
|
166
89
|
return relativeImport.replace(/\\/g, '/').replace(/\.(ts|tsx|js|jsx)$/, '');
|
|
167
90
|
}
|
|
168
|
-
/**
|
|
169
|
-
* Get the basename of a file path
|
|
170
|
-
*
|
|
171
|
-
* Simple wrapper around path.basename for convenience.
|
|
172
|
-
*
|
|
173
|
-
* @param filePath - File path (absolute or relative)
|
|
174
|
-
* @returns Base filename
|
|
175
|
-
*/
|
|
176
91
|
function getBasename(filePath) {
|
|
177
92
|
return path.basename(filePath);
|
|
178
93
|
}
|
|
179
|
-
/**
|
|
180
|
-
* Get the directory name of a file path
|
|
181
|
-
*
|
|
182
|
-
* Simple wrapper around path.dirname for convenience.
|
|
183
|
-
*
|
|
184
|
-
* @param filePath - File path (absolute or relative)
|
|
185
|
-
* @returns Directory path
|
|
186
|
-
*/
|
|
187
94
|
function getDirname(filePath) {
|
|
188
95
|
return path.dirname(filePath);
|
|
189
96
|
}
|
|
190
|
-
/**
|
|
191
|
-
* Calculate relative path from one path to another
|
|
192
|
-
*
|
|
193
|
-
* @param from - Starting path
|
|
194
|
-
* @param to - Target path
|
|
195
|
-
* @returns Relative path
|
|
196
|
-
*/
|
|
197
97
|
function getRelativePath(from, to) {
|
|
198
98
|
return path.relative(from, to);
|
|
199
99
|
}
|
|
200
|
-
/**
|
|
201
|
-
* Normalize a file path
|
|
202
|
-
*
|
|
203
|
-
* Normalizes path separators to forward slashes for consistent display.
|
|
204
|
-
*
|
|
205
|
-
* @param filePath - File path to normalize
|
|
206
|
-
* @returns Normalized path with forward slashes
|
|
207
|
-
*/
|
|
208
100
|
function normalizePath(filePath) {
|
|
209
101
|
return path.normalize(filePath).replace(/\\/g, '/');
|
|
210
102
|
}
|
|
211
|
-
/**
|
|
212
|
-
* Join path segments
|
|
213
|
-
*
|
|
214
|
-
* @param segments - Path segments to join
|
|
215
|
-
* @returns Joined path
|
|
216
|
-
*/
|
|
217
103
|
function joinPath(...segments) {
|
|
218
104
|
return path.join(...segments);
|
|
219
105
|
}
|
|
220
|
-
/**
|
|
221
|
-
* Resolve path segments to absolute path
|
|
222
|
-
*
|
|
223
|
-
* @param segments - Path segments to resolve
|
|
224
|
-
* @returns Absolute path
|
|
225
|
-
*/
|
|
226
106
|
function resolvePath(...segments) {
|
|
227
107
|
return path.resolve(...segments);
|
|
228
108
|
}
|
|
229
|
-
/**
|
|
230
|
-
* Get file extension
|
|
231
|
-
*
|
|
232
|
-
* @param filePath - File path
|
|
233
|
-
* @returns File extension including the dot (e.g., '.ts')
|
|
234
|
-
*/
|
|
235
109
|
function getExtname(filePath) {
|
|
236
110
|
return path.extname(filePath);
|
|
237
111
|
}
|