Skip to content

Commit f13e0df

Browse files
committed
[Fix] jsx-indent: Fix to work for older eslint
1 parent 137abcd commit f13e0df

File tree

2 files changed

+41
-15
lines changed

2 files changed

+41
-15
lines changed

lib/rules/jsx-indent.js

+14-11
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ module.exports = {
9999
function getFixerFunction(node, needed) {
100100
return function fix(fixer) {
101101
const indent = Array(needed + 1).join(indentChar);
102-
if (node.type === 'JSXText') {
102+
if (node.type === 'JSXText' || node.type === 'Literal') {
103103
const regExp = /\n[\t ]*(\S)/g;
104104
const fixedText = node.raw.replace(regExp, (match, p1) => `\n${indent}${p1}`);
105105
return fixer.replaceText(node, fixedText);
@@ -115,7 +115,7 @@ module.exports = {
115115
* Reports a given indent violation and properly pluralizes the message
116116
* @param {ASTNode} node Node violating the indent rule
117117
* @param {Number} needed Expected indentation character count
118-
* @param {Number|Array<number>} gotten Indentation character count in the actual node/code
118+
* @param {Number} gotten Indentation character count in the actual node/code
119119
* @param {Object} [loc] Error line and column location
120120
*/
121121
function report(node, needed, gotten, loc) {
@@ -298,11 +298,11 @@ module.exports = {
298298
}
299299

300300
/**
301-
* Check indent for JSXText
301+
* Check indent for Literal Node or JSXText Node
302302
* @param {ASTNode} node The node to check
303303
* @param {Number} indent needed indent
304304
*/
305-
function checkJSXTextNodeIndent(node, indent) {
305+
function checkLiteralNodeIndent(node, indent) {
306306
const value = node.value;
307307
const regExp = indentType === 'space' ? /\n( *)[\t ]*\S/g : /\n(\t*)[\t ]*\S/g;
308308
const nodeIndentsPerLine = Array.from(
@@ -370,6 +370,14 @@ module.exports = {
370370
checkNodesIndent(firstInLine, indent);
371371
}
372372

373+
function handleLiteral(node) {
374+
if (!node.parent) {
375+
return;
376+
}
377+
const parentNodeIndent = getNodeIndent(node.parent);
378+
checkLiteralNodeIndent(node, parentNodeIndent + indentSize);
379+
}
380+
373381
return {
374382
JSXOpeningElement: handleOpeningElement,
375383
JSXOpeningFragment: handleOpeningElement,
@@ -383,13 +391,8 @@ module.exports = {
383391
const parentNodeIndent = getNodeIndent(node.parent);
384392
checkNodesIndent(node, parentNodeIndent + indentSize);
385393
},
386-
JSXText(node) {
387-
if (!node.parent) {
388-
return;
389-
}
390-
const parentNodeIndent = getNodeIndent(node.parent);
391-
checkJSXTextNodeIndent(node, parentNodeIndent + indentSize);
392-
}
394+
Literal: handleLiteral,
395+
JSXText: handleLiteral
393396
};
394397
}
395398
};

tests/lib/rules/jsx-indent.js

+27-4
Original file line numberDiff line numberDiff line change
@@ -311,10 +311,10 @@ ruleTester.run('jsx-indent', rule, {
311311
}, {
312312
code: [
313313
'<>',
314-
'bar <>',
315-
' bar',
316-
' bar {foo}',
317-
'bar </>',
314+
' bar <>',
315+
' bar',
316+
' bar {foo}',
317+
' bar </>',
318318
'</>'
319319
].join('\n'),
320320
parser: parsers.BABEL_ESLINT
@@ -1932,6 +1932,11 @@ const Component = () => (
19321932
'text',
19331933
'</div>'
19341934
].join('\n'),
1935+
output: [
1936+
'<div>',
1937+
' text',
1938+
'</div>'
1939+
].join('\n'),
19351940
errors: [
19361941
{message: 'Expected indentation of 4 space characters but found 0.'}
19371942
]
@@ -1942,6 +1947,12 @@ const Component = () => (
19421947
'text',
19431948
'</div>'
19441949
].join('\n'),
1950+
output: [
1951+
'<div>',
1952+
' text',
1953+
' text',
1954+
'</div>'
1955+
].join('\n'),
19451956
errors: [
19461957
{message: 'Expected indentation of 4 space characters but found 2.'},
19471958
{message: 'Expected indentation of 4 space characters but found 0.'}
@@ -1953,6 +1964,12 @@ const Component = () => (
19531964
' \t text',
19541965
'</div>'
19551966
].join('\n'),
1967+
output: [
1968+
'<div>',
1969+
' text',
1970+
' text',
1971+
'</div>'
1972+
].join('\n'),
19561973
errors: [
19571974
{message: 'Expected indentation of 4 space characters but found 0.'},
19581975
{message: 'Expected indentation of 4 space characters but found 2.'}
@@ -1963,7 +1980,13 @@ const Component = () => (
19631980
'\t\ttext',
19641981
'</div>'
19651982
].join('\n'),
1983+
parser: parsers.BABEL_ESLINT,
19661984
options: ['tab'],
1985+
output: [
1986+
'<div>',
1987+
'\ttext',
1988+
'</div>'
1989+
].join('\n'),
19671990
errors: [
19681991
{message: 'Expected indentation of 1 tab character but found 2.'}
19691992
]

0 commit comments

Comments
 (0)