RubyMine 2024.3 Help

Linting TypeScript

The recommended linter for TypeScript code is ESLint which brings a wide range of linting rules that can also be extended with plugins. RubyMine shows warnings and errors reported by ESLint right in the editor, as you type. Learn more from ESLint.

RubyMine highlights errors reported by ESLint in .ts and .tsx files when @typescript-eslint/parser is set as a parser in your project ESLint configuration. Learn more from the readme file in the typescript-eslint repo.

Install and configure ESLint

  1. In the embedded Terminal (Alt+F12) , type one of the following commands:

    • npm init @eslint/config@latest to install ESLint in the current project and generate a configuration file.

      Answer the questions of the wizard. As a result, RubyMine installs the latest version of ESLint and generates a eslint.config.js configuration file.

    • npm install --save-dev eslint to install ESLint as a development dependency.

    • npm install --g eslint for global installation.

    • pnpm create @eslint/config@latest to install ESLint in the current project and generate a configuration file.

      Answer the questions of the wizard. As a result, RubyMine installs the latest version of ESLint and generates a eslint.config.js configuration file.

    • pnpm add -D eslint to install ESLint as a development dependency.

    • pnpm add -g eslint for global installation.

    • yarn create @eslint/config to install ESLint in the current project and generate a configuration file.

      Answer the questions of the wizard. As a result, RubyMine installs the latest version of ESLint and generates a eslint.config.js configuration file.

    • yarn add -D eslint to install ESLint as a development dependency.

    • yarn add eslint for global installation.

  2. By default, ESLint is disabled. Enable it on the Settings | Languages and Frameworks | JavaScript | Code Quality Tools | ESLint as described in Activate and configure ESLint in RubyMine".

Use ESLint for TypeScript in a new project

  1. In the embedded Terminal (Alt+F12) , type:

    npm install @typescript-eslint/parser @typescript-eslint/eslint-plugin --save-dev

  2. In the .eslintrc configuration file or under eslintConfig in package.json, add:

    { "parser": "@typescript-eslint/parser", "plugins": [ "@typescript-eslint" ], "extends": [ "plugin:@typescript-eslint/recommended" ] }

Suppress linting TypeScript code with ESLint

  • If you are already using @typescript-eslint/parser but you do not want to check TypeScript code with ESLint, add .ts or .tsx to the .eslintignore file.

TSLint

You can also use the TSLint code verification tool from inside RubyMine and check your TypeScript code for the most common mistakes without running the application. When the tool is activated, it lints all the opened TypeScript files and marks the detected problems.

To view the description of a problem, hover over the highlighted code. By default, RubyMine marks detected problems based on the severity levels from the TSLint configuration file. See Configuring TSLint highlighting to learn how to override these settings.

TSLint: errors and warnings are highlighted, the description of a problem is shown in a tooltip.

Before you start

  1. Download and install Node.js.

  2. Make sure a local Node.js interpreter is configured in your project: open the Settings dialog (Ctrl+Alt+S) and go to Languages & Frameworks | Node.js. The Node interpreter field shows the default project Node.js interpreter. Learn more from Configuring a local Node.js interpreter.

  3. Make sure the JavaScript and TypeScript and TSLint required plugins are enabled on the Settings | Plugins page, tab Installed. For more information, refer to Managing plugins.

Installing TSLint

  • In the embedded Terminal (Alt+F12) , type:

    npm install tslint typescript --save-dev

    Learn more from the TSLint official website.

Activating and configuring TSLint in RubyMine

By default, RubyMine uses the TSLint package from the project node_modules folder and the tslint.json configuration file from the folder where the current file is stored. If no tslint.json configuration file is found in the current file folder, RubyMine will look for one in its parent folders up to the project root.

If you have several package.json files with TSLint listed as a dependency, RubyMine starts a separate process for each package.json and processes everything below it. This lets you apply a specific TSLint version or a specific set of plugins to each path in a monorepo or a project with multiple TSLint configurations.

This behavior is default in all new RubyMine projects. To enable it in a previously created project, go to Languages & Frameworks | TypeScript | TSLint in the Settings dialog (Ctrl+Alt+S) and select the Automatic TSLint configuration option.

