Skip to content

Commit 8d06814

Browse files
committed
not allow to use loops in strings
1 parent 0e182ae commit 8d06814

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

extensions/assert-linesOfCode.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import assert from 'assert';
22

33
function linesOfCode(fn, expected, recommended) {
4-
const actualLinesOfCode = fn.toString().split('\n').length - 2;
4+
const actualLinesOfCode = fn.toString().split('\n').filter(s => s.trim()).length - 2;
55
const isValid = actualLinesOfCode <= expected;
66
if (actualLinesOfCode <= expected && actualLinesOfCode > recommended) {
77
console.log(`Recommended lines of code for ${fn.name} is ${recommended}`);

test/01-strings-tests.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import assert from 'assert';
2-
import {
2+
import * as tasks from '../task/01-strings-tasks';
3+
4+
const {
35
concatenateStrings,
46
getStringLength,
57
getStringFromTemplate,
@@ -15,7 +17,7 @@ import {
1517
encodeToRot13,
1618
isString,
1719
getCardId
18-
} from '../task/01-strings-tasks';
20+
} = tasks;
1921

2022
it.optional = require('../extensions/it-optional');
2123

@@ -155,4 +157,15 @@ describe('01-strings-tasks', () => {
155157
});
156158
assert.linesOfCode(getCardId, 3);
157159
});
160+
161+
it.optional('Functions from 01-strings-test.js should not use basic loops statements', () => {
162+
Object.getOwnPropertyNames(tasks)
163+
.filter(x => tasks[x] instanceof Function)
164+
.forEach(f => {
165+
assert(
166+
!/([;{]\s*(for|while)\s*\()|(\.forEach\s*\()/.test(tasks[f].toString()),
167+
`Function "${f}" should not use basic loop statements (for, while or Array.forEach)! Please use specialized array methods (Array.map, Array.reduce etc).`
168+
);
169+
});
170+
});
158171
});

test/04-arrays-tests.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -950,7 +950,6 @@ describe('04-arrays-tasks', () => {
950950
assert.linesOfCode(swapHeadAndTail, 8, 2);
951951
});
952952

953-
954953
it.optional('Functions from 04-array-test.js should not use basic loops statements', () => {
955954
Object.getOwnPropertyNames(tasks)
956955
.filter(x => tasks[x] instanceof Function)

0 commit comments

Comments
 (0)