Ignore ESLint Directives in Code
1 min read

Ignore ESLint Directives in Code

Ignore ESLint Directives in Code

The other day I've had to refactor a piece of angular code. Unfortunately, the original code was written a long time ago and has some issues like having $scope in controllers and a rather large number of unused variables. changing the $scope to this is a very time-consuming task as it implies prefixing functions and variables in the HTML code as well as selective replacements in JS ($scope.$watch for example must remain intact).

In order to minimise the effort, I've decided to only move the code blocks around using John Papa's code.

In the refactored code, I'd like to get warnings for the new code, but skip them in the old one. Therefore, I can't just disable the rules from the .eslintrc file. I found there are two ways to temporarily disable rules:

  1. for one line - using // eslint-disable-line <rule> , e.g.:

    function myController(unusedVar) {
      // eslint-disable-line no-unused-vars
      // ...
    }
    
  2. for a block - using /* eslint-disable <rule> */ and

    /* eslint-enable <rule> */
    

    , e.g.:

    <pre class="lang:js decode:true">/* eslint-disable angular/controller-as */</pre>
    
function activate() {
  $scope.myvar = 'something'

  // ...
}

/* eslint-enable angular/controller-as */

Using the two you can control the parser locally and allow incremental refactoring.

HTH,