Designate additional alias names for eslint consistent-this rule? - javascript

I recently started using the eslint module to help clean-up some JavaScript files. The following error is being reported when I lint one of my files:
127:17 error Unexpected alias 'me' for 'this' consistent-this
After checking the documentation, I understand the error is being reported because my code is assigning the value of this to a variable named me instead of that.
What is the proper way to configure my project's .eslintrc.json to make it so the following line of code is not reported as an error: var me = this;?

The rule should be like this in your .eslintrc
{
"rules" : {
"consistent-this": ["error", "me"]
}
}

Related

What lint configuration to use to eliminate the warning : Invalid JSDoc tag name

I am using eslint to set up my node js project but I keep getting the following warnings:
Now in my .eslint.json I am unable to find a rule/technique to help ignore these new tags and types. I have looked in the following documentation:
https://eslint.org/docs/rules/
I assume that you are also using https://github.com/gajus/eslint-plugin-jsdoc
If so then you have to add this rule to your eslint config file:
"jsdoc/check-tag-names": ["error", { "definedTags": ["consumes", "produces", "route"] }],
and whatever custom tags you also have in your docs...

Disable eslint for specific namespace

In my code I am using opencv.js and it has some methods which do not follow my eslint rules, for example:
/* globals cv */
...
cv.GaussianBlur(...);
...
...
The above line will give the "new-cap" eslint error (A function with a name starting with an uppercase letter should only be used as a constructor).
Can I configure eslint to ignore anything related to cv ?
Thx.

How do you run eslint for only a specific rule or set of rules - command line only

I know you can define rules in an .eslintrc file, but what if I just want to run eslint and check for one specific rule?
E.g. $ eslint helpme.js --rule some-important-rule
I don't know if this is the best way, but I was able to get this working:
$ eslint helpme.js --no-eslintrc --env "es6" --env "node" --parser-options "{ecmaVersion: 2018}" --rule "{some-important-rule: error}"
Note: With this method (ignoring .eslintrc completeley) you still have to add some stuff from .eslintrc like your environment and parser options.
If you want to use your .eslintrc file to keep your configuration (parser, plugin settings, etc), you can use eslint-nibble with the --rule=some-important-rule flag. This will respect your normal configuration, but only show you errors from that rule. There are some other flags as well like --no-interactive if you want to run this in something like a CI environment.
Disclaimer: I'm the creator of eslint-nibble.
Expanding on #matrik answer, this doesn't require me to define all eslint config again and also shows the file name.
eslint helpme.js | egrep "react/jsx-no-literals" -B 1
Try ESLint custom formatter.
It can be used to filter part of rules, files you want to pay attention to.
And you don't need to :
Edit your ESLint config file.
Use complicate command.
DEMO for filter files contain error which rules id is prop-types:
// ./eslint-file-path-formatter.js
const fs = require('fs');
function containRules(result, targetRuleId) {
if (!result || !targetRuleId) {
return false;
}
return result.messages.some((cur) => {
// console.log(`cur?.ruleId = ${cur?.ruleId}`);
if (cur?.ruleId?.includes(targetRuleId)) {
return true;
}
});
}
module.exports = function (results, context) {
const summary = [];
results.forEach((cur) => {
if (containRules(cur, 'prop-types')) {
summary.push(`'${cur.filePath}',`);
}
});
// fs.writeFileSync('eslint-error-files.txt', summary.join('\n'));
// return 'Done Write';
return summary.join('\n');
};
Usage:
eslint . -f ./eslint-file-path-formatter.js
Then this formatter will print all files name to console.
You can also write result to local files, do whatever you want.
Simple way to see single rule output while still using .eslintrc is to use grep:
$ eslint helpme.js | egrep "(^/|some\-important\-rule$)"

How to change an IIFE with an argument to ES6. [duplicate]

I am using Grunt as my Build Tool and ESLint as my linting tool for an app I am working on. I am also using the Underscore Node package, and have made use of it in my app. Unfortunately, when I run ESLint on my code, it thinks that _ is an undefined variable in the following line:
return _.pluck(objects, nameColumn);
This is the error it is giving me:
78:21 error "_" is not defined no-undef
I would prefer not to disable the no-undef rule for ESLint, and I have tried installing the Underscore plugin, but I am still receiving this error. If anyone else has any ideas for what to try with this, I would be very appreciative!
If there is any further information I can give that would help anyone with helping me get this figured out, just let me know!
The official documentation should give you an idea on how to fix this.
Any reference to an undeclared variable causes a warning, unless the variable is explicitly mentioned in a /*global ...*/ comment, or specified in the globals key in the configuration file.
The easiest fix would be to add
/* global _ */
at the top of your file.
Or better, explicitly specify that the variable is read-only, to disallow overwriting the variable:
/* global _:readonly */
But since you'll have to do that for each new js file, it can get annoying. If you are using underscore often, I'd suggest to add globals to your .eslintrc file, for example:
{
"globals": {
"_": "readonly"
}
}
And save this as .eslintrc in your project root, or optionally in your user home directory. Although some say the latter not recommended, it can sometimes be convenient, but you have to remember that you have it there :)
Explanation of the above rule: "_": "readonly" (used to be "_": false, now deprecated) means that a variable named _ tells eslint that this variable is defined globally and it will not emit any no-undef errors for this variable. As #sebastian pointed out, "readonly" (or false - deprecated) means that the variable can't be overwritten, so the code _ = 'something else' would yield an error no-global-assign. If you were to instead use "_": "writable" (or "_": true - deprecated), this means that the value can be re-assigned and the previously mentioned error will not occur.
But keep in mind that this will only happen if you assign directly to the global variable as I have shown in the example. You can still shadow it and eslint won't say anything. For example, these snippets wouldn't yield the no-global-assign:
const _ = 'haha I broke your _'
or as function argument name, e.g.
function (_) {
console.log(_, 'might not be the _ you were looking for')
}
If you are using jest for testing - in your environment - in eslintrc.json
"env":{
"jest":true
}

How to turn off JSHint error?

I have the following error for my files in tests:
Expected an assignment or function call and instead saw an expression.
It is generated from Chai libraries asserts. How can I turn it off in my .jshintrc file? I run a Gulp task based on it.
Here's how you can silence it inside of a .jshintrc file.
{
...
"expr": true
...
}
Source: http://jshint.com/docs/options/#expr
You can add the following on top of the line that's generating the error :
/*jshint -W030 */
Reference : http://jslinterrors.com/expected-an-assignment-or-function-call

Categories