Skip to content

Commit 0598b85

Browse files
committed
Tweaks for unknown operator
1 parent 56f1249 commit 0598b85

8 files changed

+46
-9
lines changed

asLogic.test.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,14 @@ describe('asLogicAsync', () => {
7070

7171
it('Should not allow you to create a builder with methods that do not exist', async () => {
7272
const builder = asLogicAsync(module)
73-
expect(builder({
74-
hello: { '+': [1, 2] }
75-
})).rejects.toThrow()
73+
try {
74+
await builder({
75+
hello: { '+': [1, 2] }
76+
})
77+
} catch (e) {
78+
return
79+
}
80+
throw new Error('Should have thrown an error')
7681
})
7782

7883
it('Should let you select logic kept', async () => {

asyncLogic.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ class AsyncLogicEngine {
7474
const data = logic[func]
7575

7676
if (this.isData(logic, func)) return logic
77-
if (!this.methods[func]) throw new Error(`Method '${func}' was not found in the Logic Engine.`)
77+
78+
// eslint-disable-next-line no-throw-literal
79+
if (!this.methods[func]) throw { type: 'Unknown Operator', key: func }
7880

7981
// A small but useful micro-optimization for some of the most common functions.
8082
// Later on, I could define something to shut this off if var / val are redefined.

async_optimizer.js

+5
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ export function optimize (logic, engine, above = []) {
8585
const keys = Object.keys(logic)
8686
const methodName = keys[0]
8787

88+
if (keys.length === 0) return logic
89+
8890
const isData = engine.isData(logic, methodName)
8991
if (isData) return () => logic
9092

@@ -113,6 +115,9 @@ export function optimize (logic, engine, above = []) {
113115
}
114116
return result
115117
}
118+
119+
// eslint-disable-next-line no-throw-literal
120+
throw { type: 'Unknown Operator', key: methodName }
116121
}
117122

118123
return logic

compiler.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ export function isDeterministic (method, engine, buildState) {
8686

8787
if (engine.isData(method, func)) return true
8888
if (lower === undefined) return true
89-
if (!engine.methods[func]) throw new Error(`Method '${func}' was not found in the Logic Engine.`)
89+
90+
// eslint-disable-next-line no-throw-literal
91+
if (!engine.methods[func]) throw { type: 'Unknown Operator', key: func }
9092

9193
if (engine.methods[func].lazy) {
9294
return typeof engine.methods[func].deterministic === 'function'
@@ -176,7 +178,8 @@ function buildString (method, buildState = {}) {
176178
if (!engine.methods[func]) {
177179
// Check if this is supposed to be "data" rather than a function.
178180
if (engine.isData(method, func)) return pushValue(method, true)
179-
throw new Error(`Method '${func}' was not found in the Logic Engine.`)
181+
// eslint-disable-next-line no-throw-literal
182+
throw { type: 'Unknown Operator', key: func }
180183
}
181184

182185
if (

defaultMethods.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ function isDeterministic (method, engine, buildState) {
2020
const lower = method[func]
2121

2222
if (engine.isData(method, func) || func === undefined) return true
23-
if (!engine.methods[func]) throw new Error(`Method '${func}' was not found in the Logic Engine.`)
23+
// eslint-disable-next-line no-throw-literal
24+
if (!engine.methods[func]) throw { type: 'Unknown Operator', key: func }
2425

2526
if (engine.methods[func].lazy) {
2627
return typeof engine.methods[func].deterministic === 'function'
@@ -45,7 +46,8 @@ function isSyncDeep (method, engine, buildState) {
4546
const func = Object.keys(method)[0]
4647
const lower = method[func]
4748
if (engine.isData(method, func) || func === undefined) return true
48-
if (!engine.methods[func]) throw new Error(`Method '${func}' was not found in the Logic Engine.`)
49+
// eslint-disable-next-line no-throw-literal
50+
if (!engine.methods[func]) throw { type: 'Unknown Operator', key: func }
4951
if (engine.methods[func].lazy) return typeof engine.methods[func][Sync] === 'function' ? engine.methods[func][Sync](lower, buildState) : engine.methods[func][Sync]
5052
return typeof engine.methods[func][Sync] === 'function' ? engine.methods[func][Sync](lower, buildState) : engine.methods[func][Sync] && isSyncDeep(lower, engine, buildState)
5153
}

logic.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ class LogicEngine {
6565

6666
if (this.isData(logic, func)) return logic
6767

68-
if (!this.methods[func]) throw new Error(`Method '${func}' was not found in the Logic Engine.`)
68+
// eslint-disable-next-line no-throw-literal
69+
if (!this.methods[func]) throw { type: 'Unknown Operator', key: func }
6970

7071
// A small but useful micro-optimization for some of the most common functions.
7172
// Later on, I could define something to shut this off if var / val are redefined.

optimizer.js

+5
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,8 @@ export function optimize (logic, engine, above = []) {
195195
const keys = Object.keys(logic)
196196
const methodName = keys[0]
197197

198+
if (keys.length === 0) return logic
199+
198200
const isData = engine.isData(logic, methodName)
199201
if (isData) return () => logic
200202

@@ -207,6 +209,9 @@ export function optimize (logic, engine, above = []) {
207209
if (deterministic) return result()
208210
return result
209211
}
212+
213+
// eslint-disable-next-line no-throw-literal
214+
throw { type: 'Unknown Operator', key: methodName }
210215
}
211216

212217
return logic

suites/bad.json

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[
2+
{
3+
"description": "Single key object, does not map to operator",
4+
"rule": { "UnknownOperator": true },
5+
"data": null,
6+
"error": { "type": "Unknown Operator", "key": "UnknownOperator" }
7+
},
8+
{
9+
"description": "Multi-Key Object",
10+
"rule": { "UnknownOperator": true, "UnknownOperator2": true },
11+
"data": null,
12+
"error": {}
13+
}
14+
]

0 commit comments

Comments
 (0)