Socket
Socket
Sign inDemoInstall

esrefactor

Package Overview
Dependencies
3
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    esrefactor

ECMAScript refactoring helper


Version published
Weekly downloads
12K
decreased by-7.18%
Maintainers
1
Install size
930 kB
Created
Weekly downloads
 

Readme

Source

esrefactor (BSD licensed) is a little helper library for ECMAScript refactoring.

Usage

With Node.js:

npm install esrefactor

In a browser, include first all the dependents:

<!-- esrefactor depends on these libraries -->
<script src="esprima.js"></src>
<script src="estraverse.js"></src>
<script src="escope.js"></src>

<script src="esrefactor.js"></src>

API

Before using the API, a context needs to be created:

var ctx = new esrefactor.Context(code);

where code is the source code.

Identification

An identifier, whether it is a variable, a function name, or a function parameter, can be identified using identify(). Example:

var ctx = new esrefactor.Context('var x = 42; y = x * 2; z = x / 2');
var id = ctx.identify(4);

The only argument to identify is the zero-based position index.

The return object has 3 (three) properties:

  • identifier: the syntax node associated with the position
  • declaration: the declaration syntax node for the identifier
  • references: an array of all identical references

If there is no declaration for the identifier (e.g. x = 42, global leak), then declaration will be null.

The resolution of the declaration syntax node and the references array take into account the identifier scope as defined in the official ECMAScript 5.1 Specification (ECMA-262).

Note that if there is no identifier in the given position index, identify() will return undefined.

Renaming

An identifier can be renamed using rename(). All other identical references associated with that identifier will be renamed as well, again taking into account the proper identifier scope. Renaming works for variable declaration, function name, and function parameter.

For rename() to work, it needs to have the identification result (via identify) and the new name for the identifier.

var ctx = new esrefactor.Context('var x; x = 42');
var id = ctx.identify(4);
var code = ctx.rename(id, 'answer');

In the above example, code is var answer; answer = 42.

FAQs

Last updated on 25 Apr 2013

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc