millermedeiros / rocambole

Recursively walk and transform EcmaScript AST
171 stars 12 forks source link

RegExp + Esprima bug (only on browser) #6

Closed millermedeiros closed 10 years ago

millermedeiros commented 11 years ago

there is some weird bug on Esprima itself related to RegExp and running it on the browser.

Paste jQuery 1.7.2 here http://esprima.org/demo/parse.html and check the "tokens" tab, the tokens inside the range 1716-1755 are messed up, there is a "Punctuator" with value "/" which shouldn't be there and the "," is at the wrong index as well (it should come after RegExp)… also since there is a gap in the "range" between "/" and "," there is a "WhiteSpace" token being created by rocambole which contains the whole RegExp, so calling toString() will give the wrong output (RegExp is duplicated):

    // Check if a string has a non-whitespace character in it
    rnotwhite = /\S/,/\S/,

    // Used for trimming whitespace
    trimLeft = /^\s+/,/^\s+/,

PS: we need to report this bug to esprima.

millermedeiros commented 11 years ago

the tokens issue can be easily replicated:

var rnotwhite = /\S/;

generates:

[
    {
        "type": "Keyword",
        "value": "var",
        "range": [
            0,
            3
        ]
    },
    {
        "type": "Identifier",
        "value": "rnotwhite",
        "range": [
            4,
            13
        ]
    },
    {
        "type": "Punctuator",
        "value": "=",
        "range": [
            14,
            15
        ]
    },
    {
        "type": "Punctuator",
        "value": "/",
        "range": [
            16,
            17
        ]
    },
    {
        "type": "Punctuator",
        "value": ";",
        "range": [
            20,
            21
        ]
    },
    {
        "type": "RegularExpression",
        "value": "/\\S/",
        "range": [
            16,
            20
        ]
    }
]

Note that tokens aren't sorted by "range" and that { "type": "Punctuator", "value": "/", "range": [ 16, 17 ] } should not exist

millermedeiros commented 11 years ago

submitted the issue to Esprima, now lets hope they fix it: https://code.google.com/p/esprima/issues/detail?id=407

millermedeiros commented 10 years ago

since it's not my fault, and was already reported to esprima, I'm going to close it.