All Projects → sambacha → prettier-config-solidity

sambacha / prettier-config-solidity

Licence: Apache-2.0 license
Prettier config optimized to reduce AST churn & conform to solidity spec

Programming Languages

solidity
1140 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to prettier-config-solidity

Eslint Config Kit
A collection of useful eslint configs
Stars: ✭ 39 (+39.29%)
Mutual labels:  config, prettier
eslint-config-hardcore
The most strict (yet practical) ESLint config. 34 plugins. 1047 rules.
Stars: ✭ 168 (+500%)
Mutual labels:  config, prettier
prettier-init
CLI for creating .prettierrc files
Stars: ✭ 34 (+21.43%)
Mutual labels:  prettier, prettier-config
eslint-config-ns
ESLint config ready to be used in multiple projects. Based on Airbnb's code style with prettier, jest and react support.
Stars: ✭ 27 (-3.57%)
Mutual labels:  prettier, prettier-config
promotion-web
基于React: v18.x.x/Webpack: v5.x.x/React Router v6.x.x/ Antd: v5..x.x/Fetch Api/ Typescript: v4.x.x 等最新版本进行构建...
Stars: ✭ 374 (+1235.71%)
Mutual labels:  prettier
mev-inspect-rs
Discover historic Miner Extractable Value (MEV) opportunities
Stars: ✭ 443 (+1482.14%)
Mutual labels:  defi
Dilettantes-Guide-to-Linting
Setting up ESLint, Prettier, VS Code, and the AirBnB style guide in beautiful harmony.
Stars: ✭ 18 (-35.71%)
Mutual labels:  prettier
webpack-boilerplate
Webpack 4 boilerplate (babel, eslint, prettier, jest, sass, postcss, hmr, browsersync)
Stars: ✭ 33 (+17.86%)
Mutual labels:  prettier
dotfiles
My dotfiles based on Makefile
Stars: ✭ 150 (+435.71%)
Mutual labels:  config
node-config
nodejs的配置中心
Stars: ✭ 53 (+89.29%)
Mutual labels:  config
profig
A straightforward configuration library for Python.
Stars: ✭ 26 (-7.14%)
Mutual labels:  config
dotfiles
My collection of dotfiles
Stars: ✭ 77 (+175%)
Mutual labels:  config
hb-config
hb-config: easy to configure your python project especially Deep Learning experiments
Stars: ✭ 21 (-25%)
Mutual labels:  config
saddle-contract
The smart contracts behind saddle.finance 🤠
Stars: ✭ 121 (+332.14%)
Mutual labels:  defi
dotfiles
My personal configuration and bootstrap files
Stars: ✭ 14 (-50%)
Mutual labels:  config
octofi-app-aquafarm
OctoFi - Track your DeFi portfolio, find new investment opportunities, buy and sell directly, and wrap your tentacles around a sea of gains. Hosted on Github Pages.
Stars: ✭ 36 (+28.57%)
Mutual labels:  defi
dotfiles
The config that makes me a productivity whiz 🧙‍♂️
Stars: ✭ 36 (+28.57%)
Mutual labels:  config
the-art-of-unit-testing
Repository that contains code in Node.js from the book The Art of Unit Testing, Second Edition by Roy Osherove
Stars: ✭ 22 (-21.43%)
Mutual labels:  prettier
api-with-express
🌈 API with Express
Stars: ✭ 25 (-10.71%)
Mutual labels:  prettier
nim config
Global project-agnostic config.nims
Stars: ✭ 54 (+92.86%)
Mutual labels:  config


prettier-config-solidity

a conformant prettier configuration for Solidity

nodejs codecov

Quickstart

See the dappspec format for additional style guide directives

Prettier & Plugin+Config

Warning A Major Semver release for prettier is upcoming, ensure you are using compatible versions

npm i -D prettier@^2 prettier-plugin-solidity@latest prettier-config-solidity@latest  --save-exact

create prettier.config.js file

'use strict';
const prettierConfig = require('prettier-config-solidity');
module.exports = prettierConfig;

Editorconfig

# Stop the editor from looking for .editorconfig files in the parent directories
# root = true

[*]
# Non-configurable Prettier behaviors
charset = utf-8
insert_final_newline = true
# Caveat: Prettier won’t trim trailing whitespace inside template strings, but your editor might.
# trim_trailing_whitespace = true

# Configurable Prettier behaviors
# (change these if your Prettier config differs)
end_of_line = lf
indent_style = space
indent_size = 2
max_line_length = 80

# Solidity
# https://github.com/sambacha/prettier-config-solidity
[*.sol]
indent_size = 4
indent_style = space

