question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Inline comment after func params is printed inside the params

See original GitHub issue

Original

I’ve migrated a large codebase which used this function definition style:

function func(a, b)  // `a` is the first arg
{
 return 'done'
}

Reprinted

prettier reprinted all comments following function params like this:

function func(
  a,
  b // `a` is the first arg
) {
  return "done";
}

This is extra confusing as the comment is on the same line as a parameter it does not refer to. We have many of these cases as you might imagine.

Proposal

The comment must be moved since an inline comment can’t appear between the closing paren and opening curly: ) {. Since the original comment was not inside the parentheses, IMHO, it should not be moved inside when reprinted.

I would think most humans would rewrite the original function with the comment on the line above the function:

// `a` is the first arg
function func(a, b) {
  return "done";
}

Would love to hear feedback on this.

https://prettier.io/playground/#{“content”%3A"function func(a%2C b) %2F%2F `a` is the first arg\n{\n return ‘done’\n}“%2C"options”%3A{“printWidth”%3A80%2C"tabWidth"%3A2%2C"singleQuote"%3Afalse%2C"trailingComma"%3A"es5"%2C"bracketSpacing"%3Atrue%2C"jsxBracketSameLine"%3Afalse%2C"parser"%3A"babylon"%2C"semi"%3Atrue%2C"useTabs"%3Afalse%2C"doc"%3Afalse%2C"ast"%3Afalse}}

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
levithomasoncommented, Aug 30, 2017

Hm, thinking this through. I’m not sure what the impact might be to code that must appear directly above a function like decorators, docblocks, etc.

A less desirable alternative might be to print the inline comment on the first line of the func body. Will leave it at that for now.

0reactions
levithomasoncommented, Aug 30, 2017

Agreed, closing for the sake of progress.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Writing comment block for function where parameters change
After @param, it should be the type of the variables you pass, if there are several possible types, you can separate them with...
Read more >
Examples of Comment-Based Help - PowerShell
The following sample function includes comment-based Help. Notice the blank lines between the closing #> and the Param statement. In a script ...
Read more >
Writing Comments in Python (Guide)
Learn how to write Python comments that are clean, concise, and useful. Quickly get up to speed on what the best practices are,...
Read more >
How to Write Doc Comments for the Javadoc Tool - Oracle
Our documentation comments define the official Java Platform API Specification. ... In this example, the block tags are @param , @return , and...
Read more >
Activity: Function Documentation - Microsoft MakeCode Arcade
To comment on a parameter, start the line with @param , followed by the parameter's name, and then a short description of what...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found