Repository: eslint/eslint
Pull request #8183 (2017-03-02 10:16:02)

Update: deprecate `applyDefaultPatterns` in `line-comment-position`
**What is the purpose of this pull request? (put an "X" next to item)**

[ ] Documentation update
[ ] Bug fix ([template](https://raw.githubusercontent.com/eslint/eslint/master/templates/bug-report.md))
[ ] New rule ([template](https://raw.githubusercontent.com/eslint/eslint/master/templates/rule-proposal.md))
[X] Changes an existing rule ([template](https://raw.githubusercontent.com/eslint/eslint/master/templates/rule-change-proposal.md))
[ ] Add autofixing to a rule
[ ] Add a CLI option
[ ] Add something to the core
[ ] Other, please explain:

<!--
    If the item you've checked above has a template, please paste the template questions below and answer them. (If this pull request is addressing an issue, you can just paste a link to the issue here instead.)
-->
**What rule do you want to change?**
`line-comment-position`

**Does this change cause the rule to produce more or fewer warnings?**
The same.

**How will the change be implemented? (New option, new default behavior, etc.)?**
New option name `applyDefaultIgnorePatterns` replacing old one `applyDefaultPatterns`.

**Please provide some example code that this change will affect:**

```js
/*eslint line-comment-position: ["error", { "position": "beside", "applyDefaultPatterns": false }]*/
// jscs-disable
1 + 1;
```
**What does the rule currently do for this code?**
Complains about comment not being beside code.

**What will the rule do after it's changed?**
Same behaviour, under the new option name, if used.

**What changes did you make? (Give an overview)**
Following the discussion in https://github.com/eslint/eslint/pull/8155  I have rename the option name so it's clearer and consistent with `lines-around-comments`.

**Is there anything you'd like reviewers to focus on?**
Old option is still present and functional, so it's not a breaking change.



mention-bot: @alberto, thanks for your PR! By analyzing the history of the files in this pull request, we identified @not-an-aardvark, @amercier and @platinumazure to be potential reviewers.
eslintbot: LGTM

Patch:
diff --git a/docs/rules/line-comment-position.md b/docs/rules/line-comment-position.md
index 2e6f3cf82..63561c831 100644
--- a/docs/rules/line-comment-position.md
+++ b/docs/rules/line-comment-position.md
@@ -77,24 +77,26 @@ Examples of **incorrect** code for the `ignorePattern` option:
 1 + 1; // invalid comment
 ```
 
-### applyDefaultPatterns
+### applyDefaultIgnorePatterns
 
 Default ignore patterns are applied even when `ignorePattern` is provided. If you want to omit default patterns, set this option to `false`.
 
-Examples of **correct** code for the `{ "applyDefaultPatterns": false }` option:
+Examples of **correct** code for the `{ "applyDefaultIgnorePatterns": false }` option:
 
 ```js
-/*eslint line-comment-position: ["error", { "ignorePattern": "pragma", "applyDefaultPatterns": false }]*/
+/*eslint line-comment-position: ["error", { "ignorePattern": "pragma", "applyDefaultIgnorePatterns": false }]*/
 1 + 1; // pragma valid comment
 ```
 
-Examples of **incorrect** code for the `{ "applyDefaultPatterns": false }` option:
+Examples of **incorrect** code for the `{ "applyDefaultIgnorePatterns": false }` option:
 
 ```js
-/*eslint line-comment-position: ["error", { "ignorePattern": "pragma", "applyDefaultPatterns": false }]*/
+/*eslint line-comment-position: ["error", { "ignorePattern": "pragma", "applyDefaultIgnorePatterns": false }]*/
 1 + 1; // falls through
 ```
 
+**Deprecated:** the object property `applyDefaultPatterns` is deprecated. Please use the property `applyDefaultIgnorePatterns` instead.
+
 ## When Not To Use It
 
 If you aren't concerned about having different line comment styles, then you can turn off this rule.
diff --git a/lib/rules/line-comment-position.js b/lib/rules/line-comment-position.js
index 07c952d98..dd8f2b9ad 100644
--- a/lib/rules/line-comment-position.js
+++ b/lib/rules/line-comment-position.js
@@ -35,6 +35,9 @@ module.exports = {
                             },
                             applyDefaultPatterns: {
                                 type: "boolean"
+                            },
+                            applyDefaultIgnorePatterns: {
+                                type: "boolean"
                             }
                         },
                         additionalProperties: false
@@ -49,7 +52,7 @@ module.exports = {
 
         let above,
             ignorePattern,
-            applyDefaultPatterns = true;
+            applyDefaultIgnorePatterns = true;
 
         if (!options || typeof options === "string") {
             above = !options || options === "above";
@@ -57,7 +60,12 @@ module.exports = {
         } else {
             above = options.position === "above";
             ignorePattern = options.ignorePattern;
-            applyDefaultPatterns = options.applyDefaultPatterns !== false;
+
+            if (options.hasOwnProperty("applyDefaultIgnorePatterns")) {
+                applyDefaultIgnorePatterns = options.applyDefaultIgnorePatterns !== false;
+            } else {
+                applyDefaultIgnorePatterns = options.applyDefaultPatterns !== false;
+            }
         }
 
         const defaultIgnoreRegExp = astUtils.COMMENTS_IGNORE_PATTERN;
@@ -71,7 +79,7 @@ module.exports = {
 
         return {
             LineComment(node) {
-                if (applyDefaultPatterns && (defaultIgnoreRegExp.test(node.value) || fallThroughRegExp.test(node.value))) {
+                if (applyDefaultIgnorePatterns && (defaultIgnoreRegExp.test(node.value) || fallThroughRegExp.test(node.value))) {
                     return;
                 }
 
diff --git a/lib/rules/space-unary-ops.js b/lib/rules/space-unary-ops.js
index 11c59c827..5b156fac2 100644
--- a/lib/rules/space-unary-ops.js
+++ b/lib/rules/space-unary-ops.js
@@ -68,6 +68,21 @@ module.exports = {
             return node.argument && node.argument.type && node.argument.type === "ObjectExpression";
         }
 
+        /**
+         * Check if it is safe to remove the spaces between the two tokens in
+         * the context of a non-word prefix unary operator. For example, `+ +1`
+         * cannot safely be changed to `++1`.
+         * @param {Token} firstToken The operator for a non-word prefix unary operator
+         * @param {Token} secondToken The first token of its operand
+         * @returns {boolean} Whether or not the spacing between the tokens can be removed
+         */
+        function canRemoveSpacesBetween(firstToken, secondToken) {
+            return !(
+                (firstToken.value === "+" && secondToken.value[0] === "+") ||
+                (firstToken.value === "-" && secondToken.value[0] === "-")
+            );
+        }
+
         /**
         * Checks if an override exists for a given operator.
         * @param {ASTnode} node AST node
@@ -244,7 +259,10 @@ module.exports = {
                             operator: firstToken.value
                         },
                         fix(fixer) {
-                            return fixer.removeRange([firstToken.range[1], secondToken.range[0]]);
+                            if (canRemoveSpacesBetween(firstToken, secondToken)) {
+                                return fixer.removeRange([firstToken.range[1], secondToken.range[0]]);
+                            }
+                            return null;
                         }
                     });
                 }


Test patch:
diff --git a/tests/lib/rules/line-comment-position.js b/tests/lib/rules/line-comment-position.js
index 554a75509..e72e85cf0 100644
--- a/tests/lib/rules/line-comment-position.js
+++ b/tests/lib/rules/line-comment-position.js
@@ -99,6 +99,16 @@ ruleTester.run("line-comment-position", rule, {
             }]
         },
         {
+            code: "// jscs: disable\n1 + 1;",
+            options: [{ position: "beside", applyDefaultIgnorePatterns: false }],
+            errors: [{
+                message: "Expected comment to be beside code.",
+                type: "Line",
+                line: 1,
+                column: 1
+            }]
+        },
+        {   // deprecated option still works
             code: "// jscs: disable\n1 + 1;",
             options: [{ position: "beside", applyDefaultPatterns: false }],
             errors: [{
@@ -108,6 +118,16 @@ ruleTester.run("line-comment-position", rule, {
                 column: 1
             }]
         },
+        {   // new option name takes precedence
+            code: "// jscs: disable\n1 + 1;",
+            options: [{ position: "beside", applyDefaultIgnorePatterns: false, applyDefaultPatterns: true }],
+            errors: [{
+                message: "Expected comment to be beside code.",
+                type: "Line",
+                line: 1,
+                column: 1
+            }]
+        },
         {
             code: "1 + 1; // mentioning falls through",
             errors: [{
diff --git a/tests/lib/rules/space-unary-ops.js b/tests/lib/rules/space-unary-ops.js
index 8c1a3f102..0da1177b6 100644
--- a/tests/lib/rules/space-unary-ops.js
+++ b/tests/lib/rules/space-unary-ops.js
@@ -417,6 +417,46 @@ ruleTester.run("space-unary-ops", rule, {
                 message: "Unary operator '++' must be followed by whitespace."
             }]
         },
+        {
+            code: "+ +foo",
+            output: null,
+            options: [{ nonwords: false }],
+            errors: [{
+                message: "Unexpected space after unary operator '+'."
+            }]
+        },
+        {
+            code: "+ ++foo",
+            output: null,
+            options: [{ nonwords: false }],
+            errors: [{
+                message: "Unexpected space after unary operator '+'."
+            }]
+        },
+        {
+            code: "- -foo",
+            output: null,
+            options: [{ nonwords: false }],
+            errors: [{
+                message: "Unexpected space after unary operator '-'."
+            }]
+        },
+        {
+            code: "- --foo",
+            output: null,
+            options: [{ nonwords: false }],
+            errors: [{
+                message: "Unexpected space after unary operator '-'."
+            }]
+        },
+        {
+            code: "+ -foo",
+            output: "+-foo",
+            options: [{ nonwords: false }],
+            errors: [{
+                message: "Unexpected space after unary operator '+'."
+            }]
+        },
         {
             code: "function *foo() { yield(0) }",
             output: "function *foo() { yield (0) }",