JS Lint: Unexpected dangling '_' in '_fnGetTrNodes' - javascript

My code looks like this:
$.extend($.fn.dataTableExt.afnSortData, {
'dom-text': function (oSettings, iColumn) {
var aData = [];
$('td:eq(' + iColumn + ') input', oSettings.oApi._fnGetTrNodes(oSettings)).each(function () {
aData.push(this.value);
});
return aData;
},
'dom-data-rk': function (oSettings, iColumn) {
var aData = [];
$('td:eq(' + iColumn + ')', oSettings.oApi._fnGetTrNodes(oSettings)).each(function () {
aData.push($(this).attr('data-rk'));
});
return aData;
}
});
I used JSLint and it came up with an error:
Warning 21 JS Lint: Unexpected dangling '_' in '_fnGetTrNodes'.
Can someone explain what this means? I don't understand the error message at all :-(

JSLint simply doesn't like identifiers to begin with an underscore character. Change the identifier and the warning will go away, or add the following directive to the top of the file:
/*jslint nomen: true */
The reason it doesn't like them is that people often use it to indicate a "private" variable, but doesn't actually change the behaviour of the variable.

Do not use _ (underbar) as the first character of a name. It is
sometimes used to indicate privacy, but it does not actually provide
privacy. If privacy is important, use the forms that provide private
members. Avoid conventions that demonstrate a lack of competence.
more about code conventions used by JSLint here

You can simply set "tolerate dangling _ in identifiers" to true to ignore this error.

Well, JSlint doesn't like a variable name that begins with an underscore (_).
It is better to use JShint.com instead of JSlint. It's a fork of JSlint and provide you more options of configuration and doesn't show stupid errors like this.
https://stackoverflow.com/a/10763615/1149495

Related

eslint object-shorthand error with variable passed in

I have the following function that is setting up a select2 plugin, which needs selects to stay open if they are multiple but closed if they are not:
function setUpSelects($selects, closeOnSelect) {
$selects.each((i, item) => {
const $item = $(item);
$item.select2({
closeOnSelect: closeOnSelect, // <-- error on this line
minimumResultsForSearch: Infinity,
placeholder: $item.data('placeholder') || $item.attr('placeholder'),
});
});
}
setUpSelects($('select:not([multiple])'), false);
setUpSelects($('select[multiple]'), true);
However, when I try to run this code, the eslint checker is giving me an error (on the line shown above) of:
error Expected property shorthand object-shorthand
I have done a search and read the docs but it doesn't show how you are meant to use a variable and the unaccepted answer on this question seems to think it may be a bug in eslint (although I have found no evidence to support that)
Is there a way to make this work or should I just disable the rule for that line?
An excerpt from eslint regarding the issue:
Require Object Literal Shorthand Syntax (object-shorthand) - Rule Details
This rule enforces the use of the shorthand syntax. This applies to
all methods (including generators) defined in object literals and any
properties defined where the key name matches name of the assigned
variable.
Change
closeOnSelect: closeOnSelect
to just
closeOnSelect
This rule checks that object literal shorthand syntax is used, e.g {a, b} instead of {a: a, b: b}. The rule is configurable, see options for more details.
Despite this shorthand syntax is convenient, in some cases you may not want to force it usage. You can disable the check in your config:
// .eslintrc.json
{
"rules": {
// Disables the rule. You can just remove it,
// if it is not enabled by a parent config.
"object-shorthand": 0
}
}
In case of TSLint there is a different option:
// tslint.json
{
"rules": {
// Disables the rule. You can just remove it,
// if it is not enabled by a parent config.
"object-literal-shorthand": false
}
}
Wants to define Object with keys and can't use any. Try this.
interface Map {
[key: string]: string | undefined
}
const HUMAN_MAP: Map = {
draft: "Draft",
}
export const human = (str: string) => HUMAN_MAP[str] || str

Hubot - no arguments entered/parameters entered

