pi-lens 1.3.8 → 1.3.9
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/clients/complexity-client.ts +9 -15
- package/package.json +1 -1
|
@@ -212,7 +212,7 @@ export class ComplexityClient {
|
|
|
212
212
|
maintainabilityIndex: Math.round(maintainabilityIndex * 10) / 10,
|
|
213
213
|
linesOfCode: codeLines,
|
|
214
214
|
commentLines,
|
|
215
|
-
codeEntropy: Math.round(codeEntropy *
|
|
215
|
+
codeEntropy: Math.round(codeEntropy * 100) / 100,
|
|
216
216
|
};
|
|
217
217
|
} catch (err: any) {
|
|
218
218
|
this.log(`Analysis error for ${filePath}: ${err.message}`);
|
|
@@ -247,11 +247,9 @@ export class ComplexityClient {
|
|
|
247
247
|
parts.push(` Max nesting: ${metrics.maxNestingDepth} levels (consider extracting)`);
|
|
248
248
|
}
|
|
249
249
|
|
|
250
|
-
// Code entropy (
|
|
251
|
-
if (metrics.codeEntropy >
|
|
252
|
-
parts.push(` Entropy: ${metrics.codeEntropy} (
|
|
253
|
-
} else if (metrics.codeEntropy < 0.3) {
|
|
254
|
-
parts.push(` Entropy: ${metrics.codeEntropy} (low — repetitive patterns detected)`);
|
|
250
|
+
// Code entropy (in bits, >3.5 = risky AI-induced complexity)
|
|
251
|
+
if (metrics.codeEntropy > 3.5) {
|
|
252
|
+
parts.push(` Entropy: ${metrics.codeEntropy.toFixed(1)} bits (>3.5 — risky AI-induced complexity)`);
|
|
255
253
|
}
|
|
256
254
|
|
|
257
255
|
// Function length
|
|
@@ -504,9 +502,9 @@ export class ComplexityClient {
|
|
|
504
502
|
}
|
|
505
503
|
|
|
506
504
|
/**
|
|
507
|
-
* Calculate Shannon entropy of code tokens
|
|
508
|
-
*
|
|
509
|
-
*
|
|
505
|
+
* Calculate Shannon entropy of code tokens (in bits)
|
|
506
|
+
* Uses log2 for entropy measured in bits
|
|
507
|
+
* Threshold: >3.5 bits indicates risky AI-induced complexity
|
|
510
508
|
*/
|
|
511
509
|
private calculateCodeEntropy(sourceText: string): number {
|
|
512
510
|
// Tokenize by splitting on whitespace and common delimiters
|
|
@@ -526,7 +524,7 @@ export class ComplexityClient {
|
|
|
526
524
|
freq.set(token, (freq.get(token) || 0) + 1);
|
|
527
525
|
}
|
|
528
526
|
|
|
529
|
-
// Calculate Shannon entropy: H = -sum(p * log2(p))
|
|
527
|
+
// Calculate Shannon entropy in bits: H = -sum(p * log2(p))
|
|
530
528
|
let entropy = 0;
|
|
531
529
|
for (const count of freq.values()) {
|
|
532
530
|
const p = count / tokens.length;
|
|
@@ -535,11 +533,7 @@ export class ComplexityClient {
|
|
|
535
533
|
}
|
|
536
534
|
}
|
|
537
535
|
|
|
538
|
-
//
|
|
539
|
-
const maxEntropy = Math.log2(freq.size);
|
|
540
|
-
if (maxEntropy === 0) return 0;
|
|
541
|
-
|
|
542
|
-
return Math.min(1, entropy / maxEntropy);
|
|
536
|
+
return entropy; // Return in bits, not normalized
|
|
543
537
|
}
|
|
544
538
|
|
|
545
539
|
private isKeyword(text: string): boolean {
|
package/package.json
CHANGED