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

Enforce capitalization of the first letter of a comment (capitalized-comments) #682

Closed
computersarecool opened this issue Nov 10, 2016 · 27 comments

Comments

@computersarecool
Copy link

commented Nov 10, 2016

I am wondering if this project favors sentence case or all lower case for comments?
i.e.
// This is my comment

or

// this is my comment

Or if it even matters.

@feross

This comment has been minimized.

Copy link
Member

commented Nov 23, 2016

I'm not entirely consistent in my own code, though I think proper capitalization is better. There's no rule for this in ESLint, but there's a proposal to add it. When that gets added, I'll post an issue to consider it.

@feross feross closed this Nov 23, 2016

@feross

This comment has been minimized.

Copy link
Member

commented Jan 7, 2017

ESLint now has a capitalized-comments rule.

I propose we enable it, as follows:

"capitalized-comments": [
        "error",
        "always",
        {
            "ignoreInlineComments": true,
            "ignoreConsecutiveComments": true
        }
    ]

This rule is also automatically fixable.

@feross feross reopened this Jan 7, 2017

@feross feross added enhancement and removed question labels Jan 7, 2017

@feross feross added this to the standard v9 milestone Jan 7, 2017

@feross feross changed the title Is there a convention for comments? Enforce capitalization of the first letter of a comment (capitalized-comments) Jan 7, 2017

@dcousens

This comment has been minimized.

Copy link
Member

commented Jan 7, 2017

Woo!
I hate having to think about this junk 👍

@timoxley

This comment has been minimized.

Copy link
Contributor

commented Jan 7, 2017

@feross not disagreeing but why ignoreInlineComments?

@feross

This comment has been minimized.

Copy link
Member

commented Jan 8, 2017

@timoxley Maybe you're right. I'll test the ecosystem impact of ignoreInlineComments enabled vs. disabled and report back here before merging this.

My thought was just that there could be reasons to do:

function foo (/* host, port */) {
  var host = arguments[0]
  var port = arguments[1]
}

I do this kind of thing in feross/buffer sometimes since there's so many argument orderings to check for in some of the node core stuff. But this is not a big deal to me.

@dcousens

This comment has been minimized.

Copy link
Member

commented Jan 8, 2017

You typically see this for those who use the type systems analysis tools which pick up on inline comments for type declaration in standard JS code. IIRC

@feross

This comment has been minimized.

Copy link
Member

commented Jan 11, 2017

@dcousens If we want to, we can explicitly allow certain uncapitalized words to start a comment without triggering this error. The rule supports exceptions. But a blanket exemption on inline comments is certainly simpler and won't need constant updating.

@Flet

This comment has been minimized.

Copy link
Member

commented Jan 11, 2017

The inline eslint pragmas need to all be lowercase:

/* Eslint-env mocha */   // Does not work!
/* eslint-env mocha */   // Works!

I wonder how many other transpilers/helpers would break because of forced uppercase.
Flow? JSDoc?

I tending to be thumbs down on this just because the value may not be worth the additional breakage (or the additional maintenance of a whitelist).

@yoshuawuyts

This comment has been minimized.

Copy link
Contributor

commented Jan 11, 2017

@feross

This comment has been minimized.

Copy link
Member

commented Jan 11, 2017

@Flet All eslint pragmas are special-cased by the rule already.

Note that the following words are always ignored by this rule: ["jscs", "jshint", "eslint", "istanbul", "global", "globals", "exported"]

JSDoc is not affected because those declarations all start with a symbol (@) and comments are allowed to start with symbols and unicode characters. It's only if they are normal letters that they must be capitalized.

@yoshuawuyts Personally, I think this is a pretty well-thought-out rule: http://eslint.org/docs/rules/capitalized-comments I'd like to try shipping it in the standard v9 beta if there isn't strong objection so we can see how it works in practice.

@timoxley

This comment has been minimized.

Copy link
Contributor

commented Jan 12, 2017

Note that the following words are always ignored by this rule: ["jscs", "jshint", "eslint", "istanbul", "global", "globals", "exported"]

Yeah I'm a little concerned about accidentally breaking non-eslint/less popular/custom pragmas. Would be interested to see ecosystem impact.

@jprichardson

This comment has been minimized.

Copy link
Member

commented Jan 13, 2017

Wouldn't commented out code be affected as well? If so, this seems pretty overreaching.

@feross

This comment has been minimized.

Copy link
Member

commented Jan 13, 2017

@jprichardson That's a good point that I hadn't considered. This rule basically prevents commented code from being committed.

For example, this would error:

function foo (x) {
  // if (x === 1) return 10
  return x
}

In one way, it would be nice to prevent commented code from ending up in a codebase. But sometimes this is not avoidable, so I think I agree with @jprichardson now: this is a bit overreaching.

@timoxley's point is good too.

@jprichardson

This comment has been minimized.

Copy link
Member

commented Jan 13, 2017

