Are there any command-line Linux tools that can catch basic syntax errors and compile time errors in my Javascript files, even if said Javascript files are written for use in a web browser?
I typically code my Javascript at the same time I'm coding my server side code in say Ruby or Perl. It would save me significant time if I could partially test my client side Javascript the same way I test my server side Ruby and Perl -- on the command line, typically from within emacs. I'm not expecting to catch run time JavaScript errors on the server, just basic things like a mistyped variable name or an extra bracket somewhere or a runaway string, things that could be found before actually attempting to execute the code.
What I do now to test/debug Javascript is the usual cycle of "visit web app in browser; check Firebug or other console; back to emacs to fix errors; repeat." Doing that is certainly unavoidable for more complex types of errors (e.g. involving user and network interaction) but a garden variety syntax error could be caught and dealt with more quickly on the command line without loading up the browser.
I've looked a bit into some server side platforms like node.js, but they all seemed geared toward writing and executing server side code (so all of the client side specific bits in my code would presumably make it barf). I also found an emacs mode for javascript REPL but it doesn't seemed designed to do just basic compile checks - it basically loads the whole page via an external graphical browser and lets you monkey with it, which is precisely what I'm trying to avoid.
Things like YUICompressor effectively do a syntax check too.
This isn't a direct answer to your question as it is a GUI tool, but I'm a big fan of Aptana. It uses SpiderMonkey to compile your code in the background and give you red squigglies for syntax errors as you type. (It also does the same for HTML.) It also tries to give you intellisense for JS, but it is hit-or-miss. It is nice when it works.
Since I probably haven't convinced you to change your development environment, let's answer your question directly. Why not use the SpiderMonkey engine to throw together a command-line app that does what you're looking for? It looks easy enough to plug in. You won't even have to worry about the fact that you're guaranteed to get runtime exceptions (there will be no DOM objects in your environment) — you don't have to actually execute the script. Just call JS_CompileScript and check for success. (And then destroy the JSScript object, of course.)
Or, if you're lazy, you could try Rhino Shell, which is a command-line Java tool which executes JavaScript.
I find JSHint, + its vim plugin are very useful. Light weight of vim and still be able to track the syntax errors of the javascript. JSHint can also be used as a command line tool.
https://github.com/walm/jshint.vim
Javascript debugger with a console in chrome:
The Chrome browser has a javascript debugger can find JavaScript errors:
In Chrome click Tools -> JavaScript Console:
This is examining a JavaScript page with the following code:
var error(){
}
And it tells me what is wrong with it, unexpected '('.
Telling me I can't define a function like that on line 14 of my javascript file.
If you click on the link next to the error, it will take you to and highlight the line that has the error/warning.
I wrote quick-lint-js for this purpose. It points out syntax errors, among other things.
quick-lint-js integrates with several code editors, such as Vim and Visual Studio Code. It also has a UNIX-style command-line utility if you prefer.
Related
I recently started some web development, with ASP.NET and some Javascript, and something is confusing me alot.
I always read that JavaScript used to be interpreted until JIT slowly made it so chunks are compiled to machine code (which made browsers alot faster).
This makes no sense to me. How can JavaScript compile to native machine code, if traditional JavaScript apps don't target the machine/CPU to begin with?
I understand if an electron.js app gets compiled to machine code using the NodeJS runtime. That I get. Because it natively compiles to machine code and as far as I understand it, doesn't run in a browser.
If traditional JavaScript apps run in a browser, why must it be compiled to machine code? The browser is responsible for running the code, not the CPU. The CPU runs the browser itself. I actually don't see how the native OS can influence anything that happens in the browser at all or vise versa. Seems like a security issue as well.
Sorry if it's a stupid question, but I can't find any resource that will go beyond saying "Javascript uses JIT"
Thank you!
Lauren
At the end of the day, the CPU has to run the code.
JIT-compiling it down to machine code is one way to make that faster.
How can JavaScript compile to native machine code, if traditional JavaScript apps don't target the machine/CPU to begin with?
It is not "Javascript" that is doing it, it is the browser (or rather, the Javascript execution engine inside the browser), and since it is "JIT" it knowns exactly which CPU to target (this is not done in a generic way, this is done for the specific CPU that the browser is currently running on).
So, yes, there is some mismatch, since Javascript will not use low-level primitive types that the CPU can work with directly, which is why there is a lot of indirection and speculative type inference guess-work. The resulting machine code is much different than you would get from hand-coded assembly, but it can still be a net positive. To help with this, WASM was developed, which is closer to "normal" machine code.
Other intermediate, non-CPU specific formats like JVM bytecode or CLR bytecode or LLVM bitcode are in a similar situation (in that can also be compiled to machine code they do not themselves target directly) -- but they have been "lowered" already from language source code to something close to machine code.
Seems like a security issue as well.
Yes, it can be. The browser has to be careful in what it is doing here, and the OS should sandbox the browser as much as possible.
Executing instructions is easier than running an interpreter, and JIT seeks to take advantage of this for a performance boost. All programs running on your computer become machine code at some point, the only question is which instructions are be executed.
let x=0;
for (let i=0;i<100;++i) {
x+=2;
}
Since it is clear that there are no side effects in a block of code like this, it is faster to compile instructions directly, rather than interpreting each line.
// NIOS 2 assembly, sorry its the only one i know
movi r2,0
movi r3,0
movi r4,100
loop:
addi r2,2
addi r3,1
blt r3,r4,loop
Executing this will be faster than executing the parsing logic for each individual instruction.
TLDR: All programs are always running CPU instructions, so it is faster to minimize the number of instructions by skipping the parsing stage when possible
I am currently trying to learn javascript. Coming from a python/R background I find it really useful for learning purposes to be able to write code in a script in Rstudio/pyCharm which I can then execute in a interpreter by highlighting specific lines of code and then pressing a ctrl+enter or some other keyboard shortcut.
The console available on firefox/chrome seems incredibly rich and useful for learning / testing specific pieces of code but I find it quite limiting that I can't store each line in a script with comments/notes to myself.
Is there a way to run lines of javascript in firefox/chrome like pycharm & rstudio can with their respective interpreters or how is it usually recommend for people to learn the language in an efficient manner ?
Use node.js which is a javascript runtime using v8 (powering the chrome console)
You can get an REPL similar to Python.
You cannot, however use it to modify the DOM or access the window. In such case, using an online IDE like jsfiddle or codepen might be a good alternative.
I'm trying to use Eclipse for JavaScript (the "Eclipse IDE for Java EE Developers" package).
My project uses Bluebird (a promises implementation), and so it has a lot of lines like:
somePromise.catch(function(err){...
Eclipse considers this to be an error, probably because it thinks that "catch" is a reserved keyword that cannot be used as a method name. Same for the promise.finaly method. Maybe it's right, but I'd rather not switch to a different library just because of this.
Is there a way to make it ignore these specific errors (but keep reporting other errors in the same files)?
This has been reported and fixed not long ago in this bug :https://bugs.eclipse.org/bugs/show_bug.cgi?id=443876
Go into Preferences->Javascript->Validator->Errors/Warnings and uncheck a new option "Strict validation of JavaScript keywords usage". That should fix it.
I had the same problem before and had implemented a plugin that manipulated bytecode of JSDT at load time to silence this error. Now such hacks are not needed.
I am new to JavaScript (coming from c++) and asking myself whether it is possible to compile and debug JavaScript code. I tried to use VisualStudio2012 and SublimeText2 with Node.js, but in both cases, I got a compile error when I tryed to use a library (3djs).
In SublimeText2 the error message is "ReferenceError: d3 is not defined".
I got that error trying to compile this d3js example: http://bl.ocks.org/mbostock/3828981
Strangely the example worked perfectly when I opened it in some browsers..
Can anyone tell me what I am doing wrong or whether it is even possible to compile or debug JavaScript code?
(Sorry, this seems to be a very beginner question but I did not find the answer searching for some hours...)
Best regards!
node.js also doesn't compile the JavaScript (not in the C++ sense; JavaScript interpreters often Just-in-Time compilation). The main difference between node.js and a browser is that node.js doesn't try to hide errors from you.
So the first question is whether you correctly included the library in your project. For this, we need to see the source code that you use to load / import it.
If you're sure that the library has loaded correctly, then you might be triggering a bug in the JavaScript interpreter. Try to install the latest version or the lastest stable version or the version before that.
[EDIT] In HTML, you use a <script> element to include scripts in the context of the page. When using node.js, then you have to use require():
var d3 = require('./d3.js')
That said, D3.js is a client framework. It expects the standard browser environment like a DOM. It will not work inside of node.js. node.js expects modules (= stuff that you can import using require) to have a certain format.
So the simple answer is: You can't "compile" and debug JavaScript code in your IDE. You always need to create a HTML page and open that in a browser. All modern browsers have consoles, plus tools to debug and examine JavaScript, CSS, HTML and the DOM.
I recently noticed and fixed a pretty bad JS bug in our software, affecting all IE versions, that was caused by a simple mistake in a .js file:
const foo = "..."
Now, IE doesn't support const; it's a syntax error. var should be used instead. (The offending keyword was actually inserted unwittingly by IntelliJ IDEA's "introduce variable... -> introduce constant" refactoring.)
Our automated Selenium tests are run with Firefox on Linux, and getting them running on IE would probably be too much hassle right now.
Anyway, my question is, is there any static JS code analysis tool that
would have caught the const bug (and similar common problems), and
can be easily triggered from a CI tool (Jenkins) against certain .js files in a codebase?
I am aware of JSHint, JSLint and Google Closure Tools, but I don't know if any of them meets my criteria above.
JSHint or JSLint would have caught the error in time. You can configure IntelliJ to show these kind of issues realtime. There is a Jenkins plugin available as well. I hope you find those useful. :)