Git Product home page Git Product logo

js-refactor's Introduction

JS Refactor :: JS CodeFormer

JS Refactor has been retired. This project is now an extension installer for its replacement, JS CodeFormer. If you install this extension, you will get that one.

You can install JS CodeFormer directly:

https://marketplace.visualstudio.com/items?itemName=cmstead.js-codeformer

The Good News/Bad News Situation

The good news, this project will install JS CodeFormer for you, and you will get nearly the same hotkeys you've developed muscle memory for. The bad news, this is not the old project.

More good news: JS CodeFormer is a significant improvement in a number of ways:

  • It's faster -- a LOT faster. In just about every way.
  • It has a broader language footprint -- JSR at its best supported standard JavaScript and some TypeScript if it wasn't too fancy. JS CodeFormer can parse and interpret a large number of languages and framework-specific file formats, including JS/TS embedded in HTML and HTML-like files.
  • It's designed for stability and maintenance -- The source code is well tested, and designed for quick easy fixes, which has already borne fruit.
  • The old JSR bugs are gone -- I combed through the old issues and either verified they were gone, or fixed them if they were a design oversight.

The Highlights

Language Support

Languages:

  • JavaScript
  • TypeScript
  • HTML (Embedded Javascript)

Frameworks:

  • Angular
  • React
  • Vue

Framework-specific formats:

Experimental framework support:

Refactorings

  • Extract Method/Function
    • Windows: ctrl+shift+j, m
    • Mac: cmd+shift+j, m
  • Extract to Parameter
    • Windows: ctrl+shift+j, p
    • Mac: cmd+shift+j, p
  • Extract Variable
    • Windows: ctrl+shift+j, v
    • Mac: cmd+shift+j, v
  • Inline Variable
    • Windows: ctrl+shift+j, i
    • Mac: cmd+shift+j, i
  • Rename (important for non js/ts files)
    • Windows: ctrl+shift+j, r
    • Mac: cmd+shift+j, r

Conversions

  • Change Variable Type
  • Convert Expression to Template Literal
  • Convert Function to Arrow Function
  • Convert Function to Function Expression
  • Convert Function Property to Method
  • Convert Function Variable to Function Declaration
  • Toggle Property Declaration Type

Other Actions

  • Surround with (open options list)
    • Windows: ctrl+shift+j, w
    • Mac: cmd+shift+j, w
  • Introduce variable
  • Introduce function
  • Lift and name function expression

Why (The Life Story)

JS Refactor was created at a time when VS Code was new and the refactoring landscape in the editor was limited. It was initially designed to simply be a refactoring extension. As time went along, it grew, and extended beyond being a simple refactoring tool.

Meanwhile the codebase was increasingly difficult to maintain. It was the first extension I authored for VS Code, and, even with significant work to improve the codebase, the problems were built too deeply into the core.

Eventually JS Refactor broke and I didn't have the energy to deal with the fallout anymore. I stopped most work for almost 2 full years. Once I resurfaced, I realized the only way out was to replace the original and build a system around certain principles which would lead to software which would work reliably, and could be maintained at a sustainable pace.

I started work on JS CodeFormer. Now that JS CodeFormer is in a stable initial state, it is time to give JS Refactor an honorable send-off. Instead of leaving a project to languish, it made more sense to send people where they can get something arguably better. This led me to the state of the software you see today.

Thank you to everyone who joined me on the initial journey. I hope you all will come and join me as we build better software together!

js-refactor's People

Contributors

adimasci avatar alirezaghey avatar andys8 avatar cmstead avatar cstead-hunter avatar dibikhin avatar myfairsyer avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

js-refactor's Issues

Integrate code analysis using Esprima

Install and integrate Esprima for code analysis to handle refactorings which are more complex than simple templates and single-line search/rewrite.

Convert to Lambda

Convert named or anonymous function to fat-arrow lambda notation

function () {
    // function body
}

Converts to

