alex
alex

Reputation: 490243

How to make JSLint tolerate a leading semi-colon?

The continuous integration software I am using runs JavaScript files through JSLint and complains 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...

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?

Upvotes: 9

Views: 1993

Answers (3)

nnnnnn
nnnnnn

Reputation: 150030

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

Upvotes: 0

Ray Toal
Ray Toal

Reputation: 88378

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.

Upvotes: 0

Ned Batchelder
Ned Batchelder

Reputation: 375584

Patch JSLint to not mind. Everyone will benefit.

Upvotes: 6

Related Questions