Skip to content

Commit 8d154e6

Browse files
authored
fix(benchmarks): strip block comments before counting LOC (#232)
loc.js filtered comments line by line, so /* ... */ block comments whose continuation lines are not *-aligned had their inner lines counted as code. A plain indented block comment scored higher than the same code written JSDoc-style. Strip /* ... */ before the line count so both are equal, and add loc.test.js to lock the behavior. Fixes #231
1 parent e353a1a commit 8d154e6

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

‎benchmarks/loc.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
module.exports = (output) => {
55
const text = String(output || '');
66
const blocks = [...text.matchAll(/```[a-zA-Z0-9_+-]*\n([\s\S]*?)```/g)].map((m) => m[1]);
7-
const code = blocks.length ? blocks.join('\n') : text;
7+
// Drop /* ... */ block comments before counting; the line filter below only
8+
// caught `*`-aligned JSDoc, so plain block comments were miscounted as code.
9+
const code = (blocks.length ? blocks.join('\n') : text).replace(/\/\*[\s\S]*?\*\//g, '');
810
const loc = code
911
.split('\n')
1012
.map((l) => l.trim())

‎benchmarks/loc.test.js‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Regression guard for loc.js comment handling. Run: node loc.test.js
2+
const assert = require('assert');
3+
const loc = require('./loc.js');
4+
5+
const score = (src) => loc(src).score;
6+
7+
let pass = 0;
8+
const cases = [
9+
// /* ... */ block comments must not count as code, whether or not the
10+
// continuation lines are *-aligned (the old filter only caught JSDoc style).
11+
['plain block comment not counted', score('```js\nfunction f() {\n /* explain\n the rest */\n return 1;\n}\n```'), 3],
12+
['jsdoc block comment not counted', score('```js\nfunction g() {\n /*\n * explain\n */\n return 2;\n}\n```'), 3],
13+
['inline block comment keeps its code line', score('```js\nconst x = 1; /* note */\nconst y = 2;\n```'), 2],
14+
['line comments still stripped', score('```js\n// header\nconst x = 1;\n```'), 1],
15+
['plain code unchanged', score('```js\nconst a = 1;\nconst b = 2;\n```'), 2],
16+
];
17+
for (const [name, got, want] of cases) {
18+
assert.strictEqual(got, want, `FAILED: ${name} (got ${got}, want ${want})`);
19+
console.log(`ok - ${name}`);
20+
pass++;
21+
}
22+
console.log(`\n${pass}/${cases.length} passed`);

0 commit comments

Comments
 (0)