Syntastic with jslint for vim not executing the checker - javascript

I have set up Syntastic https://github.com/scrooloose/syntastic with vim and I would like to use it for node.js javascript linting.
I have installed jslint
$ jslint routes/index.js
routes/index.js
#1 Expected exactly one space between 'function' and '('.
module.exports = function(app) { // Line 5, Pos 26
....
And I have put this in my ~/.vimrc
let g:syntastic_jslint_checkers=['jslint']
let g:syntastic_check_on_open=1
let g:syntastic_enable_signs=1
But I get no output on :SyntasticCheck
Yet
Syntastic: active mode enabled
Syntastic info for filetype: javascript
Available checker(s): jslint
Currently enabled checker(s): jslint

I recommend JSHint as an alternative for JavaScript linting in Vim. Here is a great answer that explains how to install it.
If you want to use JSHint for creating web sites as well, I'd additionally use RequireJS. This way your JavaScript and your HTML code stay separated (JSHint can't deal with JavaScript inside HTML files).

Related

JsHint with Ext Js in VisualStudio

I am working with Ext Js and Visual Studio. With _references.js I can get the intellisense working. I can add a definition for "Ext" in here for JsHint:
But it still thows 500 errors about ext-all-debug.js
I am wondering if using JsHint here adds any value to my project. Visual Studio seems to take care of true JavaScript syntax errors.
Some examples of the warnings:
JsHint (W116): Expected '===' and instead saw '=='.
or:
JsHint (W083): Don't make functions within a loop.
Should I disable JSHint completely or try to configure it better?
When a major framework does not respect the rules then why should I?
Update:
In web essentials you can create a file called something.weignore. It follows the same syntax than .jshintignore.
Source: http://vswebessentials.com/features/general
You definitely shouldn't be running the linter over ext-all-debug.js. Ignore that using a file called .jshintignore in the root of your project and put the path of ext-all-debug.js in there. An example of the content of .jshintignore could be:
content/js/extjs5.1/ext-debug-all.js
content/js/jquery.js
This would tell jshint to ignore jquery.js and extjs. You can also exclude whole directories. Take a look at the jshint ignore for the jQuery project:
https://github.com/jquery/jquery/blob/master/.jshintignore
external
src/intro.js
src/outro.js
test/data/jquery-1.9.1.js
test/data/badcall.js
test/data/badjson.js
test/data/json_obj.js
test/data/readywaitasset.js
test/data/readywaitloader.js
test/data/support/csp.js
test/data/support/getComputedSupport.js
Maybe Ext JS is working to address those errors internally, maybe they just don't care. But don't let another person's code practises negatively affect your own. If you feel you'll be writing better code by using jshint then use it! There are good reasons behind the warnings you are seeing.

Webstorm 7 cannot recognize node API methods

I just installed WebStorm. I'm working on a small Node.js app.
I've attached the Node.js source code, and when I click on the Node.js settings, I can see that it can recognize my various node modules, etc.
I'm having two issues:
Unresolved variable or type: WebStorm doesn't seem to recognize simple API methods (exports, require).
No code insight for…: If I call require('winston'), it tells me that it has no code insight. (Is there a way I can add the source code?)
For 2018 and later versions of WebStorm:
In Settings -> Languages & Frameworks -> Node.js and NPM, check Coding assistance for Node.js:
In older Webstorm versions, this was called Enable Node.js Core library.
If you still see unrecognized Node symbols even with that option enabled, unckeck it, restart WebStorm, then right click on the warning and choose Enable Node.js coding assistance or just check the option again. Watch for WebStorm to show it's Indexing files. (Just had this happen today - looks like a WebStorm bug, and what I just wrote fixed the situation.)
For WebStorm 7 thru 10 (on OSX)…
WebStorm → Preferences → Languages & Frameworks → Javascript → Libraries
Select "Node.js Globals" and "Node.js vXXX Core Modules".
I use WebStorm 2020 and I had everything enabled but WebStill though showed that module.exports is unknown function. Then I turned off NodeJS.core library and NodeJS code assistance, applied and then turned them on again. And suddenly it started to work.
As I've answered on the WebStorm says console is an unresolved variable question, to solve these problems on the new Webstorm versions, you need to enable the Coding assistance for Node.js.
To do this, go on the Settings > Languages & Frameworks > Node.js and NPM and click on the Coding assistance for Node.js option, and then click OK to save:
This will all Node.js unresolved variables and functions.
Update
On the new Webstorm versions, just going above error and clicking in More Actions... (or ALT+ENTER) and selecting
Enable Node.js coding assistance will solve this.
Working with WebStorm and Node.js these two code-segments regularly gave me "false positive" warnings:
FALSE POSITIVE WARNING CASE 1:
module.exports = ...
That gave me the warning "Element is not exported". I was able to get rid of the warning caused by that by putting this above the "module.exports = ..."
/** #namespace module.exports **/
FALSE POSITIVE WARNING CASE 2:
let something = global.something ;
That gave me the warning "Unresolved variable or type global". I was able to eliminate the warning caused by that by putting this above it:
/** #namespace global **/
I now put the following on top of my .js- or .mjs-files to be executed by Node.js, and get rid of warnings referring to these variables or properties:
/** #namespace global **/
/** #namespace console **/
/** #namespace process **/
/** #namespace Buffer **/
/** #namespace process.stdin **/
/** #namespace module.exports **/
I am using WebStorm 2022.2.3
Updating to Webstorm 8 or higher will fix your require methods problem. As posted earlier by checking if Settings > JavaScript > Libraries > Node.js are all checked will fix your problem

Custom settings with jshint-rhino.js

I've been using jshint with node but just recently had to switch over to using it with Rhino.
I used to be able to do:
jshint --config=jsHintConfig.json fileToLint.js
Now, I've tried replacing that call with:
rhino jshint-rhino.js --config=jsHintConfig.json fileToLint.js
But it doesn't seem to work. I only get the following printed to the console:
Usage: jshint.js file.js
Does jshint-rhino not accept a json configuration file?
Update:
http://anton.kovalyov.net/2011/03/01/jshint-edition-update/
- Says: "Added support for providing options to JSHint as command line arguments when used with our Rhino wrapper" but doesn't say how.
https://github.com/jshint/jshint/issues/27
- Something about specifying options on the cli, but also doesn't say how.
This worked:
rhino jshint-rhino.js file1.js file2.js opt1=true,opt2=true,opt3=false global1,global2,global3
No need to put a comma between file names and it is important to not have spaces before or after the commas for the options and globals.

Disable "use the function form of use strict" but keep the "Missing 'use strict' statement" warning

I am using jslint to validate my code.
I have "use strict" on all my pages.
How can I disable the message "use the function form of 'use strict'" but keep the "Missing 'use strict' statement" warning, so I won't forget to put it on new files?
Thanks
According to Crockford's post, you'll want to wrap everything in a function...
(function () {
"use strict";
// the rest of your file goes here...
}());
You could also use jshint instead, which has a "globalstrict" option that can do exactly what you're asking for without having to wrap everything in a function
Cannot be done without changing the javascript file which drives jslint.
To me function form is a cranky working practice, therefore cannot force on others.
Not everybody needs to combine and minify, but even if I did I'd combine code that applied the same rules, thus a file statement would be sufficient.
Although jshint has exactly the feature you require. The latest jslint is now more advanced than jshint, spotting more weaknesses and copes with more complicated code. I like jshint but it isn't keeping up with jslint.
The solution I found for this was to create a single line file with "use strict"; and nothing else
Make that the first file in your concatenation package, add it to jslint's exclude list, switch the sloppy=true pragma
There may be some side effects around not picking up sloppy code, but my understanding of the docs is that it just checks for the "use strict"; line
Here is a hack to suppress "Use the function form of 'use strict'."
$ uname -a
Darwin 13.0.0 Darwin Kernel Version 13.0.0
Figure out where your jslint distribution is.
$ which jslint
/usr/local/bin/jslint
$ ls -l /usr/local/bin/jslint
lrwxr-xr-x 1 root admin 40 11 Feb 2013 /usr/local/bin/jslint -> ../lib/node_modules/jslint/bin/jslint.js
$ cd /usr/local/lib/node_modules/jslint/
$ ls
LICENSE README.md lib package.json
Makefile bin node_modules
Comment out the warning.
$ sudo vim lib/jslint.js
search for 'function_strict'
comment out the line 'warn('function_strict');'
note: the exact line might vary on some versions but just comment it out.
If it does not work you probably have multiple versions of jslint installed and have not edited the right one.
sudo find / -name jslint.js

Running JSLint from command prompt

I would like to run jsLint from the command prompt.
At a later stage, as a task in an ANT build.
I downloaded rhino 1.7 R3 and the latest jslint.js and wrote this custom test.js which contents is:
for (var i = 0; i < 10; i++) { }
Notice that this single line of code should already cause jslint to warn:
Move 'var' declarations to the top of the function.
I used this command:
java -jar .\rhino1_7R3\js.jar .\douglascrockford-JSLint-e31fa4c\jslint.js .\test.js
Which ran for a couple of seconds and then finished without any output.
My question is -> Am I doing it right? What kind of output should I expect in case of an error?
You might find jslint4java useful as it has a command line interface, wrapping JSLint.
I don't know about Rhino etc, but when using JSLint in pure JS, it creates a JSLINT object, and you need to iterate through JSLINT.errors (which has methods JSLINT.errors[i].line, JSLINT.errors[i].reason, etc). Also, you have to pass your code as a string into the JSLINT function, not just run them both together.

Categories