Skip to content

Commit ea99c1c

Browse files
committed
Add a section about code checking
Fixes #39; contributed by @slothpie.
1 parent 21a52f0 commit ea99c1c

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

rules/0260-JavaScript.md

+35
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,41 @@ var assertFuzzyEquals = function(actual, expected, msg){
4545
```
4646

4747

48+
### Checking user code
49+
50+
[@slothpie][gh-slothpie] provided the following code to check the user's code
51+
for forbidden functions:
52+
53+
[gh-slothpie]: https://github.com/slothpie
54+
55+
``` javascript
56+
Test.describe("Reinforcement Test Cases:", function() {
57+
// get the code as text, (this also grabs test case code)
58+
var dump = arguments.callee.caller.caller.caller.toString();
59+
60+
// The length of our test case code.
61+
const FIXED = arguments.callee.caller.caller.toString().length;
62+
63+
// Slice out our test case code so we just have users
64+
const USER_CODE = dump.slice(0,dump.length-FIXED);
65+
66+
check_user_code_for_stuff(USER_CODE);
67+
});
68+
```
69+
70+
However, there are two things you have to keep in mind:
71+
72+
1. `USER_CODE` does not contain actual user code, but the one returned by
73+
babel.js. You never get access to the actual source code (until node
74+
supports ES6 and gets updated on Codwars).
75+
2. `arguments.callee` and `arguments.caller` only work if you don't run your
76+
script in strict mode. A stray `"use strict";` in the user's code will
77+
lead to an error, even if the code is otherwise OK.
78+
79+
Other than that, it's an easy way to check whether the user uses any loops
80+
or similar.
81+
82+
4883
### Random tests
4984

5085
In [August 2015][gitter-chat-quickcheck] the [`node-quickeck`][node-quickeck]

0 commit comments

Comments
 (0)