Skip to content

Commit

Permalink
feat: infer jsxBracketSameLine from eslint config
Browse files Browse the repository at this point in the history
I've added support for inferring the prettier jsxBracketSameLine option. Since the prettier option does not apply to self closing tags, I'm checking the "nonEmpty" property of the configuration.

Closes #151
  • Loading branch information
alexmckenley authored and zimme committed Dec 12, 2017
1 parent 3316f12 commit 59ef52b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
19 changes: 18 additions & 1 deletion src/__tests__/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,22 @@ const getPrettierOptionsFromESLintRulesTests = [
options: { jsxBracketSameLine: true },
fallbackPrettierOptions: { jsxBracketSameLine: true }
},
{
rules: { "react/jsx-closing-bracket-location": [2, "after-props"] },
options: { jsxBracketSameLine: true }
},
{
rules: { "react/jsx-closing-bracket-location": [2, "tag-aligned"] },
options: { jsxBracketSameLine: false }
},
{
rules: {
"react/jsx-closing-bracket-location": [2, {
nonEmpty: "after-props"
}]
},
options: { jsxBracketSameLine: true }
},

// If an ESLint rule is disabled fall back to prettier defaults.
{ rules: { "max-len": [0, { code: 120 }] }, options: {} },
Expand All @@ -132,7 +148,8 @@ const getPrettierOptionsFromESLintRulesTests = [
{ rules: { indent: ["warn", 2] }, options: { tabWidth: 2 } },
{ rules: { indent: ["warn", 4] }, options: { tabWidth: 4 } },
{ rules: { indent: ["error", "tab"] }, options: { useTabs: true } },
{ rules: { indent: [2, "tab"] }, options: { useTabs: true } }
{ rules: { indent: [2, "tab"] }, options: { useTabs: true } },
{ rules: { "react/jsx-closing-bracket-location": [0] }, options: {} }
];

getPrettierOptionsFromESLintRulesTests.forEach(
Expand Down
19 changes: 16 additions & 3 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ const OPTION_GETTERS = {
ruleValueToPrettierOption: getUseTabs
},
jsxBracketSameLine: {
ruleValue: () => RULE_NOT_CONFIGURED,
ruleValue: rules =>
getRuleValue(rules, "react/jsx-closing-bracket-location", "nonEmpty"),
ruleValueToPrettierOption: getJsxBracketSameLine
}
};
Expand Down Expand Up @@ -271,8 +272,20 @@ function getUseTabs(eslintValue, fallbacks) {
}

function getJsxBracketSameLine(eslintValue, fallbacks) {
// TODO: add support for setting jsxBracketSameLine based on eslint config
return makePrettierOption("jsxBracketSameLine", eslintValue, fallbacks);
let prettierValue;

if (eslintValue === "after-props") {
prettierValue = true;
} else if (
eslintValue === "tag-aligned" ||
eslintValue === "line-aligned" ||
eslintValue === "props-aligned"
) {
prettierValue = false;
} else {
prettierValue = eslintValue;
}
return makePrettierOption("jsxBracketSameLine", prettierValue, fallbacks);
}

function extractRuleValue(objPath, name, value) {
Expand Down

0 comments on commit 59ef52b

Please sign in to comment.