Code linting and formatting with prettier

AI-generated summary

In a discussion about code linting and formatting with Prettier in the Discourse project, pmusaraj shares their experience fixing Travis CI failures by installing ESLint and Prettier locally via yarn install, then running specific ESLint commands to identify and fix code style issues (e.g., unused variables). They also used Prettier’s --list-different to detect formatting problems and --write to auto-fix them. pfaffman and sam note that Rubocop is relevant for Ruby linting but distinct from JavaScript tools. tgxworld recommends IDE plugins like prettier-atom and linter-eslint for real-time feedback and auto-fixing on save. gerhard suggests using overcommit to run linters (including Rubocop and ESLint) automatically before commits, though zogstrip points out it doesn’t support Prettier by default — gerhard notes a possible plugin exists but prefers IDE-based Prettier integration. The thread highlights both manual and automated approaches to enforcing code style in Discourse, with links to relevant guides and tools.

I just wrote a PR that didn’t pass the Travis tests due to linting issues. I couldn’t find any entries in meta that discussed eslinting/prettier, so I went digging around the code and found out how to improve my code to fit with the styling and listing standards of Discourse. This may be useful for others, so here’s a log of what I did:

First, to install ESLint and prettier locally. I ran:

yarn install

and that installed all the dev dependencies listed in package.json. Depending on which part of Discourse you’re working on, you may need to run one of these commands to check for issues:

yarn eslint --ext .es6 app/assets/javascripts
yarn eslint --ext .es6 test/javascripts
yarn eslint --ext .es6 plugins/**/assets/javascripts
yarn eslint --ext .es6 plugins/**/test/javascripts
yarn eslint app/assets/javascripts test/javascripts

I was working on an improvement to core, so the first line above found my issues. When ESLint finds an error, the output is fairly clear, for example I got something like this:

.../discourse/app/assets/javascripts/discourse/models/user-stream.js.es6
  5:8  error  'UserActionGroup' is defined but never used  no-unused-vars

I fixed the issue by removing the unused variable, and then ESLint passed on a second try.

Prettier is similar. In lib/tasks/docker.rake, I found the following command:

yarn prettier --list-different "app/assets/stylesheets/**/*.scss" "app/assets/javascripts/**/*.es6" "test/javascripts/**/*.es6"

This showed me the filenames for which prettier found formatting offenses. To fix them, I ran:

yarn prettier [filename] --write

After running that command for the 2-3 files that had issues, I committed, pushed, and waited for Travis to pass (thankfully, it did!).


For Ruby linting, see Rubocop has landed on Discourse 👮‍♀️ 👮

For a more automated way to integrate ESLint and Prettier with your edit, see

I think you’re looking for Rubocop has landed on Discourse 👮‍♀️ 👮.

I personally use Sunsetting Atom - The GitHub Blog which automatically fix the errors on save and Sunsetting Atom - The GitHub Blog which provides JIT notifications on whether I broke any eslint rules.

Rubocop is related but not the same thing, we should definitely add nice links to the op pointing at each other

I recommend installing overcommit which adds hooks to your git repository and runs the linters (Rubocop and EsLint) on changed files before each commit.

It doesn’t run prettier though :frowning:

It could be added: https://github.com/juriewessels/overcommit-prettier
But I haven’t looked into it, because my IDE is running prettier. :wink: