Repository: eslint/eslint (JavaScript)

Issue:
max-len does not properly ignore trailing comments
<!--
    ESLint adheres to the [JS Foundation Code of Conduct](https://js.foundation/community/code-of-conduct).

    This template is for bug reports. If you are here for another reason, please see below:

    1. To propose a new rule: https://eslint.org/docs/developer-guide/contributing/new-rules
    2. To request a rule change: https://eslint.org/docs/developer-guide/contributing/rule-changes
    3. To request a change that is not a bug fix, rule change, or new rule: https://eslint.org/docs/developer-guide/contributing/changes
    4. If you have any questions, please stop by our chatroom: https://gitter.im/eslint/eslint

    Note that leaving sections blank will make it difficult for us to troubleshoot and we may have to close the issue.
-->

**Tell us about your environment**

* **ESLint Version:** 5.16.0
* **Node Version:** 10.16.0 (current LTS)
* **npm Version:** 6.9.0

**What parser (default, Babel-ESLint, etc.) are you using?** default

**Please show your full configuration:**

<details>
<summary>Configuration</summary>

<!-- Paste your configuration below: -->
Minimal working example for `.eslintrc.json`:
```js
{
    "rules": {
        "max-len": [
            "error",
            {
                "code": 20,
                "ignoreTrailingComments": true
            }
        ]
    }
}
```

</details>

**What did you do? Please include the actual source code causing the issue, as well as the command that you used to run ESLint.**

<!-- Paste the source code below: -->
`test.js`:
```js
"this is fine"; // Some comment... bla bla bla
"this is fine"; /* Some inline comment... */
"this isn't"; /* inline */ // Some other comment
```

<!-- Paste the command you used to run ESLint: -->
```bash
eslint test.js
```

**What did you expect to happen?**
No error, as trailing comments should be ignored.

**What actually happened? Please include the actual, raw output from ESLint.**

```
  2:1  error  Line 2 exceeds the maximum line length of 20  max-len
```

**Comments**

This happens when the trailing comments include an inline as well as a line comment.

I'm happy to submit a pull request if this is recognized as a bug.


Discussion:
Thank you for your report.

Indeed, it sounds like a bug.



Patch:
diff --git a/lib/rules/max-len.js b/lib/rules/max-len.js
index 70e4ae867f66..45b02f351178 100644
--- a/lib/rules/max-len.js
+++ b/lib/rules/max-len.js
@@ -315,6 +315,13 @@ module.exports = {
                         textToMeasure = line;
                     } else if (ignoreTrailingComments && isTrailingComment(line, lineNumber, comment)) {
                         textToMeasure = stripTrailingComment(line, comment);
+
+                        // ignore multiple trailing comments in the same line
+                        let lastIndex = commentsIndex;
+
+                        while (isTrailingComment(textToMeasure, lineNumber, comments[--lastIndex])) {
+                            textToMeasure = stripTrailingComment(textToMeasure, comments[lastIndex]);
+                        }
                     } else {
                         textToMeasure = line;
                     }


Test patch:
diff --git a/tests/lib/rules/max-len.js b/tests/lib/rules/max-len.js
index 6b95e45c147c..99822ccaef07 100644
--- a/tests/lib/rules/max-len.js
+++ b/tests/lib/rules/max-len.js
@@ -51,6 +51,9 @@ ruleTester.run("max-len", rule, {
         }, {
             code: "// really long comment on its own line sitting here",
             options: [40, 4, { ignoreComments: true }]
+        }, {
+            code: "var foo = module.exports = {}; /* inline some other comments */ //more",
+            options: [40, 4, { ignoreComments: true }]
         },
         "var /*inline-comment*/ i = 1;",
         {
@@ -87,6 +90,9 @@ ruleTester.run("max-len", rule, {
         }, {
             code: "var foo = module.exports = {}; // really long trailing comment",
             options: [40, 4, { ignoreTrailingComments: true }]
+        }, {
+            code: "var foo = module.exports = {}; /* inline some other comments */ //more",
+            options: [40, 4, { ignoreTrailingComments: true }]
         }, {
             code: "var foo = module.exports = {}; // really long trailing comment",
             options: [40, 4, { ignoreComments: true, ignoreTrailingComments: false }]