Skip to content

Commit 1105265

Browse files
committed
feat: prevent overwriting defined executors
1 parent fce68b0 commit 1105265

File tree

4 files changed

+13
-3
lines changed

4 files changed

+13
-3
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ Evaluate script. Returns Promise that resolves to the script evaluation result o
106106

107107
Add executor to the interpreter. Can be an object or a function with methods.
108108

109+
This method throws exception if you use the name that is already used (including predefined executors `array`, `calc` and `str`).
110+
109111

110112
##### .addInstruction(Object definition, Function func)
111113

lib/jsonscript.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,7 @@ function addMacro(definition, _regenerateSchemas) {
134134
* @param {Object|Function} executor executor object or function
135135
*/
136136
function addExecutor(name, executor) {
137-
// TODO check duplicates, show warnings
138-
// ? TODO whitelist methods?
137+
if (this._executors[name]) throw new Error('executor "' + name + '" is already defined');
139138
this._executors[name] = executor;
140139
}
141140

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jsonscript-js",
3-
"version": "0.5.0",
3+
"version": "0.5.1",
44
"description": "JavaScript interpreter for JSONScript",
55
"main": "lib/jsonscript.js",
66
"scripts": {

spec/jsonscript.spec.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,13 @@ describe('jsonscript', function() {
2222
assert.strictEqual(js.validate.errors, null);
2323
});
2424
});
25+
26+
27+
describe('method addExecutor', function() {
28+
it('should throw if executor is already defined', function() {
29+
assert.throws(function() {
30+
js.addExecutor('calc', {});
31+
});
32+
});
33+
});
2534
});

0 commit comments

Comments
 (0)