Skip to content

Commit 4289a8b

Browse files
author
Simon Renoult
committed
fix: use halstead.volume for JavaScript files + add missing tests
1 parent 4afea5c commit 4289a8b

File tree

2 files changed

+89
-35
lines changed

2 files changed

+89
-35
lines changed

src/lib/complexity/halstead.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function fromJavaScript(path: string): number {
3030
});
3131
if (!babelResult) throw new Error(`Error while parsing file ${path}`);
3232
const result = escomplex.analyse(babelResult.code, {});
33-
return result.aggregate.halstead;
33+
return result.aggregate.halstead.volume;
3434
}
3535

3636
function fromTypeScript(path: string): number {

test/lib/statistics.test.ts

Lines changed: 88 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,10 @@ describe("Statistics", () => {
183183
name: "a.ts",
184184
content: "if (true) if (true) console.log();",
185185
})
186+
.addFile({
187+
name: "b.js",
188+
content: "if (true) if (true) console.log();",
189+
})
186190
.writeOnDisk();
187191

188192
// When
@@ -197,6 +201,12 @@ describe("Statistics", () => {
197201
path: "a.ts",
198202
score: 3,
199203
},
204+
{
205+
churn: 1,
206+
complexity: 3,
207+
path: "b.js",
208+
score: 3,
209+
},
200210
]);
201211
});
202212
});
@@ -214,6 +224,10 @@ describe("Statistics", () => {
214224
name: "a.ts",
215225
content: "if (true) if (true) console.log();",
216226
})
227+
.addFile({
228+
name: "b.js",
229+
content: "if (true) if (true) console.log();",
230+
})
217231
.writeOnDisk();
218232

219233
// When
@@ -228,46 +242,86 @@ describe("Statistics", () => {
228242
path: "a.ts",
229243
score: 25.26619429851844,
230244
},
245+
{
246+
churn: 1,
247+
complexity: 25.26619429851844,
248+
path: "b.js",
249+
score: 25.26619429851844,
250+
},
231251
]);
232252
});
233253
});
234254

235255
context("when using sloc strategy", () => {
236-
it("returns the appropriate elements", async () => {
237-
// Given
238-
const options: Options = { ...defaultOptions, sort: "complexity" };
239-
await new TestRepositoryFixture()
240-
.addFile({ name: "a.js", lines: 8 })
241-
.addFile({ name: "b.js", lines: 6 })
242-
.addFile({ name: "c.js", lines: 2 })
243-
.addFile({ name: "d.js", lines: 4 })
244-
.writeOnDisk();
245-
246-
// When
247-
const result = await Statistics.compute(options);
248-
const statistics = Array.from(result.values());
256+
context("when analyzed file is supported by node-sloc", () => {
257+
it("returns the appropriate elements", async () => {
258+
// Given
259+
const options: Options = {
260+
...defaultOptions,
261+
complexityStrategy: "sloc",
262+
sort: "complexity",
263+
};
264+
await new TestRepositoryFixture()
265+
.addFile({ name: "a.js", lines: 8 })
266+
.addFile({ name: "b.js", lines: 6 })
267+
.addFile({ name: "c.js", lines: 2 })
268+
.addFile({ name: "d.js", lines: 4 })
269+
.writeOnDisk();
270+
271+
// When
272+
const result = await Statistics.compute(options);
273+
const statistics = Array.from(result.values());
274+
275+
// Then
276+
expect(statistics).to.deep.equal([
277+
{
278+
churn: 1,
279+
complexity: 8,
280+
path: "a.js",
281+
score: 8,
282+
},
283+
{
284+
churn: 1,
285+
complexity: 6,
286+
path: "b.js",
287+
score: 6,
288+
},
289+
{
290+
churn: 1,
291+
complexity: 4,
292+
path: "d.js",
293+
score: 4,
294+
},
295+
]);
296+
});
297+
});
249298

250-
// Then
251-
expect(statistics).to.deep.equal([
252-
{
253-
churn: 1,
254-
complexity: 8,
255-
path: "a.js",
256-
score: 8,
257-
},
258-
{
259-
churn: 1,
260-
complexity: 6,
261-
path: "b.js",
262-
score: 6,
263-
},
264-
{
265-
churn: 1,
266-
complexity: 4,
267-
path: "d.js",
268-
score: 4,
269-
},
270-
]);
299+
context("when analyzed file is not supported by node-sloc", () => {
300+
it("returns the appropriate elements", async () => {
301+
// Given
302+
const options: Options = {
303+
...defaultOptions,
304+
complexityStrategy: "sloc",
305+
sort: "complexity",
306+
};
307+
await new TestRepositoryFixture()
308+
.addFile({ name: "a.txt", lines: 8 })
309+
.writeOnDisk();
310+
311+
// When
312+
const result = await Statistics.compute(options);
313+
const statistics = Array.from(result.values());
314+
315+
// Then
316+
expect(statistics).to.deep.equal([
317+
{
318+
churn: 1,
319+
complexity: 7,
320+
path: "a.txt",
321+
score: 7,
322+
},
323+
]);
324+
});
271325
});
272326
});
273327
});

0 commit comments

Comments
 (0)