Compilers vs Interpreters in terms of code analysis - javascript

I started being a developer using Java and got used to powerful IDEs which support me when writing my source code. Being more precise, e.g. when trying to use a function that does not exist in Eclipse, I immediately get an error when typing this, without running my application. How does Eclipse does this? By pre-compiling my code?
Nowadays developing JavaScript or Node.js, I still don't understand why such features are not existing in an IDE using an interpreted programming language. I understand that an interpreter analyzes the code during execution. But why is it not possible e.g. to build a plugin for an IDE as Visual Studio Code which pre-analyses my code while writing it? I would really appreciate getting an error when trying to call a function in JS that does not exist, for example. Also a linter does not help in such cases.
Anyone can support my understanding with this? Thank you a lot :)

An IDE can analyse your code, and it could warn you that you have appear not to have defined a function which you appear to be calling. But in many cases, you might want to tell the IDE to take a hike and stop bugging you, because the function definitely exists. It's just not visible to the IDE.
C++ and Java don't permit that case. If you call a function by name, the compiler must be able to see that function. (In C++, the compiler doesn't really know whether or not that function will eventually be included in your project; it may be that all it knows is whether you have included a header which declares a prototype for the function. But you must have done at least that, and so the compiler -- and the IDE -- have good reason to assume that you intend that function to be part of the executable.
Java is even stricter. If the compiler can't see the other class modules you're using, it won't compile your function. (With C++ you could compile your function (as long as you have the header with the functions it is calling) before you even write the functions being used, although you cannot create the executable until you have all the necessary pieces available.)
But Javascript doesn't require declarations. Javascript doesn't need to know what the argument or result types of a function you're calling are. So it doesn't need to force you to provide that information, ever. When you eventually get around to running the program, the JS interpreter will have to find the functions you are calling, but they don't have to be in the text of the program you wrote. They could be in a different file which you dynamically added to the execution environment, or they could be in a web page into which your Javascript program has been loaded. Or they could even be created on the fly and added to the execution environment with eval. (Or in other ways.)

Related

How to compile Javascript console application locally?

I am a beginner in Javascript, I decided to practice Javascript by problem solving using it, I found an online judge that accepts Javascript V8 4.8.0 code.
So, I searched online to get that version of Javascript V8 on my machine, but I couldn't find any easy way, All the pages were explaining how to build it, and it seems to be a process that I don't need to go through.
Is there an easy way to compile and run command line apps written in Javascript on my machine?
Note: I don't want to use node.js because I tried using it's I/O and
as a beginner I think it is complex in some way.
Update: I found that package manager pbox.me which provides a version of V8 JavaScript Engine and I managed to install it.
Yet another problem appeared: whenever I try to run a js file writing d8 myfile.js in command line nothing happens as if it is an empty program, knowing that I tryied to d8.exe file and it is working, and I made sure the PATH is inserted in the environment variables.
What am I doing wrong?
The easiest way to get started with JavaScript is probably to use it in a browser. You can type simple things directly into the browser's JavaScript console (check the menu); or you can embed your code in a simple HTML document.
If you want, you can even pretty easily implement the readline()/print() functions, so you can pretend to be doing stdin/stdout based I/O: just read from an array of strings, and send output to console.log (or create DOM nodes if you want to be fancy and/or learn how to generate dynamic website content by hand).
Side note: V8 4.8 is severely outdated, don't use it to execute code you haven't written yourself.

Exposing a Node.js library via ambient TypeScript

In an attempt to simplify the use of pg-promise library for those who prefer coding for Node.js in TypeScript, I created a complete set of ambient declarations needed to fully represent the library, and included it into the distribution in a separate folder.
However, being not a proficient TypeScript developer, I'm stuck in my research for best practices in doing this sort of thing.
I want to know if simply including ambient declarations in a separate folder is sufficient for developers to start using it right away, without running into problems.
I also want to write some sort of a short guideline for how to use those TypeScript declarations, but frankly, I am myself not doing a good job at it, even though the declarations all seem correct.
So my question is then:
Is there any article or a guideline to explain it to Node.js developers how to do it with the latest version of TypeScript? (best practices for exposing a Node.js library via TypeScript)
UPDATE
One of the very first problems I'm running into, trying to use such ambient declarations directly, from a TypeScript file - the main file refuses to find the two dependent ones, no matter what change I try, it keeps on saying Cannot file module....
UPDATE
I've tried countless per-mutations for how to re-declare the library, and yet I haven't been able to get it work after all.
I've tried to move dependent declarations inside one file, using in their own namespace and as an inner namespace.
I've tried different combinations of ambient declarations with classic declarations.
I've tried changing modules into namespaces.
It only brought me to a point of desperation and dislike toward TypeScript. All I want is to make my Node.js library readily usable for TypeScript developers.
Most of the time I've running into issues where it says that my TypeScript is not a module. In other cases it couldn't find some types. Nothing but trouble. Please somebody help me with this.
UPDATE
Should I even include TypeScript with the library or should it be published on Definitely Typed instead?

Can I use a Dart interface from manually written JavaScript?

I consider using Dart for my next web project and it has made a very neat impression on me so far. Obviously, Dart code can be converted into JavaScript. However, I wonder if this results in code where functions can be used from other JavaScript files.
It is supported to call Dart functions from JavaScript but only functions that are made available to JavaScript explicitely.
See
Calling Dart code from javascript
Expose Dart functions to javascript
When you build Dart to JavaScript minification and tree-shaking make it hard or mostly impossible to call methods because functions might not even be included in the output if the analysis recognized that the function isn't called anywhere and if it actually is included it will have a shortened and cryptic name.
I have seen it mentioned that here are attempts for better support of this use case but there isn't anything available yet.
If your generated JavaScript is accessible then for sure it will regardless of it is generated using dart or anything else

How to find unused/dead code in web projects (90% code in javascript)

I did find a very interesting tool for identify unused css definitions in a web project.
http://www.sitepoint.com/dustmeselectors/
Are there similar tools also for javascript projects?
P.S.
I know there is no program for deterministically finding unused code. But I am looking for a report to identify possible unused code. Then the last decision will always be your own.
Problem is there is no way to be really sure. Suppose the following:
The initial HTML site is practically empty. There is a lot of JS code though, which seems to be unused.
OnLoad, a function is called which launches an AJAX query to the server. The server returns a lot of HTML code, which is the body of the site. This body contains lots of JavaScript functions.
The initial body is replaced with the body received via AJAX. Suddenly, all code is used.
Static analysis utilities are therefore useless. I do not know whether there exists a browser extension that marks all JS usage from a running browser though.
You can try using tombstones to safely locate and remove dead code from your JavaScript.
https://blog.bugsnag.com/javascript-refactoring-with-bugsnag-and-tombstones/
In order to find the unused assets, to remove manually, you can use deadfile library:
https://m-izadmehr.github.io/deadfile/
It can simply find unused files, in any JS project.
Without any config, it supports ES6, JSX, and Vue files:
The one that comes to mind most quickly is Javascript LINT (http://www.javascriptlint.com/) and JSLint (http://www.jslint.com/).
Beware though: the latter hurts your feelings.

JavaScript bytecode compiler?

For a project I'm tangentially working on, I need a way to compile JavaScript into some intermediate language or bytecode so that I can single-step through it. I know that many of the JavaScript engines in modern browsers do something like this, but they don't make the resulting bytecode accessible. Is there a good off-the-shelf tool for this sort of JavaScript compilation?
Not exactly sure of your needs, however maybe Rhino could work for you.
The JavaScript compiler translates
JavaScript source into Java class
files. The resulting Java class files
can then be loaded and executed at
another time, providing a convenient
method for transfering JavaScript, and
for avoiding translation cost.
More about the compile function is located here.

Categories

Resources