开发者

How to make JSLint tolerate a leading semi-colon?

开发者 https://www.devze.com 2023-03-29 02:31 出处:网络
The continuous integration software I am using runs JavaScript files through JSLint and complains if they fail (using default JSLint settings it appears).

The continuous integration software I am using runs JavaScript files through JSLint and complains if they fail (using default JSLint settings it appears).

I'v开发者_JS百科e always prepended a ; to the start of my jQuery plugins, so concatenating them doesn't lead to something like this...

common.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 common.js. It is appended straight to the end of common.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 complains 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 complained.

What can I do to get this safety net and pass JSLint?


Patch JSLint to not mind. Everyone will benefit.


Possibly a flippant answer, but your html can probably do this:

<script src="common.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
0

精彩评论

暂无评论...
验证码 换一张
取 消