Giter Club home page Giter Club logo

prettier-config-conformance's Introduction

prettier configuration for optimizing diff/AST's

Quickstart

wget https://raw.githubusercontent.com/sambacha/prettier-config-conformance/master/.prettierrc.js
wget https://raw.githubusercontent.com/sambacha/prettier-config-conformance/master/.prettierrc.json
curl -O https://raw.githubusercontent.com/sambacha/prettier-config-conformance/master/.prettierrc.json

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.

// 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;

Parser Differences

see prettier playground

Example Typescript file

/**
 * @package api.response
 * @function addEventListener
 */
 addEventListener("fetch", event => {
    const data = {
      // @note decimal 1337 converts to hex value 539
      chaiId: "0x539"
    }
  
    const json = JSON.stringify(data, null, 2)
  
    return event.respondWith(
      new Response(json, {
        headers: {
          "content-type": "application/json;charset=UTF-8"
        }
      })
    )
  })

Typescript Compiler

[
    {
      "type": "Block",
      "value": "*\n * @package api.response\n * @function addEventListener\n ",
      "range": [0, 62],
      "loc": {
        "start": { "line": 1, "column": 0 },
        "end": { "line": 4, "column": 3 }
      },
      "placement": "endOfLine",
      "leading": true,
      "trailing": false,
      "nodeDescription": "ExpressionStatement"
    },
    {
      "type": "Line",
      "value": " @note decimal 1337 converts to hex value 539",
      "range": [121, 168],
      "loc": {
        "start": { "line": 7, "column": 4 },
        "end": { "line": 7, "column": 51 }
      },
      "placement": "ownLine",
      "leading": true,
      "trailing": false,
      "nodeDescription": "Property chaiId"
    }
  ]

prettier-config-conformance's People

Contributors

renovate[bot] avatar sambacha avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.