{"action":"created","issue":{"url":"https://api.github.com/repos/twada/unassert/issues/7","repository_url":"https://api.github.com/repos/twada/unassert","labels_url":"https://api.github.com/repos/twada/unassert/issues/7/labels{/name}","comments_url":"https://api.github.com/repos/twada/unassert/issues/7/comments","events_url":"https://api.github.com/repos/twada/unassert/issues/7/events","html_url":"https://github.com/twada/unassert/issues/7","id":155007851,"number":7,"title":"Conditional assertions","user":{"login":"ELLIOTTCABLE","id":200,"avatar_url":"https://avatars.githubusercontent.com/u/200?v=3","gravatar_id":"","url":"https://api.github.com/users/ELLIOTTCABLE","html_url":"https://github.com/ELLIOTTCABLE","followers_url":"https://api.github.com/users/ELLIOTTCABLE/followers","following_url":"https://api.github.com/users/ELLIOTTCABLE/following{/other_user}","gists_url":"https://api.github.com/users/ELLIOTTCABLE/gists{/gist_id}","starred_url":"https://api.github.com/users/ELLIOTTCABLE/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ELLIOTTCABLE/subscriptions","organizations_url":"https://api.github.com/users/ELLIOTTCABLE/orgs","repos_url":"https://api.github.com/users/ELLIOTTCABLE/repos","events_url":"https://api.github.com/users/ELLIOTTCABLE/events{/privacy}","received_events_url":"https://api.github.com/users/ELLIOTTCABLE/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":5,"created_at":"2016-05-16T11:05:33Z","updated_at":"2016-05-18T02:06:46Z","closed_at":null,"body":"So, at the moment, using [babel-plugin-unassert](https://github.com/twada/babel-plugin-unassert), I can't write an assertion within a conditional:\r\n\r\n    if (options.class) assert(root instanceof options.class)\r\n\r\n----\r\n\r\n    > BABEL_ENV=production ./build.sh\r\n    TypeError: ./giraphe.es6.js: Property consequent of IfStatement expected node to be of a type [\"Statement\"] but instead got null\r\n        at Object.validate (/Users/ec/Documents/Code/.local/giraphe/node_modules/babel-types/lib/definitions/index.js:115:13)\r\n        at Object.validate (/Users/ec/Documents/Code/.local/giraphe/node_modules/babel-types/lib/index.js:552:9)\r\n        at NodePath._replaceWith (/Users/ec/Documents/Code/.local/giraphe/node_modules/babel-traverse/lib/path/replacement.js:214:7)\r\n        at NodePath._remove (/Users/ec/Documents/Code/.local/giraphe/node_modules/babel-traverse/lib/path/removal.js:60:10)\r\n        at NodePath.remove (/Users/ec/Documents/Code/.local/giraphe/node_modules/babel-traverse/lib/path/removal.js:31:8)\r\n        at /Users/ec/Documents/Code/.local/giraphe/node_modules/babel-traverse/lib/path/lib/removal-hooks.js:40:12\r\n        at NodePath._callRemovalHooks (/Users/ec/Documents/Code/.local/giraphe/node_modules/babel-traverse/lib/path/removal.js:51:9)\r\n        at NodePath.remove (/Users/ec/Documents/Code/.local/giraphe/node_modules/babel-traverse/lib/path/removal.js:25:12)\r\n        at PluginPass.visitor.CallExpression.enter (/Users/ec/Documents/Code/.local/giraphe/node_modules/babel-plugin-unassert/index.js:57:37)\r\n        at newFn (/Users/ec/Documents/Code/.local/giraphe/node_modules/babel-traverse/lib/visitors.js:310:19)\r\n\r\nSo, I understand the dangers of messing with code *around* the actual call to the `assert`; but this particular one is really, really egregious (assertions occur alone behind a conditional *all* the time!)\r\n\r\nHere's my thought for how to deal with it:\r\n\r\n1. By default, if assertion-statement being removed is the sole child of a conditional branch, then remove that conditional branch, and insert the *condition*-expression as a bare statement (incase it was written to have any side-effects):\r\n\r\n        if (foo(bar))\r\n           assert(widget);\r\n        # becomes\r\n        foo(bar);\r\n\r\n2. And provide an *option* to entirely remove conditionals whose only contents are an assertion (i.e. “I am acknowledging that I know that I need to write my conditional-assertions to have no side-effects, or to include a second statement within their body if they do.”\r\n\r\nIf this is welcome, I'm happy to write a few failing test-cases and pull-request them. (="},"comment":{"url":"https://api.github.com/repos/twada/unassert/issues/comments/219906154","html_url":"https://github.com/twada/unassert/issues/7#issuecomment-219906154","issue_url":"https://api.github.com/repos/twada/unassert/issues/7","id":219906154,"user":{"login":"ELLIOTTCABLE","id":200,"avatar_url":"https://avatars.githubusercontent.com/u/200?v=3","gravatar_id":"","url":"https://api.github.com/users/ELLIOTTCABLE","html_url":"https://github.com/ELLIOTTCABLE","followers_url":"https://api.github.com/users/ELLIOTTCABLE/followers","following_url":"https://api.github.com/users/ELLIOTTCABLE/following{/other_user}","gists_url":"https://api.github.com/users/ELLIOTTCABLE/gists{/gist_id}","starred_url":"https://api.github.com/users/ELLIOTTCABLE/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ELLIOTTCABLE/subscriptions","organizations_url":"https://api.github.com/users/ELLIOTTCABLE/orgs","repos_url":"https://api.github.com/users/ELLIOTTCABLE/repos","events_url":"https://api.github.com/users/ELLIOTTCABLE/events{/privacy}","received_events_url":"https://api.github.com/users/ELLIOTTCABLE/received_events","type":"User","site_admin":false},"created_at":"2016-05-18T02:06:46Z","updated_at":"2016-05-18T02:06:46Z","body":"(Clarification: The *reason* I think this is such a big deal, is because we're literally talking about code differences between development/testing and production. That's a pretty serious opportunity for subtle bugs, if Enormous Effort isn't expended educating the developer as exactly what will change.)"}}