Skip to content

gcichosz/code-style-enforcement

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

16 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Code style enforcement

Table of contents

Benefits

Benefits of enforcing code style include:

  • Smoother, easier, and deeper code reviews
  • Less time debating nits
  • Better developer experience
  • Less time doing mundane tasks, more time doing real developer "stuff"
  • Lower entry threshold
  • Less bugs caused by common mistakes
  • Higher code quality and confidence

Prettier

Prettier is an opinionated code formatter.

Why?

  • To format your code
  • To stop code style debates
  • To make code reviews about code, not code style and nits
  • Many more, read here

Installation

npm install --save-dev prettier

Configuration

Prettier configuration file reference.

See ./.prettierrc.js for example configuration, but it could be as simple as:

module.exports = {};

Ignoring files

Use .prettierignore to ignore files and/or directories. See ./.prettierignore for an example.

Use Prettier's CLI --ignore-unknown option to ignore files with extensions Prettier has no parser for, e.g.: npx prettier --check --ignore-unknown .

Usage

To check code style issues in all files run npx prettier --check .. To fix code style issues in all files run npx prettier --write ..

ESLint

ESLint is a static code analyzer for JavaScript, and by extension for TypeScript.

Why?

  • To quickly find common problems in code

Installation

In a TypeScript codebase, alongside Prettier:

npm install --save-dev eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin eslint-config-prettier eslint-plugin-prettier

In a JavaScript codebase, alongside Prettier:

npm install --save-dev eslint @babel/core @babel/eslint-parser @babel/preset-env eslint-config-prettier eslint-plugin-prettier

Configuration

ESLint configuration reference.

See ./.eslintrc.js for example configuration for TypeScript codebase with Prettier. For JavaScript codebase with Babel and Prettier it would be something like:

module.exports = {
  parser: "@babel/eslint-parser",

  env: {
    node: true,
  },

  extends: ["eslint:recommended", "plugin:prettier/recommended"],
};

Ignoring files

Use .eslintignore to ignore files and/or directories. See ./.eslintignore for an example.

Usage

To check code problems in all files run npx eslint .. To fix code problems in all files run npx eslint --fix ..

GitHub Actions

GitHub Actions is only an example of a CI/CD tool. There are many, many more, both paid and free. Here are a few examples:

Why?

  • To automate code inspection, builds, and tests before adding it to a codebase
  • To automate deployments

Installation

GitHub Actions are available out-of-the box in, drum roll, GitHub repositories.

Configuration

GitHub Actions workflow reference.

See ./.github/workflows/continuous-inspection.yaml for example continuous inspection workflow.

Husky

Husky is a Git hooks tool.

Why?

  • To inspect the code before it reaches the remote
  • To shorten the feedback loop regarding code changes

Installation

npx husky-init && npm install

Configuration

How to create a hook.

See ./.husky/pre-commit for an example hook.

Bypassing hooks

There is a way, but please, don't do this ๐Ÿ™

Usage

Just run Git commands which hooks you subscribed to and Husky will run automatically ๐Ÿถ woof!

lint-staged

lint-staged is a tool to run commands on Git staged files only.

Why?

  • To inspect only the code relevant to current change
  • To shorten the feedback loop regarding code changes even more

Installation

npm install --save-dev lint-staged

Configuration

Lint-staged configuration reference.

See ./lint-staged.config.js for example configuration.

Subscribe lint-staged to a Git hook for maximum effect. For example using Husky

Ignoring files

Files lint-stages runs on are configured using glob patterns in lint-staged configuration file, so ignore files simply by not include them in lint-staged configuration.

Usage

It's possible to run lint-staged using npx lint-staged, but the power of lint-staged comes from running it on Git hooks. Therefore, subscribe lint-staged to Git hooks, using Husky for example, and just run Git commands.

Branch protection

Branch protection is a practice of guarding branches from pushes, if set rules aren't followed, on a repository level. Different Git hosting services have their own implementation:

Why?

  • To make sure the code that doesn't meet set standards ends up in the main branches

Installation

Provided by your Git hosting service.

Configuration

Configuration depends on your Git hosting service, but the things to look for are:

  • protect the main branch at least
  • require pull requests before merging
  • require pull request approvals
  • require status checks to pass before merging

Usage

Push your branch, create a pull request to main branch, and see the magic happen ๐Ÿง™โ€โ™€๏ธ

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published