Extend ESPrima to understand `-!?` symbols in names?

140 views
Skip to first unread message

Alexey Petrushin

unread,
Nov 1, 2012, 9:33:13 PM11/1/12
to esp...@googlegroups.com
Hello, I'd like to use JavaScript transpiler and one feature is to allow `-!?` symbols in variable and property names (later, during AST parsing such names will be converted into camelCase to work with JS).

Is it possible and easy to extend ESPrima in such a manner?

Thanks!

Ariya Hidayat

unread,
Nov 1, 2012, 9:39:01 PM11/1/12
to esp...@googlegroups.com
> Is it possible and easy to extend ESPrima in such a manner?

I'm sure it's possible.




--
Ariya Hidayat, http://ariya.ofilabs.com
http://twitter.com/ariyahidayat

Yusuke Suzuki

unread,
Nov 1, 2012, 10:03:54 PM11/1/12
to esp...@googlegroups.com
It is possible to extend Esprima easily by extending this predicate :)
--
Regards,
Yusuke Suzuki

Alexey Petrushin

unread,
Nov 2, 2012, 12:23:13 AM11/2/12
to esp...@googlegroups.com
Wow, cool, thanks Yusuke! I was afraid that it take a lot of time to examine code and extend it properly, so simple!

It works :)

    var parse = require('esprima').parse;
    ast = parse('list.empty?()')
    console.log(JSON.stringify(ast, null, 2))

Output
    
    {
      "type": "Program",
      "body": [
        {
          "type": "ExpressionStatement",
          "expression": {
            "type": "CallExpression",
            "callee": {
              "type": "MemberExpression",
              "computed": false,
              "object": {
                "type": "Identifier",
                "name": "list"
              },
              "property": {
                "type": "Identifier",
                "name": "empty?"                <======== :)
              }
            },
            "arguments": []
          }
        }
      ]
    }

P.S. would be also nice if ESPrima expose its internals, like `isIdentifierPart` - then such extension would be as simple as:

    esprima.isIdentifierPartWithoutSpecialSymbols = esprima.isIdentifierPart
    esprima.isIdentifierPart = function(ch){
      return this.isIdentifierPartWithoutSpecialSymbols(ch) ||
      (ch === '-') || (ch === '?') || (ch === '!');
    }

Right now its closed in lexical scope and not available for extension.

Kris Kowal

unread,
Nov 2, 2012, 12:49:51 AM11/2/12
to esp...@googlegroups.com
I have a related request that Esprima expose a public method for
parsing a stand-alone function body, as would be done by the Function
constructor.

The parser options setup could be factored into a decorator function
that would accept any of the parser functions and return a function
with the same signature as Reflect.parse.

Kris Kowal

Gautham B A

unread,
Jan 3, 2017, 4:14:41 AM1/3/17
to esprima
The link seems to be broken. Could you please provide a live link?
Reply all
Reply to author
Forward
0 new messages