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

Evaluator fails to walk a member with object expressions with an Identifier property #1225

Closed
jesus-seijas-sp opened this issue Dec 2, 2022 · 1 comment

Comments

@jesus-seijas-sp
Copy link
Contributor

Describe the bug
Evaluator fails to walk a member with object expressions with an Identifier property

To Reproduce
Steps to reproduce the behavior:

const { Evaluator } = require('@nlpjs/evaluator');

const evaluator = new Evaluator();

const script = `
obj[""+"x"].y;
({ a: 5, b: 7 })['a'];
({ a: 5, b: 7 })[n]
`;
const context = { obj: { x: { y: 555 } }, n: 'a' };
const answer = evaluator.evaluateAll(script, context);
console.log(answer); 

It shows in console [ 555, 5, undefined ]

Expected behavior
It should show in console [555, 5, 5]

Additional context
The walkMember should avoid the shortcut by property name and resolve the identifier by walk when the object type is ObjectExpression
Here:
https://github.com/axa-group/nlp.js/blob/master/packages/evaluator/src/evaluator.js#L182
And here:
https://github.com/axa-group/nlp.js/blob/master/packages/evaluator/src/javascript-compiler.js#L239

 if (node.property.type === 'Identifier') {
      return obj[node.property.name];
    }

Should be:

    if (node.property.type === 'Identifier' && node.object.type !== 'ObjectExpression') {
      return obj[node.property.name];
    }
@ericzon
Copy link
Collaborator

ericzon commented Dec 23, 2022

Thanks @jesus-seijas-sp , improvement applied in e11249f. We'll publish a version before ending this year.

@ericzon ericzon closed this as completed Dec 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants