'debugger' command and JSLint - javascript

Google Chrome supports debugger command as a tool to setup a breakpoint in code. How can I hide warnings for the following code in JSLint:
/*globals $, console, */
/*jslint browser:true, white: true */
function test() {
"use strict";
debugger; // JSLint reports the "Unexpected 'debugger'" error
}

JSLint has an explicit option to tolerate debugger statements, called debug:
debug: true if debugger statements should be allowed.
You can specify this option via your jslint directive:
/*jslint browser:true, white: true, debug: true */

This error is raised to highlight a lack of convention and possible oversight by the developer.
You can disable it via:
function test() {
/* ignore jslint start */
debugger;
/* ignore jslint end */
}

Appears debug is gone, and this is now tolerated with the devel option, with the side effect that // TODO: etc are also tolerated with the single devel option.
devel: true if browser globals that are useful in development should be
predefined, and if debugger statements and TODO comments should be
allowed. It adds the same globals as this directive:
/*global
alert, confirm, console, prompt
*/
Be sure to turn this option off before going into production.
This lints:
/*jslint white, devel */
function test() {
"use strict";
debugger; // JSLint reports the "Unexpected 'debugger'" error
}

in react I used to do it during development like this:
debugger // eslint-disable-line

Disable no-debugger to make it work! (only applicable in Typescript tslint)
"rules": {
"no-debugger": false
}

Related

What happens if I don't put "use strict" in a function? [duplicate]

I include the statement:
"use strict";
at the beginning of most of my Javascript files.
JSLint has never before warned about this. But now it is, saying:
Use the function form of "use strict".
Does anyone know what the "function form" would be?
Include 'use strict'; as the first statement in a wrapping function, so it only affects that function. This prevents problems when concatenating scripts that aren't strict.
See Douglas Crockford's latest blog post Strict Mode Is Coming To Town.
Example from that post:
(function () {
'use strict';
// this function is strict...
}());
(function () {
// but this function is sloppy...
}());
Update:
In case you don't want to wrap in immediate function (e.g. it is a node module), then you can disable the warning.
For JSLint (per Zhami):
/*jslint node: true */
For JSHint:
/*jshint strict:false */
or (per Laith Shadeed)
/* jshint -W097 */
To disable any arbitrary warning from JSHint, check the map in JSHint source code (details in docs).
Update 2: JSHint supports node:boolean option. See .jshintrc at github.
/* jshint node: true */
If you're writing modules for NodeJS, they are already encapsulated. Tell JSLint that you've got node by including at the top of your file:
/*jslint node: true */
I'd suggest to use jshint instead.
It allows to suppress this warning via /*jshint globalstrict: true*/.
If you are writing a library, I would only suggest using global strict if your code is encapsulated into modules as is the case with nodejs.
Otherwise you'd force everyone who is using your library into strict mode.
I started creating a Node.js/browserify application following the Cross Platform JavaScript blog post. And I ran into this issue, because my brand new Gruntfile didn't pass jshint.
Luckily I found an answer in the Leanpub book on Grunt:
If we try it now, we will scan our Gruntfile… and get some errors:
$ grunt jshint
Running "jshint:all" (jshint) task
Linting Gruntfile.js...ERROR
[L1:C1] W097: Use the function form of "use strict".
'use strict';
Linting Gruntfile.js...ERROR
[L3:C1] W117: 'module' is not defined.
module.exports = function (grunt) {
Warning: Task "jshint:all" failed. Use --force to continue.
Both errors are because the Gruntfile is a Node program, and by default JSHint does not recognise or allow the use of module and the string version of use strict. We can set a JSHint rule that will accept our Node programs. Let’s edit our jshint task configuration and add an options key:
jshint: {
options: {
node: true
},
}
Adding node: true to the jshint options, to put jshint into "Node mode", removed both errors for me.
Add a file .jslintrc (or .jshintrc in the case of jshint) at the root of your project with the following content:
{
"node": true
}
There's nothing innately wrong with the string form.
Rather than avoid the "global" strict form for worry of concatenating non-strict javascript, it's probably better to just fix the damn non-strict javascript to be strict.
process.on('warning', function(e) {
'use strict';
console.warn(e.stack);
});
process.on('uncaughtException', function(e) {
'use strict';
console.warn(e.stack);
});
add this lines to at the starting point of your file
I think everyone missed the "suddenly" part of this question. Most likely, your .jshintrc has a syntax error, so it's not including the 'browser' line. Run it through a json validator to see where the error is.
This is how simple it is: If you want to be strict with all your code, add "use strict"; at the start of your JavaScript.
But if you only want to be strict with some of your code, use the function form. Anyhow, I would recomend you to use it at the beginning of your JavaScript because this will help you be a better coder.

WebStorm - Suppress unused param warning JavaScript

Does anyone know how to suppress unused parameter warning in WebStorm? I tried jslint, but that does not work
/*jslint node: true, unparam: true*/
/*global __dirname: false */
"use strict";
var
util = require('../util'),
logger = util.getLogger(module.filename),
;
var UserHelper = module.exports = function () {
};
/**
* Helper object for user facade
*/
UserHelper.prototype = {
doSomething : function(unusedParam) {
//do something implementation
}
};
You can modify WebStorm's builtin code inspections under File->Settings->Editor->Inspections. Then in Javascript->General area look for "Unused JavaScript / ActionScript global symbol".
That is likely the setting which is causing the "unused property" warning, though it might be one of the others. I am using 2016.1.3 and noticed a lot of my anonymous functions defined like this had the same buggy warning. Like most others I'd still recommend using jsHint itself above and beyond even just WebStorm's inspection.
For WebStorm with JSLint/JSHint disabled, you need to add this line before the line triggering the error:
//noinspection JSUnusedLocalSymbols
jshint instead of jslint works for me on WebStorm 8 and WebStorm 9 beta:
/*jshint node:true, unused:false */
Also, make sure you have JSHint (or JSLint) enabled in your preferences. JSHint has "Warn about unused variables". NOTE: This JSHint check does not distinguish between variables and function parameters unless JSHint is configured with unused:vars.
You can also uncheck the "Unused parameters" option under the JSLint configuration screen.
Relevant JSHint doc
unparam should work -- and if I clean the other lint issues your sample, it does.
/*jslint node: true, unparam: true, sloppy:true, white:true, nomen:true */
/*global __dirname: false */
"use strict";
var util = require('../util'),
logger = util.getLogger(module.filename),
UserHelper;
UserHelper = module.exports = function () {
console.log('this is a function');
};
/**
* Helper object for user facade
*/
UserHelper.prototype = {
doSomething : function(unusedParam) {
//do something implementation
console.log('alert');
}
};
That passes cleanly on jslint.com. Sounds like a WebStorm configuration issue. I have PhpStorm 6.x, but I'm assuming you're on the latest version of WebStorm, so I can't guarantee I could debug it.
But it certainly seems like a WebStorm issue rather than a JSLint one.

JSHint with RequireJS and AngularJS: functions / objects "is not defined" [duplicate]

I uses RequireJS AMD in my project. When i run jshint on my project, it throws error like
In AMD Scripts
'define' is not defined.
In Mocha test cases
'describe' is not defined.
'it' is not defined.
How to remove this warning in jshint?
Just to expand a bit, here's a .jshintrc setup for Mocha:
{
....
"globals" : {
/* MOCHA */
"describe" : false,
"it" : false,
"before" : false,
"beforeEach" : false,
"after" : false,
"afterEach" : false
}
}
From the JSHint Docs - the false (the default) means the variable is read-only.
If you are defining globals only for a specific file, you can do this:
/*global describe, it, before, beforeEach, after, afterEach */
jshint: {
options: {
mocha: true,
}
}
is what you want
To avoid the not defined warning in jshint for the javascript add comments like:
/*global describe:true*/
Options
Add this in your .jshintrc
"predef" : ["define"] // Custom globals for requirejs
late to the party, but use this option in your jshintrc:
"dojo": true
and thou shall rest peacefully without red warnings...
If you are working on node js. Add these two lines in the beginning of your file
/*jslint node: true */
"use strict";
Read the docs and search for /*global
If you're trying to run JSHint in WebStorm with Mocha, as I am, go into:
WebStorm > Preferences > Languages & Frameworks > JavaScript > Code Quality Tools > JSHint
Scroll down to "Environments" and make sure you have selected the checkbox to enable "Mocha" which will set up the definitions for JSHint for Mocha for you.

Use the function form of "use strict" [duplicate]

I include the statement:
"use strict";
at the beginning of most of my Javascript files.
JSLint has never before warned about this. But now it is, saying:
Use the function form of "use strict".
Does anyone know what the "function form" would be?
Include 'use strict'; as the first statement in a wrapping function, so it only affects that function. This prevents problems when concatenating scripts that aren't strict.
See Douglas Crockford's latest blog post Strict Mode Is Coming To Town.
Example from that post:
(function () {
'use strict';
// this function is strict...
}());
(function () {
// but this function is sloppy...
}());
Update:
In case you don't want to wrap in immediate function (e.g. it is a node module), then you can disable the warning.
For JSLint (per Zhami):
/*jslint node: true */
For JSHint:
/*jshint strict:false */
or (per Laith Shadeed)
/* jshint -W097 */
To disable any arbitrary warning from JSHint, check the map in JSHint source code (details in docs).
Update 2: JSHint supports node:boolean option. See .jshintrc at github.
/* jshint node: true */
If you're writing modules for NodeJS, they are already encapsulated. Tell JSLint that you've got node by including at the top of your file:
/*jslint node: true */
I'd suggest to use jshint instead.
It allows to suppress this warning via /*jshint globalstrict: true*/.
If you are writing a library, I would only suggest using global strict if your code is encapsulated into modules as is the case with nodejs.
Otherwise you'd force everyone who is using your library into strict mode.
I started creating a Node.js/browserify application following the Cross Platform JavaScript blog post. And I ran into this issue, because my brand new Gruntfile didn't pass jshint.
Luckily I found an answer in the Leanpub book on Grunt:
If we try it now, we will scan our Gruntfile… and get some errors:
$ grunt jshint
Running "jshint:all" (jshint) task
Linting Gruntfile.js...ERROR
[L1:C1] W097: Use the function form of "use strict".
'use strict';
Linting Gruntfile.js...ERROR
[L3:C1] W117: 'module' is not defined.
module.exports = function (grunt) {
Warning: Task "jshint:all" failed. Use --force to continue.
Both errors are because the Gruntfile is a Node program, and by default JSHint does not recognise or allow the use of module and the string version of use strict. We can set a JSHint rule that will accept our Node programs. Let’s edit our jshint task configuration and add an options key:
jshint: {
options: {
node: true
},
}
Adding node: true to the jshint options, to put jshint into "Node mode", removed both errors for me.
Add a file .jslintrc (or .jshintrc in the case of jshint) at the root of your project with the following content:
{
"node": true
}
There's nothing innately wrong with the string form.
Rather than avoid the "global" strict form for worry of concatenating non-strict javascript, it's probably better to just fix the damn non-strict javascript to be strict.
process.on('warning', function(e) {
'use strict';
console.warn(e.stack);
});
process.on('uncaughtException', function(e) {
'use strict';
console.warn(e.stack);
});
add this lines to at the starting point of your file
I think everyone missed the "suddenly" part of this question. Most likely, your .jshintrc has a syntax error, so it's not including the 'browser' line. Run it through a json validator to see where the error is.
This is how simple it is: If you want to be strict with all your code, add "use strict"; at the start of your JavaScript.
But if you only want to be strict with some of your code, use the function form. Anyhow, I would recomend you to use it at the beginning of your JavaScript because this will help you be a better coder.

How can I tell jslint that a function is external?

I have the following code:
/*jslint browser: true*/
/*global $, jQuery*/
if (json.RowKey !== json.NewRowKey) {
$("#row_" + row).attr('data-rk', json.RowKey);
updateGridMeta(entity, json.PartitionKey, json.NewRowKey, row, obj.table);
updateGridTitles();
}
lint is reporting that updateGridTitles is used before it is defined. Is there a way to add something to the top of my script to tell it to not report this?
The same way as the other variables you are telling JSLint are global.
Add it to this list:
/*global $, jQuery*/
Such:
/*global $, jQuery, updateGridMeta */
if updateGridTitles is defined later you could simply add
var updateGridTitles;
to the top
You can indeed tell jslint that a function is global. Have a look at their documentation here:
http://www.jslint.com/lint.html#global
and another question asked here: JSLint: was used before it was defined

Categories