All refactorings are available through the Command Palette.
Some refactorings have default keybindings configured, but you can change that.
All other refactorings are available through VS Code Quick Fixes. You can access them by clicking on the lightbulb that appear next to the code 💡 or use the default shortcut Alt ↵
.
Pro Tip: You can also disable the Quick Fixes you never use in VS Code settings 🔥 (look for Abracadabra)
- The Essentials:
- Simplifying Conditional Logic:
- Invert Boolean Logic
- Remove Redundant Else
- Simplify Boolean
- Simplify Ternary
- Flip If/Else
- Flip Ternary
- Flip Operator
- Convert If/Else to Ternary
- Convert Ternary to If/Else
- Convert If/Else to Switch
- Convert Switch to If/Else
- Split If Statement
- Merge If Statements
- Merge With Previous If Statement
- Lift Up Conditional
- Moving Features:
- Organizing data:
- Working around the syntax:
- Specific to TypeScript:
- Specific to JSX:
Keybinding |
---|
F2 |
A
Symbol
is typically a variable or a function name.
This refactoring allows you to rename things and make sure all references in your code follow! It's easier and safer to use than a classic "Find and Replace".
VS Code does this refactoring very well. That's why this refactoring is merely an alias. It delegates the work to VS Code.
Note that it handles .vue
and .svelte
files with a similar UX while VS Code doesn't handle it natively yet.
For Vue and Svelte files, the support is limited: it can only rename within the <script>
tag. It won't rename your identifier in the <template>
tag for instance.
Keybinding | On Mac |
---|---|
Ctrl + Alt + V |
⌥ ⌘ V |
This refactoring helps you give a meaning to the hardcoded constants and low-level expressions. It makes your source code easier to read and maintain.
It will extract the closest element from your cursor or partial selection.
It will also handle multiple occurrences.
Keybinding | On Mac |
---|---|
Ctrl + Alt + V |
⌥ ⌘ V |
This does exactly the same as Extract Variable, but for types!
💡 Available as Quick Fix (
Alt ↵
)
Keybinding | On Mac |
---|---|
Ctrl + Alt + N |
⌥ ⌘ N |
This refactoring is the opposite of Extract Variable. It replaces a redundant usage of a variable or a constant with its initializer. It's usually helpful to inline things so you can extract them differently.
Keybinding | On Mac |
---|---|
Ctrl + Alt + N |
⌥ ⌘ N |
This refactoring is similar to Inline Variable, but for functions. It replaces each call to the function with the function body. It helps to remove needless indirections.
💡 Available as Quick Fix (
Alt ↵
)
This refactoring allows you to add, remove, or change the order of a function parameters. It will resolve and update the references to that function.
It supports function declarations, arrow functions, and class methods.
Keybinding |
---|
Alt + Shift + U |
A
Statement
is typically a variable or a function declaration.
Moves the whole selected statement up. If the selected statement and the one above are one-liners, this is the same as doing VS Code Move Line Up. But if one of these statements is multi-lines, this refactoring is very handy!
As for all refactorings, it works even if you partially select the statement, or if the cursor is on the statement.
Keybinding |
---|
Alt + Shift + D |
Same as Move Statement Up, but it moves the selected statement down. Like, the other direction. That's it.
Move Statement Up and Move Statement Down also work on object properties. They always produce valid code, so you don't have to bother with the trailing comma anymore!
💡 Available as Quick Fix (
Alt ↵
)
Inverts the logical expression while preserving behaviour. It can be useful to tweak a logical expression before extracting meaningful chunks out of it.
This refactoring follows De Morgan's laws.
It will invert the closest expression from your cursor or partial selection.
💡 Available as Quick Fix (
Alt ↵
)
Removes the else
keyword when it's not necessary, resulting in less nested code. This refactoring helps you replace nested conditional with guard clauses to make your code easier to read.
💡 Available as Quick Fix (
Alt ↵
)
Simplify boolean expressions that you might end up with after executing other refactorings.
💡 Available as Quick Fix (
Alt ↵
)
Simplify ternary expressions that you might end up with after executing other refactorings.
💡 Available as Quick Fix (
Alt ↵
)
Flips the if
and else
statements. It's a useful refactoring to have in your toolbelt to simplify logical expressions.
💡 Available as Quick Fix (
Alt ↵
)
Flips a ternary statement. It's really similar to Flip If/Else refactoring.
💡 Available as Quick Fix (
Alt ↵
)
Flips the left and right side of an operator. Very handy to introduce Yoda Conditions in the code.
💡 Available as Quick Fix (
Alt ↵
)
Converts an if/else statement into a (shorter) ternary expression. This is very handy to improve code readability.
💡 Available as Quick Fix (
Alt ↵
)
Converts a ternary expression into an if/else statement. It reverses Convert If/Else to Ternary refactoring.
💡 Available as Quick Fix (
Alt ↵
)
Converts an if/else statement into a switch statement. This is typically what you do before introducing polymorphism to clean object-oriented code.
💡 Available as Quick Fix (
Alt ↵
)
Converts a switch statement into an if/else statement. It reverses Convert If/Else to Switch refactoring.
💡 Available as Quick Fix (
Alt ↵
)
Splits the logical expression of the closest if statement. This is an helpful tool to help you refactor complex branching logic, safely.
💡 Available as Quick Fix (
Alt ↵
)
This is the opposite of Split If Statement. It consolidates conditional expressions to clean up the code.
It also works with else-if
.
It also handles consecutive if statements that can be merged.
💡 Available as Quick Fix (
Alt ↵
)
Merges selected statement with the if statement that is above. This is handy when you want to decompose a conditional to clean the code.
If you want to merge 2 consecutive if statements, it will resolve the dead code for you:
💡 Available as Quick Fix (
Alt ↵
)
Useful when you need to have the similar conditionals at the top level. If you get there, you'll be able to convert them into a top-level switch
statement, which you can easily refactor with polymorphism.
Hocus, pocus… This refactoring takes care of the gymnastic for you! Resulting code will have the same behaviour.
💡 Available as Quick Fix (
Alt ↵
)
Sometimes, Abracadabra can determine that some code can't be reached. If so, it can also get rid of the dead code for you.
💡 Available as Quick Fix (
Alt ↵
)
Splits the declaration of the variable and its initialization. If it's a const
, it will convert it to let
.
💡 Available as Quick Fix (
Alt ↵
)
Splits multiple variables declarated together onto a single line each.
💡 Available as Quick Fix (
Alt ↵
)
Converts the declaration of a variable that is a let
to a const
if it's not mutated within the scope.
💡 Available as Quick Fix (
Alt ↵
)
Did you know you could write 10_000
instead of 10000
? Well, now you know. And you can make code easier to read with 2 keystrokes!
💡 Available as Quick Fix (
Alt ↵
)
Converts a function declaration into an arrow function, which is convenient when you want to switch the syntax.
💡 Available as Quick Fix (
Alt ↵
)
Sometimes you need to add braces before you add more code. Other times you don't need them and prefer to get rid of them.
This refactoring allows you to toggle the braces on the closest statement of your cursor!
It works on:
- If Statements (both
if
andelse
independently) - Arrow Function Expressions (e.g.
const someFunction = () => {}
) - JSX Attributes (e.g.
<SomeComponent anAttribute={"a value"} />
) - Loops (for and while blocks)
💡 Available as Quick Fix (
Alt ↵
)
This refactoring is already handled by VS Code.
But there's one scenario they don't want to handle: convert simple strings into template literals.
This is too bad because it's convenient to turn an existing string into a template literal to start adding some variables inside.
Hence, Abracadabra is proposing the refactoring for such scenario!
By default, this refactoring will automatically apply if you write ${}
in a string literal. This is convenient behavior in most scenarios, but technically changes code behaviour.
If you want to disable the automatic transformation, you can turn OFF abracadabra.autoConvertToTemplateLiteral
in VS Code settings.
💡 Available as Quick Fix (
Alt ↵
)
This one might seem obscure, but it's really replacing +
with +=
. Whenever it's possible, Abracadabra will propose you to refactor the code for a shorter (assignment) syntax.
💡 Available as Quick Fix (
Alt ↵
)
When it's possible, it converts an old-school for-loop into a for-Each()
call.
💡 Available as Quick Fix (
Alt ↵
)
When it's possible, it converts a forEach()
into a for-of
loop.
💡 Available as Quick Fix (
Alt ↵
)
Create a factory function to instantiate the selected class. This can be useful when you want to expose a regular function while using a class behind the hood.
Regular functions don't need new
to be invoked, which makes them easier to compose around.
💡 Available as Quick Fix (
Alt ↵
)
This refactoring will turn an existing type into a generic. Very handy when you need to make an interface more generic.
💡 Available as Quick Fix (
Alt ↵
)
Extract the interface from a class.
This is very useful when you need to invert a dependency: create an interface from an existing class, so you can provide a different implementation of this interface.
💡 Available as Quick Fix (
Alt ↵
)
Handy when you need to add another JSX element next to the one you have, but keep a single root!
💡 Available as Quick Fix (
Alt ↵
)
Reverse operation of "Wrap in JSX Fragment". Useful when something is unnecessarily nested inside a fragment.
It won't remove a fragment that's required though: only the ones that have a single child node.