Skip to content

Conversation

@diki
Copy link
Contributor

@diki diki commented Mar 8, 2016

I think problem occurs because sourceCode.getComments(node).leading miscalculates, this is a quick fix.

@mention-bot
Copy link

By analyzing the blame information on this pull request, we identified @kaicataldo to be a potential reviewer

@eslintbot
Copy link

Thanks for the pull request, @diki! I took a look to make sure it's ready for merging and found some changes are needed:

  • Pull requests with code require an issue to be mentioned at the end of the commit summary, such as (fixes #1234). Please update the commit summary with an issue (file a new issue if one doesn't already exist).

Can you please update the pull request to address these?

(More information can be found in our pull request guide.)

@diki diki force-pushed the issue5480 branch 2 times, most recently from ff07c57 to b4f5d87 Compare March 8, 2016 12:35
@btmills
Copy link
Member

btmills commented Mar 8, 2016

Thanks, @diki! Can you add some tests to this to make sure we don't inadvertently reintroduce this bug later?

@diki
Copy link
Contributor Author

diki commented Mar 8, 2016

@btmills 👍

@kaicataldo
Copy link
Member

I'm not at my computer right now, but it looks like blocks that only contain comments aren't getting calculated correctly. Do you know what exactly is happening?

I'd love to see a few more tests for other scenarios (if statements, etc.).

Would you mind writing the actual cause in the comment rather than just linking to the issue? Makes it easier for the next person :)

Thanks for fixing this!

@kaicataldo
Copy link
Member

I'd also love to see the inverse invalid cases tested as well, if possible :)

@diki
Copy link
Contributor Author

diki commented Mar 8, 2016

@kaicataldo

https://github.com/eslint/eslint/blob/master/lib/rules/newline-before-return.js#L70
sourceCode.getComments(node).leading miscalculates comment lines for return if there are no other expressions between return and block upside of return. That is; method counts for the comment lines inside block while it should only count for comment lines between return and block statement,

fail

function main() {
  var test = {
    //
  };

  //
  return 1;
}

calcCommentLines = 2

also fail

function main() {
  var test = {
    //
   /*

   */
  };

  //
  return 1;
}

calcCommentLines = 5

fail

function main() {
  var a = 5;
  if(true) {
    //
  }

  return 1;
}

fail if it is if statement

function main() {
  if(true) {
    //
  }

  return 1;
}

last fail :)

function main() {
  var a = 5;
  if(true) {
    var foo = 6;
    //
  }

  return 1;
}

pass

function main() {
  var test = {
    //
  };
  var foo = 5;

  //
  return 1;
}
calcCommentLines = 1

Fixing getCommentsmethod on parser (espree?) would be much nicer yet this commit quickly fixes this issue.

@kaicataldo
Copy link
Member

Ah, got it - thanks for the thorough explanation! I've asked in the issue of this is intended behavior or a bug, so hopefully we can understand why that's the case. Great work here!

@mysticatea
Copy link
Member

Looks good to me, Thank you!

mysticatea added a commit that referenced this pull request Mar 10, 2016
Fix: newline-before-return: bug with comment (fixes: 5480)
@mysticatea mysticatea merged commit b54c006 into eslint:master Mar 10, 2016
@diki
Copy link
Contributor Author

diki commented Mar 10, 2016

👍

@ericclemmons
Copy link

Thanks for doing this!

@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Feb 6, 2018
@eslint-deprecated eslint-deprecated bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Feb 6, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

archived due to age This issue has been archived; please open a new issue for any further discussion

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants