aleksandrhovhannisyan.com icon indicating copy to clipboard operation
aleksandrhovhannisyan.com copied to clipboard

How to Format Code on Save in VS Code with ESlint

Open AleksandrHovhannisyan opened this issue 3 years ago • 14 comments

AleksandrHovhannisyan avatar May 11 '21 11:05 AleksandrHovhannisyan

I just want to test this part out, forgive me, wanted to see if it will show up on your blog, thats all :)

hellomikko avatar May 14 '21 03:05 hellomikko

Lol, all good!

AleksandrHovhannisyan avatar May 14 '21 10:05 AleksandrHovhannisyan

Oh this is nice! What chat are you using for jekyll?

hellomikko avatar May 15 '21 02:05 hellomikko

@icueMike Thanks! I fetch the comments using the GitHub Issues API: https://www.aleksandrhovhannisyan.com/blog/jekyll-comment-system-github-issues/. You can also use Utterances, but I prefer to roll my own system so I have more control over the styling.

AleksandrHovhannisyan avatar May 15 '21 12:05 AleksandrHovhannisyan

There's some useful explanation here. Unfortunately, without prettier vscode extension, my eslint alone wouldn't format on save (i checked the settings).

I got it to work on prettier extension, along with this eslintrc:

"plugins": ["@typescript-eslint", "unused-imports"],
"extends": ["prettier", "eslint:recommended", "plugin:@typescript-eslint/recommended"],

FloodGames avatar May 16 '21 18:05 FloodGames

@FloodGames Hmm, interesting—I double-checked locally, and you shouldn't need the Prettier extension (it doesn't do anything by itself—you can use it to format code through the command palette with Format Code > Prettier, but the ESLint extension is actually the one that reads your workspace's eslintrc and formats your code). Glad you at least got it working, though!

AleksandrHovhannisyan avatar May 16 '21 20:05 AleksandrHovhannisyan

Thank you, your post is great. As you said, every time that a new project is created, some hours were lost with this configuration. I would like to add just a note, to complete my config I had to add this into my "settings.json" to start working. "editor.formatOnSave": true,

VanessaCarpes avatar Jul 21 '21 14:07 VanessaCarpes

@VanessaCarpes Thanks! Glad you found it useful.

I didn't need to add that setting on my end, but I do remember having used it in the past. editor.codeActionsOnSave alone should do the trick.

Thanks for sharing that note, though, in case other people run into the same issue.

AleksandrHovhannisyan avatar Jul 22 '21 11:07 AleksandrHovhannisyan

In case someone finds this useful, I'd configured format on save to use the VSCode default typescript formatter.

Fixed it by setting it back to ESLint,

"[typescript]": {
    "editor.defaultFormatter": "dbaeumer.vscode-eslint"
  },
"editor.defaultFormatter": "dbaeumer.vscode-eslint",
"editor.formatOnSave": true,

I spent way too much time figuring this out, as the on-save format was doing things the .eslintrc file did not ask for.

nkhil avatar Feb 08 '22 17:02 nkhil

editor.codeActionsOnSave shows as Unknown configuration setting for me

geoidesic avatar Aug 29 '22 20:08 geoidesic

@geoidesic Did you place that under ./vscode/settings.json or in your .eslintrc.json by accident? Only other thing I can think of is maybe an outdated VS Code version.

AleksandrHovhannisyan avatar Aug 29 '22 22:08 AleksandrHovhannisyan

Thank you!

douglashinckel avatar Oct 24 '22 22:10 douglashinckel

@geoidesic Did you place that under ./vscode/settings.json or in your .eslintrc.json by accident? Only other thing I can think of is maybe an outdated VS Code version.

In ./vscode/settings.json. VsCode version is Version: 1.72.2

geoidesic avatar Oct 25 '22 08:10 geoidesic

I always forget how to get Prettier, ESLint and Typescript working together nicely when starting a new project. This was well written and easy to follow. Cheers :)

bradley-varol avatar Nov 21 '22 16:11 bradley-varol