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

[feature request] react/jsx-newline #3033

Closed
adrienharnay opened this issue Aug 5, 2021 · 10 comments · Fixed by #3311
Closed

[feature request] react/jsx-newline #3033

adrienharnay opened this issue Aug 5, 2021 · 10 comments · Fixed by #3311

Comments

@adrienharnay
Copy link

adrienharnay commented Aug 5, 2021

Hi,

I would find it interesting to provide a new option to the react/jsx-newline rule, so that there is a newline between JSX expressions ONLY if they are multiline.

I find that single line JSX expressions are easier to read and don't require a space between them, but multiline expressions can quickly become hard to read.

Would anyone be eager to help me implement this feature, as this would be the first rule I would contributing to the project?

Thanks in advance!

@ljharb
Copy link
Member

ljharb commented Aug 11, 2021

Can you give an example of a single-line component? I'm having trouble imagining when a component would be so simple that it's best expressed as a single line.

@adrienharnay
Copy link
Author

I realise now an example would have helped, here it is:

const MyComponent = () => {
  return (
    <div>
      <OneLineComponent />
      <AnotherOneLineComponent prop={prop} />
      
      <MultilineComponent
        prop1={prop1}
        prop2={prop2}
      />

      <OneLineComponent />
    </div>
  );
}

I was more talking about JSX expressions than components, sorry again for the confusion.

@ljharb
Copy link
Member

ljharb commented Aug 12, 2021

ahhh ok, thanks. That sounds like a great option to me.

@adrienharnay
Copy link
Author

Would anyone be up to help me tackle this?

@ljharb
Copy link
Member

ljharb commented Aug 17, 2021

I'm happy to review a PR, and give guidance if you have specific questions.

@TildaDares
Copy link
Contributor

Hi @ljharb, I'd like to work on this!

@TildaDares
Copy link
Contributor

Just to clarify, when prevent == true we want to allow a newline ONLY if the JSX expression is multiline.

@ljharb
Copy link
Member

ljharb commented Jun 16, 2022

@TildaDares go for it! I think we'd want to keep the same behavior by default, but add a new option that ignored single-line jsx expressions.

@TildaDares
Copy link
Contributor

Thanks @ljharb! I'm not sure I'm on the right track but I tried adding a new option like this:

        properties: {
          prevent: {
            default: false,
            type: 'boolean',
          },
          allowMultilines: {
            type: 'boolean',
          },
        },

But I got this error:

Error: rule-tester:
	Configuration for rule "jsx-newline" is invalid:
	Value [{"prevent":true},{"allowMultilines":true}] should NOT have more than 1 items.

How do I configure the jsx-newline rule to allow more options?

@TildaDares
Copy link
Contributor

This was a mistake on my part. The allowMultilines key:value pair should have been in the same object as prevent.

Thanks @ljharb! I'm not sure I'm on the right track but I tried adding a new option like this:

        properties: {
          prevent: {
            default: false,
            type: 'boolean',
          },
          allowMultilines: {
            type: 'boolean',
          },
        },

But I got this error:

Error: rule-tester:
	Configuration for rule "jsx-newline" is invalid:
	Value [{"prevent":true},{"allowMultilines":true}] should NOT have more than 1 items.

How do I configure the jsx-newline rule to allow more options?

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