Skip to content

Commit

Permalink
feat: Support inferring config from eslint-plugin-prettier
Browse files Browse the repository at this point in the history
If the eslint rule `prettier/prettier` is configured with an object we use those options for config inference and stop there.
  • Loading branch information
zimme committed Dec 22, 2017
1 parent a10ebd6 commit 5bd3684
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
9 changes: 9 additions & 0 deletions src/__tests__/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,15 @@ const getPrettierOptionsFromESLintRulesTests = [
},
options: { arrowParens: "avoid" }
},
{
rules: {
"prettier/prettier": [2, { singleQuote: false }],
quotes: [2, "single"]
},
options: {
singleQuote: false
}
},

// If an ESLint rule is disabled fall back to prettier defaults.
{ rules: { "max-len": [0, { code: 120 }] }, options: {} },
Expand Down
16 changes: 11 additions & 5 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import getLogger from "loglevel-colored-level-prefix";
const logger = getLogger({ prefix: "prettier-eslint" });
const RULE_DISABLED = {};
const RULE_NOT_CONFIGURED = "RULE_NOT_CONFIGURED";
const ruleValueExists = prettierRuleValue =>
prettierRuleValue !== RULE_NOT_CONFIGURED &&
prettierRuleValue !== RULE_DISABLED &&
typeof prettierRuleValue !== "undefined";
const OPTION_GETTERS = {
printWidth: {
ruleValue: rules => getRuleValue(rules, "max-len", "code"),
Expand Down Expand Up @@ -131,6 +135,12 @@ function getPrettierOptionsFromESLintRules(
) {
const { rules } = eslintConfig;

const prettierPluginOptions = getRuleValue(rules, "prettier/prettier", []);

if (ruleValueExists(prettierPluginOptions)) {
prettierOptions = { ...prettierPluginOptions, ...prettierOptions };
}

return Object.keys(OPTION_GETTERS).reduce(
(options, key) =>
configureOptions(
Expand Down Expand Up @@ -364,11 +374,7 @@ function isAlways(val) {
}

function makePrettierOption(prettierRuleName, prettierRuleValue, fallbacks) {
if (
prettierRuleValue !== RULE_NOT_CONFIGURED &&
prettierRuleValue !== RULE_DISABLED &&
typeof prettierRuleValue !== "undefined"
) {
if (ruleValueExists(prettierRuleValue)) {
return prettierRuleValue;
}

Expand Down

0 comments on commit 5bd3684

Please sign in to comment.