{"action":"created","issue":{"url":"https://api.github.com/repos/eslint/eslint/issues/6023","repository_url":"https://api.github.com/repos/eslint/eslint","labels_url":"https://api.github.com/repos/eslint/eslint/issues/6023/labels{/name}","comments_url":"https://api.github.com/repos/eslint/eslint/issues/6023/comments","events_url":"https://api.github.com/repos/eslint/eslint/issues/6023/events","html_url":"https://github.com/eslint/eslint/issues/6023","id":152042999,"number":6023,"title":"New rule: no mixed logical operators","user":{"login":"jfmengels","id":3869412,"avatar_url":"https://avatars.githubusercontent.com/u/3869412?v=3","gravatar_id":"","url":"https://api.github.com/users/jfmengels","html_url":"https://github.com/jfmengels","followers_url":"https://api.github.com/users/jfmengels/followers","following_url":"https://api.github.com/users/jfmengels/following{/other_user}","gists_url":"https://api.github.com/users/jfmengels/gists{/gist_id}","starred_url":"https://api.github.com/users/jfmengels/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jfmengels/subscriptions","organizations_url":"https://api.github.com/users/jfmengels/orgs","repos_url":"https://api.github.com/users/jfmengels/repos","events_url":"https://api.github.com/users/jfmengels/events{/privacy}","received_events_url":"https://api.github.com/users/jfmengels/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/eslint/eslint/labels/evaluating","name":"evaluating","color":"fef2c0"},{"url":"https://api.github.com/repos/eslint/eslint/labels/feature","name":"feature","color":"bfd4f2"},{"url":"https://api.github.com/repos/eslint/eslint/labels/rule","name":"rule","color":"fbca04"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":15,"created_at":"2016-04-30T17:39:15Z","updated_at":"2016-05-05T08:21:10Z","closed_at":null,"body":"I'm proposing a rule that warns when logical operators like `&&` and `||` are mixed. The purpose is to avoid errors like the one fixed [here](https://github.com/sindresorhus/eslint-plugin-ava/pull/90/files):\r\n\r\n```js\r\nif (a && a.b === 1 || a.b === 2) {\r\n}\r\n```\r\nIn this example, we are mixing `&&` and `||`. We are probably expecting *not* to enter inside the `if` block if\r\n- `a` is falsy\r\n- `a.b` is neither `1` nor `2`\r\n\r\nbut what the actual operation will be is `(a && a.b === 1) || a.b === 2`. Meaning, if `a` is `undefined`, then we will be evaluating `a.b === 2`, which will create an error.\r\n\r\nIf we had wrapped our different conditions in parentheses `a && (a.b === 1 || a.b === 2)`, then this would have resulted in the wanted behavior.\r\n\r\n### Fail\r\n\r\n```js\r\nif (a && a.b === 1 || a.b === 2) {\r\n}\r\n\r\na && b || c\r\na || b && c\r\n```\r\n\r\n### Pass\r\n\r\n```js\r\na && b && c\r\na || b || c\r\na && (b || c)\r\na || (b && c)\r\n```\r\n\r\nI think the rule is pretty generic and can help avoid \"easy\" mistakes, and therefore has it's place in ESLint. I don't mind adding it to a custom plugin otherwise. I'd be happy to write the rule :)"},"comment":{"url":"https://api.github.com/repos/eslint/eslint/issues/comments/217101277","html_url":"https://github.com/eslint/eslint/issues/6023#issuecomment-217101277","issue_url":"https://api.github.com/repos/eslint/eslint/issues/6023","id":217101277,"user":{"login":"mysticatea","id":1937871,"avatar_url":"https://avatars.githubusercontent.com/u/1937871?v=3","gravatar_id":"","url":"https://api.github.com/users/mysticatea","html_url":"https://github.com/mysticatea","followers_url":"https://api.github.com/users/mysticatea/followers","following_url":"https://api.github.com/users/mysticatea/following{/other_user}","gists_url":"https://api.github.com/users/mysticatea/gists{/gist_id}","starred_url":"https://api.github.com/users/mysticatea/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mysticatea/subscriptions","organizations_url":"https://api.github.com/users/mysticatea/orgs","repos_url":"https://api.github.com/users/mysticatea/repos","events_url":"https://api.github.com/users/mysticatea/events{/privacy}","received_events_url":"https://api.github.com/users/mysticatea/received_events","type":"User","site_admin":false},"created_at":"2016-05-05T08:21:10Z","updated_at":"2016-05-05T08:21:10Z","body":"- As it caused a bug of ESLint before, a mix of logical expressions can confuse people. So I have ever seen \"enclose mixed logical expressions\" in some code style guides in Japan. I think this rule is pretty important.\r\n- Indeed, guessing developer's intention is very difficult, but detecting a mix of logical expressions is easy. I made a prototype: http://astexplorer.net/#/7os6gj7FJO"}}