|
| 1 | +<!--- |
| 2 | +{ |
| 3 | + "titles": [ |
| 4 | + "Confusing minuses", |
| 5 | + "W006" |
| 6 | + ], |
| 7 | + "slugs": [ |
| 8 | + "confusing-minuses", |
| 9 | + "w006" |
| 10 | + ], |
| 11 | + "linters": [ |
| 12 | + "jslint", |
| 13 | + "jshint" |
| 14 | + ], |
| 15 | + "author": "jallardice" |
| 16 | +} |
| 17 | +--> |
| 18 | + |
| 19 | +### History |
| 20 | + |
| 21 | +This warning has existed in a few forms in both JSLint and JSHint. It was |
| 22 | +introduced in the original version of JSLint and has remained in both tools ever |
| 23 | +since. |
| 24 | + |
| 25 | + - In JSHint prior to version 1.0.0 the warning given was *"Confusing minusses"* |
| 26 | + |
| 27 | + - In JSHint 1.0.0 and above the spelling has been corrected and the message |
| 28 | + used is now *"Confusing minuses"* |
| 29 | + |
| 30 | + - JSLint has always used the more generic *"Confusing use of '{a}"* warning in |
| 31 | + the same situation |
| 32 | + |
| 33 | +### When do I get this error? |
| 34 | + |
| 35 | +The "Confusing minuses" error is thrown when JSHint encounters **an addition |
| 36 | +operator in which the right-hand-side expression is preceded by the unary `-` |
| 37 | +operator**. In the following example we attempt to compute the addition of a |
| 38 | +numeric literal and the numeric value of a variable: |
| 39 | + |
| 40 | +<!--- |
| 41 | +{ |
| 42 | + "linter": "jshint" |
| 43 | +} |
| 44 | +--> |
| 45 | +```javascript |
| 46 | +var a = "10", |
| 47 | + b = 5 - -a; |
| 48 | +``` |
| 49 | + |
| 50 | +### Why do I get this error? |
| 51 | + |
| 52 | +This error is raised to highlight a **potentially confusing piece of code**. |
| 53 | +Your code will most likely run as expected but it could cause issues with |
| 54 | +maintenance and be confusing to other developers. |
| 55 | + |
| 56 | +The `-` operator is overloaded in JavaScript. Most commonly it can be seen as |
| 57 | +the subtraction operator but it also functions in a unary form as a numeric |
| 58 | +casting and negating operator. In this unary form the result of the expression |
| 59 | +will be the value of the operand coerced to the Number type and then negated. In |
| 60 | +the example above, because the value of `a` is a string that can be converted to |
| 61 | +a number, the `+a` expression results in the value `-10` and the value of `b` |
| 62 | +ends up as `-5`. |
| 63 | + |
| 64 | +This behaviour is described in the specification ([ES5 |
| 65 | +§11.4.7][es5-11.4.7]): |
| 66 | + |
| 67 | +> The production *UnaryExpression* : `-` *UnaryExpression* is evaluated as |
| 68 | +> follows: |
| 69 | +> 1. Let *expr* be the result of evaluating UnaryExpression. |
| 70 | +> 2. Let *oldValue* be ToNumber(GetValue(*expr*)). |
| 71 | +> 3. If *oldValue* is NaN, return NaN. |
| 72 | +> 4. Return the result of negating *oldValue*; that is, compute a Number with |
| 73 | +> the same magnitude but opposite sign. |
| 74 | +
|
| 75 | +However, when the subtraction operator is used adjacent to the unary `-` |
| 76 | +operator an unforunate resemblance to the decrement operator arises. The |
| 77 | +decrement operator, `--`, is used to subtract 1 from its operand. It can be used |
| 78 | +as a postfix or prefix operator which means it can appear after or before its |
| 79 | +operand. This makes the above example slightly confusing on first glance. |
| 80 | + |
| 81 | +To resolve this issue the easiest fix is to wrap the unary expression in |
| 82 | +parentheses to disambiguate the `-` characters: |
| 83 | + |
| 84 | +<!--- |
| 85 | +{ |
| 86 | + "linter": "jshint" |
| 87 | +} |
| 88 | +--> |
| 89 | +```javascript |
| 90 | +var a = "10", |
| 91 | + b = 5 - (-a); |
| 92 | +``` |
| 93 | + |
| 94 | +In JSHint 1.0.0 and above you have the ability to ignore any warning with a |
| 95 | +[special option syntax][jshintopts]. The identifier of this warning is **W006**. |
| 96 | +This means you can tell JSHint to not issue this warning with the `/*jshint |
| 97 | +-W006 */` directive. |
| 98 | + |
| 99 | +[es5-11.4.7]: http://es5.github.io/#x11.4.7 |
| 100 | +[jshintopts]: http://jshint.com/docs/#options |
0 commit comments