Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configure ESLint RuleTester to use Typescript Parser

I am trying to write some custom ESLint rules for my typescript based project. In my project I am using eslint/typescript for linting.

I have already written a custom eslint plugin which validates a custom rule. Now I want to write the unit tests for that custom rule. My test file looks like this:

/**
 * @fileoverview This rule verifies that logic can only depend on other logic
 * @author Dayem Siddiqui
 */
"use strict";

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const typescriptParser = require('@typescript-eslint/parser')
var rule = require("../../../lib/rules/logic-dependency"),
  RuleTester = require("eslint").RuleTester;

//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------
typescriptParser.parseForESLint()
var ruleTester = new RuleTester({ parserOptions: {} });
ruleTester.run("logic-dependency", rule, {
  valid: [
    `class DeleteLogic {

        }
    class CreateLogic {
            constructor(private deleteLogic: DeleteLogic) {}
    }`
  ],

  invalid: [
    {
      code: `class ExternalClient {

            }
            
            class UpdateLogic {
                constructor(private externalClient: ExternalClient) {}
            }`,
      errors: [
        {
          message: "Logic cannot have a dependency on client",
          type: "MethodDefinition"
        }
      ]
    }
  ]
});

Right now my tests a failing because by default eslint only understand plain Javascript code. I understand that I need to somehow configure it to use a custom parser that allows it to understand/parse typescript code. However I am unable to find a good example online on how to do that

like image 935
Dayem Siddiqui Avatar asked Feb 07 '20 10:02

Dayem Siddiqui


People also ask

How to use ESLint to understand TypeScript code?

@typescript-eslint/parser: parser that allows ESLint to understand TypeScript code @typescript-eslint/eslint-plugin: plugin with a set of recommended TypeScript rules Similar to Typescript compiler settings, you can either use the command line to generate a configuration file using the --init flag from ESLint or create it manually.

Why is @typescript-eslint-parser no longer available?

Because of these changes, the ESLint team is no longer maintaining the typescript-eslint-parser package and they instead recommend using @typescript-eslint/parser. In order to get started, you'll need to navigate to your projects directory and run the following sequence of commands:

What file type should I use for eslintrc?

Let's create a .eslintrc.js file and configure the parser and the plugin: I much prefer using .eslintrc.js over a JSON file, primarily because it lets you leave comments in your configuration!

How do I configure ESLint to read my script?

This can be in the form of a .eslintrc.* file or an eslintConfig field in a package.json file, both of which ESLint will look for and read automatically, or you can specify a configuration file on the command line. Here are some of the options that you can configure in ESLint: Environments - which environments your script is designed to run in.


1 Answers

The RuleTester constructor takes eslint's parser options that allow for configuring the parser itself. So pass it in like this and Bob's your uncle:

const ruleTester = new RuleTester({
  parser: '@typescript-eslint/parser',
});

typescript-eslint's own rule tests (e.g. this one) use exactly this.

(Was searching for an answer to this question and kept up ending here, so I posted the solution I found here in the hope it'll be useful.)


eslint 6.0+

Incorporating @Sonata's comment:

Eslint 6.0+ requires an absolute path to the parser (see 6.0 migration guide):

const ruleTester = new RuleTester({
  parser: require.resolve('@typescript-eslint/parser'),
});
like image 128
Sander Avatar answered Sep 19 '22 13:09

Sander