Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate prettier #336

Closed
char0n opened this issue Feb 4, 2018 · 6 comments · Fixed by #360
Closed

Integrate prettier #336

char0n opened this issue Feb 4, 2018 · 6 comments · Fixed by #360
Assignees
Milestone

Comments

@char0n
Copy link
Owner

char0n commented Feb 4, 2018

https://github.com/prettier/prettier

@char0n
Copy link
Owner Author

char0n commented Feb 4, 2018

@Undistraction wrote:

@char0n WIth prettier you have to just accept that there are very few config options. That is actually the best thing about it, but if you are used to finessing how you like things to look it will be a hard at first. I honestly think it is the single biggest time-saver you can use in writing JS. So much less distraction from silly little decisions about formatting. It's very fast too.

@char0n
Copy link
Owner Author

char0n commented Feb 4, 2018

@Undistraction what is the workflow ? Do I understand correctly that you don't need linting if you use it ? I've also seen using eslint + prettier working together in some projects.

@Undistraction
Copy link
Collaborator

Undistraction commented Feb 4, 2018

It's not a linter, it's a code formatter. It works by turning the code into an AST, then rendering it. This means its really consistent - it isn't trying to change elements of the code, but always runs a full transform on everything. ESlint is actually both a linter and a formatter (if you use its autofix). So you want to use Prettier for formatting and ESlint for linting. You can do this just by disabling the autofixing for Eslint.

I use VSCode and have it setup to run prettier every time I save a file.

My prettierrc:

{
  "printWidth": 80,
  "tabWidth": 2,
  "semi": false,
  "singleQuote": true,
  "trailingComma": "es5",
  "overrides": [
    {
      "files": ".prettierrc",
      "options": { "parser": "json" }
    },
    {
      "files": ".babelrc",
      "options": { "parser": "json" }
    }
  ]
}

You might need to tweak a few eslint rules to get them to play nicely (So prettier isn't doing things that eslint doesn't like. Here is my .eslint.js. Note that it extends the prettier config and uses the prettier plugin.

module.exports = {
  extends: [
    'eslint-config-airbnb-base',
    'prettier',
    'plugin:ramda/recommended',
  ],
  plugins: ['prettier', 'eslint-plugin-ramda'],
  env: {
    browser: true,
    jest: true,
  },
  parserOptions: {
    sourceType: 'module',
    ecmaFeatures: {
      jsx: true,
    },
  },
  globals: {
    l: true,
  },
  rules: {
    'func-names': ['error', 'never'],
    'no-param-reassign': 'off',
    'import/no-extraneous-dependencies': 'off',
    'no-confusing-arrow': 'off',
    quotes: [
      'error',
      'backtick',
      { avoidEscape: true, allowTemplateLiterals: true },
    ],
    'jsx-quotes': ['error', 'prefer-double'],
    'comma-dangle': ['error', 'always-multiline'],
    'valid-jsdoc': ['error'],
    'no-restricted-syntax': ['error', 'LabeledStatement', 'WithStatement'],
    'import/extensions': ['off', 'never'],
    'no-unused-vars': [
      'error',
      {
        argsIgnorePattern: '^_$',
      },
    ],
    'ramda/always-simplification': ['error'],
    'ramda/compose-simplification': ['error'],
    'ramda/eq-by-simplification': ['error'],
    'ramda/prefer-complement': ['error'],
  },
};

They are rock solid and work great together.

You can confidently drop semi-colons with prettier and it will add them in only when needed. I have also found switching to using backticks for all strings is a huge timesaver so I don't need to change the delimiter if I need to insert a value or use single quotes.

I'd be happy to get this setup, but it's probably worth you having a run at it yourself just to get your head round it.

@char0n
Copy link
Owner Author

char0n commented Feb 4, 2018

Feel free to issue a PR and I'll play with it in the PR. I am planning to give it a go on Oracle project I'm currently working on to bring consistency into codebase.

Regarding using template literals all the time, I had a debate on it with some guys in the past, I still prefer to visually distinguish between strings and dynamic template literals but I guess that only matter of subjective taste.

@Undistraction
Copy link
Collaborator

I think it's just one of those things that feels wrong until you do it and after a day you don't give it another thought. For me the time saving alone is a big win. It really adds up.

@Undistraction Undistraction self-assigned this Feb 10, 2018
@Undistraction Undistraction modified the milestones: v2.5.0, 2.6.0 Feb 14, 2018
char0n pushed a commit that referenced this issue Feb 18, 2018
@char0n
Copy link
Owner Author

char0n commented Feb 18, 2018

Very nice job! Added 2 codeclime issues as exceptions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants