Skip to content

Commit

Permalink
feat: add logging to possible unknown expression evaluation
Browse files Browse the repository at this point in the history
  • Loading branch information
simonfan committed Feb 16, 2021
1 parent 7d76ca0 commit 55e873e
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 28 deletions.
9 changes: 9 additions & 0 deletions src/expression.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ describe('evaluateTyped(expectedTypes, context, value)', () => {
)
}).toThrow(TypeError)

const _warn = console.warn
console.warn = jest.fn()

expect(() => {
console.log(
evaluateTyped(
Expand All @@ -27,6 +30,12 @@ describe('evaluateTyped(expectedTypes, context, value)', () => {
)
)
}).toThrow(TypeError)

expect(console.warn).toHaveBeenCalledWith(
'Possible missing expression error: ["$someUnknownExpression"]. ' +
"No interpreter was found for '$someUnknownExpression'"
)
console.warn = _warn
})

test('array object type', () => {
Expand Down
45 changes: 38 additions & 7 deletions src/expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ export const isExpression = (
Array.isArray(candidateExpression) &&
typeof interpreters[candidateExpression[0]] === 'function'

/**
* @function evaluate
* @param {EvaluationContext} context
* @param {Expression | *} expOrValue
* @returns {*}
*/
export const evaluate = (
const _maybeExpression = (value) =>
Array.isArray(value) &&
typeof value[0] === 'string' &&
value[0].startsWith('$')

const _evaluateProd = (
context: EvaluationContext,
expOrValue: Expression | any
): any => {
Expand All @@ -38,6 +37,38 @@ export const evaluate = (
return interpreter(context, ...interpreterArgs)
}

const _ellipsis = (str, maxlen = 50) =>
str.length > maxlen ? str.substr(0, maxlen - 1).concat('...') : str

const _evaluateDev = (
context: EvaluationContext,
expOrValue: Expression | any
): any => {
if (
!isExpression(context.interpreters, expOrValue) &&
_maybeExpression(expOrValue)
) {
console.warn(
`Possible missing expression error: ${_ellipsis(
JSON.stringify(expOrValue)
)}. No interpreter was found for '${expOrValue[0]}'`
)
}

return _evaluateProd(context, expOrValue)
}

/**
* @function evaluate
* @param {EvaluationContext} context
* @param {Expression | *} expOrValue
* @returns {*}
*/
export const evaluate =
process && process.env && process.env.NODE_ENV !== 'production'
? _evaluateDev
: _evaluateProd

/**
* @function evaluateTyped
* @param {String | string[]} expectedTypes
Expand Down
42 changes: 28 additions & 14 deletions src/expressions/logical.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,21 +155,35 @@ describe('$and', () => {
})

// eslint-disable-next-line jest/no-disabled-tests
test.skip('value coercion on unknown expressions', () => {
expect(() => {
evaluate(
{
interpreters,
scope: {
$$VALUE: [
['$unknownExpression', 1, 2],
['$anotherUnknownExpression', null],
],
},
test('value coercion on unknown expressions / expression-like values should trigger warning', () => {
const warn_ = console.warn
console.warn = jest.fn()

evaluate(
{
interpreters,
scope: {
$$VALUE: [
['$unknownExpression', 1, 2],
['$anotherUnknownExpression', null],
],
},
['$and']
)
}).toThrow(TypeError)
},
['$and']
)

expect(console.warn).toHaveBeenNthCalledWith(
1,
'Possible missing expression error: ["$unknownExpression",1,2]. ' +
"No interpreter was found for '$unknownExpression'"
)
expect(console.warn).toHaveBeenNthCalledWith(
2,
'Possible missing expression error: ["$anotherUnknownExpression",null]. ' +
"No interpreter was found for '$anotherUnknownExpression'"
)

console.warn = warn_
})

test('w/ comparison', () => {
Expand Down
4 changes: 0 additions & 4 deletions src/expressions/logical.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ import { evaluate, interpreter } from '../expression'
import { Expression, EvaluationContext, PlainObject } from '../types'

/**
* @todo logical Better handle unknown expressions for boolean logical operators
* Uninterpreted expressions are returned as simple arrays, which
* in turn return mistaken true results
*
* @function $and
* @param {Array} expressionsExp
* @returns {Boolean}
Expand Down
4 changes: 1 addition & 3 deletions todo.md
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
- logical | Better handle unknown expressions for boolean logical operators
Uninterpreted expressions are returned as simple arrays, which
in turn return mistaken true results (src/expressions/logical.ts)

0 comments on commit 55e873e

Please sign in to comment.