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] jsx-indent: Does not check indents for JSXText #2542

Merged
merged 1 commit into from Feb 1, 2020

Conversation

toshi-toma
Copy link
Contributor

@toshi-toma toshi-toma commented Jan 14, 2020

Fixes #2467, Fixes #2484, Fixes #1136
This PR fixes the problem of does not check jsx-indent for JSXText.

JSXText Node is different from other formats.
e.g. The following code. JSXText's node.value returns \n text\n text\n .

const f = () => (
    <div>
        text
        text
    </div>
);
changes
  1. add handler for JSXText Node
    https://github.com/yannickcr/eslint-plugin-react/blob/0076cb09abfed7a3777102359efb5f39dee2e01e/lib/rules/jsx-indent.js#L406-L412

  2. check JSXText Indent
    https://github.com/yannickcr/eslint-plugin-react/blob/0076cb09abfed7a3777102359efb5f39dee2e01e/lib/rules/jsx-indent.js#L304-L341

  3. add the fix for JSXText
    https://github.com/yannickcr/eslint-plugin-react/blob/0076cb09abfed7a3777102359efb5f39dee2e01e/lib/rules/jsx-indent.js#L100-L104

  4. add test cases
    tests/lib/rules/jsx-indent.js

Copy link
Member

@ljharb ljharb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this looks great!

* @param {RegExp} regex
* @returns {Array}
*/
function matchAll(str, regex) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's no need to hardcode this when https://npmjs.com/string.prototype.matchall exists; please use that instead.

lib/rules/jsx-indent.js Outdated Show resolved Hide resolved
const matches = matchAll(value, regExp);
matches.forEach((match) => {
if (match[1]) {
nodeIndentsPerLine.push(match[1].length);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this whole body can then be replaced with:

return match[1] ? match[1].length : 0;

@toshi-toma
Copy link
Contributor Author

toshi-toma commented Jan 16, 2020

@ljharb
Thank you for the review!!
I fixed.

but, CI is failed...
The cause is JSXText only available Node to ESLint v5 or more.
eslint/eslint#10152

What do you think about supporting the ESLint version?

@ljharb
Copy link
Member

ljharb commented Jan 28, 2020

@toshi-toma is there no way to identify text inside JSX in eslint < 5? It seems pretty likely that there is something, it's just got a different node type.

@golopot
Copy link
Contributor

golopot commented Jan 31, 2020

In older eslint (and current babel-eslint), the node type of text inside JSX is Literal.

@toshi-toma
Copy link
Contributor Author

toshi-toma commented Feb 1, 2020

@ljharb
I fixed to work for older eslint. and fixed some tests.
Now, CI is failing in Node v4...
I can't understand the cause.

lib/rules/jsx-indent.js Outdated Show resolved Hide resolved
@ljharb ljharb merged commit ffdf69a into jsx-eslint:master Feb 1, 2020
@toshi-toma toshi-toma deleted the jsx-indent-for-jsxtext branch February 2, 2020 01:51
@ljharb
Copy link
Member

ljharb commented Feb 2, 2020

This caused #2561, which was fixed in 3da6eb3 - namely, the issue was that the literal check assumed strings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants