Skip to content

Commit

Permalink
Merge ec12e18 into 8f35b25
Browse files Browse the repository at this point in the history
  • Loading branch information
kylemh committed Mar 6, 2021
2 parents 8f35b25 + ec12e18 commit badfbd3
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 53 deletions.
77 changes: 49 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# prettier-eslint

Formats your JavaScript using [`prettier`][prettier] followed by [`eslint --fix`][eslint]
Formats your JavaScript using [`prettier`][prettier] followed by [`eslint --fix`][eslint], giving ESLint the final say
over any formatting.

[![Build Status][build-badge]][build]
[![Code Coverage][coverage-badge]][coverage]
Expand All @@ -16,6 +17,13 @@ Formats your JavaScript using [`prettier`][prettier] followed by [`eslint --fix`
[![Star on GitHub][github-star-badge]][github-star]
[![Tweet][twitter-badge]][twitter]

## Summary

We recommend (and Prettier [officially recommends](official-recommendation)) that you leverage
[`eslint-config-prettier`](eslint-config-prettier) to simply disable lint rules which conflict with prettier's main
job: formatting; however, you may want more control over the formatting of your code via ESLint formatting rules. This
library aims to give you that control.

## The problem

The [`fix`][fix] feature of [`eslint`][eslint] is pretty great and can
Expand All @@ -41,18 +49,29 @@ This module is distributed via [npm][npm] which is bundled with [node][node] and
should be installed as one of your project's `devDependencies`:

```
npm install --save-dev prettier-eslint
npm install --save-dev prettier-eslint prettier eslint
```

```
yarn add -D prettier-eslint prettier eslint
```

Our parsing-related dependencies are listed as "peer dependencies" meaning you must have them installed in your project
as well.

If you use TypeScript, you should install `typescript` and `@typescript-eslint/parser` to "devDependencies".

If you use Vue, you should install `vue-eslint-parser` to "devDependencies".

## Usage

### Example

```javascript
const format = require("prettier-eslint");
const format = require('prettier-eslint');

// notice, no semicolon in the original text
const sourceCode = "const {foo} = bar";
const sourceCode = 'const {foo} = bar';

const options = {
text: sourceCode,
Expand All @@ -61,7 +80,7 @@ const options = {
ecmaVersion: 7,
},
rules: {
semi: ["error", "never"],
semi: ['error', 'never'],
},
},
prettierOptions: {
Expand Down Expand Up @@ -319,39 +338,41 @@ Contributions of any kind welcome!

MIT

[prettier]: https://github.com/jlongster/prettier
[eslint]: http://eslint.org/
[fix]: http://eslint.org/docs/user-guide/command-line-interface#fix
[npm]: https://www.npmjs.com/
[node]: https://nodejs.org
[all-contributors]: https://github.com/kentcdodds/all-contributors
[atom-dev-tools]: https://discuss.atom.io/t/how-to-make-developer-tools-appear/16232
[atom-plugin]: https://github.com/prettier/prettier-atom
[build-badge]: https://img.shields.io/travis/prettier/prettier-eslint.svg?style=flat-square
[build]: https://travis-ci.org/prettier/prettier-eslint
[coc-badge]: https://img.shields.io/badge/code%20of-conduct-ff69b4.svg?style=flat-square
[coc]: https://github.com/prettier/prettier-eslint/blob/master/other/CODE_OF_CONDUCT.md
[coverage-badge]: https://img.shields.io/codecov/c/github/prettier/prettier-eslint.svg?style=flat-square
[coverage]: https://codecov.io/github/prettier/prettier-eslint
[version-badge]: https://img.shields.io/npm/v/prettier-eslint.svg?style=flat-square
[package]: https://www.npmjs.com/package/prettier-eslint
[donate-badge]: https://img.shields.io/badge/$-support-green.svg?style=flat-square
[donate]: https://www.paypal.me/zimme
[downloads-badge]: https://img.shields.io/npm/dm/prettier-eslint.svg?style=flat-square
[npm-stat]: http://npm-stat.com/charts.html?package=prettier-eslint&from=2016-04-01
[emojis]: https://github.com/kentcdodds/all-contributors#emoji-key
[eslint-config-prettier]: https://github.com/prettier/eslint-config-prettier
[eslint]: http://eslint.org/
[examples-badge]: https://img.shields.io/badge/%F0%9F%92%A1-examples-8C8E93.svg?style=flat-square
[examples]: https://github.com/prettier/prettier-eslint/blob/master/other/EXAMPLES.md
[fix]: http://eslint.org/docs/user-guide/command-line-interface#fix
[github-star-badge]: https://img.shields.io/github/stars/prettier/prettier-eslint.svg?style=social
[github-star]: https://github.com/prettier/prettier-eslint/stargazers
[github-watch-badge]: https://img.shields.io/github/watchers/prettier/prettier-eslint.svg?style=social
[github-watch]: https://github.com/prettier/prettier-eslint/watchers
[license-badge]: https://img.shields.io/npm/l/prettier-eslint.svg?style=flat-square
[license]: https://github.com/prettier/prettier-eslint/blob/master/other/LICENSE
[node]: https://nodejs.org
[npm-stat]: http://npm-stat.com/charts.html?package=prettier-eslint&from=2016-04-01
[npm]: https://www.npmjs.com/
[official-recommendation]: https://prettier.io/docs/en/integrating-with-linters.html
[package]: https://www.npmjs.com/package/prettier-eslint
[prettier]: https://github.com/prettier/prettier
[prs-badge]: https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square
[prs]: http://makeapullrequest.com
[donate-badge]: https://img.shields.io/badge/$-support-green.svg?style=flat-square
[donate]: https://www.paypal.me/zimme
[coc-badge]: https://img.shields.io/badge/code%20of-conduct-ff69b4.svg?style=flat-square
[coc]: https://github.com/prettier/prettier-eslint/blob/master/other/CODE_OF_CONDUCT.md
[roadmap-badge]: https://img.shields.io/badge/%F0%9F%93%94-roadmap-CD9523.svg?style=flat-square
[roadmap]: https://github.com/prettier/prettier-eslint/blob/master/other/ROADMAP.md
[examples-badge]: https://img.shields.io/badge/%F0%9F%92%A1-examples-8C8E93.svg?style=flat-square
[examples]: https://github.com/prettier/prettier-eslint/blob/master/other/EXAMPLES.md
[github-watch-badge]: https://img.shields.io/github/watchers/prettier/prettier-eslint.svg?style=social
[github-watch]: https://github.com/prettier/prettier-eslint/watchers
[github-star-badge]: https://img.shields.io/github/stars/prettier/prettier-eslint.svg?style=social
[github-star]: https://github.com/prettier/prettier-eslint/stargazers
[twitter]: https://twitter.com/intent/tweet?text=Check%20out%20prettier-eslint!%20https://github.com/prettier/prettier-eslint%20%F0%9F%91%8D
[twitter-badge]: https://img.shields.io/twitter/url/https/github.com/prettier/prettier-eslint.svg?style=social
[emojis]: https://github.com/kentcdodds/all-contributors#emoji-key
[all-contributors]: https://github.com/kentcdodds/all-contributors
[atom-plugin]: https://github.com/prettier/prettier-atom
[atom-dev-tools]: https://discuss.atom.io/t/how-to-make-developer-tools-appear/16232
[twitter]: https://twitter.com/intent/tweet?text=Check%20out%20prettier-eslint!%20https://github.com/prettier/prettier-eslint%20%F0%9F%91%8D
[version-badge]: https://img.shields.io/npm/v/prettier-eslint.svg?style=flat-square
[vscode-plugin]: https://github.com/esbenp/prettier-vscode
36 changes: 28 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,31 @@
"keywords": [],
"author": "Kent C. Dodds <kent@doddsfamily.us> (http://kentcdodds.com/)",
"maintainers": [
"Simon Fridlund <simon@fridlund.email>"
"Simon Fridlund <simon@fridlund.email>",
"Hamza Hamidi <@hamzahamidii>",
"Kyle Holmberg <@kylemh_>"
],
"license": "MIT",
"dependencies": {
"@typescript-eslint/parser": "^3.0.0",
"common-tags": "^1.4.0",
"dlv": "^1.1.0",
"eslint": "^7.9.0",
"indent-string": "^4.0.0",
"lodash.merge": "^4.6.0",
"loglevel-colored-level-prefix": "^1.0.0",
"prettier": "^2.0.0",
"pretty-format": "^23.0.1",
"require-relative": "^0.8.7",
"typescript": "^3.9.3",
"vue-eslint-parser": "~7.1.0"
"require-relative": "^0.8.7"
},
"devDependencies": {
"@babel/cli": "^7.4.4",
"@babel/core": "^7.4.5",
"@babel/preset-env": "^7.4.5",
"@typescript-eslint/parser": "^4.0.0",
"acorn": "^6.1.1",
"ajv": "^6.12.2",
"all-contributors-cli": "^6.7.0",
"babel-jest": "^25.0.0",
"chalk": "^2.1.0",
"eslint": "^7.9.0",
"eslint-config-kentcdodds": "~16.0.1",
"husky": "^2.4.1",
"jest": "^25.0.0",
Expand All @@ -50,7 +49,28 @@
"prettier-eslint-cli": "^5.0.0",
"rimraf": "^2.5.4",
"semantic-release": "^15.13.16",
"strip-indent": "^3.0.0"
"strip-indent": "^3.0.0",
"prettier": "^2.0.0",
"typescript": "^4.0.0",
"vue-eslint-parser": "^7.0.0"
},
"peerDependencies": {
"@typescript-eslint/parser": ">=3.0.0",
"eslint": "^7.9.0",
"prettier": "^2.0.0",
"typescript": ">=3.0.0",
"vue-eslint-parser": "^7.0.0"
},
"peerDependenciesMeta": {
"@typescript-eslint/parser": {
"optional": true
},
"typescript": {
"optional": true
},
"vue-eslint-parser": {
"optional": true
}
},
"engines": {
"node": ">=10.0.0"
Expand Down
64 changes: 47 additions & 17 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
/* eslint complexity: [1, 13] */

// Add node 8 polyfills, to be removed if we decide to let go node 8 support
import 'core-js/modules/es.string.trim-start'; // eslint-disable-line
import 'core-js/modules/es.string.trim-end'; // eslint-disable-line
import 'core-js/modules/es.string.trim-start'; // eslint-disable-line import/no-extraneous-dependencies
import 'core-js/modules/es.string.trim-end'; // eslint-disable-line import/no-extraneous-dependencies

import fs from 'fs';
import path from 'path';
Expand All @@ -16,14 +16,49 @@ import merge from 'lodash.merge';
import {
getESLintCLIEngine,
getOptionsForFormatting,
requireModule
requireModule,
} from './utils';

const logger = getLogger({ prefix: 'prettier-eslint' });

// CommonJS + ES6 modules... is it worth it? Probably not...
module.exports = format;

/**
* @description Allows for a dynamic adjustment of a file's parsing mechanism.
* @param {String} fileExtension
* @param {String} initialParser
*/
function chooseParser(fileExtension, initialParser) {
if (initialParser) return initialParser;

if (['.ts', '.tsx'].includes(fileExtension)) {
const parser = '@typescript-eslint/parser';

try {
return require.resolve(parser);
} catch (error) {
throw new Error(
`When using TypeScript, you must also install \`${parser}\` to "devDependencies".`
);
}
}

