Skip to content

Commit 3373c07

Browse files
author
Kent C. Dodds
committed
[Fix] jsx-indent with tabs (fixes jsx-eslint#1057)
1 parent c97dd0f commit 3373c07

File tree

2 files changed

+176
-86
lines changed

2 files changed

+176
-86
lines changed

lib/rules/jsx-indent.js

+126-60
Original file line numberDiff line numberDiff line change
@@ -73,18 +73,16 @@ module.exports = {
7373

7474
/**
7575
* Responsible for fixing the indentation issue fix
76-
* @param {ASTNode} node Node violating the indent rule
7776
* @param {Number} needed Expected indentation character count
77+
* @param {Boolean} rangeToReplace is used to specify the range
78+
* to replace with the correct indentation.
7879
* @returns {Function} function to be executed by the fixer
7980
* @private
8081
*/
81-
function getFixerFunction(node, needed) {
82+
function getFixerFunction(needed, rangeToReplace) {
8283
return function(fixer) {
8384
var indent = Array(needed + 1).join(indentChar);
84-
return fixer.replaceTextRange(
85-
[node.start - node.loc.start.column, node.start],
86-
indent
87-
);
85+
return fixer.replaceTextRange(rangeToReplace, indent);
8886
};
8987
}
9088

@@ -93,46 +91,35 @@ module.exports = {
9391
* @param {ASTNode} node Node violating the indent rule
9492
* @param {Number} needed Expected indentation character count
9593
* @param {Number} gotten Indentation character count in the actual node/code
96-
* @param {Object} loc Error line and column location
94+
* @param {Array} rangeToReplace is used in the fixer. Defaults to the indent of the start of the node
95+
* @param {Object} loc Error line and column location (defaults to node.loc
9796
*/
98-
function report(node, needed, gotten, loc) {
97+
function report(node, needed, gotten, rangeToReplace, loc) {
9998
var msgContext = {
10099
needed: needed,
101100
type: indentType,
102101
characters: needed === 1 ? 'character' : 'characters',
103102
gotten: gotten
104103
};
104+
rangeToReplace = rangeToReplace || [node.start - node.loc.start.column, node.start];
105105

106-
if (loc) {
107-
context.report({
108-
node: node,
109-
loc: loc,
110-
message: MESSAGE,
111-
data: msgContext,
112-
fix: getFixerFunction(node, needed)
113-
});
114-
} else {
115-
context.report({
116-
node: node,
117-
message: MESSAGE,
118-
data: msgContext,
119-
fix: getFixerFunction(node, needed)
120-
});
121-
}
106+
context.report({
107+
node: node,
108+
loc: loc || node.loc,
109+
message: MESSAGE,
110+
data: msgContext,
111+
fix: getFixerFunction(needed, rangeToReplace)
112+
});
122113
}
123114

124115
/**
125-
* Get node indent
126-
* @param {ASTNode} node Node to examine
127-
* @param {Boolean} byLastLine get indent of node's last line
128-
* @param {Boolean} excludeCommas skip comma on start of line
129-
* @return {Number} Indent
116+
* Get the indentation (of the proper indentType) that exists in the source
117+
* @param {String} the source string
118+
* @param {Boolean} whether the line checked should be the last (defaults to the first line)
119+
* @param {Boolean} whether to skip commas in the check (defaults to false)
120+
* @return {Number} the indentation of the indentType that exists on the line
130121
*/
131-
function getNodeIndent(node, byLastLine, excludeCommas) {
132-
byLastLine = byLastLine || false;
133-
excludeCommas = excludeCommas || false;
134-
135-
var src = sourceCode.getText(node, node.loc.start.column + extraColumnStart);
122+
function getIndentFromString(src, byLastLine, excludeCommas) {
136123
var lines = src.split('\n');
137124
if (byLastLine) {
138125
src = lines[lines.length - 1];
@@ -154,7 +141,24 @@ module.exports = {
154141
}
155142

156143
/**
157-
* Checks node is the first in its own start line. By default it looks by start line.
144+
* Get node indent
145+
* @param {ASTNode} node Node to examine
146+
* @param {Boolean} byLastLine get indent of node's last line
147+
* @param {Boolean} excludeCommas skip comma on start of line
148+
* @return {Number} Indent
149+
*/
150+
function getNodeIndent(node, byLastLine, excludeCommas) {
151+
byLastLine = byLastLine || false;
152+
excludeCommas = excludeCommas || false;
153+
154+
var src = sourceCode.getText(node, node.loc.start.column + extraColumnStart);
155+
156+
return getIndentFromString(src, byLastLine, excludeCommas);
157+
}
158+
159+
/**
160+
* Checks if the node is the first in its own start line. By default it looks by start line.
161+
* One exception is closing tags with preceeding whitespace
158162
* @param {ASTNode} node The node to check
159163
* @return {Boolean} true if its the first in the its start line
160164
*/
@@ -165,8 +169,9 @@ module.exports = {
165169
} while (token.type === 'JSXText' && /^\s*$/.test(token.value));
166170
var startLine = node.loc.start.line;
167171
var endLine = token ? token.loc.end.line : -1;
172+
var whitespaceOnly = token ? /\n\s*$/.test(token.value) : false;
168173

169-
return startLine !== endLine;
174+
return startLine !== endLine || whitespaceOnly;
170175
}
171176

172177
/**
@@ -218,41 +223,74 @@ module.exports = {
218223
}
219224
}
220225

226+
/**
227+
* Checks the end of the tag (>) to determine whether it's on its own line
228+
* If so, it verifies the indentation is correct and reports if it is not
229+
* @param {[type]} node [description]
230+
* @param {[type]} startIndent [description]
231+
* @return {[type]} [description]
232+
*/
233+
function checkTagEndIndent(node, startIndent) {
234+
var source = sourceCode.getText(node);
235+
var isTagEndOnOwnLine = /\n\s*\/?>$/.exec(source);
236+
if (isTagEndOnOwnLine) {
237+
var endIndent = getIndentFromString(source, true, false);
238+
if (endIndent !== startIndent) {
239+
var rangeToReplace = [node.end - node.loc.end.column, node.end - 1];
240+
report(node, startIndent, endIndent, rangeToReplace);
241+
}
242+
}
243+
}
244+
245+
function getOpeningElementIndent(node) {
246+
var prevToken = sourceCode.getTokenBefore(node);
247+
if (!prevToken) {
248+
return 0;
249+
}
250+
// Use the parent in a list or an array
251+
if (prevToken.type === 'JSXText' || prevToken.type === 'Punctuator' && prevToken.value === ',') {
252+
prevToken = sourceCode.getNodeByRangeIndex(prevToken.start);
253+
prevToken = prevToken.type === 'Literal' ? prevToken.parent : prevToken;
254+
// Use the first non-punctuator token in a conditional expression
255+
} else if (prevToken.type === 'Punctuator' && prevToken.value === ':') {
256+
do {
257+
prevToken = sourceCode.getTokenBefore(prevToken);
258+
} while (prevToken.type === 'Punctuator');
259+
prevToken = sourceCode.getNodeByRangeIndex(prevToken.start);
260+
while (prevToken.parent && prevToken.parent.type !== 'ConditionalExpression') {
261+
prevToken = prevToken.parent;
262+
}
263+
}
264+
prevToken = prevToken.type === 'JSXExpressionContainer' ? prevToken.expression : prevToken;
265+
266+
var parentElementIndent = getNodeIndent(prevToken);
267+
if (prevToken.type === 'JSXElement') {
268+
parentElementIndent = getOpeningElementIndent(prevToken.openingElement);
269+
}
270+
271+
var indent = (
272+
prevToken.loc.start.line === node.loc.start.line ||
273+
isRightInLogicalExp(node) ||
274+
isAlternateInConditionalExp(node)
275+
) ? 0 : indentSize;
276+
return parentElementIndent + indent;
277+
}
278+
221279
return {
222280
JSXOpeningElement: function(node) {
223281
var prevToken = sourceCode.getTokenBefore(node);
224282
if (!prevToken) {
225283
return;
226284
}
227-
// Use the parent in a list or an array
228-
if (prevToken.type === 'JSXText' || prevToken.type === 'Punctuator' && prevToken.value === ',') {
229-
prevToken = sourceCode.getNodeByRangeIndex(prevToken.start);
230-
prevToken = prevToken.type === 'Literal' ? prevToken.parent : prevToken;
231-
// Use the first non-punctuator token in a conditional expression
232-
} else if (prevToken.type === 'Punctuator' && prevToken.value === ':') {
233-
do {
234-
prevToken = sourceCode.getTokenBefore(prevToken);
235-
} while (prevToken.type === 'Punctuator');
236-
prevToken = sourceCode.getNodeByRangeIndex(prevToken.start);
237-
while (prevToken.parent && prevToken.parent.type !== 'ConditionalExpression') {
238-
prevToken = prevToken.parent;
239-
}
240-
}
241-
prevToken = prevToken.type === 'JSXExpressionContainer' ? prevToken.expression : prevToken;
242-
243-
var parentElementIndent = getNodeIndent(prevToken);
244-
var indent = (
245-
prevToken.loc.start.line === node.loc.start.line ||
246-
isRightInLogicalExp(node) ||
247-
isAlternateInConditionalExp(node)
248-
) ? 0 : indentSize;
249-
checkNodesIndent(node, parentElementIndent + indent);
285+
var startIndent = getOpeningElementIndent(node);
286+
checkNodesIndent(node, startIndent);
287+
checkTagEndIndent(node, startIndent);
250288
},
251289
JSXClosingElement: function(node) {
252290
if (!node.parent) {
253291
return;
254292
}
255-
var peerElementIndent = getNodeIndent(node.parent.openingElement);
293+
var peerElementIndent = getOpeningElementIndent(node.parent.openingElement);
256294
checkNodesIndent(node, peerElementIndent);
257295
},
258296
JSXExpressionContainer: function(node) {
@@ -261,6 +299,34 @@ module.exports = {
261299
}
262300
var parentNodeIndent = getNodeIndent(node.parent);
263301
checkNodesIndent(node, parentNodeIndent + indentSize);
302+
},
303+
Literal: function(node) {
304+
if (!node.parent || (node.parent.type !== 'JSXElement' && node.parent.type !== 'JSXExpressionContainer')) {
305+
return;
306+
}
307+
var parentElementIndent = getOpeningElementIndent(node.parent.openingElement);
308+
var expectedIndent = parentElementIndent + indentSize;
309+
var source = sourceCode.getText(node);
310+
var lines = source.split('\n');
311+
var currentIndex = 0;
312+
lines.forEach(function(line, lineNumber) {
313+
if (line.trim()) {
314+
var lineIndent = getIndentFromString(line);
315+
if (lineIndent !== expectedIndent) {
316+
var lineStart = source.indexOf(line, currentIndex);
317+
var lineIndentStart = line.search(/\S/);
318+
var lineIndentEnd = lineStart + lineIndentStart;
319+
var rangeToReplace = [node.start + lineStart, node.start + lineIndentEnd];
320+
var locLine = lineNumber + node.loc.start.line;
321+
var loc = {
322+
start: {line: locLine, column: lineIndentStart},
323+
end: {line: locLine, column: lineIndentEnd}
324+
};
325+
report(node, expectedIndent, lineIndent, rangeToReplace, loc);
326+
}
327+
}
328+
currentIndex += line.length;
329+
});
264330
}
265331
};
266332

tests/lib/rules/jsx-indent.js

+50-26
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,16 @@ ruleTester.run('jsx-indent', rule, {
5151
].join('\n'),
5252
options: [0],
5353
parserOptions: parserOptions
54-
}, {
55-
code: [
56-
' <App>',
57-
'<Foo />',
58-
' </App>'
59-
].join('\n'),
60-
options: [-2],
61-
parserOptions: parserOptions
54+
// }, {
55+
// should we put effort in making this work?
56+
// who in their right mind would do this?
57+
// code: [
58+
// ' <App>',
59+
// '<Foo />',
60+
// ' </App>'
61+
// ].join('\n'),
62+
// options: [-2],
63+
// parserOptions: parserOptions
6264
}, {
6365
code: [
6466
'<App>',
@@ -211,17 +213,6 @@ ruleTester.run('jsx-indent', rule, {
211213
'</div>'
212214
].join('\n'),
213215
parserOptions: parserOptions
214-
}, {
215-
// Literals indentation is not touched
216-
code: [
217-
'<div>',
218-
'bar <div>',
219-
' bar',
220-
' bar {foo}',
221-
'bar </div>',
222-
'</div>'
223-
].join('\n'),
224-
parserOptions: parserOptions
225216
}, {
226217
// Multiline ternary
227218
// (colon at the end of the first expression)
@@ -459,6 +450,39 @@ ruleTester.run('jsx-indent', rule, {
459450
options: ['tab'],
460451
parserOptions: parserOptions,
461452
errors: [{message: 'Expected indentation of 1 tab character but found 0.'}]
453+
}, {
454+
code: [
455+
'function MyComponent(props) {',
456+
'\treturn (',
457+
' <div',
458+
'\t\t\tclassName="foo-bar"',
459+
'\t\t\tid="thing"',
460+
' >',
461+
' Hello world!',
462+
' </div>',
463+
'\t)',
464+
'}'
465+
].join('\n'),
466+
output: [
467+
'function MyComponent(props) {',
468+
'\treturn (',
469+
'\t\t<div',
470+
'\t\t\tclassName="foo-bar"',
471+
'\t\t\tid="thing"',
472+
'\t\t>',
473+
'\t\t\tHello world!',
474+
'\t\t</div>',
475+
'\t)',
476+
'}'
477+
].join('\n'),
478+
options: ['tab'],
479+
parserOptions: parserOptions,
480+
errors: [
481+
{message: 'Expected indentation of 2 tab characters but found 0.'},
482+
{message: 'Expected indentation of 2 tab characters but found 0.'},
483+
{message: 'Expected indentation of 3 tab characters but found 0.'},
484+
{message: 'Expected indentation of 2 tab characters but found 0.'}
485+
]
462486
}, {
463487
code: [
464488
'function App() {',
@@ -505,22 +529,22 @@ ruleTester.run('jsx-indent', rule, {
505529
' );',
506530
'}'
507531
].join('\n'),
508-
// The detection logic only thinks <App> is indented wrong, not the other
509-
// two lines following. I *think* because it incorrectly uses <App>'s indention
510-
// as the baseline for the next two, instead of the realizing the entire three
511-
// lines are wrong together. See #608
512-
/* output: [
532+
output: [
513533
'function App() {',
514534
' return (',
515535
' <App>',
516536
' <Foo />',
517537
' </App>',
518538
' );',
519539
'}'
520-
].join('\n'), */
540+
].join('\n'),
521541
options: [2],
522542
parserOptions: parserOptions,
523-
errors: [{message: 'Expected indentation of 4 space characters but found 0.'}]
543+
errors: [
544+
{message: 'Expected indentation of 4 space characters but found 0.'},
545+
{message: 'Expected indentation of 6 space characters but found 2.'},
546+
{message: 'Expected indentation of 4 space characters but found 0.'}
547+
]
524548
}, {
525549
code: [
526550
'<App>',

0 commit comments

Comments
 (0)