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

Fix: Support prettier v1.6.0 config (#46) #55

Merged
merged 1 commit into from
Sep 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not too sure about this but it proved necessary when using vscode, otherwise changes to the .prettierrc were not getting picked up.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes ESLint a lot slower. I have a partial fix here, but it would be good to figure out a better way to do this: #303

}

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