How can I tell jslint that a function is external? - javascript

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

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.

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.

JSHint Backbone not defined in CodeKit

A small test app is set up like this:
init.js:
//#codekit-prepend "vendor/jquery-1.7.2.js"
//#codekit-prepend "vendor/underscore.js"
//#codekit-prepend "vendor/backbone.js"
// Setup namespace for the app
window.app = window.app || {};
//#codekit-append "models/Ride.js"
Ride.js:
(function() {
window.app.Ride = Backbone.Model.extend({
initialize: function() {
console.log("Ride initialized");
}
});
})();
CodeKit's JSHint check reports that both Backbone and console are not defined. What am I missing here?
JSHint doesn't run your code so it doesn't know about any modules you included in other files. You have to specifically tell it about all global variables you plan to use in Ride.js. In your case it will be: /*global Backbone */. console is disallowed by default because it is not a very good idea to ship your software with filled console.log calls. To remove this warning you can use /*jshint devel:true */.
So in the end your file should look like this to pass JSHint check:
/*jshint devel:true */
/*global Backbone */
(function() {
window.app.Ride = Backbone.Model.extend({
initialize: function() {
console.log("Ride initialized");
}
});
})();
More info here: http://www.jshint.com/options/
Bryan here. CodeKit does check your files in a full, global context. (That is, it combines them first, so variables declared in an earlier file will be valid in a later one. This assumes you use CodeKit to combine the files, either with #codekit-prepend/append statements or drag/drop import links set up in CodeKit itself). If you're combining your JS files some other way (such as a build script) then CodeKit is unaware that the files go together and therefore it checks each one separately.
You can use the comment flags in the answer above, or you can configure JSHint's options directly in CodeKit. See the preferences window (or project settings area, if your project uses project-level settings). You can also enter custom globals there as well, which will remove those warnings.
Cheers!

Can uglifyjs ignore undefined symbols

The first file that I am passing to uglifyjs declares some namespaces like
window.MyNamespace = {}
when uglifyjs sees this line it complains that window is not defined.
Is there a way to have uglifyjs ignore undefined symbols? I have tried using the --no-dead-code option
You can wrap your global code in a function:
(function(window) {
window.whatever = something;
// ...
})(this);
You can also do this:
(function(window) {
"use strict";
// ...
})(this);
which is probably a good idea anyway. You'll get warnings/errors from stray undefined variables even without uglify.

Categories