if (['.vue'].includes(fileExtension)) {
const parser = 'vue-eslint-parser';

try {
return require.resolve(parser);
} catch (error) {
throw new Error(
`When using Vue, you must also install \`${parser}\` to "devDependencies".`
);
}
}

return initialParser;
}

/**
* Formats the text with prettier and then eslint based on the given options
* @param {String} options.filePath - the path of the file being formatted
Expand Down Expand Up @@ -59,7 +94,7 @@ function format(options) {
eslintPath = getModulePath(filePath, 'eslint'),
prettierPath = getModulePath(filePath, 'prettier'),
prettierLast,
fallbackPrettierOptions
fallbackPrettierOptions,
} = options;

const eslintConfig = merge(
Expand Down Expand Up @@ -98,7 +133,7 @@ function format(options) {
eslintConfig: formattingOptions.eslint,
prettierOptions: formattingOptions.prettier,
logLevel,
prettierLast
prettierLast,
})
);

Expand All @@ -108,8 +143,9 @@ function format(options) {
'.ts',
'.tsx',
'.mjs',
'.vue'
'.vue',
];

const fileExtension = path.extname(filePath || '');

// If we don't get filePath run eslint on text, otherwise only run eslint
Expand All @@ -124,16 +160,10 @@ function format(options) {
return prettify(text);
}

if (['.ts', '.tsx'].includes(fileExtension)) {
formattingOptions.eslint.parser =
formattingOptions.eslint.parser ||
require.resolve('@typescript-eslint/parser');
}

if (['.vue'].includes(fileExtension)) {
formattingOptions.eslint.parser =
formattingOptions.eslint.parser || require.resolve('vue-eslint-parser');
}
formattingOptions.eslint.parser = chooseParser(
fileExtension,
formattingOptions.eslint.parser
);

const eslintFix = createEslintFix(formattingOptions.eslint, eslintPath);

Expand Down Expand Up @@ -247,7 +277,7 @@ function getESLintConfig(filePath, eslintPath) {
);
return {
...eslintOptions,
...config
...config,
};
} catch (error) {
// is this noisy? Try setting options.disableLog to false
Expand Down

0 comments on commit badfbd3

Please sign in to comment.