VS Code + Prettier not using printWidth setting? - visual-studio-code

I'm trying to get Prettier to format my files with a printWidth of 80 chars (what I understand to be the default). However, even with my user settings configured:
"prettier.printWidth": 80
AND using a .prettierrc config in the project:
{
"tabWidth": 2,
"useTabs": false,
"printWidth": 80
}
...nothing changes when I save a file (other settings are implemented, which makes me think there is a conflict somewhere).
Even the Prettier output in VS Code shows that it's pulling in the config:
["INFO" - 8:27:53 PM] Using config file at '/Users/username/Documents/whatever/.prettierrc'
["INFO" - 8:27:53 PM] Prettier Options:
{
"filepath": "/Users/username/Documents/whatever/project/file.mdx",
"parser": "mdx",
"tabWidth": 2,
"useTabs": false,
"printWidth": 80
}
["INFO" - 8:27:53 PM] Formatting completed in 93.855083ms.

You're formatting MDX, which is a variant of Markdown. Breaking text to respect the print width in Markdown is considered unsafe by default because this can affect some renderers sensitive to line breaks. That's why Prettier's option proseWrap(docs) defaults to preserve. Set it to always for Prettier to start wrapping text.

Related

VS Code keeps using single quotes for string even after settings update

How could I configure VS Code to make it change all the single quotes to double quotes whenever I format a document? I searched in my settings for the quotes keyword and selected the double quotes everywhere I could:
But the result is that no quotes are changed on file formatting. I.e. the single quotes stay single and the double quotes stay double after formatting.
I am talking about .js files.
You have prettier installed it seems (based off your options in the your screenshot), and that is probably overriding your settings. You may see something like this in the prettier output when you save/format:
["INFO" - 9:28:24 AM] Formatting file:///c%3A/dev/GitHub/project/src/root.tsx
["INFO" - 9:28:24 AM] Using config file at 'c:\dev\GitHub\project\.prettierrc'
["INFO" - 9:28:24 AM] Using ignore file (if present) at c:\dev\GitHub\project\src\.prettierignore
["INFO" - 9:28:24 AM] File Info:
{
"ignored": false,
"inferredParser": "typescript"
}
["INFO" - 9:28:24 AM] Detected local configuration (i.e. .prettierrc or .editorconfig), VS Code configuration will not be used
["INFO" - 9:28:24 AM] Prettier Options:
{
"filepath": "c:\\dev\\GitHub\\project\\src\\root.tsx",
"parser": "typescript",
"useTabs": false,
"printWidth": 120,
"tabWidth": 2,
"endOfLine": "auto",
"singleQuote": true,
"trailingComma": "es5",
"bracketSameLine": false
}
["INFO" - 9:28:24 AM] Formatting completed in 0.061ms.
Note the "singleQuote": true, part, as well as the Detected local configuration (i.e. .prettierrc or .editorconfig) in the output. If you have a .prettierrc file, modify it and set singleQuote to false. Save the file, restart Visual Studio Code, and then try again.

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?

Prettier in vscode

I have a Problem with Visual Studio Code and Prettier. I got the following ".prettierrc"
{ "semi": false, "singleQuote": true, "arrowParens": "avoid", "bracketSpacing": false, "printWidth": 60 }
when i do know "npx prettier -w *.js" it works fine, for example it removes the Semicolon at the end of each line. But when I try to use it without the npx it doesn't work. I got the Option "format on save" activated for Prettier. But he only moves the semicolon to the end of the line and doesn't delete it.
I activated "Prettier:Require Config", Set Prettier -Code Format as default and also added "Prettier:Config Path: prettierrc" <-- Is my path maybe wrong ??

Eslint prettier error on new line incomprehensible

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

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.