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

Fix: Account for comments in implicit-arrow-linebreak #10545

Merged
merged 27 commits into from Dec 22, 2018
Merged

Fix: Account for comments in implicit-arrow-linebreak #10545

merged 27 commits into from Dec 22, 2018

Conversation

ghost
Copy link

@ghost ghost commented Jun 30, 2018

What is the purpose of this pull request? (put an "X" next to item)

[ ] Documentation update
[X ] Bug fix (template)
[ ] New rule (template)
[ ] Changes an existing rule (template)
[ ] Add autofixing to a rule
[ ] Add a CLI option
[ ] Add something to the core
[ ] Other, please explain:

Implicit-arrow-linebreak does not take comments into consideration when the option for the rule is beside.

When the rule evaluates the expression:

(foo) =>
   //comment
   bar;

The outcome is:

(foo) => bar;

The proposed fix is:

// comment
(foo) => bar;

What changes did you make? (Give an overview)

  • Add test cases including comments in tests/lib/rules/implicit-arrow-linebreak
  • Add helper for formatting comment text split by line breaks
  • Implement filtering condition, and fix for when the above bug occurs

Is there anything you'd like reviewers to focus on?

  • This is my first PR for this project -- any feedback would be heavily appreciated.
  • Do the test cases suffice? Any missing logic?
  • What should be the outcome with the test case of
(foo) =>
  // hi
     bar =>
       // there
         braz;

? This one was a doozy.
@sharmilajesupaul

…ow and expression body in implicit-arrow-linebreak
@jsf-clabot
Copy link

jsf-clabot commented Jun 30, 2018

CLA assistant check
All committers have signed the CLA.

@eslint-deprecated eslint-deprecated bot added the triage An ESLint team member will look at this issue soon label Jun 30, 2018
@ghost ghost changed the title Fix implicit arrow linebreak Bug: Fix implicit arrow linebreak Jun 30, 2018
@ilyavolodin
Copy link
Member

Hmm... It seems like this fix is improving current behavior, but I'm not 100% sure this is the best way. In the last example you showed, the only reasonable way to fix it based on your approach would be to move all of the comments above the line:

// hi
// there
(foo) =>bar => braz;

However, chances are, if there's a chaining like that, comments describe what each step of the chain is doing, and if you move them above, it would be unclear. I think best way to fix that would be to change line comments into inline comments.

(foo) => /* hi */ bar => /* there */ braz;

But that might not work and might possibly change the meaning of the code. So I'm not sure what would be the best way to do that.

@not-an-aardvark
Copy link
Member

I think the best solution would be to just not do an autofix if a line comment is found in that location. We have a similar behavior with brace-style, where we don't move the opening bracket when the code looks like this:

if (foo) // comment
{
}

@ghost ghost changed the title Bug: Fix implicit arrow linebreak Fix: Account for comments in implicit-arrow-linebreak Jun 30, 2018
@ljharb
Copy link
Sponsor Contributor

ljharb commented Jun 30, 2018

I'd expect:

(foo) =>
  // hi
     bar =>
       // there
         braz;

to become:

(foo) => (
  // hi
     bar => (
       // there
         braz
      )
);

@ghost
Copy link
Author

ghost commented Jul 1, 2018

@ljharb @not-an-aardvark @ilyavolodin Thanks for all of your input. It seems like prepending the arrow functions with parentheses clarifies the intent of the comments.

I will go ahead and implement this change.

Copy link
Contributor

@sharmilajesupaul sharmilajesupaul left a comment

Choose a reason for hiding this comment

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

Looks good 👍 thanks for fixing this bug!

lib/rules/implicit-arrow-linebreak.js Outdated Show resolved Hide resolved
lib/rules/implicit-arrow-linebreak.js Outdated Show resolved Hide resolved
lib/rules/implicit-arrow-linebreak.js Outdated Show resolved Hide resolved
@ghost
Copy link
Author

ghost commented Jul 9, 2018

