Git Product home page Git Product logo

espression's People

Contributors

ianchi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

espression's Issues

ES5Eval was renamed?

The example in README doesn't run. It seems that ES5Eval had been renamed to ES5StaticEval. BTW, is there a complete doc for the API?

Question/docs: Is this any safer than eval'ing an unstrusted string directly?

My use case is that I would like to allow admin users of a service to define expressions that would evaluate objects to either pass or reject them. The expression would be evaluated server-side.

From the readme, it's not really clear whether passing an expression through this library is intended to make evaluating such an expression safe?

Provide a way to collect useful information (e.g. finding the names of variables) while parsing

I used to use jsep and I am switching to ESpression. I had my own eval function, which is already provided by ESpression, but I also had a function to find the names of variables used in an AST. This is useful to reevaluate some expressions when the referenced variables change. Could this feature be useful for other developers and be implemented in the module? Maybe by providing an optional parameter to parse that could collect some information, or call a handler for each visited node.

For information, this was my implementation to walk the AST and gather variable names:

function _vars(set, ...nodes)
{
    for (let node of nodes)
        switch (node.type) {

            case 'Literal':
            case 'ThisExpression':
                break;

            case 'Compound':
                _vars(set, ...node.body);
                break;

            case 'ArrayExpression':
                _vars(set, ...node.elements);
                break;

            case 'UnaryExpression':
                _vars(set, node.argument);
                break;

            case 'LogicalExpression':
            case 'BinaryExpression':
                _vars(set, node.left, node.right);
                break;

            case 'ConditionalExpression':
                _vars(set, node.test, node.consequent, node.alternate);
                break;

            case 'CallExpression':
                _vars(set, node.callee, ...node.arguments);
                break;

            case 'Identifier':
                set.add(node.name);
                break;

            case 'MemberExpression':
                switch (node.object.type) {
                    case 'Identifier':
                        set.add(node.object.name);
                        break;
                    case 'ThisExpression':
                        set.add(node.property.name);
                        break;
                    case 'MemberExpression':
                        _vars(set, node.object);
                        break;
                    default:
                        throw new Error(`Unhandled member expression ${node.object.type}`);
                }
                break;

            default:
                throw new Error(`unhandled node type ${node.type}`);
        }
}

export function vars(ast)
{
    const set = new Set();
    _vars(set, ast);
    return set;
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.