() => { // function body }

Negate Boolean Expression

This is an issue coming out of the issue: #31

Code update should negate a boolean expression. The cases will work as follows:

  • simple condition: cond -> !cond
  • multiple conditions: cond1 && cond2 -> !(cond1 && cond2)
  • negate a negation: !cond -> cond
  • negate a negated multiple condition expression: !(cond1 && cond2) -> (cond1 && cond2)
  • negating a parenthetically grouped condition: (cond1 && cond2) -> !(cond1 && cond2)

Lift and name function

Selecting this or function body only

ee.on('event', function(params) {
    // do something
});

and firing convert to named function should give this:

function new_handler (params) {
    // do something
}
mediator.on('event', new_handler);

Context: I often start with closures. When they get too complex I start to refactor them by moving to named functions.

Convert To Named Function

Need action which converts anonymous function assigned to a variable to a fully qualified named function with no assignment.

var myFn = function () {
    // do stuff
}

Converts to:

function myFn () {
    // do stuff
}

Refactorings unavailable for .jsx files?

Probably my newbie-ness, but I've noticed that no refactorings seem to be available for .jsx files? If I rename the file to .js, then the refactoring commands appear.

Might it be possible to add .jsx as a compatible file type?

Inline variable

Take a variable and inlines the value

function myFn () {
    var foo = 'my value';
    console.log(foo);
    return foo;
}

converts to

function myFn () {
    console.log('my value');
    returnm 'my value';
}

Keyboard shortcuts aren't working

As an example, when I try to extract a variable and press ctrl+shift+j v, I get the following warning:

WARN: command 'cmstead.jsRefactor.extractVariable' not found

This goes for other keyboard shortcuts as well, such as extracting methods.

I am using the following:
vscode: version 1.19.2
JS Refactor: version 2.11.4

Multi-Selection

It would be nice if convert operations would work with multi-selection.

Convert To Named Function: should it convert to named function?

When I select all the code or just the function declaration here:

ee.on('event', function(params) {
// do something
});

And hit shift+ctrl+j shift+ctrl+n it only shows me a notification No appropriate anonymous or member function to convert. Should it give me lines below?

function new_handler (params) {
    // do something
}
mediator.on('event', new_handler);

I think that Convert To Named Function should extract a function from a selected code.

add wrap in anonymous function, wrap in arrow function, wrap in generator function, wrap in async function

Showcase

after(mock.stopAll);

// selecting mock.stopAll and using wrap in arrow function should produce
after(() => {
  mock.stopAll();
});

// selecting mock.stopAll and using wrap in anonymous function should produce
after(function () {
  mock.stopAll();
});

// selecting mock.stopAll and using wrap in generator function should produce
after(function *() {
  mock.stopAll();
});

// selecting mock.stopAll and using wrap in anonymous async function should produce
after(async function () {
  mock.stopAll();
});

Would love to be able to do that with your plugin.

Convert to JS template literal

It would be cool to be able to convert stuff like "test"+i to test${i} automatically (markdown swallows the template literal quotes, I hope you know what I mean).

Switch back to Babylon... again

Switching to Babylon will allow for support of Typescript, Flow and a variety of late-proposal level syntax which is not currently supported by Esprima. This may require a relatively large effort, so it may take some time.

Convert to Member Function

Convert named function to a prototype object member function:

{
    function foo () {}
}

Converts to:

{
    foo: function () {}
}

Context Menu Options

Basic idea: Add refactoring options to context menu in editor

I am adding this enhancement because it is, by far, the most requested item and I have no idea how to implement it. I am currently seeking an answer (if there is one to be had) but if someone has any information to help, it would be greatly appreciated.

If you have any ideas, please investigate and share

Snippet fixes

Requested: remove names in snippet templates, found harmful

iife: fix missing characters

Extract variable

Extract value as a variable:

function myFn () {
    console.log('my value');
    return 'my value';
}

Extraction:

function myFn () {
    var foo = 'my value';
    console.log(foo);
    return foo;
}

Extract variable not working inside arrow functions

Hi, this extensions looks great.

I found an issue when I was trying to extract a value to a variable. It's working with ES5 functions, but not with arrow functions.

How to reproduce

Extracting the string 'param' to a variable won't work in this example.

const arrorFunc = (input) => {
  this.arrowFunc('param');
};

You'll get:
image

Special case of Wrap behavior - Cast As

Should allow type casting for:

Number, Boolean, String, Object

The result would look like this:

Number(value)
Boolean(value)
String(value)
Object(value)

Item of note, any value which is cast to object will need to be accessed by valueOf, i.e. castValue.valueOf()

Extract variable scope finding issue

When trying to extract a variable in a closure, it appears the scope seeking behavior is not always correct. Though this does not seem to affect most common use cases, it is an issue which needs to be investigated and remedied in time.

Export this function

Select a function and add it to module exports if no exports object exists create a new entry.

Install silently fails on Mac OS

The install appears to work, but after reload the commands cannot be found from keybindings.

I attempted to manually install all packages via npm i but that fails on [email protected]

I've commented on an existing issue for Edge about the problem (including some potential workarounds that didn't).

This appears to be an issue with Edge or an Edge dependency.

Indentation in IIFE

When you wrap a function that contains an if conditional, the indentation of the if gets messed um.

Rebind unbound variables

When a function is wrapped in an executed function, offer an option to rebind unbound variables within the function.

console.log(foo, bar);

Without rebinding (current behavior):

function myFn () {
    console.log(foo, bar);
}

myFn();

With rebinding:

function myFn (foo, bar) {
    console.log(foo, bar);
}

myFn(foo, bar);

VS 2017 refactoring and pulling code to functions

Hi, I have a lot of inplace anonymous functions, and this is great.

I was trying to install into VS 2017 with little success, I was wondering how to pull that code out to named functions, with good practices.

Namespace actions in command palette

Update command palette action names to be namespaced with JS Refactorings: so they all come up together. This will help when people can't remember what options they have while research for the context menu is ongoing.

Wrap in Conditional

Wrap code in an if block:

// code doing stuff

wraps ->

if (predicate) {
    // code doing stuff
}

Introduce function

Select a text, hit 'introduce function', new function created with name as the selected text.

Having this:

var foo = {
    bar: quux
};

Selecting quux and hitting introduce function should lead to:

function quux() {
    <cursor>
}
var foo = {
    bar: quux
};

"Negate if" refactoring

This refactoring reduces nesting level, makes flow easier to understand

Before

function foo() {
  if (cond) {              // <-- Caret is here
    bar()
  }
}

After

function foo() {
  if (!cond) {
    return
  }
  bar();
}

Can't extract variable if it is not inside a function

It seems like this extension (perhaps?) doesn't like ES6. When I tried to extract "new Foo(SOME_CONSTANT)" from:

describe('some test', () => {
  it('does something', async () => {
    const result = await new Foo(SOME_CONSTANT).perform();
    console.log(result);
  });
});

I got the error "Cannot extract variable if it is not inside a function". Since the variable I'm trying to extract is inside a function, but it's in an arrow function and not a function function, it seems like the problem might be that the extension isn't treating arrow functions the same as their older function versions.

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.