@sharmilajesupaul Thanks for the review and comments! Still working on the above test case as well as when:

async (foo) => 
    // comment
    bar

Should be finished by tomorrow or early Wednesday.

@sharmilajesupaul
Copy link
Contributor

@peanutenthusiast instead of trying to find the parent and placing the comment before it. What if you got the line number from the node range and placed the comment on the line before that with a prepended \n?

Also, does this account for cases where you are in an object, or a method chain, or array?

eg:

const foo = {
  id: 'bar'
  prop: (foo1) =>
    // comment
    'returning this string', 
}

[ foo => 
  // comment
  'bar'
]

"foo".split('').map((char) => 
  // comment
  `-${char}-`
))

can you add test cases for those? ^

@ghost
Copy link
Author

ghost commented Jul 9, 2018

@sharmilajesupaul will need to account for the aforementioned test cases as well. thanks!

@ghost
Copy link
Author

ghost commented Jul 10, 2018

@sharmilajesupaul Would the expected output for the method chain test case be

// comment
"foo".split('').map((char) => `-${char}-`
)

or (probably nicer on the eyes)

// comment
"foo".split('').map((char) => `-${char}-`)

?

@sharmilajesupaul
Copy link
Contributor

sharmilajesupaul commented Jul 10, 2018

@peanutenthusiast The paren linebreak is handled by function-paren-newline so I don't think you need to worry about including that in your autofix.

There might be cases where you do want the paren on it's own line:

"foo".split('').map(
  (char) => 
    `-${char}-`,
)

I think that might be out of the scope of this PR.

@ljharb
Copy link
Sponsor Contributor

ljharb commented Jul 10, 2018

this seems like it needs a fresh rebase

@ghost
Copy link
Author

ghost commented Jul 10, 2018

@ljharb agreed and working on it.

@kaicataldo kaicataldo added enhancement This change enhances an existing feature of ESLint rule Relates to ESLint's core rules evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion and removed triage An ESLint team member will look at this issue soon labels Jul 10, 2018
@sharmilajesupaul
Copy link
Contributor

hey @peanutenthusiast! is this ready for another review?

@ghost
Copy link
Author

ghost commented Dec 6, 2018 via email

Copy link
Member

@platinumazure platinumazure left a comment

Choose a reason for hiding this comment

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

Small change request for readability, otherwise looking good!

tests/lib/rules/implicit-arrow-linebreak.js Outdated Show resolved Hide resolved
@nzakas
Copy link
Member

nzakas commented Dec 7, 2018

@peanutenthusiast Sorry we lost track of this!

@platinumazure have your changes been addressed?

@ilyavolodin @not-an-aardvark thoughts on moving this forward?

Copy link
Member

@platinumazure platinumazure left a comment

Choose a reason for hiding this comment

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

Looks good to me, I'm okay to merge when the team accepts.

@not-an-aardvark
Copy link
Member

Should this be marked as a bug rather than an enhancement? Fixers generally shouldn't be deleting comments.

@platinumazure
Copy link
Member

This PR is labeled as "enhancement" but the commit summary says "Fix". Is this a bug fix or an enhancement?

@not-an-aardvark not-an-aardvark added bug ESLint is working incorrectly accepted There is consensus among the team that this change meets the criteria for inclusion and removed enhancement This change enhances an existing feature of ESLint evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion labels Dec 8, 2018
@not-an-aardvark
Copy link
Member

I'm going to re-label it as a bug -- I think the original labelling was a mistake.

@platinumazure platinumazure merged commit 06b3b5b into eslint:master Dec 22, 2018
@platinumazure
Copy link
Member

Thanks for contributing @peanutenthusiast and for your saintly patience as we processed this!

@ghost ghost deleted the fix-implicit-arrow-linebreak branch December 23, 2018 14:39
@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Jun 21, 2019
@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 Jun 21, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion bug ESLint is working incorrectly rule Relates to ESLint's core rules
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants