Skip to content

Commit 7434f19

Browse files
Moong0122ljharb
authored andcommitted
[Fix] jsx-closing-bracket-location: In tag-aligned, made a distinction between tabs and spaces
Fixes ##756.
1 parent 7cf4ceb commit 7434f19

File tree

3 files changed

+91
-1
lines changed

3 files changed

+91
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
1616
* [`jsx-handler-names`]: handle whitespace ([#2789][] @AriPerkkio)
1717
* [`prop-types`]: Detect TypeScript types for destructured default prop values ([#2780][] @sunghyunjo)
1818
* [`jsx-pascal-case`]: Handle single character namespaced component ([#2791][] @daviferreira)
19+
* [`jsx-closing-bracket-location`]: In `tag-aligned`, made a distinction between tabs and spaces ([#2796][] @Moong0122)
1920

21+
[#2796]: https://github.com/yannickcr/eslint-plugin-react/pull/2796
2022
[#2791]: https://github.com/yannickcr/eslint-plugin-react/pull/2791
2123
[#2789]: https://github.com/yannickcr/eslint-plugin-react/pull/2789
2224
[#2780]: https://github.com/yannickcr/eslint-plugin-react/pull/2780

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,11 +201,17 @@ module.exports = {
201201
};
202202
}
203203
const openingLine = sourceCode.lines[opening.line - 1];
204+
const closingLine = sourceCode.lines[closing.line - 1];
205+
const isTab = {
206+
openTab: /^\t/.test(openingLine),
207+
closeTab: /^\t/.test(closingLine)
208+
};
204209
const openingStartOfLine = {
205210
column: /^\s*/.exec(openingLine)[0].length,
206211
line: opening.line
207212
};
208213
return {
214+
isTab,
209215
tag,
210216
opening,
211217
closing,
@@ -239,11 +245,17 @@ module.exports = {
239245
'JSXOpeningElement:exit'(node) {
240246
const attributeNode = lastAttributeNode[getOpeningElementId(node)];
241247
const cachedLastAttributeEndPos = attributeNode ? attributeNode.range[1] : null;
248+
242249
let expectedNextLine;
243250
const tokens = getTokensLocations(node);
244251
const expectedLocation = getExpectedLocation(tokens);
252+
let usingSameIndentation = true;
253+
254+
if (expectedLocation === 'tag-aligned') {
255+
usingSameIndentation = tokens.isTab.openTab === tokens.isTab.closeTab;
256+
}
245257

246-
if (hasCorrectLocation(tokens, expectedLocation)) {
258+
if (hasCorrectLocation(tokens, expectedLocation) && usingSameIndentation) {
247259
return;
248260
}
249261

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

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1689,5 +1689,81 @@ ruleTester.run('jsx-closing-bracket-location', rule, {
16891689
line: 4,
16901690
column: 6
16911691
}]
1692+
}, {
1693+
code: [
1694+
'\t\t<div',
1695+
'\t\t\tclassName={styles}',
1696+
' >',
1697+
'\t\t\t{props}',
1698+
'\t\t</div>'
1699+
].join('\n'),
1700+
output: [
1701+
'\t\t<div',
1702+
'\t\t\tclassName={styles}',
1703+
'\t\t>',
1704+
'\t\t\t{props}',
1705+
'\t\t</div>'
1706+
].join('\n'),
1707+
options: [{location: 'tag-aligned'}],
1708+
errors: [{
1709+
message: messageWithDetails(MESSAGE_TAG_ALIGNED, 3, false),
1710+
line: 3,
1711+
column: 3
1712+
}]
1713+
}, {
1714+
code: [
1715+
' <div',
1716+
' className={styles}',
1717+
'\t\t>',
1718+
' {props}',
1719+
' </div>'
1720+
].join('\n'),
1721+
output: [
1722+
' <div',
1723+
' className={styles}',
1724+
' >',
1725+
' {props}',
1726+
' </div>'
1727+
].join('\n'),
1728+
options: [{location: 'tag-aligned'}],
1729+
errors: [{
1730+
message: messageWithDetails(MESSAGE_TAG_ALIGNED, 3, false),
1731+
line: 3,
1732+
column: 3
1733+
}]
1734+
}, {
1735+
code: [
1736+
' <App',
1737+
' foo',
1738+
'\t\t/>'
1739+
].join('\n'),
1740+
output: [
1741+
' <App',
1742+
' foo',
1743+
' />'
1744+
].join('\n'),
1745+
options: [{location: 'tag-aligned'}],
1746+
errors: [{
1747+
message: messageWithDetails(MESSAGE_TAG_ALIGNED, 3, false),
1748+
line: 3,
1749+
column: 3
1750+
}]
1751+
}, {
1752+
code: [
1753+
'\t\t<App',
1754+
'\t\t\tfoo',
1755+
' />'
1756+
].join('\n'),
1757+
output: [
1758+
'\t\t<App',
1759+
'\t\t\tfoo',
1760+
'\t\t/>'
1761+
].join('\n'),
1762+
options: [{location: 'tag-aligned'}],
1763+
errors: [{
1764+
message: messageWithDetails(MESSAGE_TAG_ALIGNED, 3, false),
1765+
line: 3,
1766+
column: 3
1767+
}]
16921768
}]
16931769
});

0 commit comments

Comments
 (0)