Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add lexical validation for variable not present #5

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion src/syntax.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,41 @@ function isFalsy (val) {
return val === false || undefined === val || val === null
}

function validateExpression(exp, scope, errors = []) {
assert(scope, 'unable to evalExp: scope undefined')
var operatorREs = lexical.operators
var match;
for (var i = 0; i < operatorREs.length; i++) {
var operatorRE = operatorREs[i]
var expRE = new RegExp(`^(${lexical.quoteBalanced.source})(${operatorRE.source})(${lexical.quoteBalanced.source})$`)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cant use template strings - this does not go through a transpiler

if ((match = exp.match(expRE))) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can u autoformat this new code only?

like theres double (( here for no reason

errors.concat(validateExpression(match[1], scope, errors), validateExpression(match[3], scope, errors));
return errors;
}
}
if((error = validateValue(exp, scope))) {
errors.push(error);
}
return errors;
}

function validateValue (str, scope) {
str = str && str.trim()
if (!str) return `Invalid Operator Usage`;

if (lexical.isLiteral(str)) {
return;
}
if (lexical.isVariable(str)) {
if(scope.get(str) !== undefined && scope.get(str) !== null) {
return;
} else {
return `${str} variable not present`

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't use template strings?

i am not sure if they are safe. if we were using them before then you can keep them

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, we were using template strings in this module before.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK LGTM format if possible

}
}
return `cannot eval '${str}' as value`
}

module.exports = {
evalExp, evalValue, isTruthy, isFalsy
evalExp, evalValue, isTruthy, isFalsy, validateExpression, validateValue
}
19 changes: 19 additions & 0 deletions test/syntax.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var Scope = require('../src/scope.js')
var evalExp = syntax.evalExp
var evalValue = syntax.evalValue
var isTruthy = syntax.isTruthy
var validateExp = syntax.validateExpression

describe('expression', function () {
var scope
Expand Down Expand Up @@ -74,6 +75,24 @@ describe('expression', function () {
expect(evalExp('"<=" == "<="', scope)).to.equal(true)
})

it('should validate simple expression', function () {
expect(validateExp('1<2', scope)).to.deep.equal([]);
expect(validateExp('1<!2', scope)).to.deep.equal(["cannot eval '!2' as value"]);
expect(validateExp('y<2', scope)).to.deep.equal(["y variable not present"]);
expect(validateExp('y<!2', scope)).to.deep.equal(["y variable not present", "cannot eval '!2' as value"]);
expect(validateExp('x<2', scope)).to.deep.equal([]);
expect(validateExp('x!=<2', scope)).to.deep.equal(["Invalid Operator Usage"]);
})

it('should validate complex expression', function () {
expect(validateExp('1<2 and 2<3', scope)).to.deep.equal([]);
expect(validateExp('1<!2 and 2<3', scope)).to.deep.equal(["cannot eval '!2' as value"]);
expect(validateExp('y<2 and z<3', scope)).to.deep.equal(["y variable not present", "z variable not present"]);
expect(validateExp('y<!2 and z<3', scope)).to.deep.equal(["y variable not present", "cannot eval '!2' as value", "z variable not present"]);
expect(validateExp('x<2 and x<3', scope)).to.deep.equal([]);
expect(validateExp('x!=<2 and x<===3', scope)).to.deep.equal(["Invalid Operator Usage", "Invalid Operator Usage"]);
})

describe('complex expression', function () {
it('should support value or value', function () {
expect(evalExp('false or true', scope)).to.equal(true)
Expand Down