Your JavaScript is valid, the problem is with JSDT plugin for Eclipse. In the latest version they introduced a type verification which is problematic in many situations – not only for empty arrays (like in your case). Another typical case may look like this: a = b || c;
The plugin will complain when b and c are of different types (which is absolutely valid code for JavaScript). There is several bugs already reported to JSDT developers about this problem, but the issues are not fixed yet.
Unfortunately currently it is not possible to switch off the type verification using JSDT configuration screen in Eclipse. I switched it off directly from the JSDT source code. To do this, please follow these steps:
- Download the JSDT source code from Eclipse WebTools Project
- Open the
org.eclipse.wst.jsdt.debug.core
project with Eclipse. Make sure you have Eclipse SDK installed. It might also be required to adjust some dependencies in theplugin.xml
file. - The type verification is located in
computeSeverity
method ofProblemReporter
class. - To switch off type verification replace the line:
case IProblem.TypeMismatch: return ProblemSeverities.Warning;
withcase IProblem.TypeMismatch: return ProblemSeverities.Ignore;
- Build the project and close Eclipse.
- In Eclipse folder find the file named
org.eclipse.wst.jsdt.core<version>.jar
– make a safe copy of it, then open the jar file and replace theProblemReporter.class
file with the one you compiled in the step 5 (the file is inbin
folder of your project). - Start Eclipse and clean your JavaScript project. All type checking will be ignored by JSDT.
Important! Make sure you’ve downloaded the same version of JSDT that you are using in Eclipse. Eventually instead of replacing a single file you can replace the entire plugin.
If you don’t want to download and compile the plugin by yourself you can try with my fixed version. I’ve placed it on my FunctionSack webpage. I am using Eclipse 3.7 (Indigo) with JSDT 1.3.0, so make sure you have similar configuration if you want to use my file.