Silencing JSLint warnings in brackets

∥☆過路亽.° 提交于 2019-12-11 10:16:01

问题


I just started a gulp-angular Yeoman project and opened it in brackets, and nearly everywhere I'm getting 'something' was used before it was defined, specifically 'angular' was used before it was defined. I've messed around with extensions and other fixes, but I honestly don't know what I'm doing and I haven't been able to find any good documentation.

What can I do to silence these warnings across the entire project? In other words, how can I avoid using /*global angular*/ in pretty much every file in my project?

Here's a quick example of one of my controller declarations:

/*global console*/

(function () {
    'use strict';

    angular
        .module('sbk')
        .controller('MainController', MainController);

    /** @ngInject */
    function MainController() {
        console.log('Woo!');
    }
})();

Which results in the following JSLint warnings:

'angular' was used before it was defined.
'MainController' was used before it was defined.
'MainController' was used before it was defined.
Move the invocation into the parens that contain the function.

Edit: updated to make my issue a little clearer


回答1:


http://jshint.com/docs/options/#globals

Provide a list of globals, and you're done.

It's not clear if you're using jshint or jslint; your tags say one, your text another.




回答2:


Okay, so remember that JSLint is just a JavaScript file, and most JSLint plugins I've used has the source shoved in the install somewhere (though the Visual Studio plugin embeds it into a dll!), so you can always get it to do what you want [within reason].

In your case, the absolute worst case is that you initialize the list of predefined variables with the namespaces you want JSLint to assume are always considered to be in scope. I'm going to show you how to set that globally within Brackets, which might not always be the best thing to do, and you need to keep in mind if you're linting another project that doesn't have Angular, well, you've screwed up JSLint.

(Honestly, I'm disappointed that Brackets doesn't have global as a global in its preferences.json file. Guess I should submit a patch.)

There are lots of ways we could do this. I'm going to add a namespace to the standard object. You could also limit its exposure by adding to the browser collection of globals (line 497 in my Bracket install's jslint file). We could also create a new JSLint directive option, similar to browser. That's not quick 'n' dirty, but is probably The Right Way to do this without submitting a Brackets patch. But for now, let's just do the quick 'n' dirty fix.

I'm using Windows. If you're not, YMWV. I do not use Brackets daily, so I haven't tested this in a production coding environment. If anything doesn't work, I'd be interested to hear it, however, and I'll see if I can find what's wrong.


TL;DR. Just the code, ma'am.

The jslint.js file you want is here, if you went with the default install location:
C:\Program Files (x86)\Brackets\www\extensions\default\JSLint\thirdparty\jslint

(Just for fun, notice that the version of JSLint we're using is from 2011!!?! Come on, Brackets guys!)

standard is, in my file, on line 974. Here's what it had originally:

// standard contains the global names that are provided by the
// ECMAScript standard.

    standard = array_to_object([
        'Array', 'Boolean', 'Date', 'decodeURI', 'decodeURIComponent',
        'encodeURI', 'encodeURIComponent', 'Error', 'eval', 'EvalError',
        'Function', 'isFinite', 'isNaN', 'JSON', 'Math', 'Number', 'Object',
        'parseInt', 'parseFloat', 'RangeError', 'ReferenceError', 'RegExp',
        'String', 'SyntaxError', 'TypeError', 'URIError'
    ], false),

Here's a test JavaScript snippet I want to lint that uses my global object, util:

/*jslint sloppy:true, white:true */
utils.spam();
utils.spammySpam();

smutils.spam();

All we need to do is to add , 'utils' to standard, and we're done.

    standard = array_to_object([
        'Array', 'Boolean', 'Date', 'decodeURI', 'decodeURIComponent',
        'encodeURI', 'encodeURIComponent', 'Error', 'eval', 'EvalError',
        'Function', 'isFinite', 'isNaN', 'JSON', 'Math', 'Number', 'Object',
        'parseInt', 'parseFloat', 'RangeError', 'ReferenceError', 'RegExp',
        'String', 'SyntaxError', 'TypeError', 'URIError', 'utils'
    ], false),

To be overly clear, you'd add 'angular' and 'MainController' to your standard object in place of 'utils'.

Btw, Crockford (JSLint's creator and maintainer) would hate that we did that. I don't really blame him.

Now I get an error for smutils, but not utils. Luckily, the global directive still works with this setup:

/*jslint sloppy:true, white:true */
/*global smutils */
utils.spam();
utils.spammySpam();

smutils.spam();

And we're JSLint friendly again.

Hacking JSLint directly might not have most favored option status, but it works.

Btw, re: Dave's comment, "IMO JSLint has been popularly deprecated in favor of JSHint or JSCS," I'm not so sure. Just as an example, which does Brackets use out of the box? The best part about JSLint is how much less subjective discussion has to happen to get a large team on the same page. And trust me, Crockford's not going to tell you to do something that's indefensibly wrong.

He might hurt your feelings, and he might ask you to do something that hurts at first, but I really think JSLint can teach a team a lot about JavaScript best practices. It's worth living in JSLint for a while before jumping to JSHint.



来源:https://stackoverflow.com/questions/32056643/silencing-jslint-warnings-in-brackets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!