-
Notifications
You must be signed in to change notification settings - Fork 4
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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})$`) | ||
if ((match = exp.match(expRE))) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can u autoformat this new code only? like theres double |
||
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` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, we were using template strings in this module before. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} |
There was a problem hiding this comment.
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