Yeah, I agree in most cases, leaving commented code in place is a bad idea, especially since tools like Git are so pervasive. However, there are legitimate reasons to have commented code, like config files. Here is a real example from my codebase:

export const EXODUS_SERVER = EXODUS_PRODUCTION_SERVER
// export const EXODUS_SERVER = EXODUS_STAGING_SERVER

EXODUS_PRODUCTION_SERVER and EXODUS_STAGING_SERVER are defined near the beginning of the file.

So now if I just implemented a new feature on the server, I can quickly uncomment the aforementioned block to this:

// export const EXODUS_SERVER = EXODUS_PRODUCTION_SERVER
export const EXODUS_SERVER = EXODUS_STAGING_SERVER

and then the whole app is using EXODUS_SERVER still, but its value is different. Then when I'm ready to go back to a production build, I can change. Now I realize that it may be better to configure the aforementioned based upon NODE_ENV, but that'd feel like unnecessary ceremony required by a linter.

I'm sure there's other cases that are equally as valid where this rule my impede.

@dcousens

This comment has been minimized.

Copy link
Member

commented Jan 13, 2017

Now I realize that it may be better to configure the aforementioned based upon NODE_ENV

My thoughts exactly.
I'm trying to think of other cases for this, but ultimately, committed code has always been the wrong decision for me.

@jprichardson

This comment has been minimized.

Copy link
Member

commented Jan 13, 2017

@jprichardson sounds like a better use-case of process.env

You just have missed this part 😛

Now I realize that it may be better to configure the aforementioned based upon NODE_ENV, but that'd feel like unnecessary ceremony required by a linter.

@dcousens

This comment has been minimized.

Copy link
Member

commented Jan 14, 2017

@jprichardson I did, so I edited 😆 , you guys and your email notifications

@feross

This comment has been minimized.

Copy link
Member

commented Feb 9, 2017

This rule has a big ecosystem impact:

1..422
# tests 422
# pass  259
# fail  163

To reduce the impact, I investigated allowing commented out code by using the ignorePattern option. We can ignore comments that start with a JS reserved word (do, if, in, for, let, etc.). Comments that start with non-ascii characters are automatically allowed ([, (, etc.)

So, if we only catch comments with real words that need to be capitalized, we get:

1..422
# tests 422
# pass  268
# fail  154

New rule is:

    "capitalized-comments": [
      "error",
      "always",
      {
        "ignorePattern": "do|if|in|for|let|new|try|var|case|else|enum|eval|null|this|true|void|with|await|break|catch|class|const|false|super|throw|while|yield|delete|export|import|public|return|static|switch|typeof|default|extends|finally|package|private|continue|debugger|function|arguments|interface|protected|implements|instanceof",
        "ignoreInlineComments": true,
        "ignoreConsecutiveComments": true
      }
    ],

This is automatically fixable, so it's a quick fix. But jury's out on whether we actually want to do this.

@feross

This comment has been minimized.

Copy link
Member

commented Feb 9, 2017

For example, here are some diffs after running standard --fix:

screen shot 2017-02-08 at 5 35 16 pm
screen shot 2017-02-08 at 5 35 28 pm

The comments that start with variables could be wrapped in backticks like `err` so they don't need to be capitalized, but this might not be obvious to users.

@feross feross removed this from the standard v9 milestone Feb 9, 2017

@yoshuawuyts

This comment has been minimized.

Copy link
Contributor

commented Feb 9, 2017

@feross

This comment has been minimized.

Copy link
Member

commented Feb 9, 2017

Leaving open for more feedback, but yeah, I tend to agree with you @yoshuawuyts

@timoxley

This comment has been minimized.

Copy link
Contributor

commented Feb 9, 2017

This has good intentions but my feeling is that the benefits are minimal and mostly aesthetic (i.e. does not reduce risk of any class of bugs) and yet the likelihood of it being a nuisance is high. Also don't like the risk of hard-to-detect logic corruption caused by uppercasing commented out source which is later uncommented.

@feross

This comment has been minimized.

Copy link
Member

commented Feb 9, 2017

@dcousens thoughts?

@timoxley

This comment has been minimized.

Copy link
Contributor

commented Feb 9, 2017

Along similar lines, maybe one could consider enforcing a particular formatting for multi-line jsdoc /** styled comments i.e. ensure jsdoc style comments conform to the/a particular jsdoc spec.

@dcousens

This comment has been minimized.

Copy link
Member

commented Feb 9, 2017

Agreed with @timoxley, its junk to think about, but, its harmless by definition and IMHO, not worth the community impact at all.
I could not morally justify this refactor to someone paying the bills, haha.

@feross

This comment has been minimized.

Copy link
Member

commented Feb 9, 2017

Alright, it's decided. 💀

@feross feross closed this Feb 9, 2017

@Flet

This comment has been minimized.

Copy link
Member

commented Feb 9, 2017

This has been a great thread :)

@lock lock bot locked as resolved and limited conversation to collaborators May 10, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
7 participants
You can’t perform that action at this time.