Level Up Your JavaScript With These 60 Quick Fixes for VS Code
Tags: javascript, webdev, vscode, typescript
Published: 2021-12-21 13:33:35


When programming, we often edit text directly to achieve our goals. Such manual editing can be slow and error-prone. For behavior-preserving changes (refactorings), **automation can save you time, ensure the transformation is executed correctly, and warn about potential breakages**. You can communicate your intention, e.g., "I want to extract this expression into a variable" more directly, and let the machine figure out the details.

I've written an [extension for Visual Studio Code called P42 JavaScript Assistant](https://marketplace.visualstudio.com/items?itemName=p42ai.refactor) that adds 60 automated refactorings, quick fixes, and cleanups for JavaScript and TypeScript to make your life easier. This blog post provides an overview of the different code actions and their categories. 

> 💡&nbsp;&nbsp;This blog post shows P42 JavaScript Assistant v1.64

But first, here is an example of how P42 works to give you an idea of how it can help you:

![P42 JavaScript Assistant](https://p42.ai/image/vscode/vscode-intro.gif)

## Core Refactorings
Visual Study Code already contains basic refactorings such as Rename and Extract Function. P42 adds additional refactorings or extended functionality such as safety checking.

* [Extract Variable](https://p42.ai/documentation/code-action/extract-variable)
* [Extract Substring into Variable](https://p42.ai/documentation/code-action/extract-substring-to-variable)
* [Inline Variable](https://p42.ai/documentation/code-action/inline-variable)
* [Inline into Template](https://p42.ai/documentation/code-action/inline-into-template)
* [Inline Return](https://p42.ai/documentation/code-action/inline-return)

## React Refactorings and Quick Fixes
In [React](https://reactjs.org/), components often contain [JSX](https://reactjs.org/docs/introducing-jsx.html), a syntax extension for JavaScript. P42 provides code actions that make working with JSX and React easier:

* [Extract React Function Component](https://p42.ai/documentation/code-action/extract-jsx-element)
* [Add {...} to JSX Attribute](https://p42.ai/documentation/code-action/add-braces-to-jsx-attribute) & [Remove {...} from JSX Attribute](https://p42.ai/documentation/code-action/remove-braces-from-jsx-attribute)
* [Collapse JSX Element](https://p42.ai/documentation/code-action/collapse-jsx-element) & [Expand JSX Element](https://p42.ai/documentation/code-action/expand-self-closing-jsx-element)
* [Surround with <>...</>](https://p42.ai/documentation/code-action/surround-with-jsx-fragment) & [Remove Unnecessary <>...</>](https://p42.ai/documentation/code-action/remove-unnecessary-jsx-fragment)

## ECMAScript Modernizations

The Javascript ecosystem is progressing rapidly, thanks to the great work of [TC39](https://tc39.es/). However, it is hard to keep codebases up-to-date with the newer JavaScript features, and codemods are not always an option due to their significant churn and potential for breakages. P42 supports both codemod-like mass code refactoring and more opportunistic code modernization for the following upgrades:

### ES2015
* [Collapse Object Property into Shorthand](https://p42.ai/documentation/code-action/collapse-property-into-shorthand)
* [Convert .apply() to Use Spread Syntax](https://p42.ai/documentation/code-action/convert-apply-to-spread-syntax)
* [Convert Function to Arrow Function](https://p42.ai/documentation/code-action/convert-function-to-arrow-function)
* [Convert Function to Object Method](https://p42.ai/documentation/code-action/convert-function-to-object-method)
* [Convert Loop to For...Of](https://p42.ai/documentation/code-action/convert-loop-to-for-of)
* [Convert Var to Let & Const](https://p42.ai/documentation/code-action/replace-var-with-let-and-const)
* [Lift Default into Parameter](https://p42.ai/documentation/code-action/lift-default-into-parameter)
* [Use String.startsWith()](https://p42.ai/documentation/code-action/use-string-starts-with) & [Use String.endsWith()](https://p42.ai/documentation/code-action/use-string-ends-with)
* [Use Template Literal](https://p42.ai/documentation/code-action/use-template-literal)

### ES2016
* [Convert Math.pow to Exponentiation Operator](https://p42.ai/documentation/code-action/convert-math-pow-to-exponentiation)
* [Use Array.includes()](https://p42.ai/documentation/code-action/use-array-includes)

### ES2020
* [Use Nullish Coalescence in Default Expression](https://p42.ai/documentation/code-action/use-nullish-coalescence-in-default-expression)
* [Use Optional Chaining](https://p42.ai/documentation/code-action/use-optional-chaining)

### ES2021
* [Add Numeric Separator](https://p42.ai/documentation/code-action/add-numeric-separator)
* [Push Operator into Assignment](https://p42.ai/documentation/code-action/push-operator-into-assignment) (for [short-circuiting](https://p42.ai/blog/2021-11-25/short-circuiting-operators-and-assignments-in-javascript) expressions)

## Actions for Logical Expressions
Boolean logic can be challenging to read, especially as expressions get more complex. P42 provides several refactorings that can help you simplify and tease apart logical expressions to make them easier to understand:

* [Flip Operator](https://p42.ai/documentation/code-action/flip-operator)
* [Invert Condition](https://p42.ai/documentation/code-action/invert-condition)
* [Pull Up Negation](https://p42.ai/documentation/code-action/pull-up-negation) & [Push Down Negation](https://p42.ai/documentation/code-action/push-down-negation)
* [Simplify Binary Expression](https://p42.ai/documentation/code-action/simplify-binary-expression)
* [Remove Double Negation](https://p42.ai/documentation/code-action/remove-double-negation)
* [Use == null Comparison](https://p42.ai/documentation/code-action/use-eq-eq-null)
* [Extract Variable](https://p42.ai/documentation/code-action/extract-variable)
* [Use Optional Chaining](https://p42.ai/documentation/code-action/use-optional-chaining)

## Actions for If-Else Statements
If-else statements are a central element in many programs. Restructuring them can increase the readability of your programs, often in combination with refactoring their conditions:

* [Lift Statement Out Of If-Else](https://p42.ai/documentation/code-action/lift-statement-out-of-if-else)
* [Merge Nested If-Statements](https://p42.ai/documentation/code-action/merge-nested-if)
* [Merge Nested If Inside Else into Else-If](https://p42.ai/documentation/code-action/merge-nested-else-if)
* [Separate Condition into Nested If](https://p42.ai/documentation/code-action/separate-condition-into-nested-if)
* [Remove Empty If Block](https://p42.ai/documentation/code-action/remove-empty-if-block) & [Remove Empty Else Block](https://p42.ai/documentation/code-action/remove-empty-else-block)
* [Remove Redundant Else](https://p42.ai/documentation/code-action/remove-redundant-else)
* [Convert If-Else to Guard Clause](https://p42.ai/documentation/code-action/convert-if-else-to-guard-clause)
* [Convert Conditional Expression to If-Else](documentation/code-action/convert-conditional-expression-to-if-else) & [Convert If-Else into Conditional Expression](https://p42.ai/documentation/code-action/convert-if-else-to-conditional-expression)
* [Convert If-Else to Switch](https://p42.ai/documentation/code-action/convert-if-else-to-switch)

## Syntax Conversion
It is often annoying to make small syntactical changes by editing text. Often more than one position needs to be edited, and the code is broken during the edit, leading to incorrect errors and auto-completions that get in the way. You can execute the following syntax conversions with a single P42 quick fix: 

* [Add {...} to Arrow Function](https://p42.ai/documentation/code-action/add-braces-to-arrow-function) & [Remove {...} from Arrow Function](https://p42.ai/documentation/code-action/remove-braces-from-arrow-function)
* [Add {...} to JSX Attribute](https://p42.ai/documentation/code-action/add-braces-to-jsx-attribute) & [Remove {...} from JSX Attribute](https://p42.ai/documentation/code-action/remove-braces-from-jsx-attribute)
* [Collapse JSX Element](https://p42.ai/documentation/code-action/collapse-jsx-element) & [Expand JSX Element](https://p42.ai/documentation/code-action/expand-self-closing-jsx-element)
* [Collapse Object Property into Shorthand](https://p42.ai/documentation/code-action/collapse-property-into-shorthand) & [Expand Shorthand Property](https://p42.ai/documentation/code-action/expand-shorthand-property)
* [Convert Property Access to Dot Notation](https://p42.ai/documentation/code-action/convert-bracket-notation-property-access-to-dot-notation) & [Convert Property Access to Bracket Notation](https://p42.ai/documentation/code-action/convert-dot-notation-property-access-to-bracket-notation) 
* [Split Variable Declaration](https://p42.ai/documentation/code-action/split-variable-declaration)

## Convert Language Elements
Sometimes you want to switch to a language element that is a better fit for what you are doing. For example, a `for..of` loop is more concise and can replace a regular for loop in many situations.

* [Convert Conditional Expression to If-Else](documentation/code-action/convert-conditional-expression-to-if-else) & [Convert If-Else into Conditional Expression](https://p42.ai/documentation/code-action/convert-if-else-to-conditional-expression)
* [Convert Function to Arrow Function](https://p42.ai/documentation/code-action/convert-function-to-arrow-function) & [Convert Function to Object Method](https://p42.ai/documentation/code-action/convert-function-to-object-method)
* [Convert If-Else to Switch](https://p42.ai/documentation/code-action/convert-if-else-to-switch)
* [Convert Loop to For...Of](https://p42.ai/documentation/code-action/convert-loop-to-for-of) & [Convert Loop to ForEach](https://p42.ai/documentation/code-action/convert-loop-to-for-each)
* [Push Operator into Assignment](https://p42.ai/documentation/code-action/push-operator-into-assignment) & [Pull Operator Out of Assignment](https://p42.ai/documentation/code-action/pull-operator-out-of-assignment)
* [Convert Var to Let & Const](https://p42.ai/documentation/code-action/replace-var-with-let-and-const) & [Convert Let to Const](https://p42.ai/documentation/code-action/convert-let-to-const)

## Code Cleanups
Code cleanups remove unnecessary code. Such code can result from code churn, e.g., by applying other refactorings, adding new features, or fixing bugs. P42 shows hints and automates the cleanup for the following situations:

* [Remove Double Negation](https://p42.ai/documentation/code-action/remove-double-negation)
* [Remove Empty If Block](https://p42.ai/documentation/code-action/remove-empty-if-block) & [Remove Empty Else Block](https://p42.ai/documentation/code-action/remove-empty-else-block)
* [Remove Redundant Else](https://p42.ai/documentation/code-action/remove-redundant-else)
* [Remove Unnecessary Conditional Expression](https://p42.ai/documentation/code-action/remove-unnecessary-conditional-expression)
* [Remove Unnecessary Expression Statement](https://p42.ai/documentation/code-action/remove-unnecessary-expression-statement)
* [Remove Unnecessary JSX Fragment](https://p42.ai/documentation/code-action/remove-unnecessary-jsx-fragment)
* [Remove Unnecessary Template Literal](https://p42.ai/documentation/code-action/remove-unnecessary-template)
* [Simplify Binary Expression](https://p42.ai/documentation/code-action/simplify-binary-expression)

## Other Actions
* [Insert console.log for Variable](https://p42.ai/documentation/code-action/insert-console-log)
* [Surround with Try-Catch](https://p42.ai/documentation/code-action/surround-with-try-catch)

If you find these refactorings & actions useful, you can install the [**P42 JavaScript Assistant** from the VS Code marketplace](https://marketplace.visualstudio.com/items?itemName=p42ai.refactor).
For feedback and updates, you can find P42 on [Twitter](https://twitter.com/p42ai) or [LinkedIn](https://www.linkedin.com/company/p42-software/).

Cheers!