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.

Questions about new indent rule behavior in regards to comments

See original GitHub issue

Tell us about your environment

  • ESLint Version: Master
  • Node Version: 6.x
  • npm Version: 4.x

What parser (default, Babel-ESLint, etc.) are you using? Espree

Please show your full configuration:

{
  "rules": {
    "indent": ["error", 2]
  }
}

What did you do? Please include the actual source code causing the issue. Noticed an inconsistency with how comments are handled by the new indent rule

What did you expect to happen? For this to pass:

function foo() {
  if (true) {
    // some comment
  }
}

And for these to not:

function foo() {
  if (true) {
// some comment
  }
}

function foo() {
  if (true) {
  // some comment
  }
}

What actually happened? Please include the actual, raw output from ESLint. Both the following code snippets do not throw an error with the above configuration:

function foo() {
  if (true) {
    // some comment
  }
}

function foo() {
  if (true) {
  // some comment
  }
}

However, the following example does:

function foo() {
  if (true) {
// some comment
  }
}
/Users/cataldo/Code/test/eslint-test/index.js
  3:1  error  Expected indentation of 4 spaces but found 0  indent

I think there are two issues at play here:

  • I believe the error message is correct, and that it should enforce for 4 spaces with the above code snippet
  • It’s confusing that the error message expects 4 spaces but is satisfied with 2 spaces

Issue Analytics

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

github_iconTop GitHub Comments

3reactions
kaicataldocommented, Jun 10, 2017

The problem I’m trying to solve is that I’ve enabled the new rule in our codebases at work and am able to fix everything but comments. Manually going through and fixing a PR that has +3,978 −3,991 changes is difficult.

I wonder if we could make the rule smarter by saying that it’s safe to assume the comment should be indented if it’s at the top of a block?

I have a lot of instances where the following:

function foo() {
  return bar
  .then((baz) => {
    // some comment
    return qux(baz);
  }
}

get autofixed to:

function foo() {
  return bar
    .then((baz) => {
    // some comment
      return qux(baz);
    }
}

It seems like a safe assumption that the comment should also be indented, but maybe I’m missing something here!

Edit: Also wanted to mention that the new indent rule is great! Having it not do crazy things when autofixing is amazing 🎉

Edit 2: Fixed a typo in the example

1reaction
not-an-aardvarkcommented, Jul 3, 2017

@yairEO This is working as intended. If you’d like a version of the indent rule that is less strict and ignores comments, you can use the indent-legacy rule.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Allow comments that match any previous level of indentation
Comments that match a previous level of indentation should be allowed, at the end of that level of indentation.
Read more >
indentation - How to align "end of line comments" in sh-mode?
Choose the indentation to use for a right-hand-side comment. The criteria are (in this order): - try to keep the comment's text within...
Read more >
What setting in vim counteracts smartindent's refusal to indent ...
When editing a shell script, I try to create a comment at the current indent level, but smartindent will have nothing to do...
Read more >
Formatting Questions - Qualtrics
While formatting questions, navigate to the Question behavior section to add advanced behavior to your questions, such as the ability to hide questions...
Read more >
THE BLUEBOOK MADE EASY - Penn State Law
Abbreviate “on the relation of,” “for the use of,” “on behalf of,” and similar expressions to “ex rel.” This type of proceeding occurs...
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