{"action":"created","issue":{"url":"https://api.github.com/repos/istanbuljs/istanbuljs/issues/221","repository_url":"https://api.github.com/repos/istanbuljs/istanbuljs","labels_url":"https://api.github.com/repos/istanbuljs/istanbuljs/issues/221/labels{/name}","comments_url":"https://api.github.com/repos/istanbuljs/istanbuljs/issues/221/comments","events_url":"https://api.github.com/repos/istanbuljs/istanbuljs/issues/221/events","html_url":"https://github.com/istanbuljs/istanbuljs/issues/221","id":363899104,"node_id":"MDU6SXNzdWUzNjM4OTkxMDQ=","number":221,"title":"Suggestion: consider ternary statements that select between two constant expressions to be leaf nodes","user":{"login":"BernieSumption","id":838984,"node_id":"MDQ6VXNlcjgzODk4NA==","avatar_url":"https://avatars1.githubusercontent.com/u/838984?v=4","gravatar_id":"","url":"https://api.github.com/users/BernieSumption","html_url":"https://github.com/BernieSumption","followers_url":"https://api.github.com/users/BernieSumption/followers","following_url":"https://api.github.com/users/BernieSumption/following{/other_user}","gists_url":"https://api.github.com/users/BernieSumption/gists{/gist_id}","starred_url":"https://api.github.com/users/BernieSumption/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/BernieSumption/subscriptions","organizations_url":"https://api.github.com/users/BernieSumption/orgs","repos_url":"https://api.github.com/users/BernieSumption/repos","events_url":"https://api.github.com/users/BernieSumption/events{/privacy}","received_events_url":"https://api.github.com/users/BernieSumption/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2018-09-26T07:49:57Z","updated_at":"2018-10-15T13:30:53Z","closed_at":null,"author_association":"NONE","body":"I have a feature suggestion and would be interested in feedback. The title of this issue says it all, here's my justification.\r\n\r\nWhen a ternary is used to select between two constant expressions, executing the ternary once is enough to guarantee that both potential \"branches\" would execute successfully.\r\n\r\n```js\r\nconst k = true;\r\nconst v = k ? \"ya\" : \"nay\";\r\n```\r\n\r\nIf this statement is executed once, and `k` is true, the only difference you will get by executing it again with `k` being false would be to change the value of `v`. Of course, it is in principle possible that one of the two values will cause an error later on and the other won't, but that is the case with any expression e.g. an array lookup, and this is the only type of expression for which istanbul requires full coverage of all possible values.\r\n\r\nI'd suggest that it makes sense for istanbul to consider the above ternary to be a leaf expression, rather than a node with two branches. Ideally, array and object literals that consist only of constants would themselves be considered constant, e.g. `[1, \"x\"]` and `{a: \"b\"}` since they can't fail to execute.\r\n\r\nIf the ternary contains any expression that can fail, even a simple variable lookup, I'd call it a branch, e.g. `k ? a : b` should be considered a node with two branches because a or b could potentially be unbound giving a ReferenceError."},"comment":{"url":"https://api.github.com/repos/istanbuljs/istanbuljs/issues/comments/429853109","html_url":"https://github.com/istanbuljs/istanbuljs/issues/221#issuecomment-429853109","issue_url":"https://api.github.com/repos/istanbuljs/istanbuljs/issues/221","id":429853109,"node_id":"MDEyOklzc3VlQ29tbWVudDQyOTg1MzEwOQ==","user":{"login":"BernieSumption","id":838984,"node_id":"MDQ6VXNlcjgzODk4NA==","avatar_url":"https://avatars1.githubusercontent.com/u/838984?v=4","gravatar_id":"","url":"https://api.github.com/users/BernieSumption","html_url":"https://github.com/BernieSumption","followers_url":"https://api.github.com/users/BernieSumption/followers","following_url":"https://api.github.com/users/BernieSumption/following{/other_user}","gists_url":"https://api.github.com/users/BernieSumption/gists{/gist_id}","starred_url":"https://api.github.com/users/BernieSumption/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/BernieSumption/subscriptions","organizations_url":"https://api.github.com/users/BernieSumption/orgs","repos_url":"https://api.github.com/users/BernieSumption/repos","events_url":"https://api.github.com/users/BernieSumption/events{/privacy}","received_events_url":"https://api.github.com/users/BernieSumption/received_events","type":"User","site_admin":false},"created_at":"2018-10-15T13:30:53Z","updated_at":"2018-10-15T13:30:53Z","author_association":"NONE","body":"If anyone wants to reject this request on the grounds that it's a silly edge case, not required by most users, not worth the complexity/value trade-off in implementation or whatever then totally fair.\r\n\r\nBut your argument that a ternary producing a different value could lead to a bug applies to any statement, such as indexing an array or even multiplying two numbers. If a statement is given different operands then it will evaluate to a different value. And this, could in principle, cause a bug. But ternaries are the only place where this causes less than 100% code coverage.\r\n\r\nI don't see why ternaries selecting between constant expressions should be a unique special case where 100% value coverage is required."}}
{"actor":{"display_login":"BernieSumption"}}