diff --git a/benchmarks/loc.js b/benchmarks/loc.js index 3640fa0..93e13dd 100644 --- a/benchmarks/loc.js +++ b/benchmarks/loc.js @@ -4,7 +4,9 @@ module.exports = (output) => { const text = String(output || ''); const blocks = [...text.matchAll(/```[a-zA-Z0-9_+-]*\n([\s\S]*?)```/g)].map((m) => m[1]); - const code = blocks.length ? blocks.join('\n') : text; + // Drop /* ... */ block comments before counting; the line filter below only + // caught `*`-aligned JSDoc, so plain block comments were miscounted as code. + const code = (blocks.length ? blocks.join('\n') : text).replace(/\/\*[\s\S]*?\*\//g, ''); const loc = code .split('\n') .map((l) => l.trim()) diff --git a/benchmarks/loc.test.js b/benchmarks/loc.test.js new file mode 100644 index 0000000..de30281 --- /dev/null +++ b/benchmarks/loc.test.js @@ -0,0 +1,22 @@ +// Regression guard for loc.js comment handling. Run: node loc.test.js +const assert = require('assert'); +const loc = require('./loc.js'); + +const score = (src) => loc(src).score; + +let pass = 0; +const cases = [ + // /* ... */ block comments must not count as code, whether or not the + // continuation lines are *-aligned (the old filter only caught JSDoc style). + ['plain block comment not counted', score('```js\nfunction f() {\n /* explain\n the rest */\n return 1;\n}\n```'), 3], + ['jsdoc block comment not counted', score('```js\nfunction g() {\n /*\n * explain\n */\n return 2;\n}\n```'), 3], + ['inline block comment keeps its code line', score('```js\nconst x = 1; /* note */\nconst y = 2;\n```'), 2], + ['line comments still stripped', score('```js\n// header\nconst x = 1;\n```'), 1], + ['plain code unchanged', score('```js\nconst a = 1;\nconst b = 2;\n```'), 2], +]; +for (const [name, got, want] of cases) { + assert.strictEqual(got, want, `FAILED: ${name} (got ${got}, want ${want})`); + console.log(`ok - ${name}`); + pass++; +} +console.log(`\n${pass}/${cases.length} passed`);