Wednesday, October 27, 2010

JavaScript validation in a Java project with Eclipse's JavaScript developer tools

I'm not sure how may people this applies to, but I ran into the case where some of my web application packages have both Java and JavaScript files.  I use Eclipse, and wanted to be able to take advantage of Eclipse JavaScript Development Tools (JSDT) that provide rich JavaScript functionality within Eclipse. Unfortunately, JavaScript validation (e.g., catching syntax bugs) only occurs by default if you create a "JavaScript" project. Since many of these projects have both Java and JavaScript code, I had to work around this following these steps:
  1. Ensure you have JSDT installed
    • Top Menu -> Eclipse -> About Eclipse -> Installation Details -> Installed Software
  2. Exit Eclipse
  3. Add the org.eclipse.wst.jsdt.core.jsNature:
    • vim $ECLIPSE_PROJECT_ROOT/.project
    • To the natures section, add org.eclipse.wst.jsdt.core.jsNature
  4. Open Eclipse
  5. Enable the preference: JavaScript -> Validator -> Errors/Warnings -> Enable JavaScript semantic validation
    • Adjust the validation preferences as needed.
    • It's usually a good idea to set all of the "Resolution" preferences to ignore
I couldn't find this documented anywhere on the web, so passing it on here in case it's of use.