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

Put comma after any non-whitespace non-comment characters in XJSExpression #3129

Closed
wants to merge 9 commits into from

Conversation

jasom
Copy link
Contributor

@jasom jasom commented Feb 12, 2015

For some reason the comma was emitted in renderXJSExpressionContainer if
it was an XJSExpressionContainer, and in the caller of the function
otherwise. This made getting things like <Foo bar=(baz) quux={biff} />
hard.

This should fix issue #1673

@facebook-github-bot
Copy link

Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. In order for us to review and merge your code, please sign up at https://code.facebook.com/cla - and if you have received this in error or have any questions, please drop us a line at [email protected]. Thanks!

@jasom
Copy link
Contributor Author

jasom commented Feb 12, 2015

Re: the TravisCI failure:

Hmm; that test passed locally; I'll check if I'm missing anything from the commit, and update.

@jasom
Copy link
Contributor Author

jasom commented Feb 12, 2015

Added in the missing change, and signed cla.

@zpao
Copy link
Member

zpao commented Feb 12, 2015

Hmm, can you run npm test locally - it looks like that might not be working on Travis (the grunt jest task doesn't seem to be outputting anything and is passing but running locally it fails). This will run our transform tests (we should also add one for whatever changes we have here, looks like there was a test case in the linked issue).

@jasom
Copy link
Contributor Author

jasom commented Feb 12, 2015

Ah, npm test is much better; I was doing a grunt test previously to test it, per the readme. It looks like it wants the comma before the following whitespace. Is this a correctness issue with ASI in the event of a newline, or just an aesthetic thing?

@facebook-github-bot
Copy link

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks!

@jasom
Copy link
Contributor Author

jasom commented Feb 12, 2015

The tests seem to think that for:

<foo> {
 bar
//comment
}
{ baz}
</foo>

The comma ought go immediately after bar. That surprises me; does anyone happen to know why this is the case? I can sort-of see putting the comma after the last non-whitespace item in the braces, but putting it before comments seems rather odd to me.

@syranide
Copy link
Contributor

The reason for them being inlined into the sub visitors is to have control over where they end up. Moving it out like that works, but you then end up with commas after newlines/whitespace rather than before as you would write yourself.

@jasom
Copy link
Contributor Author

jasom commented Feb 12, 2015

@syranide Starting to get a bit off topic, but as far as "as you would write yourself." you probably wouldn't put a space right before a newline, so that becomes a question of how far you want to take it. I'm testing a patch that fixes #1673 without changing any existing output; it's a lot more complicated than this one though.

@syranide
Copy link
Contributor

@jasom I don't see how this is off-topic, the current output by JSX (except for whitespace before newline) was agreed on in #970 and follows common style-guides/lints and the way you would write it yourself, this PR seems to undo just that?

, \n is a deficiency in the current implementation due to the complexity of solving it (it seems).

@jasom
Copy link
Contributor Author

jasom commented Feb 12, 2015

@syranide I meant the , \n nitpick was off topic, not your comment.

@syranide
Copy link
Contributor

@jasom Ah :)

@jasom jasom changed the title Regularize emitting of comma on XJSExpressions Put comma after any non-whitespace non-comment characters in XJSExpression Feb 12, 2015
@jasom
Copy link
Contributor Author

jasom commented Feb 12, 2015

Okay, I fixed the same issue in a completely different way; it should change not at all any whitespace placement. I also added a unit test for the issue I fixed.

@syranide
Copy link
Contributor

@jasom Your commit is broken (it includes git comments).

var commaPos = 0;
//console.log('"'+value+'"')
for (var i=0;i<value.length;++i) {
switch (state) {
Copy link
Contributor

Choose a reason for hiding this comment

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

My vote goes to if instead of this nested switch nightmare.

@jasom
Copy link
Contributor Author

jasom commented Feb 12, 2015

@syranide Sorry, I pushed the wrong branch

@jasom
Copy link
Contributor Author

jasom commented Feb 12, 2015

@syranide Fixed up git garbage, converted switch to if/else. I have to go to work now so any other changes will have to wait until tonight.

@zpao
Copy link
Member

zpao commented Feb 12, 2015

cc @jeffmo

var state = "normal";
var commaPos = 0;
for (var i=0;i<value.length;++i) {
if (state === "normal") {
Copy link
Contributor

Choose a reason for hiding this comment

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

You have 3 else-branches with the same commaPos = i+1;, it should be possible to reduce and flatten this unless there's a very good reason for this verbosity.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, the state machine started out a bit differently, I'll clean that up. Also, if you know a better way of checking for whitespace than (foo.trim() === "") let me know.

Copy link
Member

Choose a reason for hiding this comment

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

Couple small style nits (can fixup later with anything else that might come):

  • 2 space indent (match the rest of the files)
  • spaces around operators (for (var i = 0; i < value.length; ++i))
  • use === and semicolons consistently. The line below this is missing the former. Line 28 the latter.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@zpao I fixed those in all the places I found; is there a style linter I can use to check for this in the future?

Copy link
Contributor

Choose a reason for hiding this comment

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

You can run eslint with grunt eslint or grunt lint

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@hzoo Yeah, I did that, but it didn't flag any of the style issues zpao pointed out.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh then it's probably because it's not being processed - yeah src/vendor is in the eslintignore.

# We can probably lint these later but not important at this point
src/vendor

So instead you could try installing it globally and run it on a single file or remove the ignore from the file temporarily.

@jasom
Copy link
Contributor Author

jasom commented Feb 19, 2015

Is there anything else I need do to get this PR ready for merge?

@zpao
Copy link
Member

zpao commented Feb 19, 2015

There are still a few stylistic things. I'm in the process of enabling eslint here so that these files get linted and can tell you what's going on (namely use single quotes, match other styles with indentation, else bracing). If you rebase on top of #3206 (or it will be in master shortly I think) then you can see this yourself with grunt lint (that grunt task doesn't take params and is actually a bit noisy about long lines in other code so you could also run ./node_modules/.bin/eslint vendor/fbtransform directly)

@jasom
Copy link
Contributor Author

jasom commented Feb 20, 2015

The changes I made should be clean to eslint now (I ran it directly on
the file last night). Do you want me to clean up the entire jsx.js?

-Jason
On 15:34 Thu 19 Feb , Paul O’Shannessy wrote:

There are still a few stylistic things. I'm in the process of enabling
eslint here so that these files get linted and can tell you what's
going on (namely use single quotes, match other styles with
indentation, else bracing). If you rebase on top of [1]#3206 (or it
will be in master shortly I think) then you can see this yourself with
grunt lint (that grunt task doesn't take params and is actually a bit
noisy about long lines in other code so you could also run
./node_modules/.bin/eslint vendor/fbtransform directly)


Reply to this email directly or [2]view it on GitHub.

References

  1. Lint vendor/fbtransform as well #3206
  2. Put comma after any non-whitespace non-comment characters in XJSExpression #3129 (comment)

@zpao
Copy link
Member

zpao commented Feb 20, 2015

Please rebase and re-run. If you were running eslint from your branch point, it wasn't configured correctly. It has since been configured appropriately and I cleaned up everything else in the above PR. The remaining errors and warnings are a result of your changes (image below is with your branch rebased on master).
screen shot 2015-02-19 at 6 44 29 pm

@@ -84,11 +121,13 @@ function renderXJSExpressionContainer(traverse, object, isLast, path, state) {
if (!isLast && object.expression.type !== Syntax.XJSEmptyExpression) {
// If we need to append a comma, make sure to do so after the expression.
utils.catchup(object.expression.range[1], state, trimLeft);
utils.append(', ', state);
utils.catchup(object.range[1] -1, state, commaAfterLastParen)
//utils.append(', ', state);
Copy link
Member

Choose a reason for hiding this comment

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

Lint doesn't catch it but please remove this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How embarrassing! I removed it now.

@jasom
Copy link
Contributor Author

jasom commented Feb 20, 2015

Thanks @zpao after rebase fixing the lint issues was easy.

@jasom
Copy link
Contributor Author

jasom commented Feb 20, 2015

Let me know if you want me to squash some of these cleanup commits together

var commaPos = 0;
for (var i = 0; i < value.length; ++i) {
if (state === 'normal') {
if (value.charAt(i) === '/') {
Copy link
Contributor

Choose a reason for hiding this comment

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

State-machine type of deal and all that... but this could be reduced to value.substr(i, 2) === '//', etc. @zpao?

Copy link
Contributor

Choose a reason for hiding this comment

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

+1

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I suppose it wouldn't surprise you that my day job uses mostly C...

@jasom
Copy link
Contributor Author

jasom commented Feb 25, 2015

Any other changes needed?

@jasom
Copy link
Contributor Author

jasom commented Mar 3, 2015

Just checking in on the status of this.

@jasom
Copy link
Contributor Author

jasom commented Mar 10, 2015

Just checking in to see if any action is needed by me on this.

@zpao
Copy link
Member

zpao commented Mar 10, 2015

Sorry, been focused on shipping 0.13. This won't make that cut but I'll come back to it after. In the mean time, this doesn't merge cleanly (probably mostly just because xjs.js is now jsx.js) so getting it back into a mergable state will help.

@jasom
Copy link
Contributor Author

jasom commented Mar 12, 2015

Now rebased on top of master

@jasom
Copy link
Contributor Author

jasom commented Mar 18, 2015

0.13 is out now. @zpao: Let me know if I'm being too annoying.

@zpao zpao closed this in ef79679 Mar 20, 2015
zpao added a commit that referenced this pull request Mar 20, 2015
Put comma after any non-whitespace non-comment characters in JSXExpression
@zpao
Copy link
Member

zpao commented Mar 20, 2015

Squashed and landed as ef79679. Thanks and I'm sorry it took as long as it did!

Pouja pushed a commit to delftswa2014/react that referenced this pull request Mar 23, 2015
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.

6 participants