From d4f9a16af7e00021e2ed63823d9c2f149bc985d6 Mon Sep 17 00:00:00 2001 From: Kai Cataldo <7041728+kaicataldo@users.noreply.github.com> Date: Thu, 19 Sep 2019 20:01:00 -0400 Subject: [PATCH] Update: add support for JSXFragments in indent rule (fixes #12208) (#12210) --- lib/rules/indent.js | 28 +++ tests/lib/rules/indent.js | 398 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 426 insertions(+) diff --git a/lib/rules/indent.js b/lib/rules/indent.js index f44eafa715c..a2fa9c4f409 100644 --- a/lib/rules/indent.js +++ b/lib/rules/indent.js @@ -81,6 +81,9 @@ const KNOWN_NODES = new Set([ "WhileStatement", "WithStatement", "YieldExpression", + "JSXFragment", + "JSXOpeningFragment", + "JSXClosingFragment", "JSXIdentifier", "JSXNamespacedName", "JSXMemberExpression", @@ -1453,6 +1456,31 @@ module.exports = { offsets.setDesiredOffsets(node.name.range, firstToken, 1); }, + JSXFragment(node) { + const firstOpeningToken = sourceCode.getFirstToken(node.openingFragment); + const firstClosingToken = sourceCode.getFirstToken(node.closingFragment); + + addElementListIndent(node.children, firstOpeningToken, firstClosingToken, 1); + }, + + JSXOpeningFragment(node) { + const firstToken = sourceCode.getFirstToken(node); + const closingToken = sourceCode.getLastToken(node); + + offsets.setDesiredOffsets(node.range, firstToken, 1); + offsets.matchOffsetOf(firstToken, closingToken); + }, + + JSXClosingFragment(node) { + const firstToken = sourceCode.getFirstToken(node); + const slashToken = sourceCode.getLastToken(node, { skip: 1 }); + const closingToken = sourceCode.getLastToken(node); + const tokenToMatch = astUtils.isTokenOnSameLine(slashToken, closingToken) ? slashToken : closingToken; + + offsets.setDesiredOffsets(node.range, firstToken, 1); + offsets.matchOffsetOf(firstToken, tokenToMatch); + }, + JSXExpressionContainer(node) { const openingCurly = sourceCode.getFirstToken(node); const closingCurly = sourceCode.getLastToken(node); diff --git a/tests/lib/rules/indent.js b/tests/lib/rules/indent.js index e2d6202a998..96b7463921c 100644 --- a/tests/lib/rules/indent.js +++ b/tests/lib/rules/indent.js @@ -4651,6 +4651,210 @@ ruleTester.run("indent", rule, { `, + /* + * JSX Fragments + * https://github.com/eslint/eslint/issues/12208 + */ + unIndent` + <> + + + `, + unIndent` + < + > + + + `, + unIndent` + <> + + < + /> + `, + unIndent` + <> + + + `, + unIndent` + < + > + + + `, + unIndent` + < + > + + < + /> + `, + unIndent` + < // Comment + > + + + `, + unIndent` + < + // Comment + > + + + `, + unIndent` + < + // Comment + > + + + `, + unIndent` + <> + + < // Comment + /> + `, + unIndent` + <> + + < + // Comment + /> + `, + unIndent` + <> + + < + // Comment + /> + `, + unIndent` + <> + + + `, + unIndent` + <> + + + `, + unIndent` + <> + + + `, + unIndent` + < /* Comment */ + > + + + `, + unIndent` + < + /* Comment */ + > + + + `, + unIndent` + < + /* Comment */ + > + + + `, + unIndent` + < + /* + * Comment + */ + > + + + `, + unIndent` + < + /* + * Comment + */ + > + + + `, + unIndent` + <> + + < /* Comment */ + /> + `, + unIndent` + <> + + < + /* Comment */ /> + `, + unIndent` + <> + + < + /* Comment */ /> + `, + unIndent` + <> + + < + /* Comment */ + /> + `, + unIndent` + <> + + < + /* Comment */ + /> + `, + unIndent` + <> + + + `, + unIndent` + <> + + + `, + unIndent` + <> + + + `, + unIndent` + <> + + + `, + unIndent` + <> + + + `, + // https://github.com/eslint/eslint/issues/8832 unIndent`
@@ -9576,6 +9780,200 @@ ruleTester.run("indent", rule, { `, errors: expectedErrors([2, 4, 0, "Punctuator"]) }, + + /* + * JSX Fragments + * https://github.com/eslint/eslint/issues/12208 + */ + { + code: unIndent` + <> + + + `, + output: unIndent` + <> + + + `, + errors: expectedErrors([2, 4, 0, "Punctuator"]) + }, + { + code: unIndent` + < + > + + + `, + output: unIndent` + < + > + + + `, + errors: expectedErrors([2, 0, 4, "Punctuator"]) + }, + { + code: unIndent` + <> + + < + /> + `, + output: unIndent` + <> + + < + /> + `, + errors: expectedErrors([4, 0, 4, "Punctuator"]) + }, + { + code: unIndent` + <> + + + `, + output: unIndent` + <> + + + `, + errors: expectedErrors([4, 0, 4, "Punctuator"]) + }, + { + code: unIndent` + < + > + + + `, + output: unIndent` + < + > + + + `, + errors: expectedErrors([ + [2, 0, 4, "Punctuator"], + [5, 0, 4, "Punctuator"] + ]) + }, + { + code: unIndent` + < + > + + < + /> + `, + output: unIndent` + < + > + + < + /> + `, + errors: expectedErrors([ + [2, 0, 4, "Punctuator"], + [5, 0, 4, "Punctuator"] + ]) + }, + { + code: unIndent` + < // Comment + > + + + `, + output: unIndent` + < // Comment + > + + + `, + errors: expectedErrors([2, 0, 4, "Punctuator"]) + }, + { + code: unIndent` + <> + + < // Comment + /> + `, + output: unIndent` + <> + + < // Comment + /> + `, + errors: expectedErrors([4, 0, 4, "Punctuator"]) + }, + { + code: unIndent` + <> + + + `, + output: unIndent` + <> + + + `, + errors: expectedErrors([4, 0, 4, "Punctuator"]) + }, + { + code: unIndent` + < /* Comment */ + > + + + `, + output: unIndent` + < /* Comment */ + > + + + `, + errors: expectedErrors([2, 0, 4, "Punctuator"]) + }, + { + code: unIndent` + <> + + < /* Comment */ + /> + `, + output: unIndent` + <> + + < /* Comment */ + /> + `, + errors: expectedErrors([4, 0, 4, "Punctuator"]) + }, + { + code: unIndent` + <> + + + `, + output: unIndent` + <> + + + `, + errors: expectedErrors([4, 0, 4, "Punctuator"]) + }, + { code: unIndent` ({