Why does JSHint throw a warning if I am using const? - javascript

This is the error I get when using const:
<error line="2" column="1" severity="warning" message="&apos;const&apos; is available in ES6 (use esnext option) or Mozilla JS extensions (use moz)." source="jshint.W104" />
My code looks like this:
const Suites = {
Spade: 1,
Heart: 2,
Diamond: 3,
Club: 4
};
The code works fine only JSHint is warning me every time.

When relying upon ECMAScript 6 features such as const, you should set this option so JSHint doesn't raise unnecessary warnings.
/*jshint esnext: true */ (Edit 2015.12.29: updated syntax to reflect #Olga's comments)
/*jshint esversion: 6 */
const Suites = {
Spade: 1,
Heart: 2,
Diamond: 3,
Club: 4
};
This option, as the name suggests, tells JSHint that your code uses ECMAScript 6 specific syntax.
http://jshint.com/docs/options/#esversion
Edit 2017.06.11: added another option based on this answer.
While inline configuration works well for an individual file, you can also enable this setting for the entire project by creating a .jshintrc file in your project's root and adding it there.
{
"esversion": 6
}

You can add a file named .jshintrc in your app's root with the following content to apply this setting for the whole solution:
{
"esversion": 6
}
James' answer suggests that you can add a comment /*jshint esversion: 6 */ for each file, but it is more work than necessary if you need to control many files.

I got this same warning when using an export statement. I'm using VS Code and used a similar approach to Wenlong Jiang's solution.
User Settings
JSHint config
"jshint.config": {} (Edit)
Use double quotes when specifying "esversion"
Or copy this snippet into User Settings:
"jshint.options": {
"esversion": 6,
}
Creating a .jshintrc file isn't necessary if you want to configure the global jshint settings for your editor

If you're using VSCode:
1.
Go to preferences -> settings (cmd + ,)
Type jshint.options into the search bar
Hover over it and click on the pencil icon
Its now appended on the right side.
Add "esversion": 6 to the options object.
2.
Or simply add this to your user settings:
"jshint.options": {
"esversion": 6
}
[UPDATE] new vscode settings
Go to preferences -> settings (cmd + ,)
type jshint into search
continue with step 2.

I spent ages trying to fix this. Every solution talks about 'setting options'. I don't know what that means. Finally, I figured it out. You can just include a commented out line at the top of the file /*jshint esversion: 6 */.

You can specify esversion:6 inside jshint options object. Please see the image. I am using grunt-contrib-jshint plugin.

Create .jshintrc file in the root dir and add there the latest js version: "esversion": 9 and asi version: "asi": true (it will help you to avoid using semicolons)
{
"esversion": 9,
"asi": true
}

When you start using ECMAScript 6 this error thrown by your IDE.
There are two options available:
if you have only one file and want to use the es6 then simply add below line at the top of the file.
/*jshint esversion: 6 */
Or if you have number of js file or you are using any framework(like nodejs express)you can create a new file named .jshintrc in your root directory and add code below in the file:
{
"esversion": 6
}
If you want to use the es6 version onward for each project you can configure your IDE.

In your package.json you can tell Jshint to use es6 like this
"jshintConfig":{
"esversion": 6
}

Creating a .jshintrc file is not necessary.
If you are using ECMAScript 6 then all you need to do is tell JSHint that:
Go to File > Settings
Navigate to Languages & Frameworks > JavaScript > Code Quality Tools > JSHint.
Scroll down to find Warn about incompatibilities with the specified ECMAScript version.
Click on Set.
Enter 6 and then press [Set].
Click [OK]

For SublimeText 3 on Mac:
Create a .jshintrc file in your root directory (or wherever you prefer) and specify the esversion:
# .jshintrc
{
"esversion": 6
}
Reference the pwd of the file you just created in SublimeLinter user settings
(Sublime Text > Preference > Package Settings > SublimeLinter > Settings)
// SublimeLinter Settings - User
{
"linters": {
"jshint": {
"args": ["--config", "/Users/[your_username]/.jshintrc"]
}
}
}
Quit and relaunch SublimeText

If you are using Webstorm and if you don't have your own config file, then just enable EcmaScript.next in Relaxing options in in
Settings | Languages & Frameworks | JavaScript | Code Quality Tools |
JSHint
See this question How-do-I-resolve-these-JSHint-ES6-errors

If you are using Grunt configuration, You need to do the following steps
Warning message in Jshint:
Solution:
Set the jshint options and map the .jshintrc.js file
Create the .jshintrc.js file in that file add the following code
{
"esversion": 6
}
After configured this, Run again It will skip the warning,

Create a file called, say jshint_opts with this content:
{
"esversion": 6
}
Then invoke jshint with something like this command line:
jshint --config jshint_opts lib/*.js

May 2020 Here's a simple solution i found and it will resolve for all of my projects ,on windows if your project is somewhere inside c: directory , create new file .jshintrc and save it in C directory open this .jshintrc file and write { "esversion": 6} and that's it. the warnings should go away , same will work in d directory
yes you can also enable this setting for the specific project only by same creating a .jshintrc file in your project's root and adding { "esversion": 6}

To fix this in Dreamweaver CC 2018, I went to
preferences,
edit rule set - select JS,
edit/apply changes,
find "esnext" and changed the false setting to true.
It worked for me after hours of research. Hope it helps others.

I had the same issue, and I found that by adding:
/* jshint esversion: 8 */
(or whatever jshint esversion you need, like 6)
To the top of my .js file satisfies the cause for the warnings.

If using Sublime Text 3:
Go to Preferences -> Settings
Under Preferences.sublime-settings—User add "esversion": 6

In a new version of Dreamweaver to solve this error
Go to Edit->Preference->Linting
And the go-to js Edit rule set
and past
"jshintConfig":{
"esversion": 6
}

Related

Linter error in Deno project tsconfig.json, and cascading into project files - but code runs successfully?

I need to use the ES2019 flatMap() method on an array in a Deno project, so I have created a tsconfig.json file as below:
{
"compilerOptions": {
"target": "es5",
"lib": [
"es2019"
]
}
}
This gives what appear to be 6 of the same linter errors, specifically Cannot find type definition file for 'cacheable-request' in the tsconfig.json file. Have I created this file incorrectly or somehow caused conflict with the Deno structure?
In a project module where I am attempting to use flatMap() I am seeing the error
Property 'flatMap' does not exist on type 'number[][]'. Do you need to change your target library? Try changing the `lib` compiler option to 'es2019' or later.
This doesn't make much sense since I have set my lib to es2019 in my tsonfig - unless I've created it wrong and it's thus not being picked up... then again the code works, so I'm assuming that it has correctly compiled to es2019.
Am I missing some aspect of creating a tsconfig file for a Deno project that allows use of es2019?
I believe there's no need normally to include a tsconfig.json in order to use ES2019 features in Deno. I was able to use flatMap without problems in Deno 1.9.0, for example:
// mod.ts
const map = [1,2,3,[1,2,3],[1,2]].flatMap((_value, _index, _array) => {
return 1;
});
console.log(map);
deno run mod.ts
>> [ 1, 1, 1, 1, 1 ]
I couldn't find if this was unsupported or bugged in previous versions but try upgrading Deno and omitting tsconfig.json to fix this.

I can't fix the "let" warning in JSHint plugin for vscode in all the new projects I create

When I write:
let name = "Henry";
The following warning message appear:
'let' is available in ES6 (use 'esversion: 6') or Mozilla JS extensions (use moz).
I don't have any idea what to do for correct that warning. The answers I found are create a file named .jshintrc and then add this:
{
"esversion": 6
}
The thing is, It just work for the current project I'm programming, If a create a new one, I have to do again the same file. There is another way to do it that apply all the new projects?
You need to enable es6 in your jshint options with the esversion option. If you do not have a .jshintrc file at top level, create one and add this
{
"esversion": 6
}
Optionally, you can add this to the file, but it could get rather annoying if you have to add this to every file
/* jshint esversion: 6 */
https://jshint.com/docs/options/#esversion
You need to add a tag to let JSHint know you want to you ES6.
/* jshint esversion: 6 */
let name = "Henry";

How to properly re-configure JSPrettier after an update with a breaking change?

I am trying to make Jsprettier work in Sublime, but the formats I set up can't be applied, because I keep getting an error when I want to save a document (jsprettier: format failed). When I open the console to see what the error is it says: The system cannot find the path specified. (CR).
The settings in the user settings tab of jsprettier are the following:
{
"prettier_cli_path": "/c/Users/Adri/AppData/Roaming/npm/prettier",
"node_path": "/c/Program Files/nodejs/node",
"auto_format_on_save": true,
"prettier_options": {
"printWidth": 120,
"tabWidth": 2,
"singleQuote": true,
"trailingComma": "none",
"bracketSpacing": true,
"jsxBracketSameLine": false,
"parser": "flow",
"semi": true,
"tabs": false
}
}
Thank you
In your terminal run the following:
which node
which prettier
Copy the result of the output for each step and in SublimeText3 proceed to:
Preferences --> PackageSettings --> JsPrettier --> Settings-User
In the user settings build your file as such:
{
"prettier_cli_path": "output from terminal here",
"node_path": "output from terminal here",
"auto_format_on_save": true,
}
I found that when JsPrettier updates it erased these settings on me and needed to re-enter them...
Hope that helps.
tested on: Sublime Text 3.2.2, MacBook Pro
Install Js​Prettier globally using npm:
npm install --global prettier
Install JsPrettier from Package Control by:
Sublime Text-->Preferences-->Package Control-->type and enter: Install Package-->type and enter: JsPrettier
restart Sublime Text
In case this helps anyone:
While this installed easily on one computer, I struggled with it on a second one.
After being required to set the node and prettier_cli path, I got node errors.
What worked in the end was setting the prettier_cli_path to prettier.js within the prettier node_modules directory as such:
C:/Users/Me/AppData/Roaming/npm/node_modules/prettier/bin-prettier.js
Problem solved!
I just took the first 2 lines out (paths) and now it works!
In my case, I had to fully restart Sublime 3 after it has installed a new Sublime version.
If someone is having trouble with this even now, it may help to know that Prettier requires node >= v10.
To get this to work, you may have to set your node version as >= 10, I personally prefer 12 or 13.
You can do this either by using nvm alias default 12 fornvm or n 12 for n
Now, update the user settings file to make sure prettier has the right paths to prettier and node. Find the paths to node and prettier using which
which prettier
which node
Add the paths and flags to auto format on save as desired
{
"prettier_cli_path": "/Users/user/.nvm/versions/node/v12.13.1/bin/prettier",
"node_path": "/Users/user/.nvm/versions/node/v12.13.1/bin/node",
"auto_format_on_save": true,
"format_on_save_extensions": ["tsx", "json"]
}
Now reload sublime
You're running an outdated version of Prettier. Please update again to the latest version, which is v1.6.1 (as of Sept. 13, 2017).
If you want to use v1.5.x on purpose, you can add the --no-config option to the additional_cli_args setting,
and tell Prettier not to attempt to find config files.
json
{
"additional_cli_args": {
"--no-config": ""
}
}

How to specify custom --rulesdir for eslint in vim editor

I have installed syntastic plugin in vim and installed eslint in npm globally. Below is the snippet of my .vimrc for syntastic configuration:
set statusline+=%#warningmsg#
set statusline+=%{SyntasticStatuslineFlag()}
set statusline+=%*
let g:syntastic_javascript_checkers = ["eslint"]
let g:syntastic_always_populate_loc_list = 1
let g:syntastic_auto_loc_list = 1
let g:syntastic_check_on_open = 1
let g:syntastic_check_on_wq = 0
Here's the result when I run :SyntasticInfo javascript,
Syntastic version: 3.6.0-64 (Vim 704, CYGWIN_NT-6.3)
Info for filetype: javascript
Global mode: active
Filetype javascript is active
Available checker: eslint
Currently enabled checker: eslint
Assume I have following project structure, there're some custom rules activated in .eslintrc, and the definition of those rules are in .eslintrules dir:
xxx_project:
|--.eslintrc
|--.eslintrules
|-- rule1.js
|-- rule2.js
|-- ...
|-- src
|-- abc.js
Everytime I run :SyntasticCheck on some source file, nothing happens. So I try running eslint against some js file directly in command line. There're some errors threw indicating cannot find definition of some custom rules.
So I think eslint has found the configuration file, but it doesn't know where the --rulesdir is.
Can someone help here? As far as I know, the --rulesdir option is only available in command line.
Edit:
function! ESLintArgs()
let rules = findfile('.eslintrules', '.;')
return rules != '' ? '--rulesdir ' . shellescape(fnamemodify(rules, ':p:h')) : ''
endfunction
autocmd FileType javascript let b:syntastic_javascript_eslint_args = ESLintArgs()
This tries to find a file named .eslintrules and sets --ruledir to its base directory.

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