# Ignore fixtures and vendored files
[{dist,artifacts,vendor,test,fixtures,tests,cache,__snapshot__,}/**]
charset = unset
end_of_line = unset
indent_size = unset
indent_style = unset
insert_final_newline = unset
trim_trailing_spaces = unset

Style Convention

Example:

  • Changes unit to unit256

unformatted

    function swapExactETHForTokens(uint amountOutMin, address[] calldata path, address to, uint deadline)
        external
        override
        payable
        ensure(deadline)
        returns (uint[] memory amounts)
    {

formatted

    function swapExactETHForTokens(
        uint256 amountOutMin,
        address[] calldata path,
        address to,
        uint256 deadline
    ) external payable override ensure(deadline) returns (uint256[] memory amounts) {
        require(path[0] == WETH, "UniswapV2Router: INVALID_PATH");

Overview

Prettier configuration for Solidity

Motivation

This configuration is optimized to reduce diff churn and improve AST results.

The following rules are employed, with specific reasonings to their choice (source: airbnb style guide):

printWidth

The behavior of printWidth is located here: prettier-solidity/prettier-plugin-solidity/blob/b504261047d0019c924d53a2b9ab0738b1e05703/src/nodes/FunctionDefinition.js#L99

See more here prettier-plugin-solidity/issues/474#issuecomment-823670541

whitespace

source@airbnb/javascript#whitespace--in-braces

  • 19.12 Add spaces inside curly braces. eslint: object-curly-spacing
// bad
const foo = { clark: 'kent' };

// good
const foo = { clark: 'kent' };

arrow-parens

  • 8.4 Always include parentheses around arguments for clarity and consistency. eslint: arrow-parens

source@airbnb/javascript#arrows--one-arg-parens

Why? Minimizes diff churn when adding or removing arguments.

// bad
[1, 2, 3].map((x) => x * x);

// good
[1, 2, 3].map((x) => x * x);

// bad
[1, 2, 3].map(
  (number) =>
    `A long string with the ${number}. It’s so long that we don’t want it to take up space on the .map line!`,
);

// good
[1, 2, 3].map(
  (number) =>
    `A long string with the ${number}. It’s so long that we don’t want it to take up space on the .map line!`,
);

// bad
[1, 2, 3].map((x) => {
  const y = x + 1;
  return x * y;
});

// good
[1, 2, 3].map((x) => {
  const y = x + 1;
  return x * y;
});

one var

  • 13.2 Use one const or let declaration per variable or assignment. eslint: one-var

Why? It’s easier to add new variable declarations this way, and you never have to worry about swapping out a ; for a , or introducing punctuation-only diffs. You can also step through each declaration with the debugger, instead of jumping through all of them at once.

ref:eslint/rules/one-var
// bad
const items = getItems(),
  goSportsTeam = true,
  dragonball = 'z';

// bad
// (compare to above, and try to spot the mistake)
const items = getItems(),
  goSportsTeam = true;
dragonball = 'z';

// good
const items = getItems();
const goSportsTeam = true;
const dragonball = 'z';

20.2 Additional trailing comma: Yup. eslint: comma-dangle

Why? This leads to cleaner git diffs. Also, transpilers like Babel will remove the additional trailing comma in the transpiled code which means you don’t have to worry about the trailing comma problem in legacy browsers.
// bad - git diff without trailing comma
const hero = {
     firstName: 'Florence',
-    lastName: 'Nightingale'
+    lastName: 'Nightingale',
+    inventorOf: ['coxcomb chart', 'modern nursing']
};

// good - git diff with trailing comma
const hero = {
     firstName: 'Florence',
     lastName: 'Nightingale',
+    inventorOf: ['coxcomb chart', 'modern nursing'],
};

8.6 Enforce the location of arrow function bodies with implicit returns. eslint: implicit-arrow-linebreak

// bad
(foo) => bar;

(foo) => bar;

// good
(foo) => bar;
(foo) => bar;
(foo) => bar;

Install

Prettier

npm i -D prettier prettier-plugin-solidity@latest prettier-config-solidity  --save-exact
npm i -D prettier prettier-plugin-solidity@latest prettier-config-solidity  --save-exact --legacy-peer-deps

or

yarn add -D prettier prettier-plugin-solidity@latest prettier-config-solidity  --save-exact

with SolHint

npm install --save-dev solhint solhint-plugin-prettier prettier prettier-plugin-solidity --save-exact
{
  "plugins": ["prettier"],
  "rules": {
    "prettier/prettier": "error"
  }
}

License

SPDX-License-Identifier: Apache-2.0

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].