Skip to content

Commit bc89153

Browse files
kombuchanot-an-aardvark
authored andcommitted
Update: Support .prettierrc config files (fixes #46) (#55)
1 parent 95f0808 commit bc89153

File tree

7 files changed

+59
-13
lines changed

7 files changed

+59
-13
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ Then, in your `.eslintrc.json`:
5454
## Options
5555

5656
* The first option:
57-
58-
- Objects are passed directly to Prettier as [options](https://github.com/prettier/prettier#api). Example:
57+
- Objects are passed directly to Prettier as [options](https://github.com/prettier/prettier#options). Example:
5958

6059
```json
6160
"prettier/prettier": ["error", {"singleQuote": true, "parser": "flow"}]
@@ -78,6 +77,7 @@ Then, in your `.eslintrc.json`:
7877
"parser": "flow"
7978
}]
8079
```
80+
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))
8181

8282
* The second option:
8383

eslint-plugin-prettier.js

+19-5
Original file line numberDiff line numberDiff line change
@@ -306,11 +306,6 @@ module.exports = {
306306
]
307307
},
308308
create(context) {
309-
const prettierOptions =
310-
context.options[0] === 'fb'
311-
? FB_PRETTIER_OPTIONS
312-
: context.options[0];
313-
314309
const pragma = context.options[1]
315310
? context.options[1].slice(1) // Remove leading @
316311
: null;
@@ -342,12 +337,31 @@ module.exports = {
342337
}
343338
}
344339

340+
if (prettier) {
341+
prettier.clearConfigCache();
342+
}
343+
345344
return {
346345
Program() {
347346
if (!prettier) {
348347
// Prettier is expensive to load, so only load it if needed.
349348
prettier = require('prettier');
350349
}
350+
351+
const eslintPrettierOptions =
352+
context.options[0] === 'fb'
353+
? FB_PRETTIER_OPTIONS
354+
: context.options[0];
355+
const prettierRcOptions =
356+
prettier.resolveConfig && prettier.resolveConfig.sync
357+
? prettier.resolveConfig.sync(context.getFilename())
358+
: null;
359+
const prettierOptions = Object.assign(
360+
{},
361+
prettierRcOptions,
362+
eslintPrettierOptions
363+
);
364+
351365
const prettierSource = prettier.format(source, prettierOptions);
352366
if (source !== prettierSource) {
353367
const differences = generateDifferences(source, prettierSource);

test/prettier.js

+24-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,21 @@ ruleTester.run('prettier', rule, {
4040
// Facebook style with pragma.
4141
{ code: `/** @format */\n('');\n`, options: ['fb', '@format'] },
4242
// Shebang with pragma.
43-
{ code: `#!/bin/node\n/** @format */\n"";\n`, options: [null, '@format'] }
43+
{ code: `#!/bin/node\n/** @format */\n"";\n`, options: [null, '@format'] },
44+
// Single quote from .prettierrc.
45+
{ code: `'';\n`, filename: getPrettierRcJsFilename('single-quote') },
46+
// Override .prettierrc from object option.
47+
{
48+
code: `var foo = {bar: 0};\n`,
49+
filename: getPrettierRcJsFilename('bracket-spacing'),
50+
options: [{ bracketSpacing: false }]
51+
},
52+
// Override .prettierrc from facebook option.
53+
{
54+
code: `('');\n`,
55+
filename: getPrettierRcJsFilename('double-quote'),
56+
options: ['fb']
57+
}
4458
],
4559
invalid: [
4660
'01',
@@ -120,3 +134,12 @@ function loadInvalidFixture(name) {
120134
};
121135
return item;
122136
}
137+
138+
/**
139+
* Builds a dummy javascript file path to trick prettier into resolving a specific .prettierrc file.
140+
* @param {string} name - Prettierrc fixture basename.
141+
* @returns {string} A javascript filename relative to the .prettierrc config.
142+
*/
143+
function getPrettierRcJsFilename(name) {
144+
return path.resolve(__dirname, `./prettierrc/${name}/dummy.js`);
145+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"bracketSpacing": true
3+
}
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"singleQuote": false
3+
}
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"singleQuote": true
3+
}

yarn.lock

+5-5
Original file line numberDiff line numberDiff line change
@@ -556,9 +556,9 @@ isarray@^1.0.0, isarray@~1.0.0:
556556
version "1.0.0"
557557
resolved "https://registry.yarnpkg.com/isarray/-/isarray-1.0.0.tgz#bb935d48582cba168c06834957a54a3e07124f11"
558558

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

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

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

769769
process-nextick-args@~1.0.6:
770770
version "1.0.7"

0 commit comments

Comments
 (0)