如何告诉JSHint在另一个JS文件中查找定义 [英] How to tell JSHint to look for definitions in another JS file

查看:101
本文介绍了如何告诉JSHint在另一个JS文件中查找定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是问题:
我在Grunt中使用JSHint。我有几个JS文件,最后他们会合并成一个文件。但在合并之前,我想运行JSHint。但是,JSHint抱怨一些未定义的变量。下面是一个例子:
1. JavaScript / file_one.js定义了一个变量:var Foo =(function(){}());
2. JavaScript / file_two.js使用该模块。新Foo()。



  jshint:{files:['Gruntfile.js ','javascript / ** / *。js',],选项:{//一些选项。 }}  



JSHint抱怨Foo没有在file_two中定义。 js。



以下是我的目标(如果可能的话):
1.我想在单独的文件中包含代码。所以有可能在一个不同的文件中定义了一些变量。
2.我希望JSHint检查原始JS文件。我不想将所有JS文件合并为一个,并让JSHint检查合并的文件。
3. JSHint可以检查<%= jshint.files%>(例如javascript / ** / *。js)中的所有文件并查找定义。



我知道一个解决方案,把Foo作为一个全局选项,就像在这篇文章中一样如何判断已经定义了全局变量的JSLint / JSHint 。但我不想把我定义的变量放在那里,因为每个这样的变量都在我的源文件中。



所以我的问题是是否有办法实现我目标3告诉JSHint在其他文件中寻找定义。 解决方案

你的目标不可实现。 JSHint将在你的.jshintrc中寻找配置,或者在你的情况下寻找你的Gruntfile。如果你依赖于全局变量,你应该禁用这个设置,覆盖以指定每个变量,或者你可以在单个文件中本地创建其他覆盖。 在您的配置文件或每个文件中,或将它们全部放在名称空间中。



强烈建议切换到模块模式,并禁止任何泄漏到全局范围内的内容


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 How 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.

解决方案

Your goal is not achievable. JSHint will look for configuration in your .jshintrc or in your case your Gruntfile. The only other overrides you can make is locally in the individual file.

If you are relying on global variables you should disable that setting, override to specify each of the variables either in your config or each file, or put them all in a name space.

I highly recommend switching to a module pattern and disallow anything leaking into the global scope

这篇关于如何告诉JSHint在另一个JS文件中查找定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