Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I resolve dependencies when linting single javascript files with grunt?

I want to separate my application logic into multiple Javascript files for sanity and developer friendliness, stored in the /src folder. These files should be linted and concatenated into /dist/app.js during the build process. I am using grunt for my build process, as it already comes with handy lint and concat tasks.


    +
    |- grunt.js
    |- readme
    |-vendors
       |-backbone.js
    |- src
       |- core.js
       |- user.js
    |- dist
       |-app.js

I am running into an annoying problem. I use backbone.js for application structure, and most of my source files start by defining models by extending Backbone.Model. When linting these files, JSHint complains that Backbone is not defined, and rightly so - backbone resides outside in its own directory. Including all necessary scripts in the right order is something I assume is done in the html. Each individual source file should only know about itself.

I know that I can suppress these undefined warnings by setting lint's undef flag in grunt.js to false but I want to keep it set to true in order to be warned about other undefined variables in my application, as it is a common pointer to typos. Is there a clean way to tell grunt (or lint) what files to include prior to linting them? Am I doing something wrong with my build process, or with my application architecture? Or is this simply something I have to live with?

like image 923
timkg Avatar asked Aug 14 '12 19:08

timkg


1 Answers

The jshint options allow you to specify a list of globals that come from other libraries you are using, in your grunt.js file:



    jshint: {
      options: {
        curly: true,
        eqeqeq: true,
        immed: false,
        latedef: true,
        newcap: true,
        noarg: true,
        sub: true,
        undef: true,
        boss: true,
        eqnull: true,
        browser: true
      },

      globals: {
        jQuery: true,
        Backbone: true,
        _: true
      }
    },

note the globals setting at the bottom. This allows JSHint to ignore these variables, but still run your undef: true setting (as shown above).

like image 140
Derick Bailey Avatar answered Sep 28 '22 06:09

Derick Bailey