Skip to content

Commit 87a0e79

Browse files
authored
chore(refactor): moved skipped commented usages to helper (#417)
* chore(refactor): moved skipped commented usages to helper * chore: added space
1 parent 8370c1c commit 87a0e79

2 files changed

Lines changed: 48 additions & 46 deletions

File tree

packages/cli/src/commands/scanUsage.ts

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { scanJsonOutput } from '../ui/scan/scanJsonOutput.js';
1212
import { printMissingExample } from '../ui/scan/printMissingExample.js';
1313
import { processComparisonFile } from '../services/processComparisonFile.js';
1414
import { printComparisonError } from '../ui/scan/printComparisonError.js';
15-
import { hasIgnoreComment } from '../core/security/secretDetectors.js';
15+
import { skipCommentedUsages } from '../core/helpers/skipCommentedUsages.js';
1616
import { frameworkValidator } from '../core/frameworks/frameworkValidator.js';
1717
import { detectSecretsInExample } from '../core/security/exampleSecretDetector.js';
1818
import {
@@ -192,51 +192,6 @@ export async function scanUsage(opts: ScanUsageOptions): Promise<ExitResult> {
192192
return { exitWithError: result.exitWithError };
193193
}
194194

195-
/**
196-
* Filters out commented usages from the list.
197-
* Skipping comments:
198-
* // process.env.API_URL
199-
* # process.env.API_URL
200-
* /* process.env.API_URL
201-
* * process.env.API_URL
202-
* <!-- process.env.API_URL -->
203-
* @param usages - List of environment variable usages
204-
* @returns Filtered list of environment variable usages
205-
*/
206-
function skipCommentedUsages(usages: readonly EnvUsage[]): EnvUsage[] {
207-
let insideHtmlComment = false;
208-
let insideIgnoreBlock = false;
209-
210-
return usages.filter((u) => {
211-
if (!u.context) return true;
212-
const line = u.context.trim();
213-
214-
if (/<!--\s*dotenv[\s-]*diff[\s-]*ignore[\s-]*start\s*-->/i.test(line)) {
215-
insideIgnoreBlock = true;
216-
return false;
217-
}
218-
219-
if (/<!--\s*dotenv[\s-]*diff[\s-]*ignore[\s-]*end\s*-->/i.test(line)) {
220-
insideIgnoreBlock = false;
221-
return false;
222-
}
223-
224-
if (line.includes('<!--')) insideHtmlComment = true;
225-
if (line.includes('-->')) {
226-
insideHtmlComment = false;
227-
return false;
228-
}
229-
230-
if (insideIgnoreBlock) return false;
231-
232-
return (
233-
!insideHtmlComment &&
234-
!/^\s*(\/\/|#|\/\*|\*|<!--|-->)/.test(line) &&
235-
!hasIgnoreComment(line)
236-
);
237-
});
238-
}
239-
240195
/**
241196
* Recalculates statistics for a scan result after filtering usages.
242197
* @param scanResult The current scan result
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import type { EnvUsage } from '../../config/types.js';
2+
import { hasIgnoreComment } from '../../core/security/secretDetectors.js';
3+
4+
/**
5+
* Filters out commented usages from the list.
6+
* Skipping comments:
7+
* // process.env.API_URL
8+
* # process.env.API_URL
9+
* /* process.env.API_URL
10+
* * process.env.API_URL
11+
* <!-- process.env.API_URL -->
12+
* @param usages - List of environment variable usages
13+
* @returns Filtered list of environment variable usages
14+
*/
15+
export function skipCommentedUsages(usages: readonly EnvUsage[]): EnvUsage[] {
16+
let insideHtmlComment = false;
17+
let insideIgnoreBlock = false;
18+
19+
return usages.filter((u) => {
20+
if (!u.context) return true;
21+
const line = u.context.trim();
22+
23+
if (/<!--\s*dotenv[\s-]*diff[\s-]*ignore[\s-]*start\s*-->/i.test(line)) {
24+
insideIgnoreBlock = true;
25+
return false;
26+
}
27+
28+
if (/<!--\s*dotenv[\s-]*diff[\s-]*ignore[\s-]*end\s*-->/i.test(line)) {
29+
insideIgnoreBlock = false;
30+
return false;
31+
}
32+
33+
if (line.includes('<!--')) insideHtmlComment = true;
34+
if (line.includes('-->')) {
35+
insideHtmlComment = false;
36+
return false;
37+
}
38+
39+
if (insideIgnoreBlock) return false;
40+
41+
return (
42+
!insideHtmlComment &&
43+
!/^\s*(\/\/|#|\/\*|\*|<!--|-->)/.test(line) &&
44+
!hasIgnoreComment(line)
45+
);
46+
});
47+
}

0 commit comments

Comments
 (0)