unshit 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +93 -2
- package/bin/unshit.js +10 -0
- package/dist/src/cli.d.ts +1 -0
- package/dist/src/cli.js +119 -0
- package/dist/src/cli.js.map +1 -0
- package/dist/src/config.d.ts +6 -0
- package/dist/src/config.js +45 -0
- package/dist/src/config.js.map +1 -0
- package/dist/src/contracts.d.ts +93 -0
- package/dist/src/contracts.js +2 -0
- package/dist/src/contracts.js.map +1 -0
- package/dist/src/core/fixer.d.ts +18 -0
- package/dist/src/core/fixer.js +120 -0
- package/dist/src/core/fixer.js.map +1 -0
- package/dist/src/core/pinner.d.ts +19 -0
- package/dist/src/core/pinner.js +59 -0
- package/dist/src/core/pinner.js.map +1 -0
- package/dist/src/core/reporters.d.ts +3 -0
- package/dist/src/core/reporters.js +52 -0
- package/dist/src/core/reporters.js.map +1 -0
- package/dist/src/core/scanner.d.ts +13 -0
- package/dist/src/core/scanner.js +45 -0
- package/dist/src/core/scanner.js.map +1 -0
- package/dist/src/core/scorer.d.ts +5 -0
- package/dist/src/core/scorer.js +50 -0
- package/dist/src/core/scorer.js.map +1 -0
- package/dist/src/detectors/clones.d.ts +5 -0
- package/dist/src/detectors/clones.js +93 -0
- package/dist/src/detectors/clones.js.map +1 -0
- package/dist/src/detectors/css.d.ts +3 -0
- package/dist/src/detectors/css.js +127 -0
- package/dist/src/detectors/css.js.map +1 -0
- package/dist/src/detectors/dead-exports.d.ts +5 -0
- package/dist/src/detectors/dead-exports.js +77 -0
- package/dist/src/detectors/dead-exports.js.map +1 -0
- package/dist/src/detectors/god-file.d.ts +2 -0
- package/dist/src/detectors/god-file.js +35 -0
- package/dist/src/detectors/god-file.js.map +1 -0
- package/dist/src/detectors/index.d.ts +2 -0
- package/dist/src/detectors/index.js +18 -0
- package/dist/src/detectors/index.js.map +1 -0
- package/dist/src/detectors/ts-ast.d.ts +6 -0
- package/dist/src/detectors/ts-ast.js +255 -0
- package/dist/src/detectors/ts-ast.js.map +1 -0
- package/dist/src/mcp/server.d.ts +4 -0
- package/dist/src/mcp/server.js +78 -0
- package/dist/src/mcp/server.js.map +1 -0
- package/dist/src/walker.d.ts +5 -0
- package/dist/src/walker.js +60 -0
- package/dist/src/walker.js.map +1 -0
- package/package.json +55 -4
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
export function formatScanOutput(findings, score, format) {
|
|
2
|
+
if (format === 'json') {
|
|
3
|
+
return JSON.stringify({ score, findings }, null, 2);
|
|
4
|
+
}
|
|
5
|
+
if (format === 'sarif') {
|
|
6
|
+
return JSON.stringify({
|
|
7
|
+
$schema: 'https://json.schemastore.org/sarif-2.1.0.json',
|
|
8
|
+
version: '2.1.0',
|
|
9
|
+
runs: [
|
|
10
|
+
{
|
|
11
|
+
tool: { driver: { name: 'unshit', version: '0.1.0' } },
|
|
12
|
+
results: findings.map((f) => ({
|
|
13
|
+
ruleId: f.ruleId,
|
|
14
|
+
level: f.severity === 'error' ? 'error' : f.severity === 'warn' ? 'warning' : 'note',
|
|
15
|
+
message: { text: f.message },
|
|
16
|
+
locations: [
|
|
17
|
+
{
|
|
18
|
+
physicalLocation: {
|
|
19
|
+
artifactLocation: { uri: f.file },
|
|
20
|
+
region: { startLine: f.span.line, startColumn: f.span.column },
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
],
|
|
24
|
+
})),
|
|
25
|
+
},
|
|
26
|
+
],
|
|
27
|
+
}, null, 2);
|
|
28
|
+
}
|
|
29
|
+
if (format === 'markdown') {
|
|
30
|
+
const lines = [
|
|
31
|
+
`# unshit Report — Score: **${score.score}/100 (${score.grade})**`,
|
|
32
|
+
'',
|
|
33
|
+
`Scanned **${score.totalFiles}** files (${score.totalLines} lines) · **${score.totalFindings}** findings`,
|
|
34
|
+
'',
|
|
35
|
+
'| Severity | Rule | Location | Message |',
|
|
36
|
+
'|---|---|---|---|',
|
|
37
|
+
...findings.slice(0, 100).map((f) => `| ${f.severity} | \`${f.ruleId}\` | \`${f.file}:${f.span.line}\` | ${f.message} |`),
|
|
38
|
+
];
|
|
39
|
+
return lines.join('\n');
|
|
40
|
+
}
|
|
41
|
+
const header = [
|
|
42
|
+
`unshit · Health Score: ${score.score}/100 (Grade ${score.grade})`,
|
|
43
|
+
`Files: ${score.totalFiles} | Lines: ${score.totalLines} | Findings: ${score.totalFindings} (error:${score.bySeverity.error}, warn:${score.bySeverity.warn}, info:${score.bySeverity.info})`,
|
|
44
|
+
'',
|
|
45
|
+
];
|
|
46
|
+
const rows = findings.slice(0, 60).map((f) => ` ${f.severity.toUpperCase().padEnd(5)} ${f.ruleId.padEnd(24)} ${f.file}:${f.span.line}:${f.span.column} — ${f.message}`);
|
|
47
|
+
if (findings.length > 60) {
|
|
48
|
+
rows.push(` ... and ${findings.length - 60} more (use --format json or markdown for full list)`);
|
|
49
|
+
}
|
|
50
|
+
return [...header, ...rows].join('\n');
|
|
51
|
+
}
|
|
52
|
+
//# sourceMappingURL=reporters.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reporters.js","sourceRoot":"","sources":["../../../src/core/reporters.ts"],"names":[],"mappings":"AAIA,MAAM,UAAU,gBAAgB,CAC9B,QAAmB,EACnB,KAAqB,EACrB,MAAoB;IAEpB,IAAI,MAAM,KAAK,MAAM,EAAE,CAAC;QACtB,OAAO,IAAI,CAAC,SAAS,CAAC,EAAE,KAAK,EAAE,QAAQ,EAAE,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC;IACtD,CAAC;IAED,IAAI,MAAM,KAAK,OAAO,EAAE,CAAC;QACvB,OAAO,IAAI,CAAC,SAAS,CACnB;YACE,OAAO,EAAE,+CAA+C;YACxD,OAAO,EAAE,OAAO;YAChB,IAAI,EAAE;gBACJ;oBACE,IAAI,EAAE,EAAE,MAAM,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,OAAO,EAAE,OAAO,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;QACF,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAC1B,CAAC;IAED,MAAM,MAAM,GAAG;QACb,0BAA0B,KAAK,CAAC,KAAK,eAAe,KAAK,CAAC,KAAK,GAAG;QAClE,UAAU,KAAK,CAAC,UAAU,aAAa,KAAK,CAAC,UAAU,gBAAgB,KAAK,CAAC,aAAa,WAAW,KAAK,CAAC,UAAU,CAAC,KAAK,UAAU,KAAK,CAAC,UAAU,CAAC,IAAI,UAAU,KAAK,CAAC,UAAU,CAAC,IAAI,GAAG;QAC5L,EAAE;KACH,CAAC;IACF,MAAM,IAAI,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,GAAG,CACpC,CAAC,CAAC,EAAE,EAAE,CAAC,KAAK,CAAC,CAAC,QAAQ,CAAC,WAAW,EAAE,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC,IAAI,IAAI,CAAC,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC,CAAC,IAAI,CAAC,MAAM,MAAM,CAAC,CAAC,OAAO,EAAE,CACjI,CAAC;IACF,IAAI,QAAQ,CAAC,MAAM,GAAG,EAAE,EAAE,CAAC;QACzB,IAAI,CAAC,IAAI,CAAC,aAAa,QAAQ,CAAC,MAAM,GAAG,EAAE,qDAAqD,CAAC,CAAC;IACpG,CAAC;IACD,OAAO,CAAC,GAAG,MAAM,EAAE,GAAG,IAAI,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AACzC,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Detector, FileEntry, Finding, UnshitConfig } from '../contracts.js';
|
|
2
|
+
export interface ScanResult {
|
|
3
|
+
rootDir: string;
|
|
4
|
+
config: UnshitConfig;
|
|
5
|
+
files: FileEntry[];
|
|
6
|
+
findings: Finding[];
|
|
7
|
+
detectors: Detector[];
|
|
8
|
+
}
|
|
9
|
+
export declare function findingFingerprint(f: Finding): string;
|
|
10
|
+
/**
|
|
11
|
+
* Runs all enabled detectors read-only and returns deterministically sorted findings.
|
|
12
|
+
*/
|
|
13
|
+
export declare function runScan(rootDir: string, config: UnshitConfig, extraDetectors?: Detector[]): Promise<ScanResult>;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { BUILTIN_DETECTORS } from '../detectors/index.js';
|
|
2
|
+
import { collectFiles } from '../walker.js';
|
|
3
|
+
function isIgnoredByComment(file, finding) {
|
|
4
|
+
const lineIdx = finding.span.line - 1;
|
|
5
|
+
const current = file.lines[lineIdx] ?? '';
|
|
6
|
+
const prev = lineIdx > 0 ? (file.lines[lineIdx - 1] ?? '') : '';
|
|
7
|
+
const re = new RegExp(`unshit-ignore:\\s*${finding.ruleId.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}\\b`);
|
|
8
|
+
return re.test(current) || re.test(prev);
|
|
9
|
+
}
|
|
10
|
+
export function findingFingerprint(f) {
|
|
11
|
+
const normEvidence = f.evidence.replace(/\s+/g, ' ').trim();
|
|
12
|
+
return `${f.ruleId}::${f.file}::${normEvidence}`;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Runs all enabled detectors read-only and returns deterministically sorted findings.
|
|
16
|
+
*/
|
|
17
|
+
export async function runScan(rootDir, config, extraDetectors = []) {
|
|
18
|
+
const files = await collectFiles(rootDir, config);
|
|
19
|
+
const disabled = new Set(config.disabledRules);
|
|
20
|
+
const detectors = [...BUILTIN_DETECTORS, ...extraDetectors].filter((d) => !disabled.has(d.id));
|
|
21
|
+
const fileMap = new Map(files.map((f) => [f.relPath, f]));
|
|
22
|
+
const ctx = { rootDir, config, files };
|
|
23
|
+
const raw = [];
|
|
24
|
+
for (const detector of detectors) {
|
|
25
|
+
raw.push(...detector.detect(ctx));
|
|
26
|
+
}
|
|
27
|
+
const seen = new Set();
|
|
28
|
+
const findings = [];
|
|
29
|
+
for (const f of raw) {
|
|
30
|
+
const file = fileMap.get(f.file);
|
|
31
|
+
if (file && isIgnoredByComment(file, f))
|
|
32
|
+
continue;
|
|
33
|
+
const key = `${f.ruleId}|${f.file}|${f.span.line}|${f.span.column}`;
|
|
34
|
+
if (seen.has(key))
|
|
35
|
+
continue;
|
|
36
|
+
seen.add(key);
|
|
37
|
+
findings.push(f);
|
|
38
|
+
}
|
|
39
|
+
findings.sort((a, b) => a.file.localeCompare(b.file)
|
|
40
|
+
|| a.span.line - b.span.line
|
|
41
|
+
|| a.span.column - b.span.column
|
|
42
|
+
|| a.ruleId.localeCompare(b.ruleId));
|
|
43
|
+
return { rootDir, config, files, findings, detectors };
|
|
44
|
+
}
|
|
45
|
+
//# sourceMappingURL=scanner.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"scanner.js","sourceRoot":"","sources":["../../../src/core/scanner.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAC;AAC1D,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAU5C,SAAS,kBAAkB,CAAC,IAAe,EAAE,OAAgB;IAC3D,MAAM,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,CAAC;IACtC,MAAM,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,EAAE,CAAC;IAC1C,MAAM,IAAI,GAAG,OAAO,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;IAChE,MAAM,EAAE,GAAG,IAAI,MAAM,CAAC,qBAAqB,OAAO,CAAC,MAAM,CAAC,OAAO,CAAC,qBAAqB,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC;IACvG,OAAO,EAAE,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,C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,MAAM,CAAC,IAAI,GAAG,EAAE,CAAC;QACpB,MAAM,IAAI,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;QACjC,IAAI,IAAI,IAAI,kBAAkB,CAAC,IAAI,EAAE,CAAC,CAAC;YAAE,SAAS;QAClD,MAAM,GAAG,GAAG,GAAG,CAAC,CAAC,MAAM,IAAI,CAAC,CAAC,IAAI,IAAI,CAAC,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC;QACpE,IAAI,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC;YAAE,SAAS;QAC5B,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QACd,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IACnB,CAAC;IAED,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CACrB,CAAC,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC,CAAC,IAAI,CAAC;WACzB,CAAC,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,CAAC,IAAI,CAAC,IAAI;WACzB,CAAC,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,IAAI,CAAC,MAAM;WAC7B,CAAC,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC,CAAC,MAAM,CAAC,CACpC,CAAC;IAEF,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,QAAQ,EAAE,SAAS,EAAE,CAAC;AACzD,CAAC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { FileEntry, Finding, ScoreBreakdown } from '../contracts.js';
|
|
2
|
+
/**
|
|
3
|
+
* Computes a deterministic 0-100 codebase health score from findings and codebase size.
|
|
4
|
+
*/
|
|
5
|
+
export declare function computeScore(files: FileEntry[], findings: Finding[]): ScoreBreakdown;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
const WEIGHT = {
|
|
2
|
+
error: 4,
|
|
3
|
+
warn: 2,
|
|
4
|
+
info: 0.5,
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* Computes a deterministic 0-100 codebase health score from findings and codebase size.
|
|
8
|
+
*/
|
|
9
|
+
export function computeScore(files, findings) {
|
|
10
|
+
const totalFiles = files.length;
|
|
11
|
+
const totalLines = files.reduce((acc, f) => acc + f.lines.length, 0);
|
|
12
|
+
const byCategory = {
|
|
13
|
+
css: 0,
|
|
14
|
+
types: 0,
|
|
15
|
+
i18n: 0,
|
|
16
|
+
architecture: 0,
|
|
17
|
+
slop: 0,
|
|
18
|
+
duplication: 0,
|
|
19
|
+
'dead-code': 0,
|
|
20
|
+
};
|
|
21
|
+
const bySeverity = { error: 0, warn: 0, info: 0 };
|
|
22
|
+
let penaltyPoints = 0;
|
|
23
|
+
for (const f of findings) {
|
|
24
|
+
byCategory[f.category] += 1;
|
|
25
|
+
bySeverity[f.severity] += 1;
|
|
26
|
+
penaltyPoints += WEIGHT[f.severity];
|
|
27
|
+
}
|
|
28
|
+
const kloc = Math.max(1, totalLines / 1000);
|
|
29
|
+
const densityPenalty = (penaltyPoints / kloc) * 1.4;
|
|
30
|
+
const score = Math.max(0, Math.min(100, Math.round(100 - densityPenalty)));
|
|
31
|
+
let grade = 'F';
|
|
32
|
+
if (score >= 90)
|
|
33
|
+
grade = 'A';
|
|
34
|
+
else if (score >= 78)
|
|
35
|
+
grade = 'B';
|
|
36
|
+
else if (score >= 62)
|
|
37
|
+
grade = 'C';
|
|
38
|
+
else if (score >= 45)
|
|
39
|
+
grade = 'D';
|
|
40
|
+
return {
|
|
41
|
+
score,
|
|
42
|
+
grade,
|
|
43
|
+
totalFiles,
|
|
44
|
+
totalLines,
|
|
45
|
+
totalFindings: findings.length,
|
|
46
|
+
byCategory,
|
|
47
|
+
bySeverity,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
//# sourceMappingURL=scorer.js.map
|
|
@@ -0,0 +1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AAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,IAAI,CAAC,KAAK,CAAC,GAAG,GAAG,cAAc,CAAC,CAAC,CAAC,CAAC;IAE3E,IAAI,KAAK,GAA4B,GAAG,CAAC;IACzC,IAAI,KAAK,IAAI,EAAE;QAAE,KAAK,GAAG,GAAG,CAAC;SACxB,IAAI,KAAK,IAAI,EAAE;QAAE,KAAK,GAAG,GAAG,CAAC;SAC7B,IAAI,KAAK,IAAI,EAAE;QAAE,KAAK,GAAG,GAAG,CAAC;SAC7B,IAAI,KAAK,IAAI,EAAE;QAAE,KAAK,GAAG,GAAG,CAAC;IAElC,OAAO;QACL,KAAK;QACL,KAAK;QACL,UAAU;QACV,UAAU;QACV,aAAa,EAAE,QAAQ,CAAC,MAAM;QAC9B,UAAU;QACV,UAAU;KACX,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
const TOKEN_RE = /[A-Za-z_$][\w$]*|\d+(?:\.\d+)?|"(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|[{}()[\].,:;+-/*=<>!&|?]+/g;
|
|
3
|
+
const KEYWORDS = new Set([
|
|
4
|
+
'const', 'let', 'var', 'function', 'return', 'if', 'else', 'for', 'while',
|
|
5
|
+
'switch', 'case', 'break', 'import', 'export', 'from', 'type', 'interface',
|
|
6
|
+
'async', 'await', 'try', 'catch', 'throw', 'new', 'true', 'false', 'null',
|
|
7
|
+
]);
|
|
8
|
+
function tokenize(content) {
|
|
9
|
+
const units = [];
|
|
10
|
+
const lines = content.split(/\r?\n/);
|
|
11
|
+
for (let i = 0; i < lines.length; i++) {
|
|
12
|
+
const lineText = lines[i] ?? '';
|
|
13
|
+
if (lineText.trimStart().startsWith('import '))
|
|
14
|
+
continue;
|
|
15
|
+
TOKEN_RE.lastIndex = 0;
|
|
16
|
+
let m;
|
|
17
|
+
while ((m = TOKEN_RE.exec(lineText)) !== null) {
|
|
18
|
+
const raw = m[0];
|
|
19
|
+
let norm = raw;
|
|
20
|
+
if (raw.startsWith('"') || raw.startsWith("'"))
|
|
21
|
+
norm = '$STR';
|
|
22
|
+
else if (/^\d/.test(raw))
|
|
23
|
+
norm = '$NUM';
|
|
24
|
+
else if (/^[A-Za-z_$]/.test(raw) && !KEYWORDS.has(raw))
|
|
25
|
+
norm = '$ID';
|
|
26
|
+
units.push({ norm, line: i + 1 });
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return units;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* jscpd-inspired rolling token-window clone detector.
|
|
33
|
+
*/
|
|
34
|
+
export const duplicateBlockDetector = {
|
|
35
|
+
id: 'code/duplicate-block',
|
|
36
|
+
category: 'duplication',
|
|
37
|
+
tier: 'structural',
|
|
38
|
+
severity: 'info',
|
|
39
|
+
description: 'Detects copy-pasted structural token blocks across source files.',
|
|
40
|
+
detect(ctx) {
|
|
41
|
+
const windowSize = ctx.config.cloneMinTokens;
|
|
42
|
+
const buckets = new Map();
|
|
43
|
+
for (const file of ctx.files) {
|
|
44
|
+
if (!file.sourceFile || /\.(test|spec|d)\./.test(file.relPath))
|
|
45
|
+
continue;
|
|
46
|
+
const tokens = tokenize(file.content);
|
|
47
|
+
if (tokens.length < windowSize)
|
|
48
|
+
continue;
|
|
49
|
+
const step = Math.max(10, Math.floor(windowSize / 3));
|
|
50
|
+
for (let i = 0; i <= tokens.length - windowSize; i += step) {
|
|
51
|
+
const slice = tokens.slice(i, i + windowSize);
|
|
52
|
+
const sig = slice.map((t) => t.norm).join(' ');
|
|
53
|
+
const hash = createHash('sha1').update(sig).digest('hex').slice(0, 12);
|
|
54
|
+
const startLine = slice[0].line;
|
|
55
|
+
const endLine = slice[slice.length - 1].line;
|
|
56
|
+
const list = buckets.get(hash) ?? [];
|
|
57
|
+
list.push({ file: file.relPath, startLine, endLine });
|
|
58
|
+
buckets.set(hash, list);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
const findings = [];
|
|
62
|
+
const reported = new Set();
|
|
63
|
+
for (const occurrences of buckets.values()) {
|
|
64
|
+
const distinctFiles = new Set(occurrences.map((o) => o.file));
|
|
65
|
+
if (distinctFiles.size < 2)
|
|
66
|
+
continue;
|
|
67
|
+
const first = occurrences[0];
|
|
68
|
+
for (let i = 1; i < occurrences.length; i++) {
|
|
69
|
+
const dup = occurrences[i];
|
|
70
|
+
if (dup.file === first.file)
|
|
71
|
+
continue;
|
|
72
|
+
const key = `${dup.file}:${Math.floor(dup.startLine / 10)}`;
|
|
73
|
+
if (reported.has(key))
|
|
74
|
+
continue;
|
|
75
|
+
reported.add(key);
|
|
76
|
+
findings.push({
|
|
77
|
+
ruleId: this.id,
|
|
78
|
+
category: this.category,
|
|
79
|
+
tier: this.tier,
|
|
80
|
+
severity: this.severity,
|
|
81
|
+
fixability: 'ai',
|
|
82
|
+
confidence: 0.82,
|
|
83
|
+
file: dup.file,
|
|
84
|
+
span: { line: dup.startLine, column: 1, endLine: dup.endLine },
|
|
85
|
+
message: `Duplicate logic block (~${windowSize} tokens) matches ${first.file}:${first.startLine}-${first.endLine}.`,
|
|
86
|
+
evidence: `${dup.file}:${dup.startLine}-${dup.endLine} <-> ${first.file}:${first.startLine}-${first.endLine}`,
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return findings;
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
//# sourceMappingURL=clones.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"clones.js","sourceRoot":"","sources":["../../../src/detectors/clones.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAQzC,MAAM,QAAQ,GAAG,+FAA+F,CAAC;AACjH,MAAM,QAAQ,GAAG,IAAI,GAAG,CAAC;IACvB,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,U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+
}
|
|
5
|
+
function buildLineOffsets(content) {
|
|
6
|
+
const offsets = [0];
|
|
7
|
+
for (let i = 0; i < content.length; i++) {
|
|
8
|
+
if (content.charCodeAt(i) === 10)
|
|
9
|
+
offsets.push(i + 1);
|
|
10
|
+
}
|
|
11
|
+
return offsets;
|
|
12
|
+
}
|
|
13
|
+
function lineAtOffset(offsets, offset) {
|
|
14
|
+
let lo = 0;
|
|
15
|
+
let hi = offsets.length - 1;
|
|
16
|
+
while (lo <= hi) {
|
|
17
|
+
const mid = (lo + hi) >> 1;
|
|
18
|
+
if ((offsets[mid] ?? 0) <= offset)
|
|
19
|
+
lo = mid + 1;
|
|
20
|
+
else
|
|
21
|
+
hi = mid - 1;
|
|
22
|
+
}
|
|
23
|
+
return hi + 1;
|
|
24
|
+
}
|
|
25
|
+
export const cssNoImportantDetector = {
|
|
26
|
+
id: 'css/no-important',
|
|
27
|
+
category: 'css',
|
|
28
|
+
tier: 'structural',
|
|
29
|
+
severity: 'warn',
|
|
30
|
+
description: 'Flags `!important` overrides in stylesheets (ignoring comments).',
|
|
31
|
+
detect(ctx) {
|
|
32
|
+
const findings = [];
|
|
33
|
+
for (const file of ctx.files) {
|
|
34
|
+
if (!CSS_EXTS.has(file.ext))
|
|
35
|
+
continue;
|
|
36
|
+
const strippedLines = stripCssCommentsPreserveLines(file.content).split(/\r?\n/);
|
|
37
|
+
for (let i = 0; i < strippedLines.length; i++) {
|
|
38
|
+
const line = strippedLines[i] ?? '';
|
|
39
|
+
const idx = line.indexOf('!important');
|
|
40
|
+
if (idx !== -1) {
|
|
41
|
+
findings.push({
|
|
42
|
+
ruleId: this.id,
|
|
43
|
+
category: this.category,
|
|
44
|
+
tier: this.tier,
|
|
45
|
+
severity: this.severity,
|
|
46
|
+
fixability: 'ai',
|
|
47
|
+
confidence: 0.99,
|
|
48
|
+
file: file.relPath,
|
|
49
|
+
span: { line: i + 1, column: idx + 1 },
|
|
50
|
+
message: 'Avoid `!important`; use CSS cascade order or `@layer` instead.',
|
|
51
|
+
evidence: (file.lines[i] ?? line).trim().slice(0, 100),
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return findings;
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
export const cssDuplicateSelectorDetector = {
|
|
60
|
+
id: 'css/duplicate-selector',
|
|
61
|
+
category: 'css',
|
|
62
|
+
tier: 'structural',
|
|
63
|
+
severity: 'info',
|
|
64
|
+
description: 'Flags CSS selectors defined multiple times in the same at-rule scope (skipping @keyframes).',
|
|
65
|
+
detect(ctx) {
|
|
66
|
+
const seen = new Map();
|
|
67
|
+
for (const file of ctx.files) {
|
|
68
|
+
if (!CSS_EXTS.has(file.ext))
|
|
69
|
+
continue;
|
|
70
|
+
const clean = stripCssCommentsPreserveLines(file.content);
|
|
71
|
+
const offsets = buildLineOffsets(clean);
|
|
72
|
+
const scopeStack = [];
|
|
73
|
+
let headerStart = 0;
|
|
74
|
+
for (let i = 0; i < clean.length; i++) {
|
|
75
|
+
const ch = clean[i];
|
|
76
|
+
if (ch === '{') {
|
|
77
|
+
const header = clean.slice(headerStart, i).trim().replace(/\s+/g, ' ');
|
|
78
|
+
if (header.startsWith('@')) {
|
|
79
|
+
scopeStack.push(header);
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
const insideKeyframes = scopeStack.some((s) => /^@(-webkit-|-moz-)?keyframes\b/i.test(s));
|
|
83
|
+
if (header && !insideKeyframes) {
|
|
84
|
+
const scopeKey = scopeStack.join(' > ');
|
|
85
|
+
const mapKey = `${scopeKey}::${header}`;
|
|
86
|
+
const list = seen.get(mapKey) ?? [];
|
|
87
|
+
list.push({ file: file.relPath, line: lineAtOffset(offsets, headerStart), selector: header });
|
|
88
|
+
seen.set(mapKey, list);
|
|
89
|
+
}
|
|
90
|
+
scopeStack.push(`RULE:${header}`);
|
|
91
|
+
}
|
|
92
|
+
headerStart = i + 1;
|
|
93
|
+
}
|
|
94
|
+
else if (ch === '}') {
|
|
95
|
+
scopeStack.pop();
|
|
96
|
+
headerStart = i + 1;
|
|
97
|
+
}
|
|
98
|
+
else if (ch === ';' && !scopeStack.some((s) => s.startsWith('RULE:'))) {
|
|
99
|
+
headerStart = i + 1;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
const findings = [];
|
|
104
|
+
for (const occurrences of seen.values()) {
|
|
105
|
+
if (occurrences.length < 2)
|
|
106
|
+
continue;
|
|
107
|
+
const first = occurrences[0];
|
|
108
|
+
for (let i = 1; i < occurrences.length; i++) {
|
|
109
|
+
const dup = occurrences[i];
|
|
110
|
+
findings.push({
|
|
111
|
+
ruleId: this.id,
|
|
112
|
+
category: this.category,
|
|
113
|
+
tier: this.tier,
|
|
114
|
+
severity: this.severity,
|
|
115
|
+
fixability: 'ai',
|
|
116
|
+
confidence: 0.88,
|
|
117
|
+
file: dup.file,
|
|
118
|
+
span: { line: dup.line, column: 1 },
|
|
119
|
+
message: `Duplicate CSS selector \`${dup.selector}\` (also defined at ${first.file}:${first.line}).`,
|
|
120
|
+
evidence: dup.selector,
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return findings;
|
|
125
|
+
},
|
|
126
|
+
};
|
|
127
|
+
//# sourceMappingURL=css.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"css.js","sourceRoot":"","sources":["../../../src/detectors/css.ts"],"names":[],"mappings":"AAEA,MAAM,QAAQ,GAAG,IAAI,GAAG,CAAC,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;AAE5C,SAAS,6BAA6B,CAAC,OAAe;IACpD,OAAO,OAAO,CAAC,OAAO,CAAC,mBAAmB,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,UAAU,EAAE,GAAG,CAAC,CAAC,CAAC;AACjF,CAAC;AAED,SAAS,gBAAgB,CAAC,OAAe;IACvC,MAAM,OAA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AU,EAAE,IAAI;oBAChB,UAAU,EAAE,IAAI;oBAChB,IAAI,EAAE,GAAG,CAAC,IAAI;oBACd,IAAI,EAAE,EAAE,IAAI,EAAE,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,CAAC,EAAE;oBACnC,OAAO,EAAE,4BAA4B,GAAG,CAAC,QAAQ,uBAAuB,KAAK,CAAC,IAAI,IAAI,KAAK,CAAC,IAAI,IAAI;oBACpG,QAAQ,EAAE,GAAG,CAAC,QAAQ;iBACvB,CAAC,CAAC;YACL,CAAC;QACH,CAAC;QACD,OAAO,QAAQ,CAAC;IAClB,CAAC;CACF,CAAC"}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import ts from 'typescript';
|
|
2
|
+
function isEntryFile(relPath) {
|
|
3
|
+
return /(^|\/)(index|main|cli|app|server|vite\.config|vitest\.config)\.[cm]?[jt]sx?$/i.test(relPath)
|
|
4
|
+
|| /\.(test|spec|d)\./.test(relPath);
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* knip-inspired cross-file exported symbol reachability detector.
|
|
8
|
+
*/
|
|
9
|
+
export const unusedExportDetector = {
|
|
10
|
+
id: 'ts/unused-export',
|
|
11
|
+
category: 'dead-code',
|
|
12
|
+
tier: 'structural',
|
|
13
|
+
severity: 'info',
|
|
14
|
+
description: 'Flags named exports that are never referenced in any other file.',
|
|
15
|
+
detect(ctx) {
|
|
16
|
+
const exportsList = [];
|
|
17
|
+
const allContents = new Map();
|
|
18
|
+
for (const file of ctx.files) {
|
|
19
|
+
if (!file.sourceFile)
|
|
20
|
+
continue;
|
|
21
|
+
allContents.set(file.relPath, file.content);
|
|
22
|
+
if (isEntryFile(file.relPath))
|
|
23
|
+
continue;
|
|
24
|
+
const sf = file.sourceFile;
|
|
25
|
+
const record = (nameNode, name) => {
|
|
26
|
+
const pos = sf.getLineAndCharacterOfPosition(nameNode.getStart(sf));
|
|
27
|
+
exportsList.push({ name, file: file.relPath, line: pos.line + 1, column: pos.character + 1 });
|
|
28
|
+
};
|
|
29
|
+
for (const stmt of sf.statements) {
|
|
30
|
+
const mods = ts.canHaveModifiers(stmt) ? ts.getModifiers(stmt) : undefined;
|
|
31
|
+
const isExported = mods?.some((m) => m.kind === ts.SyntaxKind.ExportKeyword) ?? false;
|
|
32
|
+
const isDefault = mods?.some((m) => m.kind === ts.SyntaxKind.DefaultKeyword) ?? false;
|
|
33
|
+
if (!isExported || isDefault)
|
|
34
|
+
continue;
|
|
35
|
+
if ((ts.isFunctionDeclaration(stmt) || ts.isClassDeclaration(stmt) || ts.isInterfaceDeclaration(stmt) || ts.isTypeAliasDeclaration(stmt) || ts.isEnumDeclaration(stmt)) && stmt.name) {
|
|
36
|
+
record(stmt.name, stmt.name.text);
|
|
37
|
+
}
|
|
38
|
+
else if (ts.isVariableStatement(stmt)) {
|
|
39
|
+
for (const decl of stmt.declarationList.declarations) {
|
|
40
|
+
if (ts.isIdentifier(decl.name)) {
|
|
41
|
+
record(decl.name, decl.name.text);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
const findings = [];
|
|
48
|
+
for (const exp of exportsList) {
|
|
49
|
+
const symRe = new RegExp(`\\b${exp.name.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}\\b`);
|
|
50
|
+
let referencedElsewhere = false;
|
|
51
|
+
for (const [otherFile, content] of allContents) {
|
|
52
|
+
if (otherFile === exp.file)
|
|
53
|
+
continue;
|
|
54
|
+
if (symRe.test(content)) {
|
|
55
|
+
referencedElsewhere = true;
|
|
56
|
+
break;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
if (!referencedElsewhere) {
|
|
60
|
+
findings.push({
|
|
61
|
+
ruleId: this.id,
|
|
62
|
+
category: this.category,
|
|
63
|
+
tier: this.tier,
|
|
64
|
+
severity: this.severity,
|
|
65
|
+
fixability: 'unsafe',
|
|
66
|
+
confidence: 0.78,
|
|
67
|
+
file: exp.file,
|
|
68
|
+
span: { line: exp.line, column: exp.column },
|
|
69
|
+
message: `Exported symbol \`${exp.name}\` appears unused outside ${exp.file}.`,
|
|
70
|
+
evidence: `export ... ${exp.name}`,
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return findings;
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
//# sourceMappingURL=dead-exports.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dead-exports.js","sourceRoot":"","sources":["../../../src/detectors/dead-exports.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,YAAY,CAAC;AAU5B,SAAS,WAAW,CAAC,OAAe;IAClC,OAAO,+EAA+E,CAAC,IAAI,CAAC,OAAO,CAAC;WAC/F,mBAAmB,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;AACzC,CAAC;AAED;;GAEG;AACH,MAAM,CAAC,MAAM,oBAAoB,GAAa;IAC5C,EAAE,EAAE,kBAAkB;IACtB,QAAQ,EAAE,WAAW;IACrB,IAAI,EAAE,YAAY;IAClB,QAAQ,EAAE,MAAM;IAChB,WAAW,EAAE,kEAAkE;IAC/E,MAAM,CAAC,GAAoB;QACzB,MAAM,WAAW,GAAiB,EAAE,CAAC;QACrC,MAAM,WAAW,GAAG,IAAI,GAAG,EAAkB,CAAC;QAE9C,KAAK,MAAM,IAAI,IAAI,GAAG,CAAC,KAAK,EAAE,CAAC;YAC7B,IAAI,CAAC,IAAI,CAAC,UAAU;gBAAE,SAAS;YAC/B,WAAW,CAAC,GAAG,CAAC,IAAI,CAAC,OAAO,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC;YAC5C,IAAI,WAAW,CAAC,IAAI,CAAC,OAAO,CAAC;gBAAE,SAAS;YAExC,MAAM,EAAE,GAAG,IAAI,CAAC,UAAU,CAAC;YAC3B,MAAM,MAAM,GAAG,CAAC,QAAiB,EAAE,IAAY,EAAQ,EAAE;gBACvD,MAAM,GAAG,GAAG,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G,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,CAAC,MAAM,EAAE;oBAC5C,OAAO,EAAE,qBAAqB,GAAG,CAAC,IAAI,6BAA6B,GAAG,CAAC,IAAI,GAAG;oBAC9E,QAAQ,EAAE,cAAc,GAAG,CAAC,IAAI,EAAE;iBACnC,CAAC,CAAC;YACL,CAAC;QACH,CAAC;QACD,OAAO,QAAQ,CAAC;IAClB,CAAC;CACF,CAAC"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
const HOOK_RE = /\buse(State|Effect|Memo|Callback|Ref|Reducer|Context|LayoutEffect)\s*\(/g;
|
|
2
|
+
export const godFileDetector = {
|
|
3
|
+
id: 'react/god-file',
|
|
4
|
+
category: 'architecture',
|
|
5
|
+
tier: 'heuristic',
|
|
6
|
+
severity: 'warn',
|
|
7
|
+
description: 'Flags oversized files or hooks exceeding line/hook-density thresholds.',
|
|
8
|
+
detect(ctx) {
|
|
9
|
+
const findings = [];
|
|
10
|
+
const { godFileMaxLines, godFileMaxHooks } = ctx.config;
|
|
11
|
+
for (const file of ctx.files) {
|
|
12
|
+
if (!file.sourceFile || /\.(test|spec)\./.test(file.relPath))
|
|
13
|
+
continue;
|
|
14
|
+
const lineCount = file.lines.length;
|
|
15
|
+
const hookMatches = file.content.match(HOOK_RE);
|
|
16
|
+
const hookCount = hookMatches ? hookMatches.length : 0;
|
|
17
|
+
if (lineCount > godFileMaxLines || hookCount > godFileMaxHooks) {
|
|
18
|
+
findings.push({
|
|
19
|
+
ruleId: this.id,
|
|
20
|
+
category: this.category,
|
|
21
|
+
tier: this.tier,
|
|
22
|
+
severity: this.severity,
|
|
23
|
+
fixability: 'ai',
|
|
24
|
+
confidence: 0.9,
|
|
25
|
+
file: file.relPath,
|
|
26
|
+
span: { line: 1, column: 1 },
|
|
27
|
+
message: `Oversized module (${lineCount} lines, ${hookCount} hooks; limits: ${godFileMaxLines} lines / ${godFileMaxHooks} hooks). Split by domain.`,
|
|
28
|
+
evidence: `${lineCount} LOC, ${hookCount} React hooks`,
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return findings;
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
//# sourceMappingURL=god-file.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"god-file.js","sourceRoot":"","sources":["../../../src/detectors/god-file.ts"],"names":[],"mappings":"AAEA,MAAM,OAAO,GAAG,0EAA0E,CAAC;AAE3F,MAAM,CAAC,MAAM,eAAe,GAAa;IACvC,EAAE,EAAE,gBAAgB;IACpB,QAAQ,EAAE,cAAc;IACxB,IAAI,EAAE,WAAW;IACjB,QAAQ,EAAE,MAAM;IAChB,WAAW,EAAE,wEAAwE;IACrF,MAAM,CAAC,GAAoB;QACzB,MAAM,QAAQ,GAAc,EAAE,CAAC;QAC/B,MAAM,EAAE,eAAe,EAAE,eAAe,EAAE,GAAG,GAAG,CAAC,MAAM,CAAC;QAExD,KAAK,MAAM,IAAI,IAAI,GAAG,CAAC,KAAK,EAAE,CAAC;YAC7B,IAAI,CAAC,IAAI,CAAC,UAAU,IAAI,iBAAiB,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC;gBAAE,SAAS;YACvE,MAAM,SAAS,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC;YACpC,MAAM,WAAW,GAAG,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;YAChD,MAAM,SAAS,GAAG,WAAW,CAAC,CAAC,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;YAEvD,IAAI,SAAS,GAAG,eAAe,IAAI,SAAS,GAAG,eAAe,EAAE,CAAC;gBAC/D,QAAQ,CAAC,IAAI,CAAC;oBACZ,MAAM,EAAE,IAAI,CAAC,EAAE;oBACf,QAAQ,EAAE,IAAI,CAAC,QAAQ;oBACvB,IAAI,EAAE,IAAI,CAAC,IAAI;oBACf,QAAQ,EAAE,IAAI,CAAC,QAAQ;oBACvB,UAAU,EAAE,IAAI;oBAChB,UAAU,EAAE,GAAG;oBACf,IAAI,EAAE,IAAI,CAAC,OAAO;oBAClB,IAAI,EAAE,EAAE,IAAI,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE;oBAC5B,OAAO,EAAE,qBAAqB,SAAS,WAAW,SAAS,mBAAmB,eAAe,YAAY,eAAe,2BAA2B;oBACnJ,QAAQ,EAAE,GAAG,SAAS,SAAS,SAAS,cAAc;iBACvD,CAAC,CAAC;YACL,CAAC;QACH,CAAC;QACD,OAAO,QAAQ,CAAC;IAClB,CAAC;CACF,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { duplicateBlockDetector } from './clones.js';
|
|
2
|
+
import { cssDuplicateSelectorDetector, cssNoImportantDetector } from './css.js';
|
|
3
|
+
import { unusedExportDetector } from './dead-exports.js';
|
|
4
|
+
import { godFileDetector } from './god-file.js';
|
|
5
|
+
import { magicTimerDetector, noDoubleCastDetector, noExplicitAnyDetector, noInlineI18nTernaryDetector, redundantCatchDetector, } from './ts-ast.js';
|
|
6
|
+
export const BUILTIN_DETECTORS = [
|
|
7
|
+
noExplicitAnyDetector,
|
|
8
|
+
noDoubleCastDetector,
|
|
9
|
+
noInlineI18nTernaryDetector,
|
|
10
|
+
magicTimerDetector,
|
|
11
|
+
redundantCatchDetector,
|
|
12
|
+
cssNoImportantDetector,
|
|
13
|
+
cssDuplicateSelectorDetector,
|
|
14
|
+
godFileDetector,
|
|
15
|
+
duplicateBlockDetector,
|
|
16
|
+
unusedExportDetector,
|
|
17
|
+
];
|
|
18
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/detectors/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,sBAAsB,EAAE,MAAM,aAAa,CAAC;AACrD,OAAO,EAAE,4BAA4B,EAAE,sBAAsB,EAAE,MAAM,UAAU,CAAC;AAChF,OAAO,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AACzD,OAAO,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAChD,OAAO,EACL,kBAAkB,EAClB,oBAAoB,EACpB,qBAAqB,EACrB,2BAA2B,EAC3B,sBAAsB,GACvB,MAAM,aAAa,CAAC;AAErB,MAAM,CAAC,MAAM,iBAAiB,GAAe;IAC3C,qBAAqB;IACrB,oBAAoB;IACpB,2BAA2B;IAC3B,kBAAkB;IAClB,sBAAsB;IACtB,sBAAsB;IACtB,4BAA4B;IAC5B,eAAe;IACf,sBAAsB;IACtB,oBAAoB;CACrB,CAAC"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { Detector } from '../contracts.js';
|
|
2
|
+
export declare const noExplicitAnyDetector: Detector;
|
|
3
|
+
export declare const noDoubleCastDetector: Detector;
|
|
4
|
+
export declare const noInlineI18nTernaryDetector: Detector;
|
|
5
|
+
export declare const magicTimerDetector: Detector;
|
|
6
|
+
export declare const redundantCatchDetector: Detector;
|