javascript code analysis, abstract syntax tree & stuff - javascript

I need to make a script that will analyse javascript files for specific errors in the code.
One of the first envisioned ways of doing so is by building an abstract syntax tree of the files. Is this doable with nodejs or any other tool? (preferably in a scripting language, or better yet in javascript itself, or python).
Or is there even a better method that I could use to analyse javascript code?
EDIT: As pointed out in some answers, JSLint and JSHint are not the tools I'm looking for. I use them as well, of course, but they are not specific enough for my requirements.

A better method to go for some pre-written tool instead of trying to reinvent the wheel.

Somebody already awnsered this question on another stackoverflow page
JavaScript Source Code Analyzer

Looks like esprima - http://esprima.org/ and the Parser API is what I'm looking for :)

Related

Is there a JavaScript-based Python?

I have a lot of JavaScript I need to write. I can do that, but I have some Python already which does something similar. I'd much prefer to adapt that and have it run within the browser.
There's the C-based reference implementation, and there's Jython in Java. Is there a JavaScript implementation along the same lines?
I have looked, but it may be that my google-fu has failed me. And it seems like something that ought to exist. Have I missed it?
You may be looking for something likeWebAssembly, used for running non JavaScript code in the browser. pyodide would be a good starting point
https://github.com/pyodide/pyodide
https://medium.com/swlh/python-in-web-easy-5f7de3813055

Compiling Oniguruma regex library to javascript using Emscripten

I'm trying to get a more powerful regex library into javascript. The only solution I found is to compile Oniguruma regex library to javascript using Emscripten
I've installed Emscripten and tested it with their small test scripts, also downloaded oniguruma source code, but still don't know what should be done next.
Anyone familiar with emscripten?
When you utilize Emscripten, the general way of building/compiling from C/C++ stays similar. The steps which change, are that you don't use e.g. the gcc compiler but Emscripten compiler.
That said there is the general question of whether you are familiar with C/C++ and more specific with autotools (which seems like the build tool Oniguruma uses). If you are not, you will probably have a very hard time understanding what needs to be done and how.
Last I checked Emscripten did not have support for Libtool, so building, utilizing autotools, will probably fail. Feel free to ask at Emscripten IRC channel though, whether this is indeed not possible.
Another way I can think of is using autotools to generate Makefiles and then writing custom targets for Emscripten programs. Beware that this is for advanced users, familiar with the make cruft.
If these steps are to taxing for you perhaps you should see whether a Javascript library can be sufficient for you.
A more realistic approach to do this is going to be to use http://xregexp.com. It adds many more features to RegExps and compiles them down to JavaScripts more limited RegExp dialect so it can get the best of both features and performance. Compiling a regexp library using emscripten is very unlikely to be performant enough to use in production. For some uses, emscripten is excellent, but in this case it seems like the overhead is going to be not worth the cost.
The author of XRegExp even has an article on lookbehinds http://blog.stevenlevithan.com/archives/javascript-regex-lookbehind

Is there any ActionScript 3 to JavaScript (and vice versa) migration guide?

as a skilled AS3 developer I wonder if there is something like AS3 to JS migration guide. After all both languages have a lot in common.
I have in mind something like the old AS2 to AS3 migration guide - a list where I could simply look up the AS3 functionality and the JS counterpart, or vice versa.
I am not saying you can easily convert all your code like that but it would tell me where to start.
PS: I think it is a great idea and if such guide doesn't exist someone should write one :).
I've read that with Haxe you can compile somewhat transparently to JS code. This requires Haxe code, I think, which is not ActionScript 3.0 but is close and should be easy to pick up if you're already comfortable with AS.
I haven't tried this myself but it's probably worth to check out. Here's a blog post that might give you some ideas: http://gamehaxe.com/2010/05/25/javascript-ready-or-not/
You're going to have a difficult time of this considering a lot of the flash.* packages don't have any native JavaScript counterparts. Maybe a robust JavaScript library can ease the transition, but there will be a lot not migrate-able.
However, many of the Top-level datatypes do have a JavaScript counterpart (i.e; Date, Array, String, Object, null, Number/Integer)

JavaScript equivalent of C indent

There is a GNU indent for styling C sources, are there any equivalents for JavaScript?
I want it to have options for coding style, indention style, etc. just like indent.
I will be using it for a pretty large project on *NIX platform, so I will not like Windows-only utilities or online tools.
I haven't used it, but I bookmarked js-beautify a while back, and a quick check seems to indicate it might satisfy your requirements. (And, being written in JavaScript, you probably can improve on it easily.)
You have a few options:
jsbeautifier
Google Closure Linter
Google Closure Compiler Service (w/ pretty printing option)
Some editors also do this for you:
Emacs
vi(m)
Eclipse
...
EDITS:
Another online tool, JavaScript Indent claims it uses Indent in the background itself. Maybe the author would share the settings. Not so great, but a start.
the JS Beautifier page also mentions that a Python version if being developed as we speak. Let's check again later. In the meantime you can use it with Rhino as mentioned on their page.
Finally, there are already a few SO questions answering this which you can find by searching (some of the results point to formatters to display code online, which is a different thing, though you could use that as well):
Best Source Code Formatter for JavaScript
Formatting JavaScript source code
There's jsBeautifier, which can be run from the command line via Rhino in a Java-enabled environment or run via V8 (or a couple of others, check out the github page). Apparently they're working on a Python command-line version.
Apparently someone's made a start (at least) on updating indent itself to understand JavaScript. The joy of open source! Even if incomplete, the syntaxes are so similar... (No, apparently it's just a web page front-end to indent — not helpful at all.) So rephrasing: I wonder how hard it would be to modify indent to support JavaScript? The syntaxes are so similar...
There is Javascript Tidy, which uses the browser itself to reformat Javascript code. So the results depend on the browser you use.
You cannot determine the style, but once the Javascript has been reformatted to a standard format, it's easier to modify the sourcecode to match your own style.
libjavascript-beautifier-perl package has got /usr/bin/js_beautify, which does the trick for me.

Garble JavaScript code before sending to client-side

How can I garble JavaScript code before sending it to client-side? I don't want to expose functionalities. The code is already compressed using jsmin.
You need an obfuscator, see this for an example.
Anyone determined enough can always reverse it though, and tools like this can help.
Javascript is interpreted plaintext. As such any obfuscation is a placebo at best and easily reversed.
More to the point, if you're writing enough Javascript to constitute a work valuable enough to protect, you're doing something wrong.
My Javascript these days consists of including jQuery, a handful of plug-ins and some code to activate those plug-ins and do some styling.
I have a hard time envisioning writing anything in Javascript significant enough that I'd care about protecting it, which is just as well because you can't protect it. Not really.
You could try some online obfuscator like this:
http://www.javascriptobfuscator.com/Default.aspx
Search google for: Javascript obfuscator
You might add your platform to the query, as well. There are plenty of tools already out there that you can incorporate into your project.
A word of warning: most obfuscated JS on the internet is security hole exploit code. You risk being mistaken for a sploit, particularly by the more paranoid anti-virus scanners.

Categories

Resources