Skip to content

Commit

Permalink
Update: Support .prettierrc config files (fixes #46) (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
kombucha authored and not-an-aardvark committed Sep 18, 2017
1 parent 95f0808 commit bc89153
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 13 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ Then, in your `.eslintrc.json`:
## Options

* The first option:

- Objects are passed directly to Prettier as [options](https://github.com/prettier/prettier#api). Example:
- Objects are passed directly to Prettier as [options](https://github.com/prettier/prettier#options). Example:

```json
"prettier/prettier": ["error", {"singleQuote": true, "parser": "flow"}]
Expand All @@ -78,6 +77,7 @@ Then, in your `.eslintrc.json`:
"parser": "flow"
}]
```
NB: This option will merge and override any config set with `.prettierrc` files (for Prettier < 1.7.0, [config files are ignored](https://github.com/prettier/eslint-plugin-prettier/issues/46))

* The second option:

Expand Down
24 changes: 19 additions & 5 deletions eslint-plugin-prettier.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,11 +306,6 @@ module.exports = {
]
},
create(context) {
const prettierOptions =
context.options[0] === 'fb'
? FB_PRETTIER_OPTIONS
: context.options[0];

const pragma = context.options[1]
? context.options[1].slice(1) // Remove leading @
: null;
Expand Down Expand Up @@ -342,12 +337,31 @@ module.exports = {
}
}

if (prettier) {
prettier.clearConfigCache();
}

return {
Program() {
if (!prettier) {
// Prettier is expensive to load, so only load it if needed.
prettier = require('prettier');
}

const eslintPrettierOptions =
context.options[0] === 'fb'
? FB_PRETTIER_OPTIONS
: context.options[0];
const prettierRcOptions =
prettier.resolveConfig && prettier.resolveConfig.sync
? prettier.resolveConfig.sync(context.getFilename())
: null;
const prettierOptions = Object.assign(
{},
prettierRcOptions,
eslintPrettierOptions
);

const prettierSource = prettier.format(source, prettierOptions);
if (source !== prettierSource) {
const differences = generateDifferences(source, prettierSource);
Expand Down
25 changes: 24 additions & 1 deletion test/prettier.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,21 @@ ruleTester.run('prettier', rule, {
// Facebook style with pragma.
{ code: `/** @format */\n('');\n`, options: ['fb', '@format'] },
// Shebang with pragma.
{ code: `#!/bin/node\n/** @format */\n"";\n`, options: [null, '@format'] }
{ code: `#!/bin/node\n/** @format */\n"";\n`, options: [null, '@format'] },
// Single quote from .prettierrc.
{ code: `'';\n`, filename: getPrettierRcJsFilename('single-quote') },
// Override .prettierrc from object option.
{
code: `var foo = {bar: 0};\n`,
filename: getPrettierRcJsFilename('bracket-spacing'),
options: [{ bracketSpacing: false }]
},
// Override .prettierrc from facebook option.
{
code: `('');\n`,
filename: getPrettierRcJsFilename('double-quote'),
options: ['fb']
}
],
invalid: [
'01',
Expand Down Expand Up @@ -120,3 +134,12 @@ function loadInvalidFixture(name) {
};
return item;
}

/**
* Builds a dummy javascript file path to trick prettier into resolving a specific .prettierrc file.
* @param {string} name - Prettierrc fixture basename.
* @returns {string} A javascript filename relative to the .prettierrc config.
*/
function getPrettierRcJsFilename(name) {
return path.resolve(__dirname, `./prettierrc/${name}/dummy.js`);
}
3 changes: 3 additions & 0 deletions test/prettierrc/bracket-spacing/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"bracketSpacing": true
}
3 changes: 3 additions & 0 deletions test/prettierrc/double-quote/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"singleQuote": false
}
3 changes: 3 additions & 0 deletions test/prettierrc/single-quote/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"singleQuote": true
}
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -556,9 +556,9 @@ isarray@^1.0.0, isarray@~1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/isarray/-/isarray-1.0.0.tgz#bb935d48582cba168c06834957a54a3e07124f11"

jest-docblock@^20.0.1:
version "20.0.3"
resolved "https://registry.yarnpkg.com/jest-docblock/-/jest-docblock-20.0.3.tgz#17bea984342cc33d83c50fbe1545ea0efaa44712"
jest-docblock@^21.0.0:
version "21.1.0"
resolved "https://registry.yarnpkg.com/jest-docblock/-/jest-docblock-21.1.0.tgz#43154be2441fb91403e36bb35cb791a5017cea81"

js-tokens@^3.0.0:
version "3.0.1"
Expand Down Expand Up @@ -763,8 +763,8 @@ prelude-ls@~1.1.2:
resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.1.2.tgz#21932a549f5e52ffd9a827f570e04be62a97da54"

prettier@^1.6.1:
version "1.6.1"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.6.1.tgz#850f411a3116226193e32ea5acfc21c0f9a76d7d"
version "1.7.0"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.7.0.tgz#47481588f41f7c90f63938feb202ac82554e7150"

process-nextick-args@~1.0.6:
version "1.0.7"
Expand Down

0 comments on commit bc89153

Please sign in to comment.