JSLint’s ‘unexpected end of line’ vs. JQuery style

June 10, 2010

I recently added a script to run JSLint over any javascript files that were changed in my last batch of commits when I push changes to our main repository (if you’re interested, you can take a look).  Immediately, I had my feelings hurt when it started reporting a bunch of errors and warnings.

Most of them I agreed with, but the ambiguous end of line warning when the line was catching syntax that I had very purposefully used to increase readability (for me).  Basically, this warning appears if a line doesn’t end with an operator like ‘+’ or ‘.’ that indicates there’s another part required.  However, I like to use these operators at the beginning of a new line in order to make it easy to scan and see which lines are continuations.

For example, I would write my jQuery code like this:

$("#someId").after("<br />")
     .css("color","red")
     .show();

However, this will generate “lint warning: unexpected end of line; it is ambiguous whether these lines are part of the same statement” for the last two lines.  Instead, JSLint expects the code to be written like this:

$("#someId").after("<br />").
     css("color","red").
     show();

To me, this makes it too easy to not notice that methods like ‘css’ and ‘show’ are properties of an object and not just individual methods. On the other hand, I wasn’t sure whether it would be a good idea to just ignore a warning from lint because it would look prettier.

Therefore, I talked it over with my coworker, Xavi, and he suggested we turn off the Lint check for this ‘error’. In addition to us, pretty much the whole jQuery community agrees with our formatting. As long as we pay attention to the other errors in Lint, we shouldn’t be writing code with missing semi-colons.

To fix this, I just added a configuration file with a single line: “-ambiguous_newline”.  Once I referenced the config file in our script, these errors stopped being reported.

Advertisement

One Response to “JSLint’s ‘unexpected end of line’ vs. JQuery style”

  1. Sara Says:

    Hi, as a webdesigner I always use these statements on a single line. Another example is conditional statements (using :) on multiple lines its a nono!…
    So even if jquery dont mind to step out of javascript standards, I do not… :)


Leave a Reply

Fill in your details below or click an icon to log in:

Gravatar
WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.