EzDevInfo.com

grunt-contrib-jshint

Validate files with JSHint. Grunt: The JavaScript Task Runner

difference between globals and predef in .jshintrc?

What the difference and purpose of having both of them in the .jshintrc? When I want to add a variable to be ignored, which one is the best one I should use? Also I can't find 'predef' in http://www.jshint.com/docs/options/


Source: (StackOverflow)

Best practice for jshint w/ grunt and handlebars templates

I am working on a web-application using node/grunt for building. I am using jshint, requirejs, and handlebars.

I started with a boilerplate that runs jshint on my source code, but jshint is giving me errors for referencing pre-compiled handlebar templates in my source code (the compiled handlebar templates are unavailable to jshint). Is it an okay practice to run jshint on my "compiled" code? This seems to be the only option, however, I'm uncertain if this is good practice.


Source: (StackOverflow)

Advertisements

javascript event function being called multiple times

I am integrating maps on a webpage with Overlapping Marker Spiderfier on google maps. I added a click listener on the marker as below.

$scope.setMarkers = function() {
    for (var i = 0; i < $scope.markers.length; i++) {
        $scope.markers[i].setMap($scope.map);
        $scope.oms.addMarker($scope.markers[i]);
        var marker = $scope.markers[i];
        var iw = new google.maps.InfoWindow({
            content: ""
        });
        $scope.oms.addListener('click', function(marker) {
            iw.setContent(marker.desc);
            iw.open($scope.map, marker);
        });
    }
};

and it works fine but jshint is giving me error for making function inside loop. So i changed it to.

$scope.setMarkers = function() {
    for (var i = 0; i < $scope.markers.length; i++) {
        $scope.markers[i].setMap($scope.map);
        $scope.oms.addMarker($scope.markers[i]);
        $scope.addMarkerEventListener(i);
    }
};
$scope.addMarkerEventListener = function(i) {
    var marker = $scope.markers[i];
    var iw = new google.maps.InfoWindow({
        content: ""
    });
    $scope.oms.addListener('click', function(marker) {
        iw.setContent(marker.desc);
        iw.open($scope.map, marker);
    });
};

now when I am clicking on the marker its opening upto 90 info windows one behind another(i have 90 markers in an array). What am i missing.


Source: (StackOverflow)

Setting jshint and travis-ci for javascript project

I'm trying to setup Travis CI on one JavaScript project hosted on GitHub but I'm getting error like

Loading "jshint.js" tasks...ERROR

>> Error: Cannot find module 'jshint/src/cli/cli'

Those are my files:

Gruntfile.js

module.exports = function(grunt) {

  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    jshint: {
      myFiles: ['cyrlatconverter-v0.5.4.js']
    }
  });

  grunt.loadNpmTasks('grunt-contrib-jshint');
};

.travis.yml

language: node_js
node_js:
  - 0.10

package.json

{
  "name": "node-travis",
  "version": "0.1.0",
  "devDependencies": {
    "grunt": "0.4.1",
    "grunt-cli": "0.1.9",
    "grunt-contrib-jshint": "0.6.0"
  },
  "scripts": {
    "test": "grunt --verbose"
  }
}

Source: (StackOverflow)

jshint grunt targets generate message: 0 files linted. Please check your ignored files

I have multiple jshint configurations in my gruntfile.

I tested each configuration, and it works great.

However, when I define a target in the gruntfile for each configuration, jshint stops working and all I can see is :

0 files linted. Please check your ignored files.

This is how my jshint configuration looks like with targets:

    jshint: {
        backend: {
            options: {
                jshintrc: '.jshintrc'
            },
            all: [
                'Gruntfile.js',
                'server.js',
                '*.js',
                'backend/{,*/}*.js'
            ]
        },
        test: {
            options: {
                jshintrc: 'test.jshintrc'
            },
            all: [
                'test/{,*/}*.js'
            ]
        }
    }

Source: (StackOverflow)

grunt jshint fail only if more than N errors

I am using jshint to validate my scripts with grunt. I configured it and it works correctly. The only problem is that it either pass (when there are no errors) or fails if there is at least 1 error.

I am aware that I can use --force true in my options to suppress the fail

options: {
  force : true
}

