{"action":"created","issue":{"url":"https://api.github.com/repos/jashkenas/coffeescript/issues/3642","labels_url":"https://api.github.com/repos/jashkenas/coffeescript/issues/3642/labels{/name}","comments_url":"https://api.github.com/repos/jashkenas/coffeescript/issues/3642/comments","events_url":"https://api.github.com/repos/jashkenas/coffeescript/issues/3642/events","html_url":"https://github.com/jashkenas/coffeescript/issues/3642","id":43105427,"number":3642,"title":"Confusion regarding trailing \\ at the end of line in Heregex","user":{"login":"kumarharsh","id":760112,"avatar_url":"https://avatars.githubusercontent.com/u/760112?v=3","gravatar_id":"","url":"https://api.github.com/users/kumarharsh","html_url":"https://github.com/kumarharsh","followers_url":"https://api.github.com/users/kumarharsh/followers","following_url":"https://api.github.com/users/kumarharsh/following{/other_user}","gists_url":"https://api.github.com/users/kumarharsh/gists{/gist_id}","starred_url":"https://api.github.com/users/kumarharsh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kumarharsh/subscriptions","organizations_url":"https://api.github.com/users/kumarharsh/orgs","repos_url":"https://api.github.com/users/kumarharsh/repos","events_url":"https://api.github.com/users/kumarharsh/events{/privacy}","received_events_url":"https://api.github.com/users/kumarharsh/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2014-09-18T07:56:50Z","updated_at":"2015-01-10T19:23:28Z","closed_at":null,"body":"In the multiline regexes, there is an anomaly:\r\n```coffee\r\n    ///\r\n    abc\\*\r\n    \\*\r\n    ///\r\n```\r\ncompiles to: `/abc\\*\\*/;`\r\n\r\n```coffee\r\n    ///\r\n    abc\\*\\\r\n    *\r\n    ///\r\n```\r\ncompiles to `/abc\\*\\n*/;`\r\n\r\nI find it very confusing, especially, wrt to how the newer version of coffee-script compile the multiline strings:\r\n\r\n```coffee\r\n    \"\"\"\r\n    a \\\r\n    b\r\n    \"\"\"\r\n```\r\ncompiles to `\"a b\";` and **not** `\"a \\nb\";`\r\n\r\nIMO, the `\\n` being added to the compiled regex if the line ends with a trailing `\\` is **not consistent** with the behaviour for multiline strings.\r\n\r\nAlso, I would expect to break a multiline-regex into smaller lines from anywhere, and still get the same result. But the addition of the `\\n` doesn't satisfy that expectation.\r\n\r\nI have taken a look at the discussion at #3214 , and especially at [this comment](https://github.com/jashkenas/coffeescript/pull/3214#issuecomment-26933867), but since the newer versions of coffee-script have changed the behaviour of multiline strings, this anomaly should also be fixed."},"comment":{"url":"https://api.github.com/repos/jashkenas/coffeescript/issues/comments/69468265","html_url":"https://github.com/jashkenas/coffeescript/issues/3642#issuecomment-69468265","issue_url":"https://api.github.com/repos/jashkenas/coffeescript/issues/3642","id":69468265,"user":{"login":"lydell","id":2142817,"avatar_url":"https://avatars.githubusercontent.com/u/2142817?v=3","gravatar_id":"","url":"https://api.github.com/users/lydell","html_url":"https://github.com/lydell","followers_url":"https://api.github.com/users/lydell/followers","following_url":"https://api.github.com/users/lydell/following{/other_user}","gists_url":"https://api.github.com/users/lydell/gists{/gist_id}","starred_url":"https://api.github.com/users/lydell/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lydell/subscriptions","organizations_url":"https://api.github.com/users/lydell/orgs","repos_url":"https://api.github.com/users/lydell/repos","events_url":"https://api.github.com/users/lydell/events{/privacy}","received_events_url":"https://api.github.com/users/lydell/received_events","type":"User","site_admin":false},"created_at":"2015-01-10T19:23:28Z","updated_at":"2015-01-10T19:23:28Z","body":"So, the complaint in the OP is:\r\n\r\n> IMO, the \\n being added to the compiled regex if the line ends with a trailing \\ is **not consistent** with the behaviour for multiline strings.\r\n\r\nI’d say that a trailing \\ does not need to be “consistent” with multiline strings (assuming heredocs, since that’s what’s in the example in the OP). Why? Because heregexes are not “consistent” with heredocs in the first place! Heredocs preserve newlines and indentation, while heregexes do the opposite: _removing_ whitespace!\r\n\r\nRegarding the linked discussion in #3214:\r\n\r\n> Seems to me allowing trailing \\ as \\n is: Much simpler to explain: \"Heregexes ignore non-escaped whitespaces.\"\r\n\r\nThat’s a good point.\r\n\r\n> It may seem somewhat illogical, but my reasoning was that an operator (\\ at end of line) shouldn't have two opposite meanings depending on context. (That would be counter-intuitive IMO.)\r\n>\r\n> [...] I think it should compile to /ab/.\r\n>\r\n> > In what context the other meaning comes into play?\r\n>\r\n> ```coffee\r\nx = some.long thing \\\r\n      or something.else\r\n```\r\n\r\nI see the point. The following:\r\n\r\n```coffee\r\nx = some.long thing \\\r\n      or something.else\r\n'''\r\n  First line \\\r\n    continued here.\r\n  Second line.\r\n'''\r\n'Some text that is line-\\\r\n    wrapped to the next line.'\r\n```\r\n\r\nis equivalent to:\r\n\r\n```coffee\r\nx = some.long thing or something.else\r\n'''\r\n  First line continued here.\r\n  Second line.\r\n''' # This also explains the @connec’s question at the very end of the previous comment.\r\n'Some text that is line-wrapped to the next line.'\r\n```\r\n\r\nFollowing the above, perhaps\r\n\r\n```coffee\r\n///\r\n  a \\\r\n    b\r\n///\r\n```\r\n\r\nshould be equivalent to\r\n\r\n```coffee\r\n///\r\n  a b\r\n///\r\n```\r\n\r\nThat might be more “consistent“ with the other uses of \\ at the end of a line. On the other hand it is also inconsistent: In the other cases removing the \\ makes a difference, while removing it in the heregex wouldn’t.\r\n\r\n> Another option is for an end-of-line \\ in a heregex to produce a compile-time error. I'm sure we all agree that a literal \\n is clearer. Disallowing \\ at end of line in this context would remove the possibility for confusion.\r\n\r\nThat’s also a valid point.\r\n\r\nBut finally, we have the following quotes from @jashkenas:\r\n\r\n> > Isn't this counter-intuitive? I'd expect [...] to mean /a\\nb/ rather than /a\\b/.\r\n>\r\n> Agreed.\r\n>\r\n> [...]\r\n>\r\n> I think @satyr's reasoning here is convincing. Let's allow it to escape whitespace and newlines\r\n\r\nIn the end it doesn’t matter that much. Nobody uses a \\ at the end of heregexes anyway. So I think it is settled."}}