Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: isSpaceBetweenTokens() recognizes spaces in JSXText (fixes #12614) #12616

Merged
merged 5 commits into from Nov 30, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
)
mysticatea marked this conversation as resolved.
Show resolved Hide resolved
) {
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", () => {
kaicataldo marked this conversation as resolved.
Show resolved Hide resolved
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