Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2b832cd

Browse files
committedOct 27, 2016
jsx-indent autofixer now supports tabs -> spaces and vice versa
1 parent 99cec3e commit 2b832cd

File tree

3 files changed

+58
-21
lines changed

3 files changed

+58
-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: 48 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,19 @@ ruleTester.run('jsx-indent', rule, {
282287
' );',
283288
'}'
284289
].join('\n'),
290+
// The detection logic only thinks <App> is indented wrong, not the other
291+
// two lines following. I *think* because it incorrectly uses <App>'s indention
292+
// as the baseline for the next two, instead of the realizing the entire three
293+
// lines are wrong together. See #608
294+
/* output: [
295+
'function App() {',
296+
' return (',
297+
' <App>',
298+
' <Foo />',
299+
' </App>',
300+
' );',
301+
'}'
302+
].join('\n'), */
285303
options: [2],
286304
parserOptions: parserOptions,
287305
errors: [{message: 'Expected indentation of 4 space characters but found 0.'}]
@@ -291,6 +309,11 @@ ruleTester.run('jsx-indent', rule, {
291309
' {test}',
292310
'</App>'
293311
].join('\n'),
312+
output: [
313+
'<App>',
314+
' {test}',
315+
'</App>'
316+
].join('\n'),
294317
parserOptions: parserOptions,
295318
errors: [
296319
{message: 'Expected indentation of 4 space characters but found 3.'}
@@ -305,6 +328,15 @@ ruleTester.run('jsx-indent', rule, {
305328
' ))}',
306329
'</App>'
307330
].join('\n'),
331+
output: [
332+
'<App>',
333+
' {options.map((option, index) => (',
334+
' <option key={index} value={option.key}>',
335+
' {option.name}',
336+
' </option>',
337+
' ))}',
338+
'</App>'
339+
].join('\n'),
308340
parserOptions: parserOptions,
309341
errors: [
310342
{message: 'Expected indentation of 12 space characters but found 11.'}
@@ -315,6 +347,11 @@ ruleTester.run('jsx-indent', rule, {
315347
'{test}',
316348
'</App>'
317349
].join('\n'),
350+
output: [
351+
'<App>',
352+
'\t{test}',
353+
'</App>'
354+
].join('\n'),
318355
parserOptions: parserOptions,
319356
options: ['tab'],
320357
errors: [
@@ -330,6 +367,15 @@ ruleTester.run('jsx-indent', rule, {
330367
'\t))}',
331368
'</App>'
332369
].join('\n'),
370+
output: [
371+
'<App>',
372+
'\t{options.map((option, index) => (',
373+
'\t\t<option key={index} value={option.key}>',
374+
'\t\t\t{option.name}',
375+
'\t\t</option>',
376+
'\t))}',
377+
'</App>'
378+
].join('\n'),
333379
parserOptions: parserOptions,
334380
options: ['tab'],
335381
errors: [
@@ -369,10 +415,7 @@ ruleTester.run('jsx-indent', rule, {
369415
errors: [
370416
{message: 'Expected indentation of 2 space characters but found 4.'}
371417
]
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-
/* , {
418+
}, {
376419
code: [
377420
'<App>\n',
378421
' <Foo />\n',
@@ -404,5 +447,5 @@ ruleTester.run('jsx-indent', rule, {
404447
errors: [
405448
{message: 'Expected indentation of 2 space characters but found 0.'}
406449
]
407-
}*/]
450+
}]
408451
});

0 commit comments

Comments
 (0)
Please sign in to comment.