jshint / jshint

JSHint is a tool that helps to detect errors and potential problems in your JavaScript code
http://jshint.com
MIT License
8.99k stars 1.65k forks source link

JSHINT error Redefinition of 'casper' #3104

Closed tirthaguha closed 7 years ago

tirthaguha commented 7 years ago

I was working wiht casperJS and I have the following in my code

var casper = require('casper').create
({
  verbose: true,
  logLevel: "debug"
});

and here's the error I get

Running "jshint:all" (jshint) task

   tasks\casperStarter.js
      5 |var casper = require('casper').create
             ^ Redefinition of 'casper'.

>> 1 error in 5 files
jugglinmike commented 7 years ago

This isn't quite enough information. From our contribution guidelines:

Bug Reports

If you believe you have identified incorrect behavior, please let the team know by filing an issue. In order to help the team diagnose and fix the problem, the issue report should have the following information:

  • Version of JSHint being used
  • Input source code (simplified to only contain details necessary to demonstrate the problem)
  • Configuration values
  • Description of expected behavior
  • Description of actual behavior

Also: from the error message, it looks like the offending code is the fifth line of the input file. The preceding lines may be relevant, so please include those, too.

tirthaguha commented 7 years ago

Hi, Version of JSHint being used: "grunt-contrib-jshint": "^0.9.2", Input source code: We were running casperJS with Grunt, The following command executes in shell. casperjs --ignore-ssl-errors=true --ssl-protocol=any, casperStarter.js

and in casperStarter.js we have

var casper = require('casper').create
({
  verbose: true,
  logLevel: "debug"
});
casper.on('remote.alert', function (message) {
  this.echo(message);
});

Configuration values: in jshintrc

{
  "curly": true,
  "eqeqeq": true,
  "immed": true,
  "latedef": true,
  "newcap": true,
  "noarg": true,
  "sub": true,
  "undef": true,
  "unused": false,
  "boss": true,
  "eqnull": true,
  "node": true,
  "nonew": false,
  "phantom": true,
  "globals": {
    "casper": false
  }
}

Description of expected behavior: should not show this error

jugglinmike commented 7 years ago

JSHint is warning about the casper redefinition because you have listed that identifier as a read-only global variable in your .jshintrc file and also included a casper variable declaration in your source code. This is expected behavior. There are a couple of ways you can avoid the warning:

  1. Remove the "casper" entry in the globals configuration from your .jshintrc file
  2. Wrap your code in an immediately-invoked function expression, e.g.

    (function() {
     var casper = require('casper').create
     // etc.
    }());
  3. Specify true as the value for the "casper" entry in the globals configuration (which means, "this is a writable global")

Based on the way your code is written, where casper is being explicitly defined (as opposed to provided globally by the environment), I would recommend #1.

Hope that helps!