Git Product home page Git Product logo

esrefactor's Introduction

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.

esrefactor's People

Contributors

ariya avatar constellation avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.