Skip to content
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

[Fix] jsx-closing-bracket-location: In tag-aligned, made a distinction between tabs and spaces #2796

Merged
merged 1 commit into from Sep 16, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -16,7 +16,9 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
* [`jsx-handler-names`]: handle whitespace ([#2789][] @AriPerkkio)
* [`prop-types`]: Detect TypeScript types for destructured default prop values ([#2780][] @sunghyunjo)
* [`jsx-pascal-case`]: Handle single character namespaced component ([#2791][] @daviferreira)
* [`jsx-closing-bracket-location`]: In `tag-aligned`, made a distinction between tabs and spaces ([#2796][] @Moong0122)

[#2796]: https://github.com/yannickcr/eslint-plugin-react/pull/2796
[#2791]: https://github.com/yannickcr/eslint-plugin-react/pull/2791
[#2789]: https://github.com/yannickcr/eslint-plugin-react/pull/2789
[#2780]: https://github.com/yannickcr/eslint-plugin-react/pull/2780
Expand Down
14 changes: 13 additions & 1 deletion lib/rules/jsx-closing-bracket-location.js
Expand Up @@ -201,11 +201,17 @@ module.exports = {
};
}
const openingLine = sourceCode.lines[opening.line - 1];
const closingLine = sourceCode.lines[closing.line - 1];
const isTab = {
openTab: /^\t/.test(openingLine),
closeTab: /^\t/.test(closingLine)
};
const openingStartOfLine = {
column: /^\s*/.exec(openingLine)[0].length,
line: opening.line
};
return {
isTab,
tag,
opening,
closing,
Expand Down Expand Up @@ -239,11 +245,17 @@ module.exports = {
'JSXOpeningElement:exit'(node) {
const attributeNode = lastAttributeNode[getOpeningElementId(node)];
const cachedLastAttributeEndPos = attributeNode ? attributeNode.range[1] : null;

let expectedNextLine;
const tokens = getTokensLocations(node);
const expectedLocation = getExpectedLocation(tokens);
let usingSameIndentation = true;

if (expectedLocation === 'tag-aligned') {
usingSameIndentation = tokens.isTab.openTab === tokens.isTab.closeTab;
}

if (hasCorrectLocation(tokens, expectedLocation)) {
if (hasCorrectLocation(tokens, expectedLocation) && usingSameIndentation) {
return;
}

Expand Down
76 changes: 76 additions & 0 deletions tests/lib/rules/jsx-closing-bracket-location.js
Expand Up @@ -1689,5 +1689,81 @@ ruleTester.run('jsx-closing-bracket-location', rule, {
line: 4,
column: 6
}]
}, {
code: [
'\t\t<div',
'\t\t\tclassName={styles}',
' >',
'\t\t\t{props}',
'\t\t</div>'
].join('\n'),
output: [
'\t\t<div',
'\t\t\tclassName={styles}',
'\t\t>',
'\t\t\t{props}',
'\t\t</div>'
].join('\n'),
options: [{location: 'tag-aligned'}],
errors: [{
message: messageWithDetails(MESSAGE_TAG_ALIGNED, 3, false),
line: 3,
column: 3
}]
}, {
code: [
' <div',
' className={styles}',
'\t\t>',
' {props}',
' </div>'
].join('\n'),
output: [
' <div',
' className={styles}',
' >',
' {props}',
' </div>'
].join('\n'),
options: [{location: 'tag-aligned'}],
errors: [{
message: messageWithDetails(MESSAGE_TAG_ALIGNED, 3, false),
line: 3,
column: 3
}]
}, {
code: [
' <App',
' foo',
'\t\t/>'
].join('\n'),
output: [
' <App',
' foo',
' />'
].join('\n'),
options: [{location: 'tag-aligned'}],
errors: [{
message: messageWithDetails(MESSAGE_TAG_ALIGNED, 3, false),
line: 3,
column: 3
}]
}, {
code: [
'\t\t<App',
'\t\t\tfoo',
' />'
].join('\n'),
output: [
'\t\t<App',
'\t\t\tfoo',
'\t\t/>'
].join('\n'),
options: [{location: 'tag-aligned'}],
errors: [{
message: messageWithDetails(MESSAGE_TAG_ALIGNED, 3, false),
line: 3,
column: 3
}]
}]
});