expression-namer
v1.1.1
Published
Get a sensible name for English JavaScript variable names.
Downloads
16
Maintainers
Readme
Expression Namer
This library provides plausible names for JavaScript expressions. For example,
given the expression view.width
good names might be "width" and "viewWidth".
This could potentially be used in part to build an IDE's "extract variable"
feature, or any feature that requires source code transformation.
Install
Via npm:
$ npm install expression-namer
Usage
This library is designed to be used with the AST nodes generated by esprima.
Here's an example of using esprima to get the names suggested above for
view.width
:
> allNamesForExpression = require('expression-namer').allNamesForExpression
> parse = require('esprima').parse
> allNamesForExpression(parse('view.width').body[0].expression)
[ 'width', 'viewWidth' ]
Note that we did not use the root object returned by parse
. That is because
that node is of type Program
, which is not an expression. We needed to get the
program's body's (.body
) first statement's ([0]
, an ExpressionStatement
)
expression (.expression
).
This library can be used with all kinds of expressions, and has special handling
for some types. For example, CallExpression
s like getFirstPersonById(1)
are
treated specially:
> allNamesForExpression = require('expression-namer').allNamesForExpression
> parse = require('esprima').parse
> allNamesForExpression(parse('getFirstPersonById(1)').body[0].expression)
[ 'firstPerson', 'result', 'getFirstPersonByIdResult' ]
Notice that the "get" verb and the "ById" prepositional phrase are removed from the preferred expression name result.