Skip to content

Commit 4e577d2

Browse files
committed
jsx-indent autofixer now supports tabs -> spaces and vice versa
1 parent 99cec3e commit 4e577d2

File tree

3 files changed

+54
-21
lines changed

3 files changed

+54
-21
lines changed

docs/rules/jsx-indent.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
This option validates a specific indentation style for JSX.
44

55
**Fixable:** This rule is automatically fixable using the `--fix` flag on the command line.
6-
Fixer will fix whitespace and tabs indentation. It won't replace tabs with whitespaces and vice-versa.
6+
Fixer will fix whitespace and tabs indentation.
77

88
## Rule Details
99

lib/rules/jsx-indent.js

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ module.exports = {
3939
category: 'Stylistic Issues',
4040
recommended: false
4141
},
42-
// fixer will fix whitespace and tabs indentation.
43-
// It won't replace tabs with whitespaces and vice-versa.
4442
fixable: 'whitespace',
4543
schema: [{
4644
oneOf: [{
@@ -77,20 +75,16 @@ module.exports = {
7775
* Responsible for fixing the indentation issue fix
7876
* @param {ASTNode} node Node violating the indent rule
7977
* @param {Number} needed Expected indentation character count
80-
* @param {Number} gotten Indentation character count in the actual node/code
8178
* @returns {Function} function to be executed by the fixer
8279
* @private
8380
*/
84-
function getFixerFunction(node, needed, gotten) {
85-
if (needed > gotten) {
86-
var spaces = indentChar.repeat(needed - gotten);
87-
return function(fixer) {
88-
return fixer.insertTextBeforeRange([node.range[0], node.range[0]], spaces);
89-
};
90-
}
91-
81+
function getFixerFunction(node, needed) {
9282
return function(fixer) {
93-
return fixer.removeRange([node.range[0] - (gotten - needed), node.range[0]]);
83+
var indent = Array(needed + 1).join(indentChar);
84+
return fixer.replaceTextRange(
85+
[node.start - node.loc.start.column, node.start],
86+
indent
87+
);
9488
};
9589
}
9690

@@ -99,7 +93,7 @@ module.exports = {
9993
* @param {ASTNode} node Node violating the indent rule
10094
* @param {Number} needed Expected indentation character count
10195
* @param {Number} gotten Indentation character count in the actual node/code
102-
* @param {Object=} loc Error line and column location
96+
* @param {Object} loc Error line and column location
10397
*/
10498
function report(node, needed, gotten, loc) {
10599
var msgContext = {
@@ -115,14 +109,14 @@ module.exports = {
115109
loc: loc,
116110
message: MESSAGE,
117111
data: msgContext,
118-
fix: getFixerFunction(node, needed, gotten)
112+
fix: getFixerFunction(node, needed)
119113
});
120114
} else {
121115
context.report({
122116
node: node,
123117
message: MESSAGE,
124118
data: msgContext,
125-
fix: getFixerFunction(node, needed, gotten)
119+
fix: getFixerFunction(node, needed)
126120
});
127121
}
128122
}

tests/lib/rules/jsx-indent.js

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,11 @@ ruleTester.run('jsx-indent', rule, {
233233
' <Foo />',
234234
'</App>'
235235
].join('\n'),
236+
output: [
237+
'<App>',
238+
'\t<Foo />',
239+
'</App>'
240+
].join('\n'),
236241
options: ['tab'],
237242
parserOptions: parserOptions,
238243
errors: [{message: 'Expected indentation of 1 tab character but found 0.'}]
@@ -282,6 +287,15 @@ ruleTester.run('jsx-indent', rule, {
282287
' );',
283288
'}'
284289
].join('\n'),
290+
output: [
291+
'function App() {',
292+
' return (',
293+
' <App>',
294+
' <Foo />',
295+
'</App>',
296+
' );',
297+
'}'
298+
].join('\n'),
285299
options: [2],
286300
parserOptions: parserOptions,
287301
errors: [{message: 'Expected indentation of 4 space characters but found 0.'}]
@@ -291,6 +305,11 @@ ruleTester.run('jsx-indent', rule, {
291305
' {test}',
292306
'</App>'
293307
].join('\n'),
308+
output: [
309+
'<App>',
310+
' {test}',
311+
'</App>'
312+
].join('\n'),
294313
parserOptions: parserOptions,
295314
errors: [
296315
{message: 'Expected indentation of 4 space characters but found 3.'}
@@ -305,6 +324,15 @@ ruleTester.run('jsx-indent', rule, {
305324
' ))}',
306325
'</App>'
307326
].join('\n'),
327+
output: [
328+
'<App>',
329+
' {options.map((option, index) => (',
330+
' <option key={index} value={option.key}>',
331+
' {option.name}',
332+
' </option>',
333+
' ))}',
334+
'</App>'
335+
].join('\n'),
308336
parserOptions: parserOptions,
309337
errors: [
310338
{message: 'Expected indentation of 12 space characters but found 11.'}
@@ -315,6 +343,11 @@ ruleTester.run('jsx-indent', rule, {
315343
'{test}',
316344
'</App>'
317345
].join('\n'),
346+
output: [
347+
'<App>',
348+
'\t{test}',
349+
'</App>'
350+
].join('\n'),
318351
parserOptions: parserOptions,
319352
options: ['tab'],
320353
errors: [
@@ -330,6 +363,15 @@ ruleTester.run('jsx-indent', rule, {
330363
'\t))}',
331364
'</App>'
332365
].join('\n'),
366+
output: [
367+
'<App>',
368+
'\t{options.map((option, index) => (',
369+
'\t\t<option key={index} value={option.key}>',
370+
'\t\t\t{option.name}',
371+
'\t\t</option>',
372+
'\t))}',
373+
'</App>'
374+
].join('\n'),
333375
parserOptions: parserOptions,
334376
options: ['tab'],
335377
errors: [
@@ -369,10 +411,7 @@ ruleTester.run('jsx-indent', rule, {
369411
errors: [
370412
{message: 'Expected indentation of 2 space characters but found 4.'}
371413
]
372-
}
373-
// Tests for future work. See the comment on line 42-43 in the rule near to fixable: 'whitespace' meta property,
374-
// Right now fixer function doesn't support replacing tabs with whitespaces and vice-versa.
375-
/* , {
414+
}, {
376415
code: [
377416
'<App>\n',
378417
' <Foo />\n',
@@ -404,5 +443,5 @@ ruleTester.run('jsx-indent', rule, {
404443
errors: [
405444
{message: 'Expected indentation of 2 space characters but found 0.'}
406445
]
407-
}*/]
446+
}]
408447
});

0 commit comments

Comments
 (0)