Automatically Fix "missing semicolon before statement" - javascript

I have a large javascript file that is missing a lot of semicolons. It works fine as it is, but when I try to use a minifier = kaboom! Is there a way to automatically fix this? I tried to go manually through it but it's not humanly possible.

check out the JavaScript Lint application that checks your script for common errors (semicolon, curly brackets, trailing decimals, nested comments, etc.)

Related

Strange JavaScript warning in VS IDE

I'm getting an odd code syntax warning in Visual Studio, for a bit of JavaScript:
If(1==1) {
alert("!");
}
The warning is on the opening curly brace:
TS1005 (JS) ';' expected.
I tried adding a semicolon after the closing curly brace, but that didn't make a difference:
I've also determined that when this if block is included anywhere in the script—with or without the trailing semicolon—the entire script fails to execute in the browser (Chrome, latest version).
According to W3Schools, the syntax is correct.
This seems very strange. What's going wrong here?
If should not have a capital letter. If is recognized as a function by VS and therefore expects a ;. The warning should be fixed by using if instead of If. I tested it locally on my VS IDE and using If generated the same warning, but if is fine.

Bypass ESLint's `no-unused-var` for Should in a Mocha test

I'm using ESLint on all my files, including the test files, with a no-unused-var: true rule. I'm using Should.js in my Mocha tests, and in one of the files, I'm getting an error on the should variable. A quick comparison to the other tests shows, that in other files I have at least one line that starts with should (i.e. should.not.exist(err);), whereas in this particular file, I only use it in property form (i.e. a.should.equal(b)).
Short of turning the rule off for the entire file, or coercing perfectly readable tests into the variable use of should, is there any way around this? Can I turn off the rule just for the should variable? Perhaps add an exception for it? Or (hopefully) a more elegant solution?
In this particular file you can just require instead of declaring.
var should = require('should');
instead do
require('should');
Well, doesn't seem like there's an elegant solution to this, but I think I came up with a close approximation: I just added a single line to my before() hook, stating simply should;. Since this evaluates to the should object, or in other words a "truthy" value, it can be treated as a no-op. The side effect is that now the should variable is "used", and ESLint does not throw a warning.
I'm going to let this answer hang for a while, hoping one of you has a better solution.
Per request, here's a gist showing the hack in action.

Why does Eclipse complain about missing semicolon in my javascript?

I'm having an issue with Eclipse where it's complaining about missing semicolons in javascript code in a jsp file. Given the two lines below, Eclipse complains about the first line and indicates that there's a missing semicolon right before the closing curly brace. It has no complaints about the second line. I'd prefer to use the first way, but I'm annoyed with the warnings. Is there a different syntax that I should be using? I'm also using JQuery, so I don't know if this is contributing to the parsing error in Eclipse.
var isFoo = ${actionBean.isFoo}; // javascript type is boolean
var isFoo = '${actionBean.isFoo}'; // javascript type is string
To answer some of the questions people have posed in the comments...
This code is from a JSP file and actionBean refers to the Java action bean for this page.
"isFoo" is a member variable of the action bean. The syntax ${actionBean.isFoo} is JSP Expression Language (or EL for short) and it's used to evaluate a java variable in a JSP. In this case, my code takes the value from the java variable and assigns it to the javascript variable. The code works just fine, but Eclipse complains.
var isFoo = ${actionBean.isFoo};
This is not valid JavaScript, which is why you're getting a syntax error. I'm assuming the ${...} is supposed to do some interpolation in NetBeans, but Eclipse has no way of knowing that, and just tries to parse it as JavaScript. Unless there's an Eclipse plugin or setting for dealing with mixed code like this, you may just have to deal with seeing syntax errors.
Eclipse is complaining because it is not really Javascript, and it is trying to parse it like Javascript. If it is really annoying you could disable the warning in the preferences, under your own risk to not be notified in other circumstances:
var isFoo = ${actionBean.isFoo};
is not javascript, it is JSF

