Change 'shouldFix' parameter in ESLint - javascript

I'm using ESLint with its VS Code extension to format my code.
At some point it stopped auto-formatting the code on save. I uninstalled everything and reinstalled anew: VS Code, ESLint and ESLint extension for VS Code.
I think I'm close to making the formatter work, but ESLint logs this in VS Code when I try and save a file with a linting error:
2020-10-10T10:41:45.345Z eslint:source-code-fixer shouldFix parameter was false, not attempting fixes
I guess if I find that shouldFix parameter and set it to true it'll work, but where is it?

The above solution didn't work for me. I had to explicity add the following to my VSCode settings.
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
}
I think maybe something in the newest VSCode update communicating this with the plugin went wrong.

I managed to fix it changing the .eslintrc.js at the root of the project.
'parserOptions': {
'ecmaVersion': 12,
}
instead of
'parserOptions': {
'ecmaVersion': 2018,
}
I don't know why it worked till yesterday and then stopped.... but well that solved it for me.

Configuring a default formatter resolved this for me. From the extension's page:
eslint.format.enable (#since 2.0.0): uses ESlint as a formatter for files that are validated by ESLint. If enabled please ensure to disable other formatters if you want to make this the default. A good way to do so is to add the following setting "[javascript]": { "editor.defaultFormatter": "dbaeumer.vscode-eslint" } for JavaScript. For TypeScript you need to add "[typescript]": { "editor.defaultFormatter": "dbaeumer.vscode-eslint" }.

run npm run lint
this will start debugging your code with eslint and your error should be solve then,

Related

Why does Prettier not format code in VS Code?

In my Nuxt application where ESlint and Prettier are installed and enabled, I switched to Visual Studio Code.
When I open a .vue file and press CMD+ Shift + P and choose Format Document, my file does not get formatted at all.
My .prettierrc settings:
{
"tabWidth": 2,
"semi": false,
"singleQuote": true
}
I have so many source code lines, so I cannot format them manually. What am I doing wrong?
Select File -> Preferences -> Settings (Ctrl + comma) and search form formatter
Set Prettiers as Default formatter.
If doing what #Simin Maleki mentioned does not solve it for you, there is a chance that your default formatter is not set:
File > Preferences > Settings > Search for "default formatter"
Make sure your Editor: Default Formatter field is not null but rather Prettier - Code formatter (esbenp.prettier-vscode) and that all the languages below are ticked. This fixed my issue.
STEP BY STEP WALKTHROUGH
Also make sure that your format on save is enabled:
Sometimes, prettier stops working when there are syntactic errors in the code. You can view the errors by clicking on the x button on the bottom right corner beside Prettier
Prettier could also format your files on save.
However, installing and enabling does not result in working.
You have to check "format on Save" in VSCode: Setting >> User >> Text Editor >> Formatting
You only have to configure your Default Formatter and check the checkbox in Format On Save in the settings, after installing prettier to make it work. Don't mess with other configuration files.
1 - Select Default Formatter
Open Files -> Preferences -> Settings (or Ctrl + , in Windows).
Search for Editor: Default Formatter
Select your default formatter as Prettier - Code Formatter;
See the image below;
2 - Save On Format
Open Files -> Preferences -> Settings (or Ctrl + , in Windows).
Search for Editor: Format on Save
Click the check box under Format On Save;
See the image below;
disable and enable prettier extension solves my problem
I am not using Vue, but had the same problem.
I already had the settings
Editor: default formatter to prettier
Editor: Format on Save to true
I already had .eslintrc.js and .prettierrc files
But nothing worked.
The solution to my problem was that I had all set properly, except I needed to:
Command + Shift + p
type format document with
select Configure Default Formatter...
select Prettier as default.
I don't know why the Editor: Format on Save set to true was not enough.
I needed to select default formatter using the above steps so it worked.
can you try to add this section to your VS Code settings.json file?
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true,
},
These three steps may solve your problem:
1 - Go to settings, then search for auto format
2 - Select Text Editor
3 - Select esbenp.prettier-vscode as your Default Formatter.
Simply said, go to Settings > User tab > Text Editor > Editor: Default Formatter and change it to prettier.
On Windows:
We can open the below file using:
Start > Run
File Path:
%AppData%\Code\User\settings.json
Change
From:
"[javascript]": {
"editor.defaultFormatter": "vscode.typescript-language-features"
},
To:
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
Note:
If the above is not present, add it instead of changing.
You should already have installed the "Prettier - code formatter" to see the effect of the above change - https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode
you can still have issues in spite of all these settings. In this case, as pointed out in an earlier answer, then it would be a good point to check the prettier notification at bottom status bar in VSCode.
When clicking on that status, the output panel should report the issue in the HTML file. For me, the issue was I had a div inside a p tag which I assume prettier/VSCode conventions are against. When I removed it (and combined with all the settings above, namely default formatter and format on save) I got prettier working.
.prettierrc is not required unless you want to override VSCode settings
For me the problem was with HTML files where formatting stopped working one day. I had Format On Save configured, which worked in all files except HTML.
I then realized that I experimentally set Format On Save Mode to modification instead of file and forgot about it. This had an effect of not formatting anything in HTML files, surprisingly not even my changes. Setting it back to file solved the issue.
This is not a problem with Prettier itself, but prettier-vscode, the VSCode extension. According to its documentation, Vue formatting is disabled by default:
prettier.disableLanguages (default: ["vue"])
A list of languages IDs to disable this extension on. Restart required. Note: Disabling a language enabled in a parent folder will prevent formatting instead of letting any other formatter to run
In this case, to enable you should set "prettier.disableLanguages": []. And since this is an extension configuration, you should do it in VSCode settings file, not .prettierrc.
in my case it was being hijacked by typescript formatter.
it was driving me crazy because it kept re-formatting my spaces!
to fix i did cmd+. (settings) type -> "default formatter"
and unchecked typescript
1 .Use the other extension prettier was not working for me i just use the other VSCODE extension named PrettierNow i think this will help, done for me.Checkout the extension here
2 .From Latest Updates of prettier you need to add .prettierrc file in your root of the projects if you want to stick with prettier.
An example of .prettierrc is this-
{
"tabWidth": 4,
"singleQuote": true,
"semi": false
}
If Prettier formats all other files except HTML files automatically on save:
Press Cmd + P or Ctrl + P to open the command palette and type the following text in it:
> open settings
Click on Preferences: Open Settings (JSON) from the suggestion dropdown.
Inside the settings.json file, Check if "[html]" key exists. If the key exists and its value indicates using another formatting extension installed in Visual Studio Code, you should reset it back to use Prettier.
"[html]": {
"esbenp.prettier-vscode"
}
For an instance, sometimes, the value of "[html]" key could be "remimarsal.prettier-now" when you would have Prettier Now extension installed.
If you don't have any other formatting extension installed other than Prettier, you can also remove the "[html]" key altogether from settings.json file.
This is what worked for me (my default formatter was already set to Prettier)
Change default formatter to default
Restart vscode
Change default formatter back to Prettier.
How to format your code through VScode's ESlint plugin
Alright, rather than giving a guide on how to use VScode's Prettier extension, I'd rather explain how to rely on ESlint and have both worlds: checking that your code is correct (ESlint), then formatting it (Prettier).
What are the advantages of this?
not forcing your entire team to use VScode with the Prettier extension, maybe some prefer Vim, IntelliJ's Webstorm, Emacs etc... A tool-agnostic solution is IMO always better.
I think that linting your code is more important that formatting it, but if you have both extensions working at the same time, you may have conflicts between the formatting and the linting.
your hardware will struggle less, if you have less extensions running (mainly because it can stop the conflicts)
using an ESlint + Prettier combo will strip the need to have a specific personal configuration aside of the codebase (untracked). You'll also benefit from having Vue/Nuxt specific ESlint rules and a simpler/more universal configuration.
an ESlint configuration can be configured to be run before a commit, in a CI/CD or anywhere really.
How to achieve this kind of setup?
Let's start first by installing the ESlint extension and only it, DO NOT install the Prettier one.
Not installed Vetur yet?
I do heavily recommend it for Vue2 apps (what Nuxt is running as of today), you can find it below. It will allow to quickly and simply ESlint (+ Prettier) any .vue files.
When it's done, access the Command Palette with either ctrl + shift + p (Windows/Linux) or cmd + shift + p (Mac) and type Preferences: Open Default Settings (JSON)
There, you should have something like this
{
"workbench.colorTheme": "Solarized Dark", // example of some of your own configuration
"editor.codeActionsOnSave": {
"source.fixAll": true,
},
"eslint.options": {
"extensions": [
".html",
".js",
".vue",
".jsx",
]
},
"eslint.validate": [
"javascript",
"javascriptreact",
"typescript",
"typescriptreact",
"html",
"vue",
],
}
How to try that your configuration is now working?
To see if my solution is working, please download this Github repo, get the latest stable Node version (eg: 14) and run yarn to have it working. Otherwise, simply open VScode.
This repo can also be used to double-check that yours is properly configured by inspecting my files there!
Then, you could access any .js or .vue file and see the problems there (Command Palette: Problems: Focus on Problems View). nuxt.config.js and /pages/index.vue are good examples, here is the index.vue file.
You can see that we do have several things that can be fixed by Prettier but that we do also have an eslint(vue/require-v-for-key) error. The solution is available as a comment just below btw.
PS: if you want to have inline ESlint warnings/errors as in the screenshot, you can install Error Lens, it's a super amazing extension if you want to get rid of errors.
Save this file and you should saw that every auto-fixable things are done for you. Usually it's mainly Prettier issues but it can also sometimes be ESlint too. Since we do have the ESlint rules from Nuxt, you'll get some nice good practices out of the box too!
Tada, it's working! If it's not, read the section at the end of my answer.
If you want to create a brand new project
You can run npx create-nuxt-app my-super-awesome-project and select few things there, the most important being Linting tools: Eslint + Prettier of course (hit space to opt-in for one of them).
Warning: as of today, there is an additional step to do to have ESlint + Prettier working properly as shown in this Github issue. The fix should be released pretty soon, then the configuration below will not be needed anymore!
To fix this, run yarn add -D eslint-plugin-prettier and double check that your .eslintrc.js file is a follows
module.exports = {
root: true,
env: {
browser: true,
node: true
},
parserOptions: {
parser: '#babel/eslint-parser',
requireConfigFile: false
},
extends: [
'#nuxtjs',
'plugin:prettier/recommended', // this line was updated
'prettier'
],
plugins: [
],
// add your custom rules here
rules: {}
}
Then, you can have it working totally fine as above. Save the file and it should run ESlint then Prettier one after the other!
If you still have some issues
try to use the Command Palette again and ESLINT: restart ESLint Server or even Developer: Reload Window
feel free to leave a comment or contact me if you need some help
Go to Manage(located on left-bottom corner) -> Settings -> Users tab -> Text-Editor -> Formatting -> check the format on save
if not working then close and again open your vscode editor
Enabling "format on Save" in VSCode: Setting >> User >> Text Editor >> Formatting worked for me!
Recently I got the same problem, that Prettier does not format code automatically on saving. Checking Prettier, I saw an error: Invalid "arrowParens" value. Expected "always" or "avoid", but received true.
The error message was seen when I clicked this:
It turned out that I have Prettier Now installed also. This has a boolean value in my config file. After uninstalling Prettier Now, everything works fine.
If none of the other answers work, check that a conflicting prettier config .prettierrc does not exist in your working directory or check for .prettierignore to be sure the files/folders are not being ignored.
Check if there is a .vscode/settings.json file in your project directory (workspace). In my case someone had checked in this file:
{
"editor.formatOnSave": false
}
Solution: Delete the file (delete it from source control too) and add .vscode/ to .gitignore (if you're using git).
In some cases where prettier is provided as a dependency, you might need to install it before prettier vscode recognizes it using one of the following commands, depending on the package manager you are using
npm i or yarn
From the menu navigate through: view -> Command Palette
Form the command palette search for Format Document and then select Prettier as your format engine.
I had prettier already working on another project, but for the new one I had do it through this way to enable it again for the new project.
In my case it turned out I had configured prettier to use a configuration file that didn't exist (see screnshot below). That was hard to find since there wasn't any error message but prettier just didn't work. Maybe this helps somebody, too.
Check your package.json file for a property of prettier as this will take precedence.
{
"name": "example",
"scripts": { ... },
"prettier": {},
"dependencies": { ... },
"devDependencies": { ... },
}
Delete this property and the .prettierrc file will be used.
The order of precedence is stated in the prettier docs.
Some times with auto plugins update Required files used by Prettier might go missing .
Check this path if files are present here or folder is empty
C:\Users\YOURUSERNAME\.vscode\extensions\esbenp.prettier-vscode-2.2.2\out
If missing uninstall and reinstall prettier
For me - it was to do with ESlint which also works with Prettier. Eslint wasn't working (a local installation vs global installation conflict) which broke Prettier.
I Rolling back prettier to 1.7.3 and fixed it

Removing [ts] errors from JS files In VS Code

I get these TypeScript errors VS Code while I'm working in JS files. Is there anything I can do to disable this? I have put this in my settings and did not solve the issue:
"typescript.validate.enable": false
The error can been seen here
There's a GitHub issue that discusses the [ts] token from the errors in a bit more detail. The most relevant comment to this discussion is:
Yes. The TypeScript extension powers our javascript intellisense which is why you see [TS] in your js file. That only indicates what extension is providing that error.
You can disable this validation itself by adding the following to an appropriate settings.json file:
"javascript.validate.enable": false
The docs discusses this option a little bit further:
With javascript.validate.enable: false, you disable all built-in syntax checking. If you do this, we recommend that you use a linter like ESLint to validate your source code.
As noted above, this disables all built-in syntax checking. Although the suggestion is to use something like ESLint instead, there might be another option if you're specifically concerned about the import/export errors. You can add a jsconfig.json file to your project with the following content:
{
"compilerOptions": {
"module": "es2015"
}
}
This instructs VS Code to use the es2015 module syntax (import/export), which appears to make it happier.
On Windows- File > Preferences > Settings
Go to Extensions->TypeScript-> Javascript>Validate
make sure Enable/disable JavaScript validation. is not checked
This works for me
On Windows- File > Preferences > Settings
make sure validate is not enable
Make sure javascript.implicitProjectConfig.checkJs is false VSCode settings.
On the settings.json file add a line
"js/ts.implicitProjectConfig.checkJs": true,
"javascript.validate.enable": false, // => required
I don't have enough information about project setup and code, but it looks like you are trying to load .js files as typescript.
To use JavaScript files in Typescript projects you must enable allowJs flag, either in command line --allowJs or in the tsconfig.json as "allowJs": true.
But, if the .js files should not be part of the TS project but just are in the same directory tree, you need to review your exclude and include properties of tsconfig.json
On visual studio code File > Preferences > Settings Go to Extensions->TypeScript-> Javascript>Validate make sure Enable/disable JavaScript validatio
Open settings in your VSC.
To open your user and workspace settings, use the following VS Code menu command:
On Windows/Linux - File > Preferences > Settings
On macOS - Code > Preferences > Settings
Check if tslint.jsEnable is set to false
// Control whether tslint is enabled for JavaScript files or not.
"tslint.jsEnable": false,
Set it to false in workspace settings section
From the documentation:
tslint.enable - enable/disable tslint.
tslint.jsEnable - enable/disable tslint for .js files, default is false.

Ember JS : Exlude running eslint on js files of addon

I am using eslint ("ember-cli-eslint") for my ember application. Whenever i run the tests, eslint includes files from addon's as well(check screenshot). We have 5 custom add-ons which is used in this project. Eslint currently includes the files from these add-ons. Need guidance on how to exclude files from eslint.
I have also created ".eslintignore" file and added below line, but its of not use
modules/**/*.js
An addon's hintings are run when isDevelopingAddon function of addon returns true. To disable it, you should give hintingEnabled: function() { return false; } in index.js.
Releated issue: https://github.com/ember-cli/ember-cli/issues/5594
--
Edit
Returning false from hintingEnabled also disables linter to run in that add-on. So if you want to enable linter in add-on and disable it while using in another app use the following check:
return this.moduleName() === this.app.project.name()

How to disable JavaScript build error in Visual Studio 2017?

I just updated Visual Studio 2017 from RC to final. I didn’t get the following error but recently I get this error. In building the project, I get the following error and it prevents the web project to start:
Severity Code Description Project File Line Suppression State
Error eqeqeq (ESLint) Expected '===' and instead saw '=='. VistaBest.Shop.Web C:\***\Request.js 21
How can I disable JavaScript building error in Visual Studio 2017?
I think, find the solution:
Open Tools > Options
Navigate to Text Editor > JavaScript/TypeScript > EsLint (in VS2017 15.8 it is Linting not EsLint)
Set Enable ESLint to False
Visual Studio >= 15.8.5
In Visual Studio 2017 (v 15.8.0):
Option 1: Options > JS Errors
Open Tools > Options
Navigate to Text Editor > JavaScript/TypeScript > Code Validation
Set Enable JavaScript errors to false
or, set Enable JavaScript errors to true and Show errors as warnings to true
I needed to restart Visual Studio for this to take effect.
Option 2: Options > Linting
There is another option below which will let you edit your global linting settings:
Option 3: .eslint file
You can also create a file named .eslintrc in the root of your project.
Option 4: ESLint commands in-file
See #user9153924's answer
Resources
ESLint file syntax
ESLint Rules
I tried Mohammad`s solution but it didn't work. I managed to work doing the following:
Righ click on your web .csproj file
On the first <PropertyGroup> add the following entry:
<TypeScriptCompileBlocked>true</TypeScriptCompileBlocked>
Add /*eslint eqeqeq: ["error", "smart"]*/ to the first line of your Javascript code to remove the errors.
https://eslint.org/docs/rules/eqeqeq
Following Mohammad's solution will turn off ESLint for syntax checking. This works in VS2015 and should work in later versions.
For Visual Studio 2019.
Open Tools > Options
Navigate to Text Editor > JavaScript/TypeScript
=> Linting > General.
Then unchecked ESLint check box.
Please The bellow Image for reference.
I've just had to change the "eqeqeq" rule behaviour to include "smart":
Edit the .eslintrc file found in your user root folder mentioned in other answers already.
The change is made to the rules section by adding the smart rule
"rules": {
"eqeqeq": [2, "smart"],
Copied from the web article:
This option enforces the use of === and !== except for these cases:
Comparing two literal values
Evaluating the value of typeof
Comparing against null
I found the specifics at:
https://eslint.org/docs/2.0.0/rules/eqeqeq
I tried Mohammad's solution but with no luck, I followed Rafeel answer and instead of adding his suggested code sample I removed below code from web .csproj and finally I was able to build and run my project. There were two places where you should remove that in the same file. Still, I don't have any clue how the removed code will affect my solution.
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\TypeScript\Microsoft.TypeScript.Default.props" Condition="Exists('$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\TypeScript\Microsoft.TypeScript.Default.props')" />
Hope this will also help someone to save the day..!!!

VSCode TypeScript lint

People, who develop TypeScript used Visual Studio Code? I'm trying to use it too. But I have no validation(ts-lint) warnings or errors. I just got such messages on build:
ts-lint start
src/constants/type.ts[22, 2]: file should end with a newline
src/constants/type.ts[6, 5]: misplaced opening brace
ts-lint end
Example:
1 == 2
Such expression in JS file will message me that it will be better to use 1 === 2.
But this expression in TS file will work and not even show any warning.
What can I do to enable such validation in TypeScript, while typing?
Just had the exact same problem and seem to have resolved by adding "typescript" to the eslint.validate array in workspace settings
{
"eslint.validate": [
"javascript",
"javascriptreact",
"typescript"
]
}
Should you be new to VSCode. F1 => type "settings" => from the dropdown select either "user settings" (any project for that logged in user) or "workspace settings" (that one project only) => paste the above object on the right pane (or extend the existing object should there be one)
Please update to 0.8.0 version of vs code, just released. JavaScript Linting as you Type is now supported. https://code.visualstudio.com/updates
I did some experimentation running into a similar problem and found that I had a jsconfig.json file laying around that was created prior to converting code over to TypeScript. After removing/renaming that file and restarting VSCode it appears that my TypeScript compiler errors are now showing up as the error output when doing a build in VSCode as opposed to seemingly unrelated errors about unresolved imports.
I'm not sure if this is the same problem you're experiencing but hopefully it's of some help to you or somebody else.

Categories