Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/slonoed/jsref

JavaScript refactoring language server
https://github.com/slonoed/jsref

coc coc-nvim javascript language-server-protocol refactoring vim vim-lsc vscode vscode-extension

Last synced: 3 months ago
JSON representation

JavaScript refactoring language server

Awesome Lists containing this project

README

        

# jsref

JavaScript refactoring language server

![Demo Animation](../assets/preview.gif?raw=true)

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][ls] (LSP) to avoid any direct binding to code editors.
This means any [editor with LSP support][ls-page] 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][coc-nvim-repo]
2. Run `:CocInstall coc-jsref`
3. Configure hotkeys. For example to use `ga`:

```
nmap ga (coc-codeaction-cursor)
xmap ga (coc-codeaction-selected)
```

### [VSCode][vscode-jsref-marketplace]

_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][new-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][coc-ls-debug]

### 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][fixer-example]. To avoid duplication, create [an issue][new-issue] first.

[js-refactor]: https://github.com/cmstead/js-refactor/blob/master/package.json
[babylon]: https://github.com/babel/babel/tree/master/packages/babylon
[lsc]: https://github.com/natebosch/vim-lsc
[jtl]: https://github.com/sourcegraph/javascript-typescript-langserver/blob/master/src/plugins.ts
[grasp]: http://www.graspjs.com/
[ls]: https://microsoft.github.io/language-server-protocol/
[ls-page]: https://langserver.org/
[vim-lsc]: https://github.com/natebosch/vim-lsc/tree/master/after/plugin
[new-issue]: https://github.com/slonoed/jsref/issues/new
[issue-atom]: https://github.com/slonoed/jsref/issues/3
[issue-emacs]: https://github.com/slonoed/jsref/issues/10
[issue-sublime]: https://github.com/slonoed/jsref/issues/7
[fixer-example]: https://github.com/slonoed/jsref/blob/master/src/fixers/implicit-return-to-explicit.ts
[vscode-jsref-marketplace]: https://marketplace.visualstudio.com/items?itemName=slonoed.jsref
[coc-ls-debug]: https://github.com/neoclide/coc.nvim/wiki/Debug-language-server
[coc-nvim-repo]: https://github.com/neoclide/coc.nvim