How to keep "*/" in regular expression from being interpreted as block comment?

I'm working on a bookmarklet which is using the replaceText plugin to wrap all words (and extraneous spaces/punctuation) on a page in span tags. That plugin traverses all the text nodes on a page and allows me to call a function to manipulate the contents of each one without breaking any other HTML formatting on the page. (None of this is the problem, I'm pretty sure, but I felt like the context might be useful). My call of the function looks like this, for your reference:
$("body *").replaceText(/\S+\s*/g, spanWrap);
The problem is that the best regular expression I've found for separating these words for my purposes -- /\S+\s*/g -- contains the characters for the end of a block comment ("*/"). If I add the opening of a block comment a few lines before it in the .js file in Notepad++, I can see that the syntax highlighter is reading it as that.
When I run my bookmarklet, most sites seem to have no problem with this issue and the bookmarklet works as intended. However, some sites, for reasons I can't predict, throw up an "Uncaught SyntaxError: Unexpected token <" error and the bookmarklet breaks/stops running. If I change the regular expression I'm using in the replaceText function to one I had been using in an earlier version of the bookmarklet -- /\b(\S+?)\b/g -- while changing absolutely nothing else in the bookmarklet, these sites stop giving the error and the bookmarklet works just fine, so I have to believe that it's the presence of the block comment closure that's causing it.
For the purposes of what I'm trying to do with the bookmarklet, though, the expression with that comment closure in it --/\S+\s*/g-- works much, much better than the other one, which doesn't catch punctuation and white space. However, I'd also really like it if my bookmarklet didn't break on certain sites.
So, is there either a way to fix the regular expression that I have so that it's not being read as a comment or can you suggest one that can do the same job maybe with a different syntax or something? (If it's not obvious from my question, I have the barest understanding of how regular expressions work and have gotten the ones I'm using in this example by copying them from other Stack Overflow questions/answers)
Use the long version:
var regex = new RegExp("\\S+\\s*", "g");
$("body *").replaceText(regex, spanWrap);
(EDIT: Escaped the backslashes in the string)
So, is there either a way to fix the regular expression that I have so that it's not being read as a comment
I can't think of anything sane. (You could get the effect by using the RegExp constructor and breaking the regex up into two strings and then concatenating them back together for the regex. I wouldn't call that sane though.)
I'd use a series of line comments // instead of a block comment.

Check javascript for missing ";" before compression

I'm doing some optimizations and decided to compress my javascript files using YUI Compressor. The problem is, that some code lines are missing ";" at the end, since javascript allows that and developers do not look too much at this.
Is it going to be a problem when code is compressed? If it is, is there a way to check javascript for lines that are missing ";"?
jsLint can check your code for that. And yes it will most likely cause issues unless the compressor actually contains a JavaScript parser and actively fixes missing semicolons.
According to this SO answer, YUI Compressor can handle it.
I've done a simple test on three JavaScript compressors: Yuicompressor, Yuglify and Google Closure Compiler. On my PC with Ubuntu 12.10, I've downloaded the binaries of every compressor, and then tested this file on every one of them:
function dbz(){
var goku = 1
var vegeta = 2
var freeza = 3
console.log(goku + vegeta + freeza)
}
dbz()
And here is the results:
Yuicompressor (2.4.7):
function dbz(){var b=1;var c=2;var a=3;console.log(b+c+a)}dbz();
Yuglify (0.1.2):
function dbz(){var e=1,t=2,n=3;console.log(e+t+n)}dbz();
Closure-Compiler (version 20121212 revision 2388):
function dbz(){console.log(6)}dbz();
Althought this is a very simple example, all of then worked fine on lines with missing semicolons. All of them detected lines without semicolons at the end, added it and removed the line break afterwards.
You could always use regexp to match for newlines that dont have a ; before them, and naturally make exceptions for stuff like empty lines, )} etc.
But to be honest, if it really doesn't already do this sort of thing automatically, seems like it really is broken \ plain bad.

Categories

Resources