简体   繁体   English

防止JSHint警告“functionName已定义但从未使用过”

[英]Prevent JSHint warning that 'functionName is defined but never used'

I have just started using JSHint (through the Sublime-Linter package for Sublime Text 2). 我刚开始使用JSHint(通过Sublime-Linter包用于Sublime Text 2)。 I would like to suppress its warnings regarding functions that are used before they are defined, as I see no problem with using function definitions like this. 我想抑制它在定义之前使用的函数的警告,因为我发现使用这样的函数定义没有问题。 For example, the following code generates warnings: 例如,以下代码生成警告:

(function($){ 

    $(document).ready(function()
    {
      formValidationSetup();
      refreshErrorMessages();
    });

    function formValidationSetup()
    {

    }

    function refreshErrorMessages()
    {

    }

})(jQuery);

The warnings: 警告:

  1. formValidationSetup is defined but never used formValidationSetup已定义但从未使用过
  2. refreshErrorMessages is defined but never used refreshErrorMessages已定义但从未使用过

I've tried setting undef to false in the JSHint options, but I'm still getting these errors. 我已经尝试在JSHint选项中将undef设置为false ,但我仍然遇到这些错误。 Is there another option I should be setting? 我应该设置另一种选择吗? Form the JSLint docs for undef : 形成undefJSLint文档

true if variables and functions need not be declared before used. 如果在使用之前不需要声明变量和函数,则为true。 This is not available in strict mode. 这在严格模式下不可用。

To avoid the warning 避免警告

defined but never used 定义但从未使用过

in jslint in your javascript add comments like: 在你的javascript中的jslint中添加评论:

 /*exported formValidationSetup, refreshErrorMessages */

In jshint and jslint you can set the unused option to false: 在jshint和jslint中,您可以将未使用的选项设置为false:

 /*jshint unused:false*/

See Options 请参阅选项

I had this problem with should and expect in Chai tests. 我在Chai测试中shouldexpect这个问题。 I ended up with this pattern: 我最终得到了这种模式:

'use strict';

var request = require('supertest');
var should = require('chai').should();  // jshint ignore:line
var expect = require('chai').expect;    // jshint ignore:line

process.env.NODE_ENV = 'test';
var appPromise = require('./index');

describe('GET /r_u_up', function() {

  it('respond with 204', function(done) {
    appPromise.then(function(app) {
      request(app)
      .get('/r_u_up')
      .expect(204, done);
    });
  });

});

You can simply use 你可以简单地使用

"unused": false,

in your .jshintrc 在你的.jshintrc中

A better way not touching the Gruntfile.js in a typical Yoeman setup is to edit the .jshintrc file (a hidden file in Unix system). 在典型的Yoeman设置中不接触Gruntfile.js更好方法是编辑.jshintrc文件(Unix系统中的隐藏文件)。 And update the content as the following: 并按以下方式更新内容:

{
  "curly": true,
  "eqeqeq": true,
  "immed": true,
  "latedef": true,
  "newcap": true,
  "noarg": true,
  "sub": true,
  "undef": true,
  "unused": false, // the change is here
  "boss": true,
  "eqnull": true,
  "node": true
}

set the "unused" to false . "unused"设置为false

Interestingly, adding 'use strict'; 有趣的是,添加'use strict'; inside the IIFE suppresses the error. IIFE内部抑制了错误。 Not sure why though. 不知道为什么。

你会想要使用'latedef'选项

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM