I feel I'm overlooking something here. I've created a nice Typescript file, but WebStorm complains that all my functions are 'Unused'. Well, they are .. just not locally to the file itself. Is there some JSDoc or other commented code I can put in to tell WebStorm that I KNOW this function IS used .. just used externally?
Yes - //noinspection JSUnusedGlobalSymbols; hit Alt+Enter on your function that is reported to be unused, then hit Right and choose 'Suppress for statement'
Related
When using the Yeoman Extension Generator, yo code to initialize an extension, it creates an exported function called deactivate(). I want to implement this for my extension to clean up when it is deactivated or disabled, but I don't know if it takes an extension context like the activate(context) export.
I can't find any uses for the function in example extensions or the relevant documentation.
If anyone knows how this function is used or what arguments are passed to it, when it is called, please let me know, and maybe we should update the documentation to include this export.
No, the deactivate() function has no parameters at all. And to be honest, I'm not sure if this function is called at all. At least I have no code there and I have seen quite a number of extensions that either don't implement it at all or just have an empty body.
In NetBeans I just added a comment with global, e.g.
/* global myLibrary */
in order to get it to recognize my functions.
However, this seems not to work in VS Code. For example, if I have a function named myFunction in the myLibrary module, when I click on "Go To Definition", it tells me that there was "No definition found for myFunction".
So how do I get VS Code to recognize my function?
I believe VSCode does not provide this feature by default. You will have to define the configuration yourself if your project has mixed content in it. I use VSCode for Angular 2 (using ng-cli) so it has all the setup generated for me (i can goto definition if it is valid).
have a look at these two links, hope this helps you:
https://code.visualstudio.com/docs/languages/javascript#_automatic-type-acquisition
https://code.visualstudio.com/docs/languages/jsconfig#_what-is-jsconfigjson
I know that I can type into Chrome or FF the following command:
Object.keys(window);
but this displays DHTMLX stuff and also function names in which I'm not interested in. And it doesn't display variables in functions that have not been executed yet. We have more than 20,000 lines of JavaScript codebase, so I would prefer static code analyis. I found JavsScript Lint. It is a great tool but I don't know how to use it for displaying global vars.
So I'm searching for memory leaks, forgotten var keywords, and so on...
To do [only] what you're asking, I think you're looking for this:
for each (obj in window) {
if (window.hasOwnProperty(obj)) {
console.log(obj);
}
}
I haven't linted that code, which is unlike me, but you get the idea. Try setting something first (var spam = "spam";) and you'll see it reported on your console, and not the cruft you asked about avoiding.
That said, JLRishe is right; JSLint executes JavaScript in your browser without "phoning home", so feel free to run it. There are also many offline tools for JSLinting your code. I use a plugin for Sublime Text, for instance.
If you'd like some simplest-case html/JavaScript code to "wrap" JSLint, I've got an example here. Just download the latest jslint.js file from Crockford's repository into the same directory, and poof, you're linting with a local copy of JSLint.js. Edit: Added code in a new answer here.
Though understand that you're linting locally with that wrapper or when you visit JSLint.com. Honestly, I can say with some confidence, Crockford would rather not see our code. ;^) You download JSLint.js (actually webjslint, a minified compilation of a few files) from JSLint.com, but you execute in the browser.
(Admittedly, you're technically right -- you never know when that site could be compromised, and to be completely on the up and up, you sh/c/ould vet jslint.js each time you grab a fresh copy. It is safer to run locally, but as of this writing, you appear safe to use JSLint.com. Just eyeball your favorite browser's Net tab while running some test, non-proprietary code, and see if any phoning happens. Or unplug your box's network cable!)
Rick's answer to use "use strict"; is another great suggestion.
A great way to catch undeclared variables is to add 'use strict' to your code.
The errors will appear in the console, or you could display them in a try ... catch block:
'use strict';
try {
var i= 15;
u= 25;
} catch(ee) {
alert(ee.message);
}
I found a very good solution to list all the global variables with the jsl command line tool:
Here is the documentation
I just have to put /*jsl:option explicit*/ into each file that I want to check. Then it is enough to run ./jsl -process <someFile> | grep 'undeclared identifier'
It is also possible to use referenceFile that contains some intentional global variables /*jsl:import <referenceFile>*/ so these variables will not be listed.
WebStorm IDE having some issues with azure-storage intelligent coding does not find methods for that library (but the code works fine when executed).
image of unresolved function or method
Thank you in advance!
Per my experience, some issues for code completion or highlight hint not always mean the code exists errors, especially for Dynamic Language like JavaScript.
I tried to reproduce your issues, and I found some interesting things.
After I writed the code var azureStorage = require('azure-storage'), the code completion for the symbol azureStorage could not display the function suggestions for azure-storage module. WebStorm regard the varable azureStorage as a normal object, see below.
However, the code completion for any undeclared symbol will display all functions of all modules depended on the project after I commented the code var azureStorage = require('azure-storage'), see below. It seems that WebStorm default bind all functions of all modules with undeclared symbols automatically.
Although we can use the code completion in this way, the require code must be required when the code works.
For more details about Auto-completing code, you can refer to https://www.jetbrains.com/webstorm/help/auto-completing-code.html.
I'm trying to move my workspace to c9 because the Ace editor's autocompletion really pleased me when I worked on NodeJS projects.
But now I would like to work on JS files client-sided. It is from this point autocompletion going wrong. Indeed, there is nothing such as "require" command in client-side JS inside of the JS files themselves (except using some plugins) to inform of the other source files used in.
So when I use, in one of my JS files, a function that is defined in an other file (even libraries, frameworks : jquery, etc), Ace notifies me that the function is not defined (cause it has no way to know that the function is defined in another file, I guess).
Here we go : is there some comment line I could put in my code, or some configuration of c9 I could set, to correct that behavior ?
To remove the errors and warning, you can just add the following line near the top of your javascript file:
/* globals jquery lodash someOtherLibrary */
However, Cloud9 doesn't do autocomplete for client side libraries yet.
Abuse C9's require support
When you use var yourLibrary = require("./somefile.js");, auto-completion works perfectly.
But, as you stated, require() doesn't exist, nor do you want to have yourLibrary set to undefined. (Or to just throw an error)
As it turns out, C9 isn't that smart:
//Your library was defined in some other file
var yourLibrary; //"This does nothing other than making C9 happy
function require() {return 1;} //Define the require function
if(false) {
yourLibrary = require("yourLibraryFile.js");
}
Now, you can use autocomplete (it even shows the documentation comments)!
Note: It doesn't always work.