Ignoring a global variable in JSHint - javascript

I have a global variable called Filters that is loaded before anything else, but JSHint tells me it's undefined in all files that use Filters. Of course, JSHint does not know the loading order (and yes, the order is enforced), so how would it know?
I tried adding it to the globals:
"globals": {
"Filters": false
}
And that had no effect, so I tried:
"predef": [ "Filters" ]
Again, no effect.
These are also present:
"undef" : true,
"latedef" : false,
What am I missing here?

In this example, I would normally just put the following at the top of my files:
/* globals Filters */
I prefer to specify globals on a per file basis.

Related

new URLPattern raises error in react "'URLPattern' is not defined" [duplicate]

I have got multiple javascript files and I have defined some global variable in a file which loads before the others.
As a consequence all of the files loaded after the first have access to the global variable.
However ESLint shows the global variable as "not defined". I don't want to change the rules of ESLint and I would like to find an elegant way to get rid of these error messages.
Any clue?
Thanks
I don't think hacking ESLint rules per file is a great idea.
You should rather define globals in .eslintrc or package.json.
For .eslintrc:
"globals": {
"angular": true
}
For package.json:
"eslintConfig": {
"globals": {
"angular": true
}
}
Check https://eslint.org/docs/user-guide/configuring/language-options#specifying-globals
You can add globals either per file or in your config.
If you don't want to change your config, you'll have to add the used globals in every file.
To specify globals using a comment inside of your JavaScript file, use the following format:
/* global var1, var2 */
This defines two global variables, var1 and var2. If you want to optionally specify that these global variables should never be written to (only read), then you can set each with a false flag:
/* global var1:false, var2:false */
http://eslint.org/docs/2.0.0/user-guide/configuring#specifying-globals

Auto Fix is enabled by default. Use the single string form

When I configure my vscode with eslint & prettier, I met a problem in .settings.json file with error message "Auto Fix is enabled by default. Use the single string form.":
My eslint configuration is:
My prettier configuration is:
module.exports = {
singleQuote: true,
semi: false
}
Does anybody know what's the reason and how to fix?
It seems a tab width issue, try add "tabWidth": 4 in your prettier config.
EDIT:
According to ESLint Reference: "eslint.validate" is an array of language identifiers specifying the files for which validation is to be enforced.
"eslint.validate" accept an array of language identifiers, not an array of objects.
No need for "autoFix", it defaults to be true.
So your settings should be:
"eslint.validate": [
"vue",
"html",
"javascript"
]

Global variables in Javascript and ESLint

I have got multiple javascript files and I have defined some global variable in a file which loads before the others.
As a consequence all of the files loaded after the first have access to the global variable.
However ESLint shows the global variable as "not defined". I don't want to change the rules of ESLint and I would like to find an elegant way to get rid of these error messages.
Any clue?
Thanks
I don't think hacking ESLint rules per file is a great idea.
You should rather define globals in .eslintrc or package.json.
For .eslintrc:
"globals": {
"angular": true
}
For package.json:
"eslintConfig": {
"globals": {
"angular": true
}
}
Check https://eslint.org/docs/user-guide/configuring/language-options#specifying-globals
You can add globals either per file or in your config.
If you don't want to change your config, you'll have to add the used globals in every file.
To specify globals using a comment inside of your JavaScript file, use the following format:
/* global var1, var2 */
This defines two global variables, var1 and var2. If you want to optionally specify that these global variables should never be written to (only read), then you can set each with a false flag:
/* global var1:false, var2:false */
http://eslint.org/docs/2.0.0/user-guide/configuring#specifying-globals

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
}

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.

Categories