You can also configure TSLint manually to use a custom TSLint package and tslint.json and specify some additional rules.

  1. In the Settings dialog (Ctrl+Alt+S) , go to Languages & Frameworks | TypeScript | TSLint.

  2. Select the Manual Configuration option.

  3. Specify the path to the TSLint package.

  4. In the Configuration File area, appoint the configuration to use.

    By default, RubyMine first looks for a tslint.json, tslint.yaml, or tslint.yml configuration file. RubyMine starts the search from the folder where the file to be checked is stored, then searches in the parent folder, and so on until reaches the project root. If no tslint.json, tslint.yaml, or tslint.yml configuration file is found, TSLint uses its default embedded configuration file. Accordingly, you have to define the configuration to apply either in a tslint.json, tslint.yaml, or tslint.yml configuration file, or in a custom configuration file, or rely on the default embedded configuration.

    • To have RubyMine look for a tslint.json, tslint.yaml, or tslint.yml file, choose the Automatic search option.

    • To use a custom file, choose the Configuration File option and specify the location of the file in the Path field. Choose the path from the list, or type it manually, or click the Browse button and select the relevant file from the dialog that opens.

    Learn more about configuring TSLint from the TSLint official website.

  5. If necessary, in the Additional Rules Directory field, specify the location of the files with additional code verification rules. These rules will be applied after the rules from tslint.json, tslint.yaml, tslint.yml, or from the above specified custom configuration file and accordingly will override them.

Configuring highlighting for TSLint

By default, RubyMine marks the detected errors and warnings based on the severity levels from the TSLint configuration file. For example, errors are highlighted with a red squiggly line, while warnings are marked with a yellow background. For more information, refer to Code inspections and Change inspection severity.

Change the severity level of a rule in the TSLint configuration

  • In TSLint configuration file, locate the rule you want to edit and set its ID to warning or to error. Learn more from the TSLint official website.

You can override the severities from the TSLint configuration file so that RubyMine ignores them and shows everything reported by the linter as errors, warnings, or in a custom color.

Ignore the severity levels from the configuration

  1. In the Settings dialog (Ctrl+Alt+S) , go to Editor | Inspections. The Inspections page opens.

  2. In the central pane, go to TypeScript | TSLint.

  3. In the right pane, clear the Use rule severity from the configuration file checkbox and select the severity level to use instead of the default one.

    Specifying a custom severity level for TSLint

Importing code style from a TSLint configuration file

RubyMine understands some TSLint rules that are described in tslint.json, tslint .yaml, or tslint.yml configuration files and lets you apply them to the TypeScript code style configuration in your project.

  • When you open your project for the first time, RubyMine imports the code style from the project tslint.json, tslint.yaml, or tslint.yml automatically.

  • If tslint.json, tslint.yaml, or tslint.yml is updated (manually or from your version control), open it in the editor and choose Apply TSLint Code Style Rules from the context menu.

    Import TSLint code style rules from the editor: context menu

    Alternatively, just answer Yes to the "Apply code style from TSLint?" question on top of the file.

    RubyMine suggests importing the code style from tslint.json

TSLint quick-fixes

RubyMine lets you automatically fix some of the issues that TSLint reports.

  • To fix a specific error, place the caret at the highlighted code, press Alt+Enter, and then select TSLint: fix current error from the list.

  • To fix all the issues detected in the file, choose TSLint: fix current file.

    ws_tslint_quick_fixes.png

You can suppress TSLint rules for the current file and even for the current line. RubyMine automatically generates disable comments in the format /* tslint:disable:<rule name> or // tslint:disable-next-line:<rule name> and places them on top of the file or above the current line respectively.

Suppress a TSLint rule on the fly

  1. Place the caret at an error or a warning reported by TSLint and press Alt+Enter.

  2. Select the quick-fix for the rule that you want to disable and press Right.

  3. From the list, select Suppress <rule name> for current file or Suppress <rule name> for current line.

    Disabling TSLint rules on the fly by adding disable comments
Last modified: 24 October 2024