Skip to content

Latest commit

 

History

History
48 lines (37 loc) · 985 Bytes

README.md

File metadata and controls

48 lines (37 loc) · 985 Bytes

ESLint plugin to disallow unary + operators.

The Problem with the Unary Plus

The unary + operator can be used on any type and converts it to a number. As a result, a plus before any value is valid javascript code and unintentional + operators can be hard to find. This is especially troublesome with string concatenation over several lines:

const str = "Concatenating several strings " + 
    + "can be dangerous " + "because having more than one " +
    "+ " + + "between the strings " + "leads to conversions to " +
    + "numbers.";

console.log(str);

Results in:

Concatenating several strings NaNbecause having more than one + NaNleads to conversions to NaN

Usage

Install plugin npm:

$ npm install eslint-plugin-no-unary-plus --save-dev

Add plugin and rule in .eslintrc:

{
  "plugins": [
    ...
    "no-unary-plus",
    ...
  ],
  ...
  "rules": {
    ...
    "no-unary-plus/no-unary-plus": "error",
    ...
  }
}