Reputation: 101
Is there a quick guide / tutorial for how to easily set up auto-code-formatting, eslint/prettier, etc. in vscode? (Specifically for code formatting of React, React-Native projects).
Simply downloading the extension doesn't work and there seem to be two main versions.
I have been rattling my brain over it for a little while and I'd like to be able to quickly get this set up on any project - ideally with whatever the current orthodox styling / best practices are.
I have tried simply downloading the extensions and following the instructions but ended up running into loads of issues with the guides and auto-format on save did not work.
I really just needed a basic setup which would automatically become the default and work for all my projects and any new projects I created.
Then, perhaps a simple guide to adding a bit more customization, etc.
Upvotes: 6
Views: 15664
Reputation: 99
steps for windows 11+ OS for a specific workspace only
1.ctrl+shift+p
2.type: settings
--preferences: Open Workspace settings(JSON)
--select/enter
--folder is generate in your specified project/workspace folder .vscode/settings.json
file: settings.json
{
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true,
"singleQuote": true,
"tabWidth": 2,
"trailingComma": "none"
}
Upvotes: 1
Reputation: 2945
I'm more of the "edit the JSON" type of user of VSCode, and the answer from Masud was confusing to me because of that. It did push me in the right direction and I want to share for other like myself, more familiar with the .vscode/settings.json
(s) than with that graphical Settings Editor.
settings.json
edits - add these:
{
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true
}
.prettierrc
edit or add a file by that name at the root of your project for any custom settings. Here are some of my favs:
{
"singleQuote": true,
"tabWidth": 2,
"trailingComma": "none"
}
Then, you can sanity check against those rules. I just added a trailing comma in an object block (in a Styles block in the sample Expo tsx component, in my case - brand new project) and see it removed on save... Then, remove the trailingComma rule, save, and try the comma again and see that the comma stays.
Upvotes: 3
Reputation: 101
Regarding Auto Code Formatting:
For anyone starting out with VSCode - Expo, React-Native devs or having some issues with code formatting, please find below the simplest solution I could find (ESLint is a bit confusing / complex unless you are fairly familiar) - link to Expo guide is here if you would really like to do this (https://docs.expo.dev/guides/using-eslint/) - It is a bit complicated if you are just starting out and I find ESLint to cause too much bloat messaging on my code editor screen, especially now that there are things such as copilot etc.
Now get to Coding!!!
You can check out the Expo Guide (or similar) for further best practices but it will take some time to get it all setup.
Prettier should already auto-format on save and you can further configure it by adding a .prettierrc file at the root of your project and following https://github.com/expo/expo/tree/main/packages/eslint-config-universe#customizing-prettier which should override the default settings you have set up for all your projects.
You can later setup linting as well which is generally more useful down the line for when working with remote/shared repositories (ie: they contain explanations for the correction, etc.). Causes a bit of bloat but it's quite helpful for navigating a new team / codebase (ex Amazon SDE) to understand the style guidelines being used, fixing an entire project programmatically via a script etc. with ESLint.
Upvotes: 4