eslint-plugin-react icon indicating copy to clipboard operation
eslint-plugin-react copied to clipboard

autofix for jsx-sort-props doesn't respect comments

Open 7rulnik opened this issue 6 years ago • 14 comments

Autofix for jsx-sort-props doesn't respect comments.

For example:

<foo
  b={1}
  // comment for a
  a={0}
/>

will be fixed like this:

<foo
  a={0}
  // comment for a
  b={1}
/>

But I think it should be like this:

<foo
  // comment for a
  a={0}
  b={1}
/>

7rulnik avatar Jul 31 '19 23:07 7rulnik

Comment attachment is a very hard problem; some people put comments above, and some below, and many don’t comment at all.

We may be able to handle something here, but we’d be constrained by how Babel implemented comment attachment.

ljharb avatar Aug 01 '19 00:08 ljharb

Yeah, I agree.

About Babel AST:

<foo
  a={0}
  // comment for a
  b={1}
/>

For this example Babel will keep comment in ast for a prop as trailing comment and for b same comment as leading. I think we can configure it via options. Something like preserveComment:trailing|leading. But seems that it will be not so trivial.

But maybe we can implement option just for disabling autofix?

7rulnik avatar Aug 01 '19 09:08 7rulnik

It seems prudent to disable autofixes entirely for props that have a leading or a trailing comment, no option needed.

ljharb avatar Aug 01 '19 21:08 ljharb

Maybe we should disable it entirely for component's that contains props with comments? Otherwise result of autofix will be confusing.

For example group of props before comment sorted in alphabetical order and after comment too. But it doesn't make sense, because it's still not valid.

<input
  a
  b
  c
  // fofof
  f
  a
  b
  c
/>

If so, I can submit PR

7rulnik avatar Aug 02 '19 00:08 7rulnik

In that example I’d expect everything to be sorted except the ones surrounding the comment, and the dev being forced to manually fix those.

ljharb avatar Aug 02 '19 03:08 ljharb

So, something like this? Ok, I will take a look

<input
  a
  a
  b
  b
  c
  c
  // fofof
  f
/>

7rulnik avatar Aug 02 '19 08:08 7rulnik

Have you had a look yet, @7rulnik ? No pressure; I'm just evaluating whether to have a go at this myself, and I'd like to hear if you're stuck at some bump which I'm also likely to hit.

Vages avatar Nov 11 '19 11:11 Vages

And by the way, in your example: Has the c directly above the f prop been moved because it is connected to the comment, or has it just been moved because that's the correct place to put it when sorting alphabetically?

Vages avatar Nov 11 '19 11:11 Vages

@Vages if the original was:

<input
  m
  n // this is n
  o
  c // this is c
  // fofof
  f // this is f
  a
  b
  c
/>

it should autocorrect to:

<input
  a
  b
  c
  m
  n // this is n
  o
  c // this is c
  // fofof
  f // this is f
/>

In other words, any line touching a standalone comment on either side should be left alone / sorted last, as a single group.

ljharb avatar Nov 29 '19 05:11 ljharb

I think there are two distinct issues here: Not moving the beside comment is a bug. Behavior for other comment locations is a feature.

jsphstls avatar Dec 02 '19 18:12 jsphstls

@jsphstls i agree that a same-line comment should be moved along with it, and should not interfere with sorting.

ljharb avatar Dec 02 '19 19:12 ljharb

For single-line props:

<input f e d /* comment */ c b a />

should autocorrect to:

<input a b d /* comment */ c e f />

or

<input a b e f d /* comment */ c />

as long as the d+comment+c group was kept atomic.

ljharb avatar Jul 15 '22 16:07 ljharb

@ljharb I am having trouble with my approach to the problem and wanted to clarify if it is appropriate. As per the examples you have given above, I believe grouping to the AST nodes and comments before sorting would work. I have been able to identify the attribute nodes and comments. However, I tried using AST explorer to find comments as nodes, but I was unable to.

Question: Am I heading in the right direction? If so, How should attributes and comments be identified and linked?

Any advice or guidance would be greatly appreciated!

ROSSROSALES avatar Aug 02 '22 04:08 ROSSROSALES

@ROSSROSALES I think the comments will be attached as a property on an adjacent node of another kind.

ljharb avatar Aug 04 '22 18:08 ljharb