Skip to content

Support tab indentation #909

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions lib/rules/jsx-closing-bracket-location.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,33 @@ module.exports = {
}
}

/**
* Get the characters used for indentation on the line to be matched
* @param {Object} tokens Locations of the opening bracket, closing bracket and last prop
* @param {String} expectedLocation Expected location for the closing bracket
* @param {Number} correctColumn Expected column for the closing bracket
* @return {String} The characters used for indentation
*/
function getIndentation(tokens, expectedLocation, correctColumn) {
var indentation, spaces = [];
switch (expectedLocation) {
case 'props-aligned':
indentation = /^\s*/.exec(sourceCode.lines[tokens.lastProp.firstLine - 1])[0];
break;
case 'tag-aligned':
case 'line-aligned':
indentation = /^\s*/.exec(sourceCode.lines[tokens.opening.line - 1])[0];
break;
default:
indentation = '';
}
if (indentation.length + 1 < correctColumn) {
// Non-whitespace characters were included in the column offset
spaces = new Array(+correctColumn + 1 - indentation.length);
}
return indentation + spaces.join(' ');
}

/**
* Get the locations of the opening bracket, closing bracket, last prop, and
* start of opening line.
Expand Down Expand Up @@ -244,9 +271,8 @@ module.exports = {
case 'props-aligned':
case 'tag-aligned':
case 'line-aligned':
var spaces = new Array(+correctColumn + 1);
return fixer.replaceTextRange([cachedLastAttributeEndPos, node.end],
'\n' + spaces.join(' ') + closingTag);
'\n' + getIndentation(tokens, expectedLocation, correctColumn) + closingTag);
default:
return true;
}
Expand Down
Loading