diff --git a/lib/rules/jsx-indent.js b/lib/rules/jsx-indent.js index 496043e9ac..390d700503 100644 --- a/lib/rules/jsx-indent.js +++ b/lib/rules/jsx-indent.js @@ -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); @@ -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} 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) { @@ -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( @@ -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, @@ -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 }; } }; diff --git a/tests/lib/rules/jsx-indent.js b/tests/lib/rules/jsx-indent.js index f1acb5e1cd..9a245f1e18 100644 --- a/tests/lib/rules/jsx-indent.js +++ b/tests/lib/rules/jsx-indent.js @@ -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 @@ -1932,6 +1932,11 @@ const Component = () => ( 'text', '' ].join('\n'), + output: [ + '
', + ' text', + '
' + ].join('\n'), errors: [ {message: 'Expected indentation of 4 space characters but found 0.'} ] @@ -1942,6 +1947,12 @@ const Component = () => ( 'text', '' ].join('\n'), + output: [ + '
', + ' text', + ' text', + '
' + ].join('\n'), errors: [ {message: 'Expected indentation of 4 space characters but found 2.'}, {message: 'Expected indentation of 4 space characters but found 0.'} @@ -1953,6 +1964,12 @@ const Component = () => ( ' \t text', '' ].join('\n'), + output: [ + '
', + ' text', + ' text', + '
' + ].join('\n'), errors: [ {message: 'Expected indentation of 4 space characters but found 0.'}, {message: 'Expected indentation of 4 space characters but found 2.'} @@ -1963,7 +1980,13 @@ const Component = () => ( '\t\ttext', '' ].join('\n'), + parser: parsers.BABEL_ESLINT, options: ['tab'], + output: [ + '
', + '\ttext', + '
' + ].join('\n'), errors: [ {message: 'Expected indentation of 1 tab character but found 2.'} ]