Skip to content

Latest commit

 

History

History
356 lines (291 loc) · 7.34 KB

README.md

File metadata and controls

356 lines (291 loc) · 7.34 KB

jsref

JavaScript refactoring language server

Demo Animation

An idea behind this project is to have desirable refactoring experience for JavaScript (JSX, TypeScript, Flowtype) without tying to any editor.

This tool implements language server protocol (LSP) to avoid any direct binding to code editors. This means any editor with LSP support can use it.

It uses babylon parser to parse and generate JavaScript.

Supported refactorings:

Refactoring Code before Code after
const arrow to function declaration
const a = () => { return 1; }
function a() { return 1; }
experimental jest only test
it('hello', () => {})
it.only('hello', () => {})
experimental jest revert only
it.only('hello', () => {})
it('hello', () => {})
experimental jest revert skip test
it.skip("s", () => { hello(); })
it("s", () => { hello(); })
experimental jest skip test
it("s", () => { hello(); })
it.skip("s", () => { hello(); })
experimental use styled component
import r from 'r-dom'

const A = () => { return r.div() }

import r from 'r-dom'
import { styled } from "styletron-react";

const StyledDiv = styled("div", {});

const A = () => { return r(StyledDiv) }

explicit return to implicit
() => { return test() }
() => test()
extract return
return a + b
const result = a + b;

return result;

flip if else
if (a) {
  b()
} else {
  c()
}
if (!a) {
  c()
} else {
  b()
}
flip ternary
a ? b : c
!a ? c : b
function to arrow
const a = function () { return 1; }
const a = () => { return 1; }
implicit return to explicit
const foo = () => hello()
const foo = () => {
    return hello();
}
jsx expand empty tag
<input/>
<input></input>
replace with concatenation
`hello ${a}`
"hello " + a
require to import
const a = require('b')
import a from "b";

Installation

Vim and Neovim (via coc.nvim)

  1. Install coc.nvim plugin
  2. Run :CocInstall coc-jsref
  3. Configure hotkeys. For example to use ga:
nmap ga <Plug>(coc-codeaction-cursor)
xmap ga <Plug>(coc-codeaction-selected)

VSCode extension contains server and you don't need to install global one with brew.

Search in Extensions panel for jsref or install via CLI

code --install-extension slonoed.jsref

Sublime Text 3

Install jsref language binary via brew

brew install slonoed/tap/jsref

or npm

npm i -g @slonoed/jsref

Install LSP package from Package Control.

Add new client to LSP via Preferences: LSP Setting.

"jsref": {
  "command": ["jsref", "--stdio"],
	"scopes": ["source.js"],
	"syntaxes": [
		"Packages/babel-sublime/JavaScript (Babel).tmLanguage",
		"Packages/Babel/JavaScript (Babel).sublime-syntax",
		"Packages/JavaScript/JavaScript.sublime-syntax"
	],
	"languageId": "javascript",
},

Final config should look like this

{
  "clients": {
    "jsref": {
      "command": ["jsref", "--stdio"],
      "scopes": ["source.js"],
      "syntaxes": [
        "Packages/babel-sublime/JavaScript (Babel).tmLanguage",
        "Packages/Babel/JavaScript (Babel).sublime-syntax",
        "Packages/JavaScript/JavaScript.sublime-syntax"
      ],
      "languageId": "javascript"
    }
  }
}

Other editors

All other editors are supported via standard plugins for language servers.

jsref language server can be installed via brew

brew install slonoed/tap/jsref

or npm

npm i -g @slonoed/jsref

Help needed to add instructions for other editors.

Plans

  • Ability to create custom refactorings (per user and per workspace)
  • More refactorings! If you need some specific, create an issue

Development

Install deps npm i

coc.nvim extension

Build package make coc-pack

Add set runtimepath^=~/THISREPO/build/coc/ to vimrc or run as command.

Debug guide

Debug VScode extension

Install LSP Inspector. Run debug version with extension

make run-vscode

Debug server

Run jsbin with --lspi flag and running inspector.

Deploy

Release npm package

make npm-publish

Release coc packaged

make coc-publish

Release brew tap (after npm release)

Install noob package

brew install zmwangx/npm-noob/noob

Publishing

make brew-publish

Release vscode extension

make vscode-publish

Contributing

You can easily contribute by creating new kinds of refactoring. A good example can be found here. To avoid duplication, create an issue first.