Skip to content

Commit 2498341

Browse files
authored
fix: tabWidth
* Prefer eslint's max-len.tabWidth value over indent value when setting prettier's tabWidth property. Fixes #138 * Add test for #138 * This check is redundant now that max-len.tabWidth is also passed to getTabWidth
1 parent d2c74d2 commit 2498341

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

src/__tests__/utils.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,26 @@ test('if fallbacks are provided, those are used if not found in eslint', () => {
173173
expect(prettier).toMatchObject({singleQuote: false})
174174
})
175175

176+
test('eslint max-len.tabWidth value should be used for tabWidth when tabs are used', () => {
177+
const {prettier} = getOptionsForFormatting({rules: {
178+
indent: ['error', 'tab'],
179+
'max-len': [2, {
180+
tabWidth: 4,
181+
}],
182+
}})
183+
184+
expect(prettier).toMatchObject({
185+
printWidth: 80,
186+
tabWidth: 4,
187+
parser: 'babylon',
188+
singleQuote: false,
189+
trailingComma: 'none',
190+
bracketSpacing: true,
191+
semi: true,
192+
useTabs: true,
193+
})
194+
})
195+
176196
test('eslint config has only necessary properties', () => {
177197
const {eslint} = getOptionsForFormatting({
178198
globals: {window: false},

src/utils.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@ const OPTION_GETTERS = {
1111
ruleValueToPrettierOption: getPrintWidth,
1212
},
1313
tabWidth: {
14-
ruleValue: rules => getRuleValue(rules, 'indent'),
14+
ruleValue: rules => {
15+
let value = getRuleValue(rules, 'indent')
16+
if (value === 'tab') {
17+
value = getRuleValue(rules, 'max-len', 'tabWidth')
18+
}
19+
return value
20+
},
1521
ruleValueToPrettierOption: getTabWidth,
1622
},
1723
parser: {
@@ -158,10 +164,7 @@ function getPrintWidth(eslintValue, fallbacks) {
158164
}
159165

160166
function getTabWidth(eslintValue, fallbacks) {
161-
// if it's set to tabs, then the tabWidth value doesn't matter
162-
const prettierValue = eslintValue === 'tab' ? RULE_DISABLED : eslintValue
163-
164-
return makePrettierOption('tabWidth', prettierValue, fallbacks, 2)
167+
return makePrettierOption('tabWidth', eslintValue, fallbacks, 2)
165168
}
166169

167170
function getParser(eslintValue, fallbacks) {

0 commit comments

Comments
 (0)