Skip to content

Commit

Permalink
[Fix] jsx-indent: Fix to work for older eslint
Browse files Browse the repository at this point in the history
  • Loading branch information
toshi-toma committed Feb 1, 2020
1 parent 137abcd commit f13e0df
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 15 deletions.
25 changes: 14 additions & 11 deletions lib/rules/jsx-indent.js
Expand Up @@ -99,7 +99,7 @@ module.exports = {
function getFixerFunction(node, needed) {
return function fix(fixer) {
const indent = Array(needed + 1).join(indentChar);
if (node.type === 'JSXText') {
if (node.type === 'JSXText' || node.type === 'Literal') {
const regExp = /\n[\t ]*(\S)/g;
const fixedText = node.raw.replace(regExp, (match, p1) => `\n${indent}${p1}`);
return fixer.replaceText(node, fixedText);
Expand All @@ -115,7 +115,7 @@ module.exports = {
* Reports a given indent violation and properly pluralizes the message
* @param {ASTNode} node Node violating the indent rule
* @param {Number} needed Expected indentation character count
* @param {Number|Array<number>} gotten Indentation character count in the actual node/code
* @param {Number} gotten Indentation character count in the actual node/code
* @param {Object} [loc] Error line and column location
*/
function report(node, needed, gotten, loc) {
Expand Down Expand Up @@ -298,11 +298,11 @@ module.exports = {
}

/**
* Check indent for JSXText
* Check indent for Literal Node or JSXText Node
* @param {ASTNode} node The node to check
* @param {Number} indent needed indent
*/
function checkJSXTextNodeIndent(node, indent) {
function checkLiteralNodeIndent(node, indent) {
const value = node.value;
const regExp = indentType === 'space' ? /\n( *)[\t ]*\S/g : /\n(\t*)[\t ]*\S/g;
const nodeIndentsPerLine = Array.from(
Expand Down Expand Up @@ -370,6 +370,14 @@ module.exports = {
checkNodesIndent(firstInLine, indent);
}

function handleLiteral(node) {
if (!node.parent) {
return;
}
const parentNodeIndent = getNodeIndent(node.parent);
checkLiteralNodeIndent(node, parentNodeIndent + indentSize);
}

return {
JSXOpeningElement: handleOpeningElement,
JSXOpeningFragment: handleOpeningElement,
Expand All @@ -383,13 +391,8 @@ module.exports = {
const parentNodeIndent = getNodeIndent(node.parent);
checkNodesIndent(node, parentNodeIndent + indentSize);
},
JSXText(node) {
if (!node.parent) {
return;
}
const parentNodeIndent = getNodeIndent(node.parent);
checkJSXTextNodeIndent(node, parentNodeIndent + indentSize);
}
Literal: handleLiteral,
JSXText: handleLiteral
};
}
};
31 changes: 27 additions & 4 deletions tests/lib/rules/jsx-indent.js
Expand Up @@ -311,10 +311,10 @@ ruleTester.run('jsx-indent', rule, {
}, {
code: [
'<>',
'bar <>',
' bar',
' bar {foo}',
'bar </>',
' bar <>',
' bar',
' bar {foo}',
' bar </>',
'</>'
].join('\n'),
parser: parsers.BABEL_ESLINT
Expand Down Expand Up @@ -1932,6 +1932,11 @@ const Component = () => (
'text',
'</div>'
].join('\n'),
output: [
'<div>',
' text',
'</div>'
].join('\n'),
errors: [
{message: 'Expected indentation of 4 space characters but found 0.'}
]
Expand All @@ -1942,6 +1947,12 @@ const Component = () => (
'text',
'</div>'
].join('\n'),
output: [
'<div>',
' text',
' text',
'</div>'
].join('\n'),
errors: [
{message: 'Expected indentation of 4 space characters but found 2.'},
{message: 'Expected indentation of 4 space characters but found 0.'}
Expand All @@ -1953,6 +1964,12 @@ const Component = () => (
' \t text',
'</div>'
].join('\n'),
output: [
'<div>',
' text',
' text',
'</div>'
].join('\n'),
errors: [
{message: 'Expected indentation of 4 space characters but found 0.'},
{message: 'Expected indentation of 4 space characters but found 2.'}
Expand All @@ -1963,7 +1980,13 @@ const Component = () => (
'\t\ttext',
'</div>'
].join('\n'),
parser: parsers.BABEL_ESLINT,
options: ['tab'],
output: [
'<div>',
'\ttext',
'</div>'
].join('\n'),
errors: [
{message: 'Expected indentation of 1 tab character but found 2.'}
]
Expand Down

0 comments on commit f13e0df

Please sign in to comment.