Prevent JSHint warning that 'functionName is defined but never used'

47,494

Solution 1

To avoid the warning

defined but never used

in jslint in your javascript add comments like:

 /*exported formValidationSetup, refreshErrorMessages */

In jshint and jslint you can set the unused option to false:

 /*jshint unused:false*/

See Options

Solution 2

I had this problem with should and expect in Chai tests. I ended up with this pattern:

'use strict';

var request = require('supertest');
var should = require('chai').should();  // jshint ignore:line
var expect = require('chai').expect;    // jshint ignore:line

process.env.NODE_ENV = 'test';
var appPromise = require('./index');

describe('GET /r_u_up', function() {

  it('respond with 204', function(done) {
    appPromise.then(function(app) {
      request(app)
      .get('/r_u_up')
      .expect(204, done);
    });
  });

});

Solution 3

You can simply use

"unused": false,

in your .jshintrc

Solution 4

Interestingly, adding 'use strict'; inside the IIFE suppresses the error. Not sure why though.

Solution 5

A better way not touching the Gruntfile.js in a typical Yoeman setup is to edit the .jshintrc file (a hidden file in Unix system). And update the content as the following:

{
  "curly": true,
  "eqeqeq": true,
  "immed": true,
  "latedef": true,
  "newcap": true,
  "noarg": true,
  "sub": true,
  "undef": true,
  "unused": false, // the change is here
  "boss": true,
  "eqnull": true,
  "node": true
}

set the "unused" to false.

Share:
47,494

Related videos on Youtube

Undistraction
Author by

Undistraction

Updated on March 20, 2020

Comments

  • Undistraction
    Undistraction about 4 years

    I have just started using JSHint (through the Sublime-Linter package for Sublime Text 2). I would like to suppress its warnings regarding functions that are used before they are defined, as I see no problem with using function definitions like this. For example, the following code generates warnings:

    (function($){ 
    
        $(document).ready(function()
        {
          formValidationSetup();
          refreshErrorMessages();
        });
    
        function formValidationSetup()
        {
    
        }
    
        function refreshErrorMessages()
        {
    
        }
    
    })(jQuery);
    

    The warnings:

    1. formValidationSetup is defined but never used
    2. refreshErrorMessages is defined but never used

    I've tried setting undef to false in the JSHint options, but I'm still getting these errors. Is there another option I should be setting? Form the JSLint docs for undef:

    true if variables and functions need not be declared before used. This is not available in strict mode.

    • Markus Unterwaditzer
      Markus Unterwaditzer over 11 years
      You should take that as a hint to define the functions before the event. Not that it matters technically, but it's easier to understand the code that way.
    • Undistraction
      Undistraction over 11 years
      Surely that's just a matter of preference. I find it easier to understand like this as there is less code.
    • Markus Unterwaditzer
      Markus Unterwaditzer over 11 years
      How is there more code if you just move the function definitions to the top?
    • Undistraction
      Undistraction over 11 years
      Sorry. Misunderstood you. You're right. Same amount of code. Just more logical to me to have the initialiser first.
  • Mohamed Bana
    Mohamed Bana almost 10 years
    What about all the other unused variables? unused:false from my understanding will cause it to ignore all unused behaviour. I don't think this is what's trying to be achieved.
  • sampathsris
    sampathsris over 9 years
    Welcome to Stack Overflow. Consider formatting code elements in your answer using indented code blocks or preformatted text: stackoverflow.com/editing-help#code
  • Michael Cole
    Michael Cole about 9 years
    // jshint ignore:line will ignore that one line.
  • Ken Ingram
    Ken Ingram over 7 years
    Superior answer. Thank you.
  • DPM
    DPM over 6 years
    unused is not valid in jslint