But this is not what I want. I want my validation to fail if there are more than N (let's say 10) errors.

After reading documentation, I thought I can achieve it with "maxerr" : 10 in my .jshintrc file. But this does not solve it. The only thing it does is showing additional warning like Too many errors. (58% scanned). when you already exceed your limit.

So is there a way to fail my validation only if I have more than N errors? By this I mean that validation would pass if I have no errors (which I have right now) but also if I have N-1 errors.


Source: (StackOverflow)

A couple of issues with JSHint: 'is defined but never used' and 'is not defined'

I have this application pretty modular and as such, JSHint is giving me the 'x' is defined but never used error.

My setup goes like this:

app/assets/scripts/bootstrap.js: var x = 5;

app/assets/scripts/kickstart.js: console.log(x);

And here's the output I get from JSHint:

app/assets/scripts/bootstrap.js
  line 1  col 6   'x' is defined but never used.

app/assets/scripts/kickstart.js
  line 1  col 13  'x' is not defined.

2 problems

I know I could us something like /* exported x */ but would be really cumbersome if I have lots of variables like this.

Is there anyway to solve these 2 issues without disabling their specific options? Because they could come in handy in other, more important, situations.


Source: (StackOverflow)

Grunt Contrib JSHint Warnings

So in our build, we use jshint to warn us of any console log errors and any other anomalies. Currently, I'm unable to build (without --force) whenever I'm using the bitwise ~ operator. I am getting the following error in verbose:

>> Line 310: Unexpected '~'.
>> totalImpressions += ~~(data[i].impressions);

I added the

/*jshint bitwise:false */

flag in the top of the config file, but to no avail.

I also tried adding an ignore_warnings object like so:

/* global module */
/* Created by frank on 3/4/14. */
/*jshint bitwise:false */
module.exports = {
    options: {
        jshintrc: '.jshintrc',
        ignores: [],
        ignore_warning: {
            options: {
                '-W018': true,
                '-W052': true
            },
            src: ['**/*.js']
        }
    },
    all: []
}

But I have emptied out the strings in ignores and all for privacy... If anyone knows how to supress this error, I would greatly appreciate the help.


Source: (StackOverflow)

Sails, grunt-contrib-watch is not working with jshint

I'm new to sails. I'm using:

  • sails@0.10.5
  • grunt-contrib-watch@0.5.3
  • grunt-contrib-jshint@0.10.0
  • jshint@2.5.11

I've been starting my server using either sails lift or sails lift --verbose. According to the sails documentation grunt default is ran. The first task is compileAssets. Which starts with jshint. My compileAssets tasks ends with a watch command.

When a file is changed that is being watched, syncAssets is ran. syncAssets is identical to my compileAssets task. They both start with jshint.

The problem is that while syncAssets is running, it is not reporting any jshint errors in the console, and/or stopping the syncAssets task so I have no idea if my code as compiled with out looking at the source.

Then I must restart the server to find the error, since compileAssets will print the jshint error.

Is something configured incorrectly in my tasks? Please help, this is significantly slowing down my workflow.

tasks/register/default.js -- Unchanged

module.exports = function (grunt) {
    grunt.registerTask('default', ['compileAssets', 'linkAssets',  'watch']);
};

tasks/register/syncAssets.js

module.exports = function (grunt) {
    grunt.registerTask('syncAssets', [
    'jshint',
    'bower',
    'clean:dev',
    'copy:dev',
    'exec:compileTemplates',
    'exec:compilePartials',
    'compass'
    ]);
};

tasks/register/compileAssets.js module.exports = function (grunt) { grunt.registerTask('compileAssets', [ 'jshint', 'bower', 'clean:dev', 'copy:dev', 'exec:compileTemplates', 'exec:compilePartials', 'compass' ]); };


Source: (StackOverflow)

JSHint: Multiple Indents?

I'm using D3 in my project. I'd love to enforce a specific indentation pattern, but due to the multiple chaining I'd like to organize my code using multiple instances of indentation. JSHint throws an error when I have something like this:

var svgContainer = d3.select(location).append('svg')
    .attr()
    .attr();

    var xAxisGroup = svgContainer.append('g') // throws error here
        .attr()
        .call();

Is there any possible way I can enforce 4 indents, but ignore multiple indentations in .jshintrc? i.e. var AxisGroup stems from svgContainer, would like the nesting without warnings from JSHint.


Source: (StackOverflow)

Grunt installation

I am building a application with angularjs with a server, when building the backend, it is told to install grunt-contrib-jshint. I have installed grunt to my project but I have no idea why do I have to install grunt-contrib-jshint.


Source: (StackOverflow)

How to tell JSHint to look for definitions in another JS file

Here's the problem: I'm using JSHint in Grunt. I have a couple JS files and they'll be merged into one file in the end. But before merging, I would like to run JSHint. However, JSHint complaints some undefined variables. Here's an example: 1. JavaScript/file_one.js defines a variable: var Foo = (function (){}()); 2. JavaScript/file_two.js uses that module. new Foo().

jshint: {
            files: ['Gruntfile.js',
                    'javascript/**/*.js',

                   ],
             options: {
             // some options.
         }
}

JSHint complaints that Foo isn't defined in file_two.js.

Here are my goals (if possible): 1. I want to have codes in separate files. So it's possible that some variable is defined in a different file. 2. I want JSHint to check the origin JS files. I don't want to merge all JS files to one and have JSHint to check the merged one. 3. JSHint can check across all files in <%= jshint.files %> (javascript/**/*.js for example) and look for definition.

I know one work around that to put Foo as a global in options, as in this post Does anyone knows another way to tell JSLint / JSHint what global variables are already defined. But I don't want to put the variable I defined there, since every such variable is in my source files.

So my question is whether there's a way to achieve my goal 3 to tell JSHint to look for the definition in other files.


Source: (StackOverflow)

jshint exported directive not working

I've setup a mocha task in my gruntfile which loads a few test libraries before the tests start. Here is what it looks like:

    mochaTest: {
        unit: {
            options: {
                reporter: 'spec',
                require: function(){
                    var chai = require('chai');
                    var chaiAsPromised = require('chai-as-promised');
                    chai.use(chaiAsPromised);
                    /* exported assert, expect */
                    var assert = chai.assert;
                    var expect = chai.expect;
                    chai.should();
                }
            },
            src: ['backend/test/**/*.js']
        }
    },

so jshint complains that I'm not using assert and expect in the gruntfile. I've tried adding exported to the .jshintrc file, to the jshint grunt task and even the inline directive but jshint don't seem to care. I've just upgraded to the latest version to be on the safe side but still no luck.

I can use the // jshint ignore:line at the moment to get by but I'd like to get this working with the /*exported assert, expect */ directive if possible, as I think it is more understandable.


Source: (StackOverflow)

Grunt JSHint - Not checking indentations

I have the indent set to 4. If I remove all the indentations in a source file, JSHint doesn't fail for it. It checks everything else but the indentations. How do I get it to check for indentations?

Additionally, I have installed the jshint-eclipse plugin, it too doesn't work!

Here is the excerpt of my GruntFile:

// Make sure code styles are up to par and there are no obvious mistakes
jshint: {
  options: {
    jshintrc: '.jshintrc',
    reporter: require('jshint-stylish')
  },
  all: {
    src: [
      'Gruntfile.js',
      '<%= yeoman.app %>/scripts/{,*/}*.js'
    ]
  },
  test: {
    options: {
      jshintrc: 'test/.jshintrc'
    },
    src: ['test/spec/{,*/}*.js']
  }
},

And my jshint:

{
  "node": true,
  "browser": true,
  "esnext": true,
  "bitwise": true,
  "camelcase": true,
  "curly": true,
  "eqeqeq": true,
  "immed": true,
  "indent": 4,
  "latedef": true,
  "newcap": true,
  "noarg": true,
  "quotmark": "single",
  "regexp": true,
  "undef": true,
  "unused": true,
  "strict": true,
  "trailing": true,
  "smarttabs": false,
  "globals": {
      "angular": false
   }
}

Source: (StackOverflow)

Grunt jshint disable Expected a conditional expression and instead saw an assignment

I am using grunt-contrib-jshint and it finds the following error in my JS file:

line 5   col 70  Expected a conditional expression and instead saw an assignment.

I know the reason of this error, but all I want is to disable it. Looking here it looks like I can either use no-cond-assign to 0 in my jshintrc or by adding -W084 : true in my options.

The problem is that the first solution ended up in a corrupted jshintrc and the second one does not solve the problem. Another option is to add -W022 : true (which solution I found in the comments) also does not work.


Source: (StackOverflow)