I've just started using Hubot recently.
I'd like to know if a command is used, but no arguments have been entered.
robot.respond(/dothis (.*)/i, function(res) { ... };
This doesn't return anything if no arguments have been entered, even though it accepts 0 or more arguments.
robot.respond(/dothis/i, function(res) { ... };
This doesn't accept any arguments, but responds when called.
Not quite sure how to go about this, is it possible?
I think you'd need a regular expression engine that handled positive look-behinds to do this in a straightforward way, and I don't think V8 (which is what Node is using under the hood) has that as of this writing.
There are lots of other workarounds, though. Here's one using \b which checks for a word-boundary:
robot.respond(/dothis\b(.*)/i, function(res) {
if (res.match[1]) {
res.send('We got the paramater: ' + res.match[1].trim());
} else {
res.send('Command called with no parameter.');
}
});
robot.respond(/dothis(.*)/i, function(res) { ... };
This works, that space makes all the difference. It will now take an empty string as an argument.

'Syntax error, unexpected token =' in js function

I am getting this error in chrome while mozilla is handling good. I am getting this error to a function which is like this
function abc( xyz = false){ "My logic" }
Error is pointing to '=' operator. please help with this.
That is a standard of ECMASCRIPT version 6 and it's called Default parameters. So it might be not available in your chrome version while FF has.
You can achieve the same by two ways:
function abc( xyz ){ "My logic" }
var pVal = mightbe || false;
abc(pVal); //<---- now pass it here;
or:
function abc( xyz ){
// before processing anything you can do this
var o = xyz || false; // if there is any value in the xyz then that will
// be assigned otherwise false will be the default value.
"My logic"
}
This is ES6 syntax, most browsers only support very few ES6 features, you can check from here: https://kangax.github.io/compat-table/es6/ (In your example you used default function parameters)
If you want to write ES6 syntax (which is quite appealing in many ways), you can use some code transpiling tool like babel: https://babeljs.io/

Equivalent of private variable (leading underscore) in es6?

I have this code here:
getData(value, index) {
const {responseMetadata, responseData} = this.getResponseDatum();
return responseData.get(index).get('code').toUpperCase();
}
eslint reports an error:
19:12 "responseMetadata" is defined but never used
In python I can silent this kind of error by renaming the variable to _responseMetadata. Is there a Equivalent in es6?
If you don't need the variable, just don't create it:
const {responseData} = this.getResponseDatum();
A destructuring assignment doesn't need to match all properties of the returned object.
In your case, since you need only one property and don't use it multiple times, there's actually not much reason to use destructuring or a variable at all:
getData(value, index) {
return this.getResponseDatum().responseData.get(index).get('code').toUpperCase();
}
You can turn off a rule for a section of code. See http://eslint.org/docs/user-guide/configuring.html#configuring-rules
/*eslint-disable */
//suppress all warnings between comments
alert('foo');
/*eslint-enable */

assistance require resolving jslint errors

I am currently running JSLint against the javascript in my web application and getting some errors that I require assistance with resolving.
a. First error I am getting is: JS Lint: Unused Variable 'n'.
$.each(collection, function (n, item) {
var temp = item.Id;
// do further processing
});
b. I have all my javascript declared in a self executing function like such:
(function ($, undefined) {
// further javascript code
}
(jQuery));
The above pattern can protect the $ from conflicting with other JavaScript libraries and also protect undefined from being redefined. However I get these errors from it:
JS Lint: Expected an identifier and instead saw 'undefined' (a reserved word).
JS Lint: Unused Variable 'undefined'.
c. JS Lint: Unescaped '-'.
if (value.match(/^[A-Z0-9._%+-]+#(?:[A-Z0-9-]+\.)+[A-Z]{2,4}$/i)) {
return true;
}
d. JS Lint: Type confusion: 'printer-': string and '(': number.
var frameName = "printer-" + new Date().getTime();
I get numerous errors of Type confusion, sometimes with numbers, integers and other data types. Any idea as to how I can prevent getting these?
e. JS Lint: Insecure '^'.
var value = value.replace(/[^\d\.,\-]/gi, '');
f. JS Lint: Don't make functions within a loop.
for (i = 0, l = txts.length; i < l; i += 1) {
if (/^[0-9]+$/.test(txts[i].getAttribute("maxlength"))) {
var func = function () {
//do some processing
};
}
}
A.) See: http://api.jquery.com/jQuery.each/
you can use:
$.each(collection, function() {
doSomething(this); // this refers to the current iteration
});
B.) If you aren't actually using "undefined" you aren't protecting it from anything
C.) I'm not going to bother with regex lol EDIT: Perhaps it wants [A-Z0-9\-]
D.) You are concatenating string with number. Try 'string' + Date.getTime().toString() instead
See also JSLint Type Confusion: function and object with jQuery .css() for type confusion stuff, there are some oddities that I don't agree with
E.) Again I'm not going to try for the regex EDIT: Here's an identical question though: JSLint "insecure ^" in regular expression
F.) If you can create your function once outside of the loop and then use it inside the loop (as long as you do it well) it is a significant performance increase.
I see others have answered, so I'll at least put an attempt in for c)
c. JS Lint: Unescaped '-'.
if (value.match(/^[A-Z0-9._%+-]+#(?:[A-Z0-9-]+\.)+[A-Z]{2,4}$/i)) {
return true;
}
C. Add a backslash before the - in 9- and +-
Sorry, can't help you with E), that regex looks ok to me.

Categories