Code Monkey home page Code Monkey logo

js3-refactor's Introduction

js3-refactor.el

A JavaScript refactoring library for emacs.

This is a collection of small refactoring functions to further the idea of a JavaScript IDE in Emacs that started with js3-mode[1].

[1]: This project is a //js3-mode// fork of original js2-refactor written by Magnar Sveen for //js2-mode//. Initially js2 was simply replaced by js3 in all files including this document. So when says js3-mode, it means js2-mode formerly, therefore don't be confused, reading this text below.

Breaking change in 0.6.0

You now choose your own keybinding scheme. If you just want what you had before this change, add this to your init:

(js3r-add-keybindings-with-prefix "C-c C-m")

See Setup keybindings below for more.

Installation

I highly recommend installing js3-refactor through elpa.

It's available on melpa:

M-x package-install js3-refactor

You can also install the dependencies on your own, and just dump js3-refactor in your path somewhere:

I also recommend that you get expand-region to more easily mark vars, method calls and functions for refactorings.

Then add this to your emacs settings:

(require 'js3-refactor)

Setup keybindings

All functions in js3-refactor have a two-letter mnemonic shortcut. For instance, extract-function is ef. You get to choose how those are bound. Here's how:

(js3r-add-keybindings-with-prefix "C-c C-m")
;; eg. extract function with `C-c C-m ef`.

If you would rather have a modifier key, instead of a prefix, do:

(js3r-add-keybindings-with-modifier "C-s-")
;; eg. extract function with `C-s-e C-s-f`.

If neither of these appeal to your sense of keyboard layout aesthetics, feel free to pick and choose your own keybindings with a smattering of:

(define-key js3-mode-map (kbd "C-c C-e C-f") 'js3r-extract-function)

Refactorings

  • ef is extract-function: Extracts the marked expressions out into a new named function.
  • em is extract-method: Extracts the marked expressions out into a new named method in an object literal.
  • ip is introduce-parameter: Changes the marked expression to a parameter in a local function.
  • lp is localize-parameter: Changes a parameter to a local var in a local function.
  • eo is expand-object: Converts a one line object literal to multiline.
  • co is contract-object: Converts a multiline object literal to one line.
  • wi is wrap-buffer-in-iife: Wraps the entire buffer in an immediately invoked function expression
  • ig is inject-global-in-iife: Creates a shortcut for a marked global by injecting it in the wrapping immediately invoked function expression
  • ag is add-to-globals-annotation: Creates a /*global */ annotation if it is missing, and adds the var at point to it.
  • ev is extract-var: Takes a marked expression and replaces it with a var.
  • iv is inline-var: Replaces all instances of a variable with its initial value.
  • rv is rename-var: Renames the variable on point and all occurrences in its lexical scope.
  • vt is var-to-this: Changes local var a to be this.a instead.
  • ao is arguments-to-object: Replaces arguments to a function call with an object literal of named arguments.
  • 3i is ternary-to-if: Converts ternary operator to if-statement.
  • sv is split-var-declaration: Splits a var with multiple vars declared, into several var statements.
  • uw is unwrap: Replaces the parent statement with the selected region.
  • lt is log-this: Adds a console.log statement for what is at point (or region).
  • sl is forward-slurp: Moves the next statement into current function, if-statement, for-loop or while-loop.

There are also some minor conveniences bundled:

  • C-S-down and C-S-up moves the current line up or down. If the line is an element in an object or array literal, it makes sure that the commas are still correctly placed.

Todo

A list of some wanted improvements for the current refactorings.

  • expand- and contract-object: should work for arrays.
  • expand- and contract-object: should work for simple functions.
  • wrap-buffer-in-iife: should skip comments and namespace initializations at buffer start.
  • extract-variable: could end with a query-replace of the expression in its scope.

Contributions

  • Matt Briggs contributed js3r-add-to-globals-annotation

Thanks!

Contribute

This project is still in its infancy, and everything isn't quite sorted out yet. If you're eager to contribute, please add an issue here on github and we can discuss your changes a little before diving into the elisp. :-)

To fetch the test dependencies, install carton if you haven't already, then:

$ cd /path/to/expand-region
$ carton

Run the tests with:

$ ./run-tests.sh

License

Copyright (C) 2012-2013 Magnar Sveen

Author: Magnar Sveen [email protected] Keywords: javascript refactorings

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

js3-refactor's People

Contributors

magnars avatar katyo avatar jazzzz avatar tarsius avatar mbriggs avatar

Watchers

James Cloos avatar Aleksey Petrov 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.