Skip to content

Commit 1eaaa11

Browse files
arperrylencioni
authored andcommitted
Support tab indentation
The autofix for this rule substitutes spaces even when the code is normally indented with tabs. This change accommodates tabs, spaces, and even mixed whitespace (we won't judge).
1 parent 9a657cc commit 1eaaa11

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

lib/rules/jsx-closing-bracket-location.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,25 @@ module.exports = {
146146
}
147147
}
148148

149+
/**
150+
* Get the characters used for indentation on the line to be matched
151+
* @param {Object} tokens Locations of the opening bracket, closing bracket and last prop
152+
* @param {String} expectedLocation Expected location for the closing bracket
153+
* @return {String} The characters used for indentation
154+
*/
155+
function getIndentation(tokens, expectedLocation) {
156+
switch (expectedLocation) {
157+
case 'props-aligned':
158+
return /^\s*/.exec(sourceCode[tokens.lastProp.lastLine])[0];
159+
case 'tag-aligned':
160+
return /^\s*/.exec(sourceCode[tokens.opening.line])[0];
161+
case 'line-aligned':
162+
return /^\s*/.exec(sourceCode[tokens.openingStartOfLine.line])[0];
163+
default:
164+
return '';
165+
}
166+
}
167+
149168
/**
150169
* Get the locations of the opening bracket, closing bracket, last prop, and
151170
* start of opening line.
@@ -244,9 +263,8 @@ module.exports = {
244263
case 'props-aligned':
245264
case 'tag-aligned':
246265
case 'line-aligned':
247-
var spaces = new Array(+correctColumn + 1);
248266
return fixer.replaceTextRange([cachedLastAttributeEndPos, node.end],
249-
'\n' + spaces.join(' ') + closingTag);
267+
'\n' + getIndentation(tokens, expectedLocation) + closingTag);
250268
default:
251269
return true;
252270
}

0 commit comments

Comments
 (0)