Eslint prettier error on new line incomprehensible - visual-studio-code

I have and error of eslint in my code that I cannot understand how turn off, these are some examples:
As you can see there is a eslint(prettier/prettier) rule that is incomprehensible and I can't understand how to turn off.
This is my eslint and prettier config:
eslint
module.exports = {
root: true,
extends: ['#react-native-community'],
parser: '#typescript-eslint/parser',
plugins: ['#typescript-eslint'],
overrides: [
{
files: ['*.ts', '*.tsx'],
rules: {
'#typescript-eslint/no-shadow': ['error'],
'no-shadow': 'off',
'no-undef': 'off',
},
},
],
};
prettier
module.exports = {
printWidth: 100,
arrowParens: 'avoid',
bracketSameLine: false,
singleQuote: true,
tabWidth: 2
};

Oh Pierro... Its you again.
Your Prettier configuration is not in sync with your ESLint configuration.
When you set Prettier's print-width rule, you need to always make sure that you set ESLint's max-len rule to the same value. This is very import, and is likely causing your issue. You seem to have some other stuff going on though.
#see this post
You need to synchronize the two. Ir looks like a JSX configuration that's causing problems. Try changing the value of bracketSameLine in your prettier configuration to true, or try getting rid of it all together.
You see where it says that the rule prettier/prettier is causing the issue? That should be in your configuration.
That's odd... You don't seem to have your .eslintrc.* file configured properly. It looks like your not properly configuring the eslint-plugin-prettier plugin for ESLint.
If you look at the plugin's documentation, which is where the prettier/prettier rule comes from (the first prettier is the name if the plugin, the second prettier is the name of the rule) it says you need to add the following values to the correct settings in your .eslintrc.* configuration file.
{
"plugins": ["prettier"],
"rules": {
"prettier/prettier": "error"
}
}
Its odd that the prettier/prettier rule shows up at all without you having added that configuration.
#see this answer here, it thoroughly explains what the prettier/prettier rule is.

The if the prettier config file is not present, the auto-formatting properties of vscode are taken as the default. the settings.json in the .vscode settings should be in sync with the linter properties. In your case you don't have prettier configured thats why the error is being thrown from prettier/prettier

Related

Getting "Invalid prettier configuration file detected" error

I am getting following errors whenever I am trying to format my .ts or .html file using alt + shift + f
I have set prettier as the default formatter.
I have checked all the possible solutions from these posts.
Prettier: invalid configuration file even though the file is straight from the docs
prettier configuration error, prettier not working
https://linuxpip.org/vscode-prettier-not-working/
https://github.com/prettier/prettier-vscode/issues/1292
Also, I confirmed that the file prettier.config.js with following content does exist in my root directory
module.exports = {
tabWidth: 2,
semi: true,
singleQuote: true,
bracketSpacing: true,
printWidth: 100,
endOfLine: 'auto',
trailingComma: 'all',
};
Also, I have set the prettier as default formatter in my settings.json file
The problem is that is not giving any information of what the problem is with the config file.
And it says that See log for details. but I am not sure where to look for the logs
Any help?

ESLint: Unexpected property "indent" in eslintrc

