Export to GitHub

esprima - issue #468

Regular espression is not preserved in esparse JSON AST


Posted on Nov 5, 2013 by Helpful Hippo

What steps will reproduce the problem? 1. create a file.js with "var a = /[a-z]/;" 2. esparse file.js

What is the expected output?

{ "type": "Program", "body": [ { "type": "VariableDeclaration", "declarations": [ { "type": "VariableDeclarator", "id": { "type": "Identifier", "name": "a" }, "init": { "type": "Literal", "value": "/[a-z]/", "raw": "/[a-z]/" } } ], "kind": "var" } ] }

What do you see instead?

{ "type": "Program", "body": [ { "type": "VariableDeclaration", "declarations": [ { "type": "VariableDeclarator", "id": { "type": "Identifier", "name": "a" }, "init": { "type": "Literal", "value": {} } } ], "kind": "var" } ] }

What version of the product are you using? On what operating system? Which browser?

1.0.4 and 1.1.0

Please provide any additional information below.

The online parser uses the JSON.stringify "replacer" argument that calls the .toString() on the regular expression. I think "esparse" should do this too.

I added a .diff adding the replacer function.

Using that will fix "esparse file.js | uglifyjs --spidermonkey".

Attachments

Comment #1

Posted on Nov 27, 2013 by Quick Rabbit

esparse should handle regular expression literals. https://github.com/ariya/esprima/commit/79fbc3a4fc

Status: Fixed

Labels:
Type-Defect Priority-Medium Component-Example