I have been using Eslint for some time. I have defined a function which takes user input and assignes it to variable. The function is called in HTML on button click. I have an error in js that this function is never used. I know that this is not a problem at all. Just trying to write correct code from the start.
Thank you!
If you don't want this to be an error stopping you from starting the application, set the eslint no-unused-vars rule to off or warn in your eslint configuration file (eslintrc.js or similar). For example:
module.exports = {
...
rules: {
'no-unused-vars': 'off',
},
};
Note that in order for this to work you have to restart the application.
Related
cy.wait("api").then((xhr: any) => {
expect(xhr.method).to.eq("POST");
expect(xhr.status).to.eq(200);
expect(xhr.requestBody.test).to.not.exist;
});
expect(xhr.requestBody.test).to.not.exist; this line throws eslint error as shown below:
error Expected an assignment or function call and instead saw an expression #typescript-eslint/no-unused-expressions
Verified at tslint-playground.
Two alternates that pass the linter:
expect(xhr.requestBody).to.not.have.property(test)
expect(xhr.requestBody.test).to.eq(undefined)
In my opinion you have multiple options to solve this:
First and recommended option is to rewrite your assertion for example as follows:
const bodyContainsTest = xhr.requestBody.hasOwnProperty('test');
expect(bodyContainsTest).to.be.false;
Second option would be to simply disable that rule for the next line:
// eslint-disable-next-line #typescript-eslint/no-unused-expressions
expect(xhr.requestBody.test).to.not.exist;
Another option would be to disable this rule globally for all Cypress test files in your .eslintrc.json if needed:
"#typescript-eslint/no-unused-expressions": "off",
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"]
}
}
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
}
Due ESLint I found a rule newline-before-return about empty line before return statements. But did not see a rule about empty line before the first statement in function. F.e.:
function (a) {
var b = +a;
}
Has ESlint a rule about this? If it has, what is the name this rule?
Thanks
The padded-blocks rule allows you to require newlines at the start and end of blocks, including function bodies. In addition to function bodies, it also covers the bodies of if statements, for and while loops, and other block-like structures, which you may or may not want.
Try pasting the following code in the demo to see if it works for you:
/* eslint padded-blocks: "error" */
function foo(bar) {
if (bar) {
foo();
}
}
If you only want to check function bodies, you could follow #Dhananjay's suggestion and edit the rule's source code into your own custom rule.
I don't think there is such a rule available out of the box based on the list of available rules You can try to add a custom rule for this check as explained here
Such rule is implemented in the HAPI ESLint plugin, installed this way:
npm install #hapi/eslint-plugin-hapi --save-dev
// Add in your `.eslintrc`
{
plugins: [
'#hapi/eslint-plugin-hapi',
],
rules: {
'#hapi/hapi/scope-start': ['error'],
},
};
}
Or you may use it as part of HAPI ESLint config.
Mind, that Airbnb style guide recommends against padding blocks.
I am currently using the basic Webpack API with a custom printer for the build results, i.e.:
import webpack from 'webpack'
webpack({ /* webpack config */ }, printStats)
function printStats(err, stats) {
// my custom printing of build results
}
Now I want to switch to using webpack-dev-middleware, but retain my stat printer. I would expect maybe this to work:
import webpackDev from 'webpack-dev-middleware'
app.use(webpackDev(webpack({ /* webpack config */ }, printStats), {
quiet: true // disable default printing so I can use my own
// ... more webpack-dev-middelware configuration
}))
but it only prints the first compilation and ignores subsequent ones even though they do take place. What is the right way to do this?
I solved this problem by listening directly to the Webpack compiler object via the plugin method:
const compiler = webpack({ /* webpack config */ })
compiler.plugin('done', stats => {
// my custom printing of build results
})
app.use(webpackDev(compiler, {
quiet: true // disable default printing so I can use my own
// ... more webpack-dev-middelware configuration
}))
The done event occurs every time compilation finishes, successfully or otherwise. Other events you can listen to via the plugin method:
'run': Indicates that a one-off compilation is taking place. Aynchronous; listener takes the compiler object and callback function which must be invoked to indicate the listener is done handling the event.
'watch-run': Indicates that the source is being compiled or recompiled in watch mode. Aynchronous; listener takes the compiler object and callback function which must be invoked to indicate the listener is done handling the event.
'invalid': Indicates that a change was detected to the source in the course of watching it (will be followed shortly by a watch-run event. Synchronous; listener takes no arguments.