ReSharper Help

Code Style Assistance in JavaScript

Some of ReSharper's code style assistance features (e.g. Naming Style, Code Formatting Rules, Code Cleanup) are also supported in JavaScript. You can find the detailed information on these features in the corresponding topics of the Code Style Assistance section.

Besides, ReSharper provides a language-specific feature of normalizing quotes and terminating statements.

In this topic:

Code formatting rules

An important aspect of code style is how to format the code, i.e. how to use whitespaces and blank lines to arrange and separate code blocks, whether and how to use tabs for indents, whether and how to wrap long lines, etc.

ReSharper allows you to configure an extensive set of formatting rules and enforce them in your code. These rules are applied when ReSharper produces new code with code completion and code generation features, code templates and refactorings. The formatting rules can be also applied to the existing code in the current selection, current file, or in a larger scope up to the entire solution.

ReSharper allows you to configure formatting rules in options pages under the Code Editing | JavaScript | Formatting Style group.

As an alternative to digging through options pages, you can select a block of code and configure only those formatting rules that are applicable to this block, observing the changes right in this block.

To configure formatting rules for selected code

  1. In the editor, select a block of code that you want to reformat.
  2. Press Alt+Enter or click on the action indicator to the left of the caret to open the action list.
  3. Choose Format | Configure.
  4. In the Configure format dialog box that opens, you will see all formatting rules that affect the selected code block. The code block itself will be surrounded with a dotted frame:
    Configuring formatting rules for selected code
  5. As you change the formatting rules, you will see how they affect the code in the selected block.
  6. Click Save to apply the modifications and let ReSharper choose where to save them, or save the modifications to a specific settings layer using the Save To drop-down list. For more information, see Managing and Sharing ReSharper Settings. ReSharper will save your modifications and reformat the code block.

Quotes style

JavaScript specification allows you to use both dingle (') and double (") quotes for handling strings. It is a good idea to stick to the same quote style within your project or solution. To do so, you can configure your preference as well as the of the corresponding code inspection on the Code Editing | JavaScript | Code Style page of ReSharper options. ReSharper will detect and highlight the code that differs from your preference. To enforce this preference in the existing codebase, run code cleanup with the 'Full cleanup' default profile or with a custom profile where the corresponding option selected. If this preference is not important to you, disable the inspection by setting its severity level to 'Do not show'.

This preference is shared with the same preference of TypeScript, so when you change it in one language the preference in other language changes too.

Terminating statements

JavaScript/TypeScript do not require the semicolon to terminate a statement if the statement ends with a line break or if there is only one statement enclosed in braces. However, it is often recommended to use semicolon after each statement (see this StackOverflow thread for example). ReSharper can help you add semicolons automatically.

To terminate statements in specific file, project, or solution, run code cleanup in the desired scope with the 'Full cleanup' profile or with a custom profile where the Terminate statement is selected in the JavaScript/TypeScript section.

See Also

Last modified: 21 September 2015