25

Here's my file: app/scripts/controllers/main.js

"use strict";

angular.module('appApp')
  .controller('MainCtrl', ['$scope', function ($scope) {
    $scope.awesomeThings = [
      'HTML5 Boilerplate',
      'AngularJS',
      'Karma'
    ];
  }]);

My Gruntfile.coffee has:

jshint:
    options:
        globals:
            require: false
            module: false
            console: false
            __dirname: false
            process: false
            exports: false

    server:
        options:
            node: true
        src: ["server/**/*.js"]

    app:
        options:
            globals:
                angular: true
                strict: true

        src: ["app/scripts/**/*.js"]

When I run grunt, I get:

Linting app/scripts/controllers/main.js ...ERROR
[L1:C1] W097: Use the function form of "use strict".
"use strict";
bevacqua
  • 47,502
  • 56
  • 171
  • 285
Shamoon
  • 41,293
  • 91
  • 306
  • 570

2 Answers2

50

The issue is that if you don't use the function form it applies to everything, and not just your code. The solution to that is to scope use strict inside functions you control.

Refer to this question: JSLint is suddenly reporting: Use the function form of “use strict”.

Rather than doing

"use strict";

angular.module('appApp')
  .controller('MainCtrl', ['$scope', function ($scope) {
    $scope.awesomeThings = [
      'HTML5 Boilerplate',
      'AngularJS',
      'Karma'
    ];
  }]);

You should be doing

angular.module('appApp')
  .controller('MainCtrl', ['$scope', function ($scope) {
    "use strict";

    $scope.awesomeThings = [
      'HTML5 Boilerplate',
      'AngularJS',
      'Karma'
    ];
  }]);

It's either that or wrapping your code in a self-executing closure, like below.

(function(){
    "use strict";

    // your stuff
})();
Community
  • 1
  • 1
bevacqua
  • 47,502
  • 56
  • 171
  • 285
  • 1
    It is actually the right way to do it, but how do I tell jshint to just ignore the warning? I have a bunch of .js files that work and I don't want to edit each and every on of them. – svarog Nov 13 '14 at 09:07
  • 7
    You should, because you could be breaking external libraries if you don't. – bevacqua Nov 13 '14 at 12:26
  • 1
    `it applies to everything, and not just your code` - only if you're concatenating your script with others. Otherwise it only applies to your code. – Vsevolod Golovanov Sep 15 '17 at 09:16
8

Changed my Gruntfile.coffee to include globalstrict

jshint:
    options:
        globalstrict: true
        globals:
            require: false
            module: false
            console: false
            __dirname: false
            process: false
            exports: false
Shamoon
  • 41,293
  • 91
  • 306
  • 570
  • 5
    "This option suppresses warnings about the use of global strict mode." Sounds like a band-aid rather than a solution. – Carl G Apr 16 '14 at 02:48
  • It's been a number of years. I can see projects requiring "get strict or get out" of their dependencies. – Charles Merriam Mar 08 '17 at 19:59