Javascript 如何同时使用JSHint和Browserify?
我正在尝试使用Angular和Browserify构建一个项目。我的Javascript 如何同时使用JSHint和Browserify?,javascript,jshint,browserify,Javascript,Jshint,Browserify,我正在尝试使用Angular和Browserify构建一个项目。我的controllers.js文件如下所示 'use strict'; module.exports.testController = function($scope){ $scope.message = 'Controller 1'; console.log( 'hello' ); }; 正如您所料,这会产生三个linting错误 使用Strict的函数形式 未定义“模块” 未定义“控制台” 我确实找到了
controllers.js
文件如下所示
'use strict';
module.exports.testController = function($scope){
$scope.message = 'Controller 1';
console.log( 'hello' );
};
正如您所料,这会产生三个linting错误
- 使用Strict的函数形式
- 未定义“模块”
- 未定义“控制台”
jslint Node:true
放在文件顶部,使JSHint能够处理Node.js文件,如下所示
/*jslint node: true */
'use strict';
module.exports.testController = function($scope){
$scope.message = 'Controller 1';
console.log( 'hello' );
};
module.exports = (function(){
'use strict';
var myComponent = {};
myComponent.testController = function($scope){
$scope.message = 'hello';
console.log( 'Hello' );
};
myComponent.testDirective= function($scope){
$scope.message = 'hello';
console.log( 'Hello' );
};
return myComponent;
}());
然而,这显然解决了太多问题console.log(…)'仍应未定义
有人知道如何在Browserify中使用JSHint吗 我讨厌回答自己的问题,这感觉像是偷窃,不过,答案是这样的。有几种方法可以剥下这只猫的皮,但这个解决方案可能是最“正确的”
Modify.jshintrc 您应该做的第一件事是修改.jshintrc以便
"globals": {
"define": false
}
变成
"globals": {
"define": false,
"module": false
}
修改代码 现在您需要像这样修改代码
/*jslint node: true */
'use strict';
module.exports.testController = function($scope){
$scope.message = 'Controller 1';
console.log( 'hello' );
};
module.exports = (function(){
'use strict';
var myComponent = {};
myComponent.testController = function($scope){
$scope.message = 'hello';
console.log( 'Hello' );
};
myComponent.testDirective= function($scope){
$scope.message = 'hello';
console.log( 'Hello' );
};
return myComponent;
}());
现在JSHint将显示console.log的linting错误,但不显示模块的linting错误。这是由.jshintrc
ammend提供的
use strict
linting错误在将所有代码包装到函数中时得到修复
需要()
更进一步,因为我们使用的是browserify,所以还需要require()
。所以我需要再次修改.jshintrc
"globals": {
"module": false,
"require": false
}
注意:我已从globals中删除了define
,因为我没有使用它。从版本JSHint开始,支持browserify
标志
与所有标志一样,您可以在源文件中直接使用它:
/*jshint browserify: true */
// browserify code here
或者将其添加到.jshintrc
文件中:
{
"browserify": true
}
对于那些全局变量,为什么false
?为什么不正确?“如果值是false(默认值),JSHIN将把该变量看作是只读的。将它与UNDEF选项一起使用……”因此,基本上,如果我意外地试图覆盖这些全局变量,JSHIT将告诉我。OK,我假设FALSE是明确禁用这些全局变量的。感谢不管我做什么,我都无法让.jshintrc
解决方案与JShintV2.9.2一起工作。