Skip to content

Conversation

@jpuzz0
Copy link
Contributor

@jpuzz0 jpuzz0 commented Jan 19, 2022

Closes #2815

@jpuzz0 jpuzz0 self-assigned this Jan 19, 2022
@patternfly-build
Copy link
Collaborator

patternfly-build commented Jan 19, 2022

// {...props}
JSXSpreadAttribute(node, state) {
state.write('...(');
state.write(' {...(');
Copy link
Collaborator

Choose a reason for hiding this comment

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

is it possible that sometimes it needs the curly braces and sometime it doesn't?

Copy link
Contributor Author

@jpuzz0 jpuzz0 Jan 19, 2022

Choose a reason for hiding this comment

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

This part of the conversion is specifically for attribute spreading within JSX. I can't think of an example where we would not need the curly braces for this circumstance. I did test that spreading elsewhere in JSX does not enter this block of code, where say if you had something like <div>{[...someArray, ...someOtherArray]}</div>, this is not impacted.

attributes: [],
name: {
type: 'JSXIdentifier',
name: 'React.Fragment'
Copy link
Collaborator

Choose a reason for hiding this comment

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

does this still parse React.Fragment correctly?

Copy link
Contributor Author

@jpuzz0 jpuzz0 Jan 19, 2022

Choose a reason for hiding this comment

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

I did try both, and both are still working. There is an additional block for using JSXFragments where React.Fragment is handled already;

JSXFragment(node, state) {

@jpuzz0 jpuzz0 removed their assignment Jan 19, 2022
nicolethoen
nicolethoen previously approved these changes Jan 19, 2022
Copy link
Collaborator

@nicolethoen nicolethoen left a comment

Choose a reason for hiding this comment

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

I tested this in my react workspace - it LGTM

@nicolethoen nicolethoen requested a review from mturley January 19, 2022 19:03
mturley
mturley previously approved these changes Jan 19, 2022
Copy link
Contributor

@mturley mturley left a comment

Choose a reason for hiding this comment

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

This AST stuff is largely dark magic to me, so I hesitate to say I'm 100% confident in any changes here (when this code was originally written, was it just never tested? in what circumstances would it have worked correctly without the curly braces I wonder?)

But, I tested a bunch of variants of spread syntax and they all seem to work fine for me, so ¯\_(ツ)_/¯ LGTM

Copy link
Contributor

@DaoDaoNoCode DaoDaoNoCode left a comment

Choose a reason for hiding this comment

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

Leave a small comment here. Otherwise, all the fixes look good to me, nice job! @jeffpuzzo

// {...props}
JSXSpreadAttribute(node, state) {
state.write('...(');
state.write(' {...(');
Copy link
Contributor

@DaoDaoNoCode DaoDaoNoCode Jan 20, 2022

Choose a reason for hiding this comment

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

Would it be better to put the ( here inside the if statement below? like:

state.write(' {...');
if (node.argument.type === 'LogicalExpression') {
  state.write('(');
  ......
  state.write(')');
}
......
state.write('}');

We only need the brace when there is a logic expression like && or ||, and in this way, the converted JS code will look better.

@jpuzz0 jpuzz0 dismissed stale reviews from mturley and nicolethoen via 914d05f January 20, 2022 22:33
if (node.argument.type === 'LogicalExpression') {
this[node.argument.left.type](node.argument.left, state);
state.write(' ');
state.write('(');
Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry for not explaining clearly. The state.write('('); should be above this[node.argument.left.type](node.argument.left, state); and the state.write(')'); should be below this[node.argument.right.type](node.argument.right, state);, so it will wrap the whole expression in this way. And we should better keep these 2 state.write(' '); here because we still need the space between the props and the logical operator. So the whole if statement should be like:

if (node.argument.type === 'LogicalExpression') {
  state.write('(');
  this[node.argument.left.type](node.argument.left, state);
  state.write(' ');
  state.write(node.argument.operator);
  state.write(' ');
  this[node.argument.right.type](node.argument.right, state);
  state.write(')');
}

Or you can even combine the center 3 state.write together to be like:

if (node.argument.type === 'LogicalExpression') {
  state.write('(');
  this[node.argument.left.type](node.argument.left, state);
  state.write(` ${node.argument.operator} `);    // or state.write(' ' + node.argument.operator + ' '); 
  this[node.argument.right.type](node.argument.right, state);
  state.write(')');
}

The situation in this if statement is not very commonly seen, many generators don't even consider it, but it's great to keep it here. Hope this makes sense 🙂

Copy link
Contributor

@DaoDaoNoCode DaoDaoNoCode left a comment

Choose a reason for hiding this comment

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

LGTM, nice job, thanks! 😁 @jeffpuzzo

Copy link
Member

@evwilkin evwilkin left a comment

Choose a reason for hiding this comment

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

LGTM - thanks!

@evwilkin evwilkin merged commit 96e9706 into patternfly:main Jan 21, 2022
jessiehuff pushed a commit to jessiehuff/patternfly-org that referenced this pull request Oct 24, 2022
…& allow for empty tags (fragments) (patternfly#2816)

Co-authored-by: Jeff Puzzo <jeffrey.puzzo@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unable to spread props inside components or use empty tags for fragments within markdown examples

6 participants