Skip to content

Commit

Permalink
Fix: isSpaceBetween() recognizes spaces in JSXText (fixes #12614)
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticatea committed Nov 28, 2019
1 parent ea16de4 commit 8e73ee3
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
14 changes: 13 additions & 1 deletion lib/source-code/source-code.js
Expand Up @@ -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;
}

Expand Down
41 changes: 41 additions & 0 deletions tests/lib/source-code/source-code.js
Expand Up @@ -2225,6 +2225,47 @@ describe("SourceCode", () => {
});
});
});

it("JSXText tokens that contain only whitespaces should be handled as space", () => {
const code = "let jsx = <div>\n {content}\n</div>";
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 = <div>\n Hello\n</div>";
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 = <div>Hello</div>";
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", () => {
Expand Down

0 comments on commit 8e73ee3

Please sign in to comment.