Using JSLint in Java/GWT - java

I'm currently working on a project in Java, that will allow users to type Javascript code into a GWT Widget. How would I go about importing/using JSLint to check for errors? I looked around and found LSLint4Java, but I couldn't find the way to import it into eclipse. I just need a simple JS error checker that will check a string for errors.
If anyone has any other suggestions for error checking, please share them!
Thanks.

The JSlint javascript source can be found here. You could include the javascript in your WAR, reference it with a script link in your page(s), and make JSNI calls from/to your GWT code.
Note that JSLint has a No Evil clause in it's license:
// The Software shall be used for Good, not Evil.
Here's a good start on JSNI.

Related

Java: Parse JavaScript & Flag Errors

I've been having terrible luck trying to get this to work, so I'm hopeful someone can help here.
In Java, I need to be able to take an HTML page with JavaScript within it and detect any JavaScript errors without, preferably without executing the JavaScript code.
I found this article:
Javascript parser for Java
And I've attempted to figure out how I'm supposed to use Caja to do this, but I'm having a difficult time finding any documentation with working examples of anything close to what I'm doing.
As a result I took a look at Nashorn also referenced in that article. I found a few examples which show how to execute JavaScript code from Java, but this doesn't process the whole HTML page. Even then, the execution doesn't seem to include the ability to validate common JavaScript functions (e.g. It hadn't heard of "alert").
Can anyone recommend something that might be able to do what I want, and point me in the right direction for their documentation or give me an example?
jshint as a standalone product seems to be a good fit for this:
it can run in java inside rhino (see https://github.com/jshint/jshint/)
a nodejs package exists (see https://www.npmjs.com/package/jshint)
it works with nashorn but it's quite tricky
I will only cover the technical difficulties around 3rd solution as I finally managed to make it work too...
Spoiler alert: "alert()" is not detected yet... Solution nb 2 will help there...
You first need to grab this specific release of jshint: https://github.com/jshint/jshint/releases/tag/2.4.4
Anything later than v2.7.0 will fail for now and I personally gave up patching intensively prototypes and namespaces... Releases from v2.4.4 until v2.6.3 work without modification but are limited in functionalities.
In the release notes, it's specifically written that "support for the Nashorn JavaScript engine" is working on this release. I'm using JDK8 nashorn 1.8.0_45 for this test.
Next step is to extract from this release this single file jshint-2.4.4/dist/jshint-rhino.js
Now you need to run nashorn/jjs in scripting mode and you need to be specific about the single file you wish to verify. In solution 2 (nodejs based) you can do multiple files or a complete hierarchy below a folder...
Create a simple file file.js:
function(){}
Now run the following command (please note the presence of -- ):
jjs -scripting jshint-rhino.js -- file.js
This will give you the following output:
Missing name in function declaration. (file.js:1:9)
> function(){}
So this covers the how to run jshint in a simple manner with nashorn... With the 3rd solution, at least you can find missing semicolons and several typical errors. But it's not a silver bullet and to me it's not a real alternative.
My personal preference would be to stick to solution 2 only. If you've the possibility to install either nodejs or iojs on your dev platform, go and grab https://www.npmjs.com/package/jshint. Not only will you be able to do more than the 3rd solution, you'll also be able to configure a jshintrc file as described at http://jshint.com/docs/

How to properly type javascript codes in NetBeans 7.4?

I am using netbeans 7.4 which supports HTML & PHP only. I have a javascript tutorial and I need to practice those codes in an IDE. (I am absolutely new to javascript). I created a new project (Project type is HTML5, not PHP). Then under source files I created a new javascript file. Then I started my coding.Errors are not shown in my codes when I type something wrong. I intentionally typed some code (dfd.rt())which does not exist at all, but no error is shown. Why is that? In addition I cannot see that much of good intellisense in my javascript files whereas I could get a lot of assistance from the IDE when I typed PHP codes. For examples in PHP when I type ec, IDE showed me echo, but in JS nothing is shown when I type document for doucment.write().
I feel that I am not using javascript files correctly in the IDE.
Can anybody show me correct steps to create a javascript project and type proper javascript codes in netbeans which will underline error codes in red and provide good support in intellisense
P:S - I searched in SO and found out some plugin needs to be installed, but I guess I have already necessary stuff installed. That is why they showed me javascript templates under source files. Correct me if I am wrong
Netbeans has a plugin for JSLint - Which technically is a code quality tool not a debugger but I find it very useful for JS. It will show you probably more errors and code quality suggestions than you'll need but you can configure it to your taste. check it out. http://plugins.netbeans.org/plugin/40893/jslint

ISC_Core.js generation

I'm developing a web application with SmartGWT and I found an issue with Chrome. To solve the issue I modify part of my /war/projectName/sc/modules/ISC_Core.js. Following is the code I modify:
isc.Browser.isDOM?"
if(event.target.id.indexOf(\"Geoviewer_\") != -1){return true;}
if(returnVal==false)event.preventDefault();else if(returnVal==isc.EH.STOP_BUBBLING)event.stopPropogation();":"")+"return returnVal;"
The second line is the one I add to make the code work under specific circumstances in Chrome.
Now, unfortunately this is not a very 'clean' solution as I have to make the modification after the compilation because the file is generated after it. So my question is how can I find the piece of Java in the GWT code that is generating this Javascript so as to modify it once for ever and have the project compiling right without the necessity of further modifications?
Thanks for your help
The ISC_ Javascript files aren't being generated by GWT. SmartGWT is just a wrapper between GWT and a native Javascript library (SmartClient); it's basically the equivalent of a JNI AWT toolkit interface. If you have a repeatable bug, contact Isomorphic; they're pretty good about addressing demonstrable issues.

Compiling CSS to SWF server side Java, What is the best practice?

My project allows users to create custom css for our flex app.
In regards to compiling the CSS into SWFs on the server side:
Should I use the flex2.compiler.css.Compiler class in mxmlc-3.5.0.12683.jar?
Or
Should I invoke mxmlc from Runtime.getRuntime().exec()?
The css.Compiler class is not very well documented. Does anyone have any examples that use this?
For the Runtime exec method, what is the best way to package mxmlc into the maven build so its available to the server at runtime?
I think it is so rare that people try to compile Flex Apps on the fly at the server, that there probably isn't a best practice. My intuition is that using the jar file makes more sense if your server side code is Java.
But, if documentation is a problem the Flex command line compiler arguments are well documented and may save you a lot of time.

Structural comparison of two ASTs in Eclipse

I am working on an incremental builder for Java code in Eclipse. Eclipse provides a ResourceDelta that tells me which resources have changed since the last build. However, I would like to have more detailed information, e.g. what methods or what field definitions changed. There seems to be functionality similar to what I want in the "compare with -> each other" view. However, this code is quite disconnected from the build engine and seems incompatible with ResourceDeltas. What would be a good way to figure out what I want? The best solution I can see is to compare two ASTs, but I also could not find any built-in support for that.
JavaCore does supply this information via the IElementChangedListener and IJavaElementDelta interfaces. Here's a quick code sample to get you started:
JavaCore.addElementChangedListener(new MyJavaElementChangeReporter(), ElementChangedEvent.POST_RECONCILE);
More details available in Manipulating Java code from the JDT Plug-in Developer Guide.

Categories