diff --git a/lib/source-code/source-code.js b/lib/source-code/source-code.js index 20b442f2367..c13caf3f944 100644 --- a/lib/source-code/source-code.js +++ b/lib/source-code/source-code.js @@ -447,7 +447,19 @@ class SourceCode extends TokenStore { while (currentToken !== finalToken) { const nextToken = this.getTokenAfter(currentToken, { includeComments: true }); - if (currentToken.range[1] !== nextToken.range[0]) { + if ( + currentToken.range[1] !== nextToken.range[0] || + + /* + * For backward compatibility, check speces in JSXText. + * https://github.com/eslint/eslint/issues/12614 + */ + ( + nextToken !== finalToken && + nextToken.type === "JSXText" && + /\s/u.test(nextToken.value) + ) + ) { return true; } diff --git a/tests/lib/source-code/source-code.js b/tests/lib/source-code/source-code.js index 29dbf1d6f43..00b80b2205d 100644 --- a/tests/lib/source-code/source-code.js +++ b/tests/lib/source-code/source-code.js @@ -2225,6 +2225,47 @@ describe("SourceCode", () => { }); }); }); + + it("JSXText tokens that contain only whitespaces should be handled as space", () => { + const code = "let jsx =
\n {content}\n
"; + const ast = espree.parse(code, { ...DEFAULT_CONFIG, ecmaFeatures: { jsx: true } }); + const sourceCode = new SourceCode(code, ast); + const jsx = ast.body[0].declarations[0].init; + const interpolation = jsx.children[1]; + + assert.strictEqual( + sourceCode.isSpaceBetweenTokens(jsx.openingElement, interpolation), + true + ); + assert.strictEqual( + sourceCode.isSpaceBetweenTokens(interpolation, jsx.closingElement), + true + ); + }); + + it("JSXText tokens that contain both letters and whitespaces should be handled as space", () => { + const code = "let jsx =
\n Hello\n
"; + const ast = espree.parse(code, { ...DEFAULT_CONFIG, ecmaFeatures: { jsx: true } }); + const sourceCode = new SourceCode(code, ast); + const jsx = ast.body[0].declarations[0].init; + + assert.strictEqual( + sourceCode.isSpaceBetweenTokens(jsx.openingElement, jsx.closingElement), + true + ); + }); + + it("JSXText tokens that contain only letters should NOT be handled as space", () => { + const code = "let jsx =
Hello
"; + const ast = espree.parse(code, { ...DEFAULT_CONFIG, ecmaFeatures: { jsx: true } }); + const sourceCode = new SourceCode(code, ast); + const jsx = ast.body[0].declarations[0].init; + + assert.strictEqual( + sourceCode.isSpaceBetweenTokens(jsx.openingElement, jsx.closingElement), + false + ); + }); }); describe("should return true when there is at least one whitespace character between a token and a node", () => {