Skip to content

Commit 9e093ba

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

File tree

2 files changed

+146
-74
lines changed

2 files changed

+146
-74
lines changed

lib/rules/jsx-indent.js

+97-59
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,18 @@ module.exports = {
7575
* Responsible for fixing the indentation issue fix
7676
* @param {ASTNode} node Node violating the indent rule
7777
* @param {Number} needed Expected indentation character count
78+
* @param {Boolean} isEndOfTag is used to indent the right thing
7879
* @returns {Function} function to be executed by the fixer
7980
* @private
8081
*/
81-
function getFixerFunction(node, needed) {
82+
function getFixerFunction(node, needed, isEndOfTag) {
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+
var rangeToReplace = [node.start - node.loc.start.column, node.start];
86+
if (isEndOfTag) {
87+
rangeToReplace = [node.end - node.loc.end.column, node.end - 1];
88+
}
89+
return fixer.replaceTextRange(rangeToReplace, indent);
8890
};
8991
}
9092

@@ -93,46 +95,32 @@ module.exports = {
9395
* @param {ASTNode} node Node violating the indent rule
9496
* @param {Number} needed Expected indentation character count
9597
* @param {Number} gotten Indentation character count in the actual node/code
96-
* @param {Object} loc Error line and column location
98+
* @param {Boolean} isEndOfTag which is used in the fixer function
9799
*/
98-
function report(node, needed, gotten, loc) {
100+
function report(node, needed, gotten, isEndOfTag) {
99101
var msgContext = {
100102
needed: needed,
101103
type: indentType,
102104
characters: needed === 1 ? 'character' : 'characters',
103105
gotten: gotten
104106
};
105107

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-
}
108+
context.report({
109+
node: node,
110+
message: MESSAGE,
111+
data: msgContext,
112+
fix: getFixerFunction(node, needed, isEndOfTag)
113+
});
122114
}
123115

124116
/**
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
117+
* Get the indentation (of the proper indentType) that exists in the source
118+
* @param {String} the source string
119+
* @param {Boolean} whether the line checked should be the last (defaults to the first line)
120+
* @param {Boolean} whether to skip commas in the check (defaults to false)
121+
* @return {Number} the indentation of the indentType that exists on the line
130122
*/
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);
123+
function getIndentFromString(src, byLastLine, excludeCommas) {
136124
var lines = src.split('\n');
137125
if (byLastLine) {
138126
src = lines[lines.length - 1];
@@ -154,7 +142,24 @@ module.exports = {
154142
}
155143

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

169-
return startLine !== endLine;
175+
return startLine !== endLine || whitespaceOnly;
170176
}
171177

172178
/**
@@ -218,41 +224,73 @@ module.exports = {
218224
}
219225
}
220226

227+
/**
228+
* Checks the end of the tag (>) to determine whether it's on its own line
229+
* If so, it verifies the indentation is correct and reports if it is not
230+
* @param {[type]} node [description]
231+
* @param {[type]} startIndent [description]
232+
* @return {[type]} [description]
233+
*/
234+
function checkTagEndIndent(node, startIndent) {
235+
var source = sourceCode.getText(node);
236+
var isTagEndOnOwnLine = /\n\s*\/?>$/.exec(source);
237+
if (isTagEndOnOwnLine) {
238+
var endIndent = getIndentFromString(source, true, false);
239+
if (endIndent !== startIndent) {
240+
report(node, startIndent, endIndent, true);
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) {

tests/lib/rules/jsx-indent.js

+49-15
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>',
@@ -459,6 +461,38 @@ ruleTester.run('jsx-indent', rule, {
459461
options: ['tab'],
460462
parserOptions: parserOptions,
461463
errors: [{message: 'Expected indentation of 1 tab character but found 0.'}]
464+
}, {
465+
code: [
466+
'function MyComponent(props) {',
467+
'\treturn (',
468+
' <div',
469+
'\t\t\tclassName="foo-bar"',
470+
'\t\t\tid="thing"',
471+
' >',
472+
' Hello world!',
473+
' </div>',
474+
'\t)',
475+
'}'
476+
].join('\n'),
477+
output: [
478+
'function MyComponent(props) {',
479+
'\treturn (',
480+
'\t\t<div',
481+
'\t\t\tclassName="foo-bar"',
482+
'\t\t\tid="thing"',
483+
'\t\t>',
484+
'\t\t\tHello world!',
485+
'\t\t</div>',
486+
'\t)',
487+
'}'
488+
].join('\n'),
489+
options: ['tab'],
490+
parserOptions: parserOptions,
491+
errors: [
492+
{message: 'Expected indentation of 2 tab characters but found 0.'},
493+
{message: 'Expected indentation of 2 tab characters but found 0.'},
494+
{message: 'Expected indentation of 2 tab characters but found 0.'}
495+
]
462496
}, {
463497
code: [
464498
'function App() {',
@@ -505,22 +539,22 @@ ruleTester.run('jsx-indent', rule, {
505539
' );',
506540
'}'
507541
].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: [
542+
output: [
513543
'function App() {',
514544
' return (',
515545
' <App>',
516546
' <Foo />',
517547
' </App>',
518548
' );',
519549
'}'
520-
].join('\n'), */
550+
].join('\n'),
521551
options: [2],
522552
parserOptions: parserOptions,
523-
errors: [{message: 'Expected indentation of 4 space characters but found 0.'}]
553+
errors: [
554+
{message: 'Expected indentation of 4 space characters but found 0.'},
555+
{message: 'Expected indentation of 6 space characters but found 2.'},
556+
{message: 'Expected indentation of 4 space characters but found 0.'}
557+
]
524558
}, {
525559
code: [
526560
'<App>',

0 commit comments

Comments
 (0)