As documented here, there is an ESLint property for indentation called indent. The example looks like this:
Or for tabbed indentation:
{
"indent": ["error", "tab"]
}
However, if I paste this exact code into my .eslintrc file, VS Code shows this as an error:
ESLint: ESLint configuration in .eslintrc is invalid: - Unexpected top-level property "indent". . Please see the 'ESLint' output channel for details.
The .eslintrc file that I had before has a number of rules already, and there are no errors printed with those. If I add "indent" to that list, or by itself, it fails.
Here is the error and stack trace printed in the ESLint output channel in VS Code:
[Error - 10:38:55 PM] ESLint stack trace:
[Error - 10:38:55 PM] Error: ESLint configuration in .eslintrc is invalid:
- Unexpected top-level property "indent".
at validateConfigSchema (/home/aaronfranke/workspace/OddJobs/node_modules/eslint/lib/shared/config-validator.js:286:15)
at ConfigArrayFactory._normalizeConfigData (/home/aaronfranke/workspace/OddJobs/node_modules/eslint/lib/cli-engine/config-array-factory.js:469:9)
at ConfigArrayFactory._loadConfigDataInDirectory (/home/aaronfranke/workspace/OddJobs/node_modules/eslint/lib/cli-engine/config-array-factory.js:445:33)
at ConfigArrayFactory.loadInDirectory (/home/aaronfranke/workspace/OddJobs/node_modules/eslint/lib/cli-engine/config-array-factory.js:401:18)
at CascadingConfigArrayFactory._loadConfigInAncestors (/home/aaronfranke/workspace/OddJobs/node_modules/eslint/lib/cli-engine/cascading-config-array-factory.js:305:46)
at CascadingConfigArrayFactory.getConfigArrayForFile (/home/aaronfranke/workspace/OddJobs/node_modules/eslint/lib/cli-engine/cascading-config-array-factory.js:250:18)
at CLIEngine.executeOnText (/home/aaronfranke/workspace/OddJobs/node_modules/eslint/lib/cli-engine/cli-engine.js:860:47)
at /home/aaronfranke/.vscode/extensions/dbaeumer.vscode-eslint-1.9.1/server/out/eslintServer.js:1:60107
at /home/aaronfranke/.vscode/extensions/dbaeumer.vscode-eslint-1.9.1/server/out/eslintServer.js:1:61116
What's going on? Is this option not valid somehow? Is the example config wrong? Is there something wrong with my ESLint extension? Is there something wrong with VS Code? I'm using Ubuntu 18.04 Linux 64-bit with VS Code 1.40.1, ESLint extension 1.9.1, and eslint --version is v5.16.0.
It is not allowed to put this settings as a top level property, which means it has to be nested in another property, which is "rules". So you have to write it like this:
{
//... other stuff ...
"rules": {
"indent": [ "error", "tab" ]
}
}

How do I get flake8 to reliably ignore rules in VS Code?

