admin管理员组文章数量:1402280
The continuous integration software I am using runs JavaScript files through JSLint and plains if they fail (using default JSLint settings it appears).
I've always prepended a ;
to the start of my jQuery plugins, so concatenating them doesn't lead to something like this...
mon.js
I don't have access to this file, and can't enforce a semi colon at the end.
var abc = function() {
alert(arguments[0]);
}
plugin.js
This is my file that is concatenated with mon.js
. It is appended straight to the end of mon.js
.
(function($) {
$.fn.somePlugin = function() {}
})(jQuery);
jsFiddle of the problem this can cause.
jsFiddle of the solution (leading semi-colon of my plugin).
However, JSLint plains with...
Error:
Problem at line 1 character 1: Unexpected space between '(begin)' and ';'.
;(function($) {
Problem at line 1 character 1: Expected ';' at column 5, not column 1.
;(function($) {
Problem at line 1 character 2: Missing space between ';' and '('.
...
I tried using a bang operator (!
) instead, and a few other alternatives, but JSLint still plained.
What can I do to get this safety net and pass JSLint?
The continuous integration software I am using runs JavaScript files through JSLint and plains if they fail (using default JSLint settings it appears).
I've always prepended a ;
to the start of my jQuery plugins, so concatenating them doesn't lead to something like this...
mon.js
I don't have access to this file, and can't enforce a semi colon at the end.
var abc = function() {
alert(arguments[0]);
}
plugin.js
This is my file that is concatenated with mon.js
. It is appended straight to the end of mon.js
.
(function($) {
$.fn.somePlugin = function() {}
})(jQuery);
jsFiddle of the problem this can cause.
jsFiddle of the solution (leading semi-colon of my plugin).
However, JSLint plains with...
Error:
Problem at line 1 character 1: Unexpected space between '(begin)' and ';'.
;(function($) {
Problem at line 1 character 1: Expected ';' at column 5, not column 1.
;(function($) {
Problem at line 1 character 2: Missing space between ';' and '('.
...
I tried using a bang operator (!
) instead, and a few other alternatives, but JSLint still plained.
What can I do to get this safety net and pass JSLint?
Share Improve this question edited Aug 17, 2011 at 2:07 alex asked Aug 17, 2011 at 1:55 alexalex 491k204 gold badges889 silver badges991 bronze badges 6- 1 Perhaps a better concatenator is in order. What are you using to smush your js files together? – Paul Commented Aug 17, 2011 at 2:00
- @Paul I am not sure, this has been set up for a while and I am a relatively recent employee here. – alex Commented Aug 17, 2011 at 2:01
- If your file is being added to mon.js, how do you not have access to mon.js? – Paul Commented Aug 17, 2011 at 2:07
- 4 For what it's worth, I sent Crockford an email this morning with a reference to this thread and he responded, asking me to try it. So I think he updated jslint based on what I'm seeing here: github./douglascrockford/JSLint/mit/… – Shane Commented Apr 22, 2014 at 1:41
- 2 @Shane That was really constructive and useful. So bad you don't get more up-votes I don't know why. I have seen issues like this in JSLint grow into religious wars. – adelriosantiago Commented Aug 23, 2015 at 8:55
3 Answers
Reset to default 6Patch JSLint to not mind. Everyone will benefit.
Possibly a flippant answer, but your html can probably do this:
<script src="mon.js"></script>
<script>;</script>
<script src="plugin.js"></script>
Or if you don't like the inline script on the second line, make a file called semicolon.js
and, well, you know....
Sorry if this is ridiculous, but JSLint does not like a plain old empty statement on a line by itself. Somewhat unfortunate. Oh well.
BTW awesome fiddle. Seeing that alert run because of the missing semicolon after the var declaration of the function was, like, wow what do you know? That function got called as it should! Very interesting.
What happens if you put the ;
on a line by itself, or put some other syntactically valid but meaningless statement like this:
"begin plugin";
(function() { // etc
EDIT: what about using void
:
void("begin plugin");
(function() { // etc
//or
void(
(function() { /* your code */ })()
);
EDIT 2: what about some variation on this:
if (console && console.log) { // or if (false)?
console.log("Begining plugin definition");
}
(function { // etc
本文标签: javascriptHow to make JSLint tolerate a leading semicolonStack Overflow
版权声明:本文标题:javascript - How to make JSLint tolerate a leading semi-colon? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744352577a2602140.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论