You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Dmitri Blinov (Jira)" <ji...@apache.org> on 2019/12/03 08:38:00 UTC

[jira] [Comment Edited] (JEXL-307) Variable redeclaration option

    [ https://issues.apache.org/jira/browse/JEXL-307?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16986701#comment-16986701 ] 

Dmitri Blinov edited comment on JEXL-307 at 12/3/19 8:37 AM:
-------------------------------------------------------------

Maybe I do not fully understand what the lexical feature is all about, but from the previous test case I've learned the idea was to control the access to undeclared local variables. If I'm right, then the following test case should also pass, but it doesn't.
{code:java}
    @Test
    public void testForVariable() throws Exception {
        JexlFeatures f = new JexlFeatures();
        f.lexical(true);
        JexlEngine jexl = new JexlBuilder().strict(true).features(f).create();
        try {
            JexlScript script = jexl.createScript("for(var x : 1..3) { var c = 0}; return x");
            Assert.fail("Should not have been parsed");
        } catch (Exception ex) {
           // OK
        }
    }
 {code}
Compare this to test case that passes correctly:
{code:java}
    @Test
    public void testUndeclaredVariable() throws Exception {
        JexlFeatures f = new JexlFeatures();
        f.lexical(true);
        JexlEngine jexl = new JexlBuilder().strict(true).features(f).create();
        try {
            JexlScript script = jexl.createScript("{var x = 0}; return x");
            Assert.fail("Should not have been parsed");
        } catch (Exception ex) {
           // OK
        }
    }
{code}


was (Author: dmitri_blinov):
Maybe I do not fully understand what the lexical feature is all about, but from the previous test case I've learned the idea was to control the access to undeclared local variables. If I'm right, then the following test case should also pass, but it doesn't.
{code:java}
    @Test
    public void testForVariable() throws Exception {
        JexlFeatures f = new JexlFeatures();
        f.lexical(true);
        JexlEngine jexl = new JexlBuilder().strict(true).features(f).create();
        try {
            JexlScript script = jexl.createScript("for(var x : 1..3) { var c = 0}; return x");
            Assert.fail("Should not have been parsed");
        } catch (Exception ex) {
           // OK
        }
    }
 {code}

> Variable redeclaration option
> -----------------------------
>
>                 Key: JEXL-307
>                 URL: https://issues.apache.org/jira/browse/JEXL-307
>             Project: Commons JEXL
>          Issue Type: New Feature
>    Affects Versions: 3.1
>            Reporter: Dmitri Blinov
>            Assignee: Henri Biestro
>            Priority: Minor
>             Fix For: 3.2
>
>
> As of now, JEXL allows a script writer to redeclare a local variable during script evaluation.
> {code:java}
> var a = 1; var a = 2;{code}
> This may lead to potential errors with misspelled names and clashed variables. Checking for already defined variable is a common feature of many languages. This feature can be implemented in JEXL as an additional option of JexlFeatures class, enabled by default, thus allowing compatibility with existing code.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)