Two things that annoy me. First is the warning Flake8 gives me when I type more than 80 characters on a line. Second is the warnings I get when I haven't yet used a module name that I imported. I've looked at all the documentation on using Flake8 in the terminal. No use.
flake8 --ignore=E402
flake8 --max-line-length=120
This doesn't work. At least VS Code doesn't show any effect.
Add your arguments to your USER SETTINGS json file like this:
"python.linting.flake8Args": [
"--max-line-length=120",
"--ignore=E402,F841,F401,E302,E305",
],
note that flake8 uses
"python.linting.flake8Args": [
whereas black seems to use:
"python.formatting.blackArgs": [
if you're using both (or toggling) these settings maybe helpful:
{
"python.linting.pylintEnabled": false,
"python.linting.flake8Enabled": true,
"python.linting.enabled": true,
"python.formatting.provider": "black",
"python.formatting.blackArgs": [
"--line-length",
"120"
],
"python.linting.flake8Args": [
"--max-line-length=120",
"--ignore=E402",
],
"python.pythonPath": "venv/bin/python"
}
In my case (vscode 1.72.2, flake 5.0.4) it only worked by adding:
"flake8.args": [
"--max-line-length=120"
]
in the settings.json
I prefer editing the Workspace settings, namely <root project folder>/.vscode/settings.json, because I store it in version control. This way it is backed up and everyone working on the project will get it.
What was suggested in most of the other answers:
"python.linting.flake8Args": [
"--max-line-length=120",
],
had no effect for me.
I ran into this problem recently. I ran into problems because I was setting the argument to --config flake8.cfg instead of --config=flake8.cfg. Under the hood, vscode puts the CLI argument in quotes. Adding "--config flake8.cfg" to the flake8 command seems to confuse flake8 into thinking that it's looking at a file path and not a CLI argument.
The solution for me was to either set the args as --config=flake8.cfg (with the equals sign) or the args up into separate items in the array:
"python.linting.flake8Args": [
"--config",
"flake8.cfg"
]
The solution proposed by reka18 is great and was no doubt written specifically for the original question.
From a more general stand point, I would advise against using this kind of trick
if you work on a project that has dedicated configuration files.
You are guaranteed to run into incomprehensible configuration conflicts
and will possibly ignore rules that were purposefully enforced by the project.
In this case, you should use the following instead:
assuming the file is named .flake8 and is present at the project's root folder
// .vscode/settings.json
"python.linting.flake8Args": ["--config", ".flake8"],
Using --config .flake8 ensures only this file will be read (See official doc).
So it is important to use this option, even though it is a default value. Otherwise, a custom user configuration in a parent folder could accidentally be used.
To extend (change) the default Flake8 line length I added the following in my VS Code workspace: project.code-workspace:
{
...
"settings": {
"flake8.args": [
"--max-line-length=120",
]
}
}

TSLint Config in VS Code

I'm trying to configure the max line length (among other settings) for TS Lint in VS code but no matter what changes I make it doesn't 'take'.
So, the first strange thing is that VS code's TS Lint error for max line length says I've exceeded the 140 character limit but in the various config files I've found it only ever mentions 120 characters as the default.
I've changed this to 200 characters, disabled / enabled the extension but still get the 140 character warning. Does anyone know where and how to configure this setting? The documentation online is clear enough but I don't appear to have a tslint.json file and within the node_modules => tslint => lib => rules folder the setting is 120 and changing it makes no difference.
Edit 2020-09-30
Microsoft deprectaded the old plugin and released a newer, completely rewritten version with additional features here.
For the new plugin the setting "tslint.enable": true does not exists and is not needed anymore.
Original Answer
You need to create a tslint.json (in your workspace root) and set something like this to disable the maximum line length:
{
"defaultSeverity": "error",
"extends": [
"tslint:recommended"
],
"jsRules": {},
"rules": {
"max-line-length": [false]
},
"rulesDirectory": []
}
Furthermore, ensure that the following options are set in the in the vscode user settings (settings.json):
"tslint.configFile": "./path/to/tslint/relative/from/workspaceroot/tslint.json",
"tslint.enable": true
The tslint.configFile option can be empty if the file is in the root directory of your workspace.
Further rules can be found here.

VSCode single to double quote automatic replace

When I execute a Format Document command on a Vue Component.vue file VSCode replace all single quoted string with double quoted string.
In my specific case this rule conflicts with electron-vue lint configuration that require singlequote.
I don't have prettier extensions installed (no prettier.singleQuote in my setting)
How to customize VSCode to avoid this?
I dont have prettier extension installed, but after reading the possible duplicate answer I've added from scratch in my User Setting (UserSetting.json, Ctrl+, shortcut):
"prettier.singleQuote": true
A part a green warning (Unknown configuration setting) the single quotes are no more replaced.
I suspect that the prettier extension is not visible but is embedded inside the Vetur extension.
Well, like the guy (#user2982122) mentioned but instead of File go to Code -> Preferences -> Settings, then look for Quote, select Prettier and check both boxes
For projects that use .editorconfig file by default. The formatter will ignore the rules in the settings and use the rules in .editorconfig, then you can either:
Remove .editorconfig file, and use your VSCode settings.
Add quote_type = single to the .editorconfig file regarding your file type. You can also set quote_type value to double or auto.
It looks like it is a bug open for this issue: Prettier Bug
None of above solution worked for me.
The only thing that worked was, adding this line of code in package.json:
"prettier": {
"singleQuote": true
},
At the time of writing (June 2022):
please consider that .editorconfig overwrites every other configuration at the end, find the file (most probably on the root of your project), edit it and add the following:
[*]
quote_type = single
From the vuejs/vetur issue page https://github.com/vuejs/vetur/issues/986#
This solution worked for me.
In VSCodes settings.json file add this entry
"vetur.format.defaultFormatterOptions": {
"prettier": {
"singleQuote": true
}
},
Install prettier extension and paste below code in your VSCode settings.json file
"prettier.useEditorConfig": false,
"prettier.singleQuote": true
this will ignore your .editorconfig file setting.
What worked for me was setting up the .prettierrc.json config file. Put it to the root of your project with a sample config like this:
{
"singleQuote": true,
"trailingComma": "all",
"tabWidth": 2,
"semi": true,
"arrowParens": "always"
}
After triggering the Format Document command, all works just as expected.
Side note: What comes as a bonus with this solution is that each team member gets the same formatting outputs thanks to the present config file.
Correct solution :
I add .prettierrc.js file in my main root project
and write
module.exports = {
singleQuote: true
};
For newbies like me:
From the menu Nav bar at the top: Select File -> Preferences -> Settings.
In the search text box, type in Quote
In the filtered list that appears below, look for the gear icon and next to it - "Prettier". Click on check box to enable "Prettier: Single Quote"
I had the same issue in vscode. Just create a .prettierrc file in your root directory and add the following json.
For single quotes add:
{
"singleQuote": true
}
For double quotes add:
{
"singleQuote": false
}
Try one of these solutions
In vscode settings.json file add this entry
"prettier.singleQuote": true
In vscode if you have .editorconfig file, add this line under the root [*] symbol quote_type = single
In vscode if you have .prettierrc file, add this line
{
"singleQuote": true,
"vetur.format.defaultFormatterOptions": {
"prettier": {
"singleQuote": true
}
}
}
quote_type = single
add this inside .editorconfig
# EditorConfig is awesome: https://EditorConfig.org
# top-most EditorConfig file
root = true
[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = false
insert_final_newline = false
quote_type = single
As noted by #attdona the Vetur extension includes prettier.
While you can change the prettier settings, as per the accepted answer, you can also change the formatter for specific regions of a vue component.
Here, for example, I've set Vetur to use the vscode-typescript formatter as it uses single quotes by default:
in .prettierrc add
{
"arrowParens": "avoid",
"semi": false,
"singleQuote": true
}
After struggling with the issue I found a useful tool. If you click on the Prettier word in the right lower corner you will get the Output window opened. In that window once you run formatting (in my case it is Alt + Shift + F) you will see all the configurations which prettier will use to format the document. So, we can clearly see that specifying the prettier in the prettier.singleQuote is wrong. It should just be singleQuote. Hence, having the .prettierrc file in my user root folder with the following contents produced the desired result:
{
"trailingComma": "none",
"useEditorConfig": false,
"singleQuote": true
}
Also, make sure that you have the Prettier extension installed.
I'm using typescript, for me it got resolved with checking "Tslint integration" flag under prettier settings (in vscode preferences):
There only solution that worked for me:
and only for Angular Projects:
Just go into your project ".editorconfig" file and paste 'quote_type = single'.
Hope it should work for you as well.
In my case, the problem was in the escaping \ character inside the string:
message = 'Error argument is not an object, it\'s ' + typeof error
Turning on the avoidEscape option and using double quotes for that string solved the problem:
message = "Error argument is not an object, it's " + typeof error
.eslintrc.js
module.exports = {
rules : {
// Other rules...
'quotes' : ['error', 'single', {'avoidEscape' : true}],
}
}
I added file called .prettierrc in my project folder.
File content:
{
"singleQuote": true,
"vetur.format.defaultFormatterOptions": {
"prettier": {
"singleQuote": true
}
}
}
It works for me to check single quote in Prettier as well
tslint.autoFixOnSave as true
You can use this in settings.json
"javascript.preferences.quoteStyle": "single"
Use this extension.
https://marketplace.visualstudio.com/items?itemName=BriteSnow.vscode-toggle-quotes
cmd ' (ctrl ' on win/Linux) will cycle among ' " `
For JSX use:
{"jsxSingleQuote": false}
First, install the Prettier extension. Create a .prettierrc configuration file at the root of your project. And add config like below:
{
"trailingComma": "es5",
"singleQuote": true,
"jsxSingleQuote": true,
"printWidth": 100,
"tabWidth": 2,
"semi": true,
"endOfLine": "auto"
}
Well for me both options solved the issue:
By adding inside the .prettierrc - "singleQuote": true
Or by adding following inside the package.json ->
"prettier": {
"singleQuote": true
}
Though I tried also adding .prettierrc.js and have following
module.exports = {
singleQuote: true
};
This didn't worked.
I had a lot of issues controlling linting and prettier formating. I had rules for eslint on for prettier like
"prettier/prettier": [
"error",
{ "singleQuote": true, "trailingComma": "none" }
],
and rules inside .prettierrc file
{
"tabWidth": 2
}
But my .prettierrc file was not getting processed. My fix was installing prettier as a package on dev dependency. So the solution that worked for me was installing all these packages eslint-config-prettier eslint-plugin-prettier and prettier.
This works for me :
try right click on the current document
and choose "format document with " ,
and choose your own format extension for the document.
:)
If you're using a YAML plugin, it also has a single/double quote option that was tripping me up. Cheers.
"prettier": {
"singleQuote": true
},